Changeset 8322


Ignore:
Timestamp:
Jan 23, 2012, 5:27:08 PM (11 years ago)
Author:
gray
Message:

getting logging working

Location:
trunk/misc/tools/log_analysis
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/misc/tools/log_analysis/logAnalyser.py

    r8319 r8322  
    33import re
    44import anuga.utilities.log as log
     5import csv
    56
    6 defaultOutputFile ='/nas/gemd/georisk_models/inundation/timing/timing.csv'
     7defaultOutputFile ='timing.csv'
    78timingDelimiter = log.TimingDelimiter
    89
    9 def AnalyseLog:
    10     dictResults={}
     10def AnalyseLog(path, output_file, log_file='anuga.log'):
     11    """
     12    path - the directory to look for log files in.
     13    log_file - the
     14     
     15    """
     16    log_pairs = build_log_info(path, log_file)
     17    if log_pairs is not None:
     18        write_meta_log(log_pairs, output_file)
    1119
     20def build_log_info(path, log_file):
     21    log_pairs = []
    1222    for (path, dirs, files) in os.walk(path):   
    13 
    1423        for file in files:
    15             if "anuga.log" in file:
     24            if log_file in file:
     25                dictResults = {}
    1626                for line in open(os.path.join(path,file)):
    1727                    if line.find(timingDelimiter)>-1:
    18                         key =strip(line.split('timingDelimiter')[1]))
    19                         value =line.split(',')[1]
    20                         dictResults[key]=value
    21                         print key +value
    22                 try:
    23                     #print "numTriangles: "+ dictResults['numTriangles'] + "  start: " + dictResults['startMeshTime'] + " end: " \
    24                                       #+ dictResults['finishMeshTime'] + "   Very end: " + dictResults['finishTime']
    25 
    26                     outputFile.write(dictResults['numTriangles'] + "," + dictResults['startMeshTime'] + "," \
    27                                  + dictResults['finishMeshTime'] + "," + dictResults['finishTime']+ "," \
    28                                  + dictResults['startMemory'] + "," + dictResults['finishMemory']+ "," \
    29                                  + os.path.join(path,file) + '\n')
    30                 except:
    31                     print os.path.join(path,file) + " not in the right format"
    32 
    33     outputFile.close()
     28                        key_value = line.split(timingDelimiter)[1]
     29                        # FIXME remove the magic comma
     30                        key_value_list = key_value.split(',')
     31                        key = key_value_list[0].strip()
     32                        value =  key_value_list[1].strip()
     33                        dictResults[key] = value
     34                        #print "key", key
     35                        #print "value", value
     36                log_pairs.append(dictResults)
     37    return log_pairs
     38 
     39def write_meta_log(log_pairs, output_file):
     40    """Write the info from the log files to a file"""
     41   
     42    all_keys = {} # values aren't needed, but are there
     43    for log_p in log_pairs:
     44        all_keys.update(log_p)
     45               
     46    # sort the keys alphabetacally
     47    sorted_all_keys = sorted(all_keys.keys())
     48    print "output_file", output_file
     49    han = open(output_file, 'w')
     50    writer = csv.DictWriter(han, delimiter=',', fieldnames=sorted_all_keys,
     51                        extrasaction='ignore')
     52    # Title
     53    writer.writerow(dict(zip(sorted_all_keys, sorted_all_keys)))
     54   
     55    for pair in log_pairs:
     56        writer.writerow(pair)
     57       
     58    han.close()
    3459
    3560####################################################
    3661if __name__ == '__main__':
    3762   
    38    
    39 if len(sys.argv) < 3:
    40     outputFile = open(defaultOutputFile, "a")
    41 else:
    42     outputFile = open(sys.argv[2], "a")
     63    path = sys.argv[1]
    4364
     65    if len(sys.argv) < 3:
     66        outputFile = open(defaultOutputFile, "a")
     67    else:
     68        outputFile = open(sys.argv[2], "a")
    4469
    45 path = sys.argv[1]
     70    AnalyseLog(path, outputFile)
     71
    4672   
    4773 
Note: See TracChangeset for help on using the changeset viewer.