| 1 | import sys, cms_watcher, har_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 | # Entry point |
|---|
| 27 | # @param list of command line arguments |
|---|
| 28 | # can provide name of config directory to use |
|---|
| 29 | def 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() |
|---|
| 52 | #END LOOP |
|---|
| 53 | |
|---|
| 54 | #DO Forever |
|---|
| 55 | while True: |
|---|
| 56 | # Get simulation time |
|---|
| 57 | timeStamp = getSimTime() |
|---|
| 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 | # for unit testing |
|---|
| 90 | if __name__ == '__main__': |
|---|
| 91 | startup(sys.argv) |
|---|