source: tmcsimulator/trunk/src/python/unifiedlogger/activitylog_watcher.py @ 470

Revision 470, 2.1 KB checked in by jdalbey, 7 years ago (diff)

activity log watcher modified to output comma-delimited fields. Fixes #179.

Line 
1import json, time, ConfigParser
2from copy import deepcopy
3
4# Incident Activity log Watcher
5# Look for changes in the IncidentActivity log.
6# The comments from the Incident Activity log be will appended to the log
7# as they arrive from the web app.
8# We only need to keep track of the log length in order to
9# determine if a new comment has been added.  We will output the
10# new messages that arrived during the last wait interval.
11# jdalbey  7/24/2019
12
13lastLineNum = 0
14
15# Utility functions
16def isEmpty(cmsitem):
17    return cmsitem == ",,,,,"
18def isFull(cmsitem):
19    return not isEmpty(cmsitem)
20
21# Read the log
22def readFile():
23    # get path to input file from configuration
24    config = ConfigParser.ConfigParser()
25    config.read('config/logging_service.cfg')
26    logfilepath  = config.get('Paths', 'UnifiedLogPath')
27   
28    lines = []
29    try:
30        json_file = open (logfilepath + "IncidentActivity.log",'r')
31    except IOError:
32        print "Error: missing "+logfilepath+"IncidentActivity.log file."
33    else:
34        jsonData=json_file.read()
35        json_file.close()
36    return json.loads(jsonData)['data']   
37   
38def setup():
39    # nothing needed for setup
40    return
41
42# Retrieve new messages from activity log
43def getLogEntries():
44    global lastLineNum
45    msgList = readFile()
46    currList = []
47    currList = msgList[lastLineNum:]    # new items since last file read
48    lastLineNum = len(msgList)
49    resultList = []
50    # Format messages into desired result format
51    for item in currList:
52        # extract desired fields
53        name = item[3].replace(',','')
54        code = item[4].replace(',','')
55        msg = item[5].replace(',','')
56        desiredFields = "Activity Log.,"+name+","+code+","+msg
57        # Append to results list
58        resultList.append(desiredFields)
59    return resultList
60
61# Local main for unit testing
62def main():
63    setup()
64    # Loop Forever, checking every five seconds
65    while True:
66        # Look for new messages
67        answer = getLogEntries()
68        # Output results
69        for item in answer:
70            print item
71        # wait
72        time.sleep(5)
73
74if __name__ == "__main__":
75    main()
Note: See TracBrowser for help on using the repository browser.