Ticket #6970: pyth.mythlog.patch
File pyth.mythlog.patch, 14.2 KB (added by , 15 years ago) |
---|
-
MythTV/MythVideo.py
35 35 try: 36 36 cur.execute(u"select * from storagegroup") 37 37 except MySQLdb.Error, e: 38 log.Msg(INFO, u"! Error: Reading storagegroup MythTV table: %d: %s\n" % (e.args[0], e.args[1]))39 38 return None 40 39 41 40 videos_dir = [] … … 102 101 if os.path.exists(filename): # Handle multiple Videos SGs for one backend 103 102 break 104 103 else: 105 log. Msg(INFO, u'%s not exist, removing metadata...',filename)104 log.info('Videometadata',u'%s not exist, removing metadata...'% filename) 106 105 rmMetadata(intid) 107 106 c.close() 108 107 … … 322 321 c.close() 323 322 return intid 324 323 else: 325 log. Msg(DEBUG, 'Updating metadata for %s',id)324 log.debug('Videometadata', 'Updating metadata for %s' % id) 326 325 format_string = ', '.join(['%s = %%s' % d for d in data]) 327 326 sql = "UPDATE videometadata SET %s WHERE intid = %%s" % format_string 328 327 sql_values = data.values() … … 341 340 c = self.db.cursor() 342 341 if isinstance(video, str): 343 342 if video[0] !='/' and host == None: # Protect against accidental multiple deletions on SG's 344 log. Msg(WARNING, u"Attempting to delete a video for a storage group when the hostname is not supplied. That could potentially remove the videometadata on multiple backends " +343 log.warning('Metadata delete', u"Attempting to delete a video for a storage group when the hostname is not supplied. That could potentially remove the videometadata on multiple backends " + 345 344 u"for videometadata: %s" % video) 346 345 return 347 346 intid = self.getMetadataId(video, host) 348 347 if intid == None: 349 log. Msg(WARNING, u"Attempt to delete a video that does not exist " +348 log.warning('Metadata delete', u"Attempt to delete a video that does not exist " + 350 349 u"for videometadata: %s" % video) 351 350 return 352 351 c.execute("DELETE FROM videometadata WHERE intid = %s", (intid,)) … … 354 353 elif isinstance(video, int) or isinstance(video, long): 355 354 c.execute("DELETE FROM videometadata WHERE intid = %s", (video,)) 356 355 else: 357 log. Msg(WARNING, "Attempt to delete non-str, non-int item" +356 log.warning('Metadata delete', "Attempt to delete non-str, non-int item" + 358 357 "from videometadata: %s" % str(video)) 359 358 # Clean up this video's relationships to Genres, Country and Cast 360 359 self.cleanGenres(video) -
MythTV/MythTV.py
20 20 from MythDB import * 21 21 from MythLog import * 22 22 23 log = MythLog(CRITICAL, ' #%(levelname)s - %(message)s', 'MythTV')23 log = MythLog(CRITICAL, 'MythTV.py') 24 24 25 25 RECSTATUS = { 26 26 'TunerBusy': -8, … … 59 59 self.master_port = int(self.db.getSetting('MasterServerPort')) 60 60 61 61 if not self.master_host: 62 log. Msg(CRITICAL,'Unable to find MasterServerIP in database')62 log.critical('Settings','Unable to find MasterServerIP in database') 63 63 sys.exit(1) 64 64 if not self.master_port: 65 log. Msg(CRITICAL,'Unable to find MasterServerPort in database')65 log.critical('Settings','Unable to find MasterServerPort in database') 66 66 sys.exit(1) 67 67 68 68 try: … … 71 71 self.socket.connect((self.master_host, self.master_port)) 72 72 res = self.backendCommand('MYTH_PROTO_VERSION %s' % PROTO_VERSION).split(BACKEND_SEP) 73 73 if res[0] == 'REJECT': 74 log. Msg(CRITICAL, 'Backend has version %s and we speak version %s', res[1], PROTO_VERSION)74 log.critical('Protocol Mismatch',"Backend has version %s and we speak version %s" % (res[1], PROTO_VERSION)) 75 75 sys.exit(1) 76 76 res = self.backendCommand('ANN %s %s 0' % (conn_type, socket.gethostname())) 77 77 if res != 'OK': 78 log. Msg(CRITICAL, 'Unexpected answer to ANN command: %s',res)78 log.critical('Protocol Error',"Unexpected answer to ANN command: %s" % res) 79 79 else: 80 log. Msg(INFO, 'Successfully connected mythbackend at %s:%d', self.master_host, self.master_port)80 log.info('Info',"Successfully connected mythbackend at %s:%d" % (self.master_host, self.master_port)) 81 81 except socket.error, e: 82 log. Msg(CRITICAL, 'Couldn\'t connect to %s:%d (is the backend running)', self.master_host, self.master_port)82 log.critical('Protocol Error',"Couldn't connect to %s:%d (is the backend running)" % (self.master_host, self.master_port)) 83 83 sys.exit(1) 84 84 85 85 def __del__(self): … … 114 114 return u''.join(data) 115 115 116 116 command = u'%-8d%s' % (len(data), data) 117 log. Msg(DEBUG, 'Sending command: %s',command)117 log.debug('Info','Sending command: %s' % command) 118 118 self.socket.send(command) 119 119 return recv() 120 120 … … 126 126 res = self.backendCommand('QUERY_GETALLPENDING').split(BACKEND_SEP) 127 127 has_conflict = int(res.pop(0)) 128 128 num_progs = int(res.pop(0)) 129 log. Msg(DEBUG, '%s pending recordings',num_progs)129 log.debug('Info','%s pending recordings' % num_progs) 130 130 for i in range(num_progs): 131 131 programs.append(Program(res[i * PROGRAM_FIELDS:(i * PROGRAM_FIELDS) 132 132 + PROGRAM_FIELDS])) … … 139 139 programs = [] 140 140 res = self.backendCommand('QUERY_GETALLSCHEDULED').split(BACKEND_SEP) 141 141 num_progs = int(res.pop(0)) 142 log. Msg(DEBUG, '%s scheduled recordings',num_progs)142 log.debug('Info','%s scheduled recordings' % num_progs) 143 143 for i in range(num_progs): 144 144 programs.append(Program(res[i * PROGRAM_FIELDS:(i * PROGRAM_FIELDS) 145 145 + PROGRAM_FIELDS])) … … 247 247 programs = [] 248 248 res = self.backendCommand('QUERY_RECORDINGS Play').split('[]:[]') 249 249 num_progs = int(res.pop(0)) 250 log. Msg(DEBUG, '%s total recordings',num_progs)250 log.debug('Info','%s total recordings' % num_progs) 251 251 for i in range(num_progs): 252 252 programs.append(Program(res[i * PROGRAM_FIELDS:(i * PROGRAM_FIELDS) 253 253 + PROGRAM_FIELDS])) … … 414 414 elif mode == 'w': 415 415 write = 1 416 416 else: 417 log. Msg(CRITICAL, 'Invalid FileTransfer mode given')417 log.critical('Filetransfer', 'Invalid FileTransfer mode given') 418 418 sys.exit(1) 419 419 if isinstance(file, Program): 420 420 match = regex.match(file.filename) … … 426 426 self.port = 6543 427 427 elif isinstance(file, tuple): 428 428 if len(file) != 3: 429 log. Msg(CRITICAL, 'Incorrect FileTransfer() input size')429 log.critical('Filetransfer', 'Incorrect FileTransfer() input size') 430 430 sys.exit(1) 431 431 else: 432 432 self.host = file[0] … … 436 436 elif isinstance(file, str): 437 437 match = regex.match(file) 438 438 if match is None: 439 log. Msg(CRITICAL, 'Incorrect FileTransfer() input string: %s' % file)439 log.critical('Filetransfer', 'Incorrect FileTransfer() input string: %s' % file) 440 440 sys.exit(1) 441 441 self.sgroup = match.group('group') 442 442 self.host = match.group('host') … … 447 447 if self.port is None: 448 448 self.port = 6543 449 449 else: 450 log. Msg(CRITICAL, 'Improper input to FileTransfer()')450 log.critical('Filetransfer', 'Improper input to FileTransfer()') 451 451 sys.exit(1) 452 452 453 453 try: … … 456 456 self.datsock.connect((self.host, self.port)) 457 457 res = self.send('MYTH_PROTO_VERSION %s' % PROTO_VERSION).split(BACKEND_SEP) 458 458 if res[0] == 'REJECT': 459 log. Msg(CRITICAL, 'Backend has version %s and we speak version %s', res[1], PROTO_VERSION)459 log.critical('Filetransfer', 'Backend has version %s and we speak version %s' % (res[1], PROTO_VERSION)) 460 460 sys.exit(1) 461 461 res = self.send('ANN FileTransfer %s %d %d %d%s%s%s%s' % (socket.gethostname(), write, False, -1, BACKEND_SEP, self.filename, BACKEND_SEP, self.sgroup)) 462 462 if res.split(BACKEND_SEP)[0] != 'OK': 463 log. Msg(CRITICAL, 'Unexpected answer to ANN command: %s',res)463 log.critical('Filetransfer', 'Unexpected answer to ANN command: %s' % res) 464 464 else: 465 log. Msg(INFO, 'Successfully connected mythbackend at %s:%d', self.host, self.port)465 log.info('Filetransfer', 'Successfully connected mythbackend at %s:%d' % (self.host, self.port)) 466 466 sp = res.split(BACKEND_SEP) 467 467 self.sockno = int(sp[1]) 468 468 self.pos = 0 469 469 self.size = (int(sp[2]) + (int(sp[3])<0))*2**32 + int(sp[3]) 470 470 471 471 except socket.error, e: 472 log. Msg(CRITICAL, 'Couldn\'t connect to %s:%d (is the backend running)', self.host, self.port)472 log.critical('Filetransfer', "Couldn't connect to %s:%d (is the backend running)" % (self.host, self.port)) 473 473 sys.exit(1) 474 474 475 475 def __del__(self): … … 481 481 482 482 def send(self,data): 483 483 command = '%-8d%s' % (len(data), data) 484 log. Msg(DEBUG, 'Sending command: %s',command)484 log.debug('Filetransfer', 'Sending command: %s' % command) 485 485 self.datsock.send(command) 486 486 return self.recv() 487 487 … … 588 588 if offset < -self.size: 589 589 offset = -self.size 590 590 else: 591 log. Msg(CRITICAL, 'Whence can only be 0, 1, or 2')591 log.critical('Filetransfer', 'Whence can only be 0, 1, or 2') 592 592 593 593 curhigh,curlow = self.comsock.splitInt(self.pos) 594 594 offhigh,offlow = self.comsock.splitInt(offset) -
MythTV/MythLog.py
2 2 Provides simple logging and exception classes. 3 3 """ 4 4 5 import logging 5 from MythDB import * 6 from socket import gethostname 7 from datetime import datetime 8 from sys import version_info 6 9 10 db = MythDB() 11 7 12 # Vars to imporove readability 8 CRITICAL = logging.CRITICAL 9 FATAL = logging.FATAL 10 ERROR = logging.ERROR 11 WARNING = logging.WARNING 12 INFO = logging.INFO 13 DEBUG = logging.DEBUG 13 ALL = 8 14 DEBUG = 7 15 INFO = 6 16 NOTICE = 5 17 WARNING = 4 18 ERROR = 3 19 CRITICAL = 2 20 ALERT = 1 21 EMERGENCY = 0 14 22 15 23 class MythLog: 16 24 """ 17 25 A simple logging class 18 26 """ 19 def __init__(self, level, format, instance):20 self.log = logging.getLogger(instance)21 self.log.setLevel(level)22 self.ch = logging.StreamHandler()23 self.ch.setFormatter(logging.Formatter(format))24 self.log.addHandler(self.ch)25 27 26 def Msg(self, level, msg, *args, **kwargs): 27 self.log.log(level, msg, *args, **kwargs) 28 priority = WARNING 28 29 30 def __init__(self, priority=WARNING, module='pythonbindings', mode=3): 31 self.setPriority(priority) 32 if isinstance(module,str): 33 self.mod = module 34 self.host = gethostname() 35 self.mode = mode 36 if db.getSetting('LogEnabled') == 0: 37 self.mode %= 2 38 39 def setPriority(self, priority): 40 if priority in range(0,9): 41 self.priority = priority 42 43 def log(self, priority, message, detail=''): 44 if priority > self.priority: 45 return 46 now = datetime.now() 47 nowstr = None 48 if (version_info[0] > 2) | (version_info[1] > 5): 49 nowstr = now.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3] 50 else: 51 nowstr = now.strftime('%Y-%m-%d %H:%M:%S.000') 52 if self.mode & 1: 53 if len(detail): 54 print "%s %s: %s -- %s" % (nowstr, self.mod, message, detail) 55 else: 56 print "%s %s: %s" % (nowstr, self.mod, message) 57 if self.mode & 2: 58 cursor = db.cursor() 59 cursor.execute("""INSERT INTO mythlog 60 (module, priority, logdate, host, message, details) 61 VALUES (%s, %s, %s, %s, %s, %s)""", 62 (self.mod,priority,now,self.host,message,detail)) 63 64 def all(self, message, detail=''): 65 self.log(ALL,message,detail) 66 67 def debug(self, message, detail=''): 68 self.log(DEBUG,message,detail) 69 70 def info(self, message, detail=''): 71 self.log(INFO,message,detail) 72 73 def notice(self, message, detail=''): 74 self.log(NOTICE,message,detail) 75 76 def warning(self, message, detail=''): 77 self.log(WARNING,message,detail) 78 79 def error(self, message, detail=''): 80 self.log(ERROR,message,detail) 81 82 def critical(self, message, detail=''): 83 self.log(CRITICAL,message,detail) 84 85 def alert(self, message, detail=''): 86 self.log(ALERT,message,detail) 87 88 def emergency(self, message, detail=''): 89 self.log(EMERGENCY,message,detail) 90 29 91 class MythError: 30 92 """ 31 93 A simple exception class … … 36 98 def __repr__(self): 37 99 print ': ' + self.message 38 100 101 39 102 # vim: ts=4 sw=4: -
MythTV/MythDB.py
10 10 import getopt 11 11 from datetime import datetime 12 12 13 from MythLog import *14 15 # create logging object16 log = MythLog(CRITICAL, '#%(levelname)s - %(message)s', 'MythDB')17 18 13 # check for dependency 19 14 try: 20 15 import MySQLdb 21 16 except: 22 log.Msg(CRITICAL, "MySQLdb (python-mysqldb) is required but is not found.") 23 sys.exit(1) 17 raise MythError("MySQLdb (python-mysqldb) is required but is not found.") 24 18 25 19 class MythDB: 26 20 """ … … 73 67 pass 74 68 75 69 if dbconn['host'] != None and dbconn['name'] != None and dbconn['user'] != None and dbconn['pass'] != None: 76 log.Msg(INFO, 'Using config %s', config_file)70 # log.Msg(INFO, 'Using config %s', config_file) 77 71 found_config = True 78 72 break 79 73 … … 97 91 98 92 try: 99 93 self.db = MySQLdb.connect(user=dbconn['user'], host=dbconn['host'], passwd=dbconn['pass'], db=dbconn['name'], use_unicode=True, charset='utf8') 100 log.Msg(INFO, 'DB Connection info (host:%s, name:%s, user:%s, pass:%s)', dbconn['host'], dbconn['name'], dbconn['user'], dbconn['pass'])94 # log.Msg(INFO, 'DB Connection info (host:%s, name:%s, user:%s, pass:%s)', dbconn['host'], dbconn['name'], dbconn['user'], dbconn['pass']) 101 95 except: 102 96 raise MythError('Connection failed for \'%s\'@\'%s\' to database %s using password %s' % (dbconn['user'], dbconn['host'], dbconn['name'], dbconn['pass'])) 103 97 … … 108 102 Returns None if there are no settings. If multiple rows are 109 103 found (multiple hostnames), returns the value of the first one. 110 104 """ 111 log.Msg(DEBUG, 'Retrieving all setting for host %s', hostname)105 # log.Msg(DEBUG, 'Retrieving all setting for host %s', hostname) 112 106 c = self.db.cursor() 113 107 if hostname is None: 114 108 c.execute(""" … … 136 130 Returns None if the setting was not found. If multiple rows are 137 131 found (multiple hostnames), returns the value of the first one. 138 132 """ 139 log.Msg(DEBUG, 'Looking for setting %s for host %s', value, hostname)133 # log.Msg(DEBUG, 'Looking for setting %s for host %s', value, hostname) 140 134 c = self.db.cursor() 141 135 if hostname is None: 142 136 c.execute(""" … … 162 156 """ 163 157 Sets the value for the given MythTV setting. 164 158 """ 165 log.Msg(DEBUG, 'Setting %s for host %s to %s', value, hostname, data)159 # log.Msg(DEBUG, 'Setting %s for host %s to %s', value, hostname, data) 166 160 c = self.db.cursor() 167 161 ws = None 168 162 ss = None