Package install :: Package MoSTBioDat :: Package Log :: Module MoSTBioDatLogParse
[hide private]
[frames] | no frames]

Source Code for Module install.MoSTBioDat.Log.MoSTBioDatLogParse

  1  #!/usr/bin/env python 
  2  #################################### 
  3  # MoSTBioDatLogParse.py:           # 
  4  #Parser for MoSTBioDatLog module   # 
  5  #################################### 
  6   
  7  ###################################################### 
  8  # Copyright (c) 2007-2008 Andrzej Bak                # 
  9  # ARC Seibersdorf & University of Silesia            # 
 10  # Author: Andrzej Bak <Andrzej.Bak@us.edu.pl>        # 
 11  # License: GNU General Public License, version: 3    # 
 12  # URL: http://chemoinformatyka.us.edu.pl/mostbiodat/ # 
 13  # Version: 1, 19.11.2008                             # 
 14  ###################################################### 
 15   
 16  try: 
 17      import sys 
 18      import os 
 19      from MoSTBioDatLog import datetime 
 20      from getpass import getuser 
 21  except ImportError,e: 
 22      print 'Error: %s' %e 
 23      sys.exit(1) 
 24  ###### MoSTBioDatLogParse class ###################### 
25 -class MoSTBioDatLogParse(object):
26 - def __init__(self,path='.',filename='log'):
27 """ 28 INPUT: 29 path - str - directory path to log file, default='.' 30 filename - str- log file name, default='log' 31 """ 32 self.path=path 33 self.filename=filename 34 self.findLog()#find log
35
36 - def findLog(self):
37 """ 38 find logging file 39 INPUT: 40 class object 41 OUTPUT: 42 """ 43 fullfilename=os.path.join(self.path,self.filename) 44 if (os.path.exists(fullfilename) and os.path.isfile(fullfilename)): 45 if os.access(fullfilename,os.R_OK): 46 try: 47 file=open(fullfilename,'r') 48 except IOError,error: 49 print 'Error: %s for %s' %(error,fullfilename) 50 else: 51 self.file=file 52 else: 53 print 'Permission denied: %s' %fullfilename 54 else: 55 print 'Unable to find file!'
56
57 - def findLogLevel(self,level='INFO'):
58 """ 59 find logging level lines 60 INPUT: 61 object class 62 level - str - logging level 'INFO', 'CRITICAL', 'FATAL', 'WARNING','DEBUG','NOTSET', default INFO 63 OUTPUT: 64 """ 65 if self.file.closed: 66 print 'Error: File has been closed by previous function!' 67 sys.exit(1) 68 69 levels =("NOTSET","CRITICAL","FATAL","WARNING","INFO","DEBUG") 70 if not level.isupper(): 71 level=level.upper() 72 if level in levels: 73 counter=0 74 find=0 75 while True: 76 line=self.file.readline() 77 counter+=1 78 if not line: break 79 if line.startswith(' '): continue 80 loglevel=line.split(' ')[0] 81 if loglevel==level: 82 print '%i: %s' %(counter,line) 83 find+=1 84 else: continue 85 if not find: 86 print 'Logging level not found!' 87 self.file.close() 88 else: 89 print 'Wrong logging level!'
90
91 - def findIP(self,ip='127.0.0.1'):
92 """ 93 find IP lines in logging file 94 INPUT: 95 class object 96 ip - str - default 172.0.0.1 97 OUTPUT: 98 """ 99 if self.file.closed: 100 print 'Error: File has been closed by previous function!' 101 sys.exit(1) 102 counter=0 103 find=0 104 while True: 105 line=self.file.readline() 106 counter+=1 107 if not line: break 108 if line.startswith(' '): continue 109 ipaddr=line.split(' ')[3] 110 if ipaddr==ip: 111 print '%i: %s' %(counter,line) 112 find+=1 113 else: continue 114 if not find: 115 print 'IP not found!' 116 self.file.close()
117
118 - def findUser(self,user=''):
119 """ 120 find user lines in logging file 121 INPUT: 122 class object 123 user - str - if '' get current user 124 OUTPUT: 125 """ 126 if self.file.closed: 127 print 'Error: File has been closed by previous function!' 128 sys.exit(1) 129 if user=='': 130 user=getuser() 131 counter=0 132 find=0 133 while True: 134 line=self.file.readline() 135 counter+=1 136 if not line: break 137 if line.startswith(' '): continue 138 usr=line.split(' ')[4] 139 if user==usr: 140 print '%i: %s' %(counter,line) 141 find+=1 142 else: continue 143 if not find: 144 print 'User not found!' 145 self.file.close()
146
147 - def findModule(self,module=''):
148 """ 149 find module lines in logging file 150 INPUT: 151 class object 152 module - str, default '' 153 OUTPUT: 154 """ 155 if self.file.closed: 156 print 'Error: File has been closed by previous function!' 157 sys.exit(1) 158 if module=="": 159 print 'No module name!' 160 else: 161 counter=0 162 find=0 163 while True: 164 line=self.file.readline() 165 counter+=1 166 if not line: break 167 if line.startswith(' '): continue 168 mod=line.split(' ')[6] 169 if module==mod: 170 print '%i: %s' %(counter,line) 171 find+=1 172 else: continue 173 if not find: 174 print 'Module not found!' 175 self.file.close()
176
177 - def findFunction(self,function=''):
178 """ 179 find function lines in logging file 180 INPUT: 181 class object 182 function - str, default '' 183 OUTPUT: 184 """ 185 if self.file.closed: 186 print 'Error: File has been closed by previous function!' 187 sys.exit(1) 188 if function=="": 189 print 'No function name!' 190 else: 191 counter=0 192 find=0 193 while True: 194 line=self.file.readline() 195 counter+=1 196 if not line: break 197 if line.startswith(' '): continue 198 fun=line.split(' ')[7] 199 if function==fun: 200 print '%i: %s' %(counter,line) 201 find+=1 202 else: continue 203 if not find: 204 print 'Function not found!' 205 self.file.close()
206
207 - def findDate(self,date=''):
208 """ 209 find date lines in loggin file 210 INPUT: 211 class object 212 date - str, default '', if '' get current date 213 OUTPUT: 214 """ 215 if self.file.closed: 216 print 'Error: File has been closed by previous function!' 217 sys.exit(1) 218 if date=='': 219 date=datetime() 220 date=date.split(' ')[0] 221 counter=0 222 find=0 223 while True: 224 line=self.file.readline() 225 counter+=1 226 if not line: break 227 if line.startswith(' '): continue 228 dat=line.split(' ')[1] 229 if date==dat: 230 print '%i: %s' %(counter,line) 231 find+=1 232 else: continue 233 if not find: 234 print 'Date not found!' 235 self.file.close()
236 237 ############ MAIN ################################## 238 if __name__=='__main__': 239 pass 240 # A=MoSTBioDatLogParse(path='/tmp/Log',filename='db2smi.log') 241 # A.findLogLevel(level='Info') 242 # A=MoSTBioDatLogParse(path='/tmp/Log',filename='db2smi.log') 243 # A.findIP('172.30.15.40') 244 # A=MoSTBioDatLogParse(path='/tmp/Log',filename='db2smi.log') 245 # A.findUser(user='baka') 246 # A=MoSTBioDatLogParse(path='/tmp/Log',filename='db2smi.log') 247 # A.findModule(module='MoSTBioDatDB') 248 # A=MoSTBioDatLogParse(path='/tmp/Log',filename='db2smi.log') 249 # A.findFunction(function='closeDB') 250 # A=MoSTBioDatLogParse(path='/tmp/Log',filename='db2smi.log') 251 # A.findDate(date='2007-10-26') 252