| 1 | import cms_watcher, cad_watcher, activitylog_watcher, time, json, ConfigParser |
|---|
| 2 | # Unified Logging Service |
|---|
| 3 | # jdalbey 7/6/2019 |
|---|
| 4 | |
|---|
| 5 | outputFilename = "unifiedlog.csv" |
|---|
| 6 | seconds = 0 |
|---|
| 7 | # convert seconds to H:MM:SS |
|---|
| 8 | def 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 */ |
|---|
| 14 | def getSimTime(): |
|---|
| 15 | global seconds |
|---|
| 16 | with open ("webapps/dynamicdata/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 | def startup(): |
|---|
| 27 | # get path to output file from configuration |
|---|
| 28 | config = ConfigParser.ConfigParser() |
|---|
| 29 | config.read('config/logging_service.cfg') |
|---|
| 30 | logfilepath = config.get('Paths', 'UnifiedLogPath') |
|---|
| 31 | |
|---|
| 32 | # Delete any previously existing output file |
|---|
| 33 | f = open(logfilepath + outputFilename, "w") |
|---|
| 34 | f.close() |
|---|
| 35 | # List of the available plugin modules |
|---|
| 36 | plugins = ["cms_watcher","cad_watcher","activitylog_watcher"] |
|---|
| 37 | #FOR each plugin LOOP |
|---|
| 38 | for plugin in plugins: |
|---|
| 39 | # dynamically load the setup function for this plugin |
|---|
| 40 | plugmodule = globals()[plugin] |
|---|
| 41 | setupfunc = getattr(plugmodule, 'setup') |
|---|
| 42 | #Call setup |
|---|
| 43 | setupfunc() |
|---|
| 44 | #END LOOP |
|---|
| 45 | |
|---|
| 46 | #DO Forever |
|---|
| 47 | while True: |
|---|
| 48 | # Get simulation time |
|---|
| 49 | timeStamp = getSimTime() |
|---|
| 50 | # Reset Output Buffer |
|---|
| 51 | output = "" |
|---|
| 52 | results = [] |
|---|
| 53 | # FOR each plugin LOOP |
|---|
| 54 | for plugin in plugins: |
|---|
| 55 | # dynamically load the get log function for this plugin |
|---|
| 56 | plugmodule = globals()[plugin] |
|---|
| 57 | getfunc = getattr(plugmodule, 'getLogEntries') |
|---|
| 58 | |
|---|
| 59 | # Run the plugin process returning new log entries |
|---|
| 60 | results = getfunc() |
|---|
| 61 | # Append simulation time and the log entries to the Output Buffer |
|---|
| 62 | for item in results: |
|---|
| 63 | trimmed_item = item.strip() |
|---|
| 64 | if len(trimmed_item) > 0: |
|---|
| 65 | output += timeStamp + ", " + trimmed_item + "\n" |
|---|
| 66 | # END LOOP |
|---|
| 67 | # IF the Output Buffer has any contents THEN |
|---|
| 68 | if len(output) > 0: |
|---|
| 69 | # Write (append) Output Buffer to unified log file as CSV |
|---|
| 70 | # Assumes fields don't contain commas |
|---|
| 71 | print output, |
|---|
| 72 | f = open(logfilepath + outputFilename, "a") |
|---|
| 73 | f.write(output) |
|---|
| 74 | f.close() |
|---|
| 75 | # END IF |
|---|
| 76 | # Wait five seconds |
|---|
| 77 | time.sleep(5) |
|---|
| 78 | |
|---|
| 79 | #END DO |
|---|
| 80 | |
|---|
| 81 | if __name__ == '__main__': |
|---|
| 82 | startup() |
|---|