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

Revision 461, 2.0 KB checked in by jdalbey, 7 years ago (diff)

add activitylog_watcher to Unified Logging Service.

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)-1
49    resultList = []
50    # Format messages into desired result format
51    for item in currList:
52        # extract desired fields
53        desiredFields = "Activity Log: "+item[3]+item[4]+item[5]
54        # Append to results list
55        resultList.append(desiredFields)
56    return resultList
57
58# Local main for unit testing
59def main():
60    setup()
61    # Loop Forever, checking every five seconds
62    while True:
63        # Look for new messages
64        answer = getLogEntries()
65        # Output results
66        for item in answer:
67            print item
68        # wait
69        time.sleep(5)
70
71if __name__ == "__main__":
72    main()
Note: See TracBrowser for help on using the repository browser.