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

Revision 689, 2.7 KB checked in by jdalbey, 4 years ago (diff)

cad_watcher.py Enhance to deal with system restart during logging session.

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
13configdir = "config"  # default dir for production
14
15def setup(dir):
16    global lastLineNum, configdir
17    lastLineNum = 0
18    configdir = dir
19    return
20
21# Retrieve new messages from CAD comment log
22def getLogEntries():
23    global lastLineNum, configdir
24    # get path to input file from configuration
25    config = ConfigParser.ConfigParser()
26    config.read(configdir+'/logging_service.cfg')
27    logfilepath  = config.get('Paths', 'UnifiedLogPath')
28    pathToLog = logfilepath + "CADcomments.log"
29   
30    try:
31        text_file = open(pathToLog, "r")
32    except IOError as ex:
33        if ex.errno == 2:
34            # 'No such file or directory' - could be system was restarted or
35            # this is a read sync problem: Don't modify lastLineNum
36            print pathToLog + " missing, skipping file read."
37            #lastLineNum = 0   #Start over
38            return []
39        else:
40            print "IOError reading "+pathToLog+" file."
41            print "errno: ",ex.errno
42           
43    else:
44        # Check file size
45        fileSize = os.path.getsize(pathToLog)
46        if fileSize == 0:
47            # Assume this is a read sync problem: Don't modify lastLineNum
48            # because once the file is synced it will have same # of lines.
49            print pathToLog+" is empty, skipping file read."
50            #lastLineNum = 0   #Start over
51            return []
52        else: # file is good, read it.
53            msgList = text_file.read().strip().split('\n')
54            text_file.close()
55            currList = []
56            # If the system restarted there is usually fewer lines in the file
57            # than lastLineNum, so assume all lines are new and reset lastLineNum
58            if len(msgList) < lastLineNum:
59                print pathToLog + " has few lines than expected, assuming restart."
60                lastLineNum = 0
61            currList = msgList[lastLineNum:] # new items since last file read
62            lastLineNum = len(msgList)
63            return currList
64
65# Local main for unit testing
66def main():
67    setup("config/devlinux")
68    # Loop Forever, checking every five seconds
69    while True:
70        # Look for new messages
71        answer = getLogEntries()
72        # Output results
73        for item in answer:
74            print item
75        # wait
76        time.sleep(5)
77
78if __name__ == "__main__":
79    main()
Note: See TracBrowser for help on using the repository browser.