source: trunk/anuga_core/source/anuga/utilities/log_analyser.py @ 8323

Last change on this file since 8323 was 8323, checked in by gray, 12 years ago

adding new files

File size: 2.4 KB
Line 
1import sys
2import os
3import re
4import anuga.utilities.log as log
5import csv
6
7defaultOutputFile ='timing.csv'
8timingDelimiter = log.TimingDelimiter
9
10def analyse_log(path, output_file, log_file='anuga.log'):
11    """
12    path - the directory to look for log files in.
13    output_file - the file produced, with meta log info
14    log_file - the name of the log files
15     
16    Read log files and create a meta log file
17   
18    WARNING: if a
19    """
20    log_pairs = build_log_info(path, log_file)
21    if log_pairs is not None:
22        write_meta_log(log_pairs, output_file)
23
24def build_log_info(path, log_file):
25    log_pairs = []
26    for (path, dirs, files) in os.walk(path):   
27        for file in files:
28            if log_file in file: 
29                dictResults = {}
30                for line in open(os.path.join(path,file)):
31                    if line.find(timingDelimiter)>-1:
32                        key_value = line.split(timingDelimiter)[1]
33                        # FIXME remove the magic comma
34                        key_value_list = key_value.split(',')
35                        key = key_value_list[0].strip()
36                        value =  key_value_list[1].strip()
37                        if key in dictResults:
38                            print "WARNING. Multiple values ignored."
39                        dictResults[key] = value
40                        #print "key", key
41                        #print "value", value
42                log_pairs.append(dictResults)
43    return log_pairs
44 
45def write_meta_log(log_pairs, output_file):
46    """Write the info from the log files to a file"""
47   
48    all_keys = {} # values aren't needed, but are there
49    for log_p in log_pairs:
50        all_keys.update(log_p)
51    sorted_all_keys = sorted(all_keys.keys()) # sort the keys alphabetacally
52   
53    han = open(output_file, 'w')
54    writer = csv.DictWriter(han, delimiter=',', fieldnames=sorted_all_keys,
55                        extrasaction='ignore')
56    # Title
57    writer.writerow(dict(zip(sorted_all_keys, sorted_all_keys)))
58   
59    for pair in log_pairs: # Write the main body
60        writer.writerow(pair)
61       
62    han.close()
63
64####################################################
65if __name__ == '__main__':
66   
67    path = sys.argv[1]
68
69    if len(sys.argv) < 3:
70        outputFile = open(defaultOutputFile, "a")
71    else:
72        outputFile = open(sys.argv[2], "a")
73
74    analyse_log(path, outputFile)
75
76   
77 
Note: See TracBrowser for help on using the repository browser.