Source code for TCSLog

#!/bin/env python

import sys, time, os
import inspect
from tcssubsystem import *

[docs]class TCSLog: Off = 0, Debug = 1 Info = 2 Warn = 4 Error = 8 Fatal = 16 Alarm = 32 def __init__(self, sourceName=None, logRelayRoute=None, namedRoute=None): """ Create an Iinstance of a TCSLog object. The sourceName cannot be empty and should identify the the script or program doing the logging. The logRelayRoute is the route to the log relay server. The namedRoute is the route to the naemed server. If both are left empty, logging will be restricted to the console. If no relay route is given, but named is, the relay route will be looked up using named. If a relay route is given, it will be used as is. """ SourceName = "" LogRelayRoute = "" NamedRoute = "" if( sourceName != None ): SourceName = sourceName if( logRelayRoute != None ): LogRelayRoute = logRelayRoute if( namedRoute != None and namedRoute != ""): NamedRoute = namedRoute; #print "Calling tcs_init_named" tcs_init_named( namedRoute ) if( logRelayRoute == None ): logRelayRoute = tcs_named_lookup( "log-relay-route" ) time.sleep( .5 ) # This seems to be needed to allow zmq or messaging threads to start tcs_create_logger( SourceName, LogRelayRoute, NamedRoute ) def strip_end(text, suffix): if not text.endswith(suffix): return text return text[:len(text)-len(suffix)] def log( self, level, fmt, *kwargs): # the filename and line number we were called from trace_frame = inspect.currentframe().f_back file_name = trace_frame.f_code.co_filename # if the filename is our own, we were called from one of # routines below, and have to go up another stack frame to find the caller # in user code ... if os.path.basename(file_name).split(".",1)[0] == os.path.basename(__file__).split(".",1)[0]: trace_frame = trace_frame.f_back line_num = trace_frame.f_lineno file_name = trace_frame.f_code.co_filename func_name = trace_frame.f_code.co_name # send out the log message outMsg = fmt.format(*kwargs) return tcs_log( level, outMsg, file_name, func_name, line_num ) def log_debug( self, fmt, *args ): return self.log( self.Debug, fmt, *args ) def log_info( self, fmt, *args ): return self.log(self.Info, fmt, *args) def log_error( self, fmt, *args ): return self.log(self.Error, fmt, *args) def log_warn( self, fmt, *args ): return self.log(self.Warn, fmt, *args) def log_fatal( self, fmt, *args ): return self.log(self.Fatal, fmt, *args ) def log_alarm( self, fmt, *args ): return self.log(self.Alarm, fmt, *args ) def __exit__(self): delete_logger()