source: tmcsimulator/trunk/src/python/unifiedlogger/cad_watcher.py @ 471

Revision 471, 2.2 KB checked in by jdalbey, 7 years ago (diff)

Coordinator.java: Fix defect 181 in sorting CADcomments. logging_service error messages improved.

Line 
1import json, time, ConfigParser, os
2from copy import deepcopy
3
4# CAD comment log Watcher
5# Look for changes in the CAD comment log.
6# The CADserver will append new comments to the log as they arrive
7# from clients.  We only need to keep track of the log length in order to
8# determine if a new comment has been added.  We will output the
9# new messages that arrived during the last wait interval.
10# jdalbey  7/6/2019
11
12lastLineNum = 0
13
14# Utility functions
15def isEmpty(cmsitem):
16    return cmsitem == ",,,,,"
17def isFull(cmsitem):
18    return not isEmpty(cmsitem)
19
20def setup():
21    lastLineNum = 0
22    return
23
24# Retrieve new messages from CAD comment log
25def getLogEntries():
26    global lastLineNum
27    # get path to input file from configuration
28    config = ConfigParser.ConfigParser()
29    config.read('config/logging_service.cfg')
30    logfilepath  = config.get('Paths', 'UnifiedLogPath')
31    pathToLog = logfilepath + "CADcomments.log"
32   
33    try:
34        text_file = open(pathToLog, "r")
35    except IOError as ex:
36        if ex.errno == 2:
37            # 'No such file or directory
38            print pathToLog + " missing: assuming reset."
39            lastLineNum = 0   #Start over
40            return []
41        else:
42            print "IOError reading "+pathToLog+" file."
43            print "errno: ",ex.errno
44           
45    else:
46        # Check file size
47        fileSize = os.path.getsize(pathToLog)
48        if fileSize == 0:
49            # Assume this is a read sync problem: Don't modify lastLineNum
50            print pathToLog+" is empty, ignoring."
51            return []
52        else: # file is good, read it.
53            msgList = text_file.read().split('\n')
54            text_file.close()
55            currList = []
56            currList = msgList[lastLineNum:] # new items since last file read
57            lastLineNum = len(msgList)-1
58            return currList
59
60# Local main for unit testing
61def main():
62    setup()
63    # Loop Forever, checking every five seconds
64    while True:
65        # Look for new messages
66        answer = getLogEntries()
67        # Output results
68        for item in answer:
69            print item
70        # wait
71        time.sleep(5)
72
73if __name__ == "__main__":
74    main()
Note: See TracBrowser for help on using the repository browser.