hydrus/include/HydrusLogger.py

138 lines
3.3 KiB
Python
Raw Normal View History

2015-11-11 21:20:41 +00:00
import HydrusConstants as HC
2015-11-18 22:44:07 +00:00
import HydrusData
2015-11-11 21:20:41 +00:00
import os
import sys
2016-11-09 23:13:22 +00:00
import threading
2015-11-11 21:20:41 +00:00
import time
class HydrusLogger( object ):
2016-11-09 23:13:22 +00:00
def __init__( self, base_dir, prefix ):
2015-11-11 21:20:41 +00:00
2016-11-09 23:13:22 +00:00
self._log_path_base = os.path.join( base_dir, prefix )
self._lock = threading.Lock()
2015-11-11 21:20:41 +00:00
def __enter__( self ):
self._previous_sys_stdout = sys.stdout
self._previous_sys_stderr = sys.stderr
2015-11-18 22:44:07 +00:00
self._problem_with_previous_stdout = False
2018-01-24 23:09:42 +00:00
self._OpenLog()
2015-11-11 21:20:41 +00:00
sys.stdout = self
sys.stderr = self
return self
def __exit__( self, exc_type, exc_val, exc_tb ):
sys.stdout = self._previous_sys_stdout
sys.stderr = self._previous_sys_stderr
2018-01-24 23:09:42 +00:00
self._CloseLog()
2015-11-11 21:20:41 +00:00
return False
2016-11-09 23:13:22 +00:00
def _CloseLog( self ):
self._log_file.close()
def _GetLogPath( self ):
2018-01-31 22:58:15 +00:00
current_time_struct = time.localtime()
2016-11-09 23:13:22 +00:00
( current_year, current_month ) = ( current_time_struct.tm_year, current_time_struct.tm_mon )
log_path = self._log_path_base + ' - ' + str( current_year ) + '-' + str( current_month ) + '.log'
return log_path
def _OpenLog( self ):
self._log_path = self._GetLogPath()
self._log_file = open( self._log_path, 'a' )
def _SwitchToANewLogFileIfDue( self ):
correct_log_path = self._GetLogPath()
if correct_log_path != self._log_path:
self._CloseLog()
self._OpenLog()
2015-11-11 21:20:41 +00:00
def flush( self ):
2016-11-09 23:13:22 +00:00
with self._lock:
2015-11-18 22:44:07 +00:00
2016-11-09 23:13:22 +00:00
if not self._problem_with_previous_stdout:
2015-11-18 22:44:07 +00:00
2016-11-09 23:13:22 +00:00
try:
self._previous_sys_stdout.flush()
except IOError:
self._problem_with_previous_stdout = True
2015-11-18 22:44:07 +00:00
2016-11-09 23:13:22 +00:00
self._log_file.flush()
self._SwitchToANewLogFileIfDue()
2015-11-11 21:20:41 +00:00
2016-08-31 19:55:14 +00:00
def isatty( self ):
return False
2015-11-11 21:20:41 +00:00
def write( self, value ):
2016-11-09 23:13:22 +00:00
with self._lock:
2015-11-18 22:44:07 +00:00
2016-11-09 23:13:22 +00:00
if value in ( os.linesep, '\n' ):
2015-11-18 22:44:07 +00:00
2016-11-09 23:13:22 +00:00
prefix = ''
2015-11-18 22:44:07 +00:00
2016-11-09 23:13:22 +00:00
else:
2015-11-18 22:44:07 +00:00
2018-01-31 22:58:15 +00:00
prefix = time.strftime( '%Y/%m/%d %H:%M:%S: ' )
2015-11-18 22:44:07 +00:00
2017-03-08 23:23:12 +00:00
message = prefix + value
2016-11-09 23:13:22 +00:00
if not self._problem_with_previous_stdout:
2017-03-08 23:23:12 +00:00
stdout_message = HydrusData.ToByteString( message.replace( u'\u2026', '...' ) )
2016-11-09 23:13:22 +00:00
try:
2017-03-08 23:23:12 +00:00
self._previous_sys_stdout.write( stdout_message )
2016-11-09 23:13:22 +00:00
except IOError:
self._problem_with_previous_stdout = True
2017-03-08 23:23:12 +00:00
log_message = HydrusData.ToByteString( message )
self._log_file.write( log_message )
2016-11-09 23:13:22 +00:00
2015-11-11 21:20:41 +00:00
2017-03-08 23:23:12 +00:00