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

Revision 461, 2.5 KB checked in by jdalbey, 7 years ago (diff)

add activitylog_watcher to Unified Logging Service.

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