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

Revision 522, 2.1 KB checked in by jdalbey, 6 years ago (diff)

Unified logger - cad_watcher.py set lastLineNum to zero when fileSize == 0.

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