Changeset 467 in tmcsimulator for trunk/src/python/unifiedlogger/cad_watcher.py


Ignore:
Timestamp:
07/27/2019 08:23:36 PM (7 years ago)
Author:
jdalbey
Message:

cad_watcher.py added more elaborate error handling to cope with missing or empty CADcomments.log. Hopefully fixes #177.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/python/unifiedlogger/cad_watcher.py

    r466 r467  
    1 import json, time, ConfigParser 
     1import json, time, ConfigParser, os 
    22from copy import deepcopy 
    33 
     
    1818    return not isEmpty(cmsitem) 
    1919 
    20 # Read the cad comments log  
    21 def readFile(): 
    22     # get path to input file from configuration 
    23     config = ConfigParser.ConfigParser() 
    24     config.read('config/logging_service.cfg') 
    25     logfilepath  = config.get('Paths', 'UnifiedLogPath') 
    26      
    27     lines = [] 
    28     try: 
    29         text_file = open(logfilepath + "CADcomments.log", "r") 
    30     except IOError: 
    31         print "Error: missing "+logfilepath+"CADcomments.log file." 
    32     else: 
    33         lines = text_file.read().split('\n') 
    34         text_file.close()     
    35     return lines 
    36      
    3720def setup(): 
    3821    lastLineNum = 0 
     
    4225def getLogEntries(): 
    4326    global lastLineNum 
    44     msgList = readFile() 
    45     currList = [] 
    46     currList = msgList[lastLineNum:]    # new items since last file read 
    47     lastLineNum = len(msgList)-1 
    48     return currList 
     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." 
     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 
    4959 
    5060# Local main for unit testing 
Note: See TracChangeset for help on using the changeset viewer.