Pella86

Untitled

Jul 27th, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. # impoorts
  2.  
  3. # py imports
  4.  
  5. import time
  6. import datetime
  7.  
  8. # Timing utils
  9.  
  10. class Logger:
  11.     ''' This class will manage the logging '''
  12.    
  13.     def __init__(self, title = None, pathfile = None, debug_mode = True, debug_level = 0):
  14.         self.path_file = pathfile
  15.         self.debug_mode = debug_mode
  16.        
  17.         self.starttime = time.perf_counter()
  18.        
  19.         self.nowtime = time.perf_counter()
  20.        
  21.         self.lastcall = time.perf_counter()
  22.         self.debug_level = debug_level
  23.        
  24.         if title is not None:
  25.             today = datetime.datetime.now()  
  26.             s = title + " program started the " + today.strftime("%d of %b %Y at %H:%M")
  27.             self.log("=============================================================\n" +
  28.                      s +
  29.                      "\n=============================================================")          
  30.  
  31.  
  32.     def convert_in_ddhhss(self, seconds):
  33.         hh = 0
  34.         mm = 0
  35.         ss = 0
  36.        
  37.         ms = str(seconds % 1)[1:5]
  38.        
  39.         mm, ss = divmod(int(seconds), 60)
  40.         hh, mm = divmod(mm, 60)    
  41.        
  42.         return "{0:0>2}:{1:0>2}:{2:0>2}{3}".format(hh, mm, ss, ms)
  43.  
  44.     def startTimer(self):
  45.         self.lastcall = time.perf_counter()
  46.    
  47.    
  48.     def getSubTimerStr(self):
  49.         nowtime = time.perf_counter()
  50.         subtime =  nowtime - self.lastcall
  51.         subtimestr = self.convert_in_ddhhss(subtime)
  52.         s  = "Elapsed time for subprocess: {0}\n".format(subtimestr)
  53.         return s
  54.    
  55.     def getTotTimeStr(self):
  56.         nowtime = time.perf_counter()
  57.         totaltime = nowtime - self.starttime
  58.         totaltimestr = self.convert_in_ddhhss(totaltime)
  59.         s = "Total elapsed time: {0}".format(totaltimestr)  
  60.         return s            
  61.    
  62.     def gettimestr(self):
  63.         self.nowtime = time.perf_counter()
  64.         subtime =  self.nowtime - self.lastcall
  65.         subtime = self.convert_in_ddhhss(subtime)
  66.         s  = "Elapsed time for subprocess: {0}\n".format(subtime)
  67.        
  68.         totaltime = self.nowtime - self.starttime
  69.         totaltime = self.convert_in_ddhhss(totaltime)
  70.         s += "Total elapsed time: {0}".format(totaltime)
  71.        
  72.         self.lastcall = time.perf_counter()
  73.         return s
  74.    
  75.  
  76.    
  77.     def log(self, title, time_sub = False, time_tot = False):
  78.         title = title.encode('ascii', errors='replace').decode('utf-8')
  79.        
  80.         s = title
  81.         if time_sub or time_tot:
  82.             s += '\n'
  83.         if time_sub:
  84.             s += self.getSubTimerStr() + '\n'
  85.         if time_tot:
  86.             s += self.getTotTimeStr() + '\n'
  87.         if not time_sub and not time_tot:
  88.             s += '\n'
  89.  
  90.         if self.path_file is not None:
  91.             with open(self.path_file, 'a') as f:
  92.                 f.write(s)
  93.                
  94.         if self.debug_mode:
  95.             print(s[:-1])
Advertisement
Add Comment
Please, Sign In to add comment