source: tmcsimulator/trunk/src/python/unifiedlogger/logging_service.py @ 687

Revision 687, 3.0 KB checked in by jdalbey, 4 years ago (diff)

logging_service.py, et.al. Revise how it handles a missing CADcomments.log file - just skip the file read but continue. Also update all watchers to use config dir.

Line 
1import sys, cms_watcher, har_watcher, cad_watcher, activitylog_watcher, time, json, ConfigParser
2# Unified Logging Service
3# jdalbey 7/6/2019
4
5outputFilename = "unifiedlog.csv"
6seconds = 0
7# convert seconds to H:MM:SS
8def toHMS(seconds):
9    m, s = divmod(int(seconds), 60)
10    h, m = divmod(m, 60)
11    return  "%02d:%02d:%02d" % (h, m, s)   
12   
13# Load the sim time file and extract the seconds */
14def getSimTime(path):
15    global seconds   
16    with open (path+"sim_elapsedtime.json", 'r') as myfile:
17        jsonData=myfile.read()
18    try:     
19       seconds = json.loads(jsonData)['elapsedtime']
20    except:
21       print "Unable to read sim time. ",
22       print "Proceeding with previous time: ", toHMS(seconds)
23    # convert seconds to H:MM:SS
24    return toHMS(seconds)
25
26# Entry point
27# @param list of command line arguments
28#        can provide name of config directory to use
29def startup(args):
30    configdir = "config"  # default dir for production
31    # See if a config dir was given on command line
32    if len(args) == 2:
33        configdir = args[1]
34
35    # get path to output file from configuration
36    config = ConfigParser.ConfigParser()
37    config.read(configdir + '/logging_service.cfg')
38    logfilepath  = config.get('Paths', 'UnifiedLogPath')
39
40    # Delete any previously existing output file
41    f = open(logfilepath + outputFilename, "w")
42    f.close()           
43    # List of the available plugin modules
44    plugins = ["cms_watcher","har_watcher","cad_watcher","activitylog_watcher"]
45    #FOR each plugin LOOP
46    for plugin in plugins:
47        # dynamically load the setup function for this plugin       
48        plugmodule = globals()[plugin]
49        setupfunc = getattr(plugmodule, 'setup')
50        #Call setup
51        setupfunc(configdir)
52    #END LOOP
53   
54    #DO Forever
55    while True:
56    #    Get simulation time
57        timeStamp = getSimTime(logfilepath)
58    #    Reset Output Buffer
59        output = ""
60        results = []
61    #    FOR each plugin LOOP
62        for plugin in plugins:
63            # dynamically load the get log function for this plugin
64            plugmodule = globals()[plugin]
65            getfunc = getattr(plugmodule, 'getLogEntries')
66           
67    #        Run the plugin process returning new log entries
68            results = getfunc()
69    #       Append simulation time and the log entries to the Output Buffer
70            for item in results:
71                trimmed_item = item.strip()
72                if len(trimmed_item) > 0:
73                    output += timeStamp + ", " + trimmed_item + "\n"
74    #    END LOOP
75    #    IF the Output Buffer has any contents THEN
76        if len(output) > 0:
77    #       Write (append) Output Buffer to unified log file as CSV
78    #       Assumes fields don't contain commas
79            print output,
80            f = open(logfilepath + outputFilename, "a")
81            f.write(output)
82            f.close()           
83    #    END IF
84    #    Wait five seconds
85        time.sleep(5)
86
87#END DO
88
89# Entry point for application
90if __name__ == '__main__':
91    startup(sys.argv)
Note: See TracBrowser for help on using the repository browser.