source: tmcsimulator/trunk/webapps/unifiedlogger/logging_service.py @ 433

Revision 433, 2.2 KB checked in by jdalbey, 7 years ago (diff)

Unified Logger v1.0. Add cad watcher plugin to logging service.

Line 
1import cms_watcher, cad_watcher, time, json
2# Unified Logging Service
3# jdalbey 7/6/2019
4
5
6# Load the sim time file and extract the seconds */
7def getSimTime():
8    with open ("../dynamicdata/sim_elapsedtime.json", 'r') as myfile:
9        jsonData=myfile.read()
10         
11    seconds = json.loads(jsonData)['elapsedtime']
12    # convert seconds to H:MM:SS
13    m, s = divmod(int(seconds), 60)
14    h, m = divmod(m, 60)
15    return  "%d:%02d:%02d" % (h, m, s)   
16
17def main():
18    # Delete any previously existing output file
19    f = open("../dynamicdata/unifiedlog.html", "w")
20    startHTML = "<HTML><HEAD><meta http-equiv=\"refresh\" content=\"5\" /></HEAD><BODY><PRE>"
21    f.write(startHTML);
22    f.close()           
23    # List of the available plugin modules
24    plugins = ["cms_watcher","cad_watcher"]
25    #FOR each plugin LOOP
26    for plugin in plugins:
27        # dynamically load the setup function for this plugin
28        plugmodule = globals()[plugin]
29        setupfunc = getattr(plugmodule, 'setup')
30        #Call setup
31        setupfunc()
32    #END LOOP
33   
34    #DO Forever
35    while True:
36    #    Get simulation time
37        timeStamp = getSimTime()
38    #    Reset Output Buffer
39        output = ""
40        results = []
41    #    FOR each plugin LOOP
42        for plugin in plugins:
43            # dynamically load the setup function for this plugin
44            plugmodule = globals()[plugin]
45            getfunc = getattr(plugmodule, 'getLogEntries')
46           
47    #        Run the plugin process returning new log entries
48            results = getfunc()     
49            #    Append simulation time and the log entries to the Output Buffer
50            for item in results:
51                trimmed_item = item.strip()
52                if len(trimmed_item) > 0:
53                    output += timeStamp + " " + trimmed_item + "\n"
54    #    END LOOP
55    #    IF the Output Buffer has any contents THEN
56        if len(output) > 0:
57    #       Write (append) Output Buffer to unified log file
58            print output,
59            f = open("../dynamicdata/unifiedlog.html", "a")
60            f.write(output)
61            f.close()           
62    #    END IF
63    #    Wait one second
64        time.sleep(1)
65
66#END DO
67if __name__ == "__main__":
68    main()
Note: See TracBrowser for help on using the repository browser.