fenrir/src/fenrir-package/core/debug.py

62 lines
1.7 KiB
Python
Raw Normal View History

#!/usr/bin/python
# Debugger module for the Fenrir screen reader.
2016-07-07 15:40:10 -04:00
2016-07-08 08:29:35 -04:00
from enum import Enum
2016-09-15 06:55:52 -04:00
from datetime import datetime
2016-07-08 08:29:35 -04:00
2016-07-08 06:20:49 -04:00
class debugLevel(Enum):
DEACTIVE = 0
ERROR = 1
WARNING = 2
INFO = 3
2016-08-21 17:26:19 -04:00
def __int__(self):
return self.value
2016-09-15 06:03:55 -04:00
def __str__(self):
return self.name
class debug():
2016-08-21 17:26:19 -04:00
def __init__(self, fileName='/var/log/fenrir.log'):
2016-07-08 05:56:42 -04:00
self._fileName = fileName
2016-08-21 17:26:19 -04:00
self._file = None
2016-07-08 05:56:42 -04:00
self._fileOpened = False
2016-08-21 17:26:19 -04:00
def __del__(self):
try:
self.closeDebugFile()
except:
pass
def openDebugFile(self, fileName = ''):
2016-07-08 05:56:42 -04:00
self._fileOpened = False
if fileName != '':
self._fileName = fileName
if self._fileName != '':
2016-08-30 16:52:54 -04:00
self._file = open(self._fileName,'a')
self._fileOpened = True
2016-08-21 17:26:19 -04:00
def writeDebugOut(self, environment, text, level = debugLevel.DEACTIVE):
if environment['runtime']['settingsManager'].getSettingAsInt(environment, 'general','debugLevel') < int(level):
2016-07-08 05:56:42 -04:00
if self._fileOpened:
self.closeDebugFile()
return
else:
if not self._fileOpened:
self.openDebugFile()
2016-09-15 06:55:52 -04:00
self._file.write(str(level) +' ' + str(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')
) + ': ' + text + '\n')
def closeDebugFile(self):
if not self._fileOpened:
return False
2016-08-21 17:26:19 -04:00
if self._file != None:
self._file.close()
self._fileOpened = False
return True
def getDebugFile(self):
return self._fileName
def setDebugFile(self, fileName):
2016-07-08 05:56:42 -04:00
self.closeDebugFile()
2016-08-21 17:26:19 -04:00
self._fileName = fileName