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

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

Update actitivylog watcher to obtain path from separate entry in config file. Because in production, the path will be in a separate directory from other log files.

Line 
1import json, time, ConfigParser
2#from 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', 'ActivityLogPath')
27    logfilename = "data.json"   #"IncidentActivity.log"
28    lines = []
29   
30    try:
31        json_file = open (logfilepath + logfilename,'r')
32    except IOError:
33        print "Error: missing "+logfilepath+logfilename+" file."
34    else:
35        jsonData=json_file.read()
36        json_file.close()
37    return json.loads(jsonData)['data']   
38   
39def setup():
40    # nothing needed for setup
41    return
42
43# Retrieve new messages from activity log
44def getLogEntries():
45    global lastLineNum
46    msgList = readFile()
47    currList = []
48    currList = msgList[lastLineNum:]    # new items since last file read
49    lastLineNum = len(msgList)
50    resultList = []
51    # Format messages into desired result format
52    for item in currList:
53        # extract desired fields
54        name = item[3].replace(',','')
55        code = item[4].replace(',','')
56        msg = item[5].replace(',','')
57        desiredFields = "Activity Log.,"+name+","+code+" "+msg
58        # Append to results list
59        resultList.append(desiredFields)
60    return resultList
61
62# Local main for unit testing
63def main():
64    setup()
65    # Loop Forever, checking every five seconds
66    while True:
67        # Look for new messages
68        answer = getLogEntries()
69        # Output results
70        for item in answer:
71            print item
72        # wait
73        time.sleep(5)
74
75if __name__ == "__main__":
76    main()
Note: See TracBrowser for help on using the repository browser.