Package up2date_client :: Module up2dateLog
[hide private]
[frames] | no frames]

Source Code for Module up2date_client.up2dateLog

 1  # 
 2   
 3  import time 
 4  import traceback 
 5  from rhn.i18n import ustr, sstr 
 6  from up2date_client import config 
 7   
8 -class Log:
9 """ 10 attempt to log all interesting stuff, namely, anything that hits 11 the network any error messages, package installs, etc 12 """ # " emacs sucks
13 - def __init__(self):
14 self.app = "up2date" 15 self.cfg = config.initUp2dateConfig() 16 self.log_info = ''
17
18 - def set_app_name(self, name):
19 self.app = str(name)
20
21 - def log_debug(self, *args):
22 if self.cfg["debug"] > 1: 23 self.log_me("D: ", *args)
24
25 - def log_me(self, *args):
26 """General logging function. 27 Eg: log_me("I am a banana.") 28 29 """ 30 self.log_info = "[%s] %s" % (time.ctime(time.time()), self.app) 31 s = u"" 32 for i in args: 33 # we really need unicode(str(i)) here, because i can be anything 34 # from string or int to list, dict or even class 35 i = ustr(str(i)) 36 s += i 37 if self.cfg["debug"] > 1: 38 print(s) 39 self.write_log(s)
40
41 - def trace_me(self):
42 self.log_info = "[%s] %s" % (time.ctime(time.time()), self.app) 43 x = traceback.extract_stack() 44 msg = ''.join(traceback.format_list(x)) 45 self.write_log(msg)
46
47 - def log_exception(self, logtype, value, tb):
48 self.log_info = "[%s] %s" % (time.ctime(time.time()), self.app) 49 output = ["\n"] # Accumulate the strings in a list 50 output.append("Traceback (most recent call last):\n") 51 output = output + traceback.format_list(traceback.extract_tb(tb)) 52 output.append("%s: %s\n" % (logtype, value)) 53 self.write_log("".join(output))
54
55 - def write_log(self, s):
56 57 log_name = self.cfg["logFile"] or "/var/log/up2date" 58 log_file = open(log_name, 'a') 59 msg = u"%s %s\n" % (ustr(self.log_info), ustr(s)) 60 log_file.write(sstr(msg)) 61 log_file.flush() 62 log_file.close()
63
64 -def initLog():
65 global log 66 try: 67 log = log 68 except NameError: 69 log = None 70 71 if log == None: 72 log = Log() 73 74 return log
75