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

Revision 439, 2.4 KB checked in by jdalbey, 7 years ago (diff)

setup wing project for logging service python files. Add config file for file paths.

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