1
This commit is contained in:
parent
3da70d9a27
commit
30c25277eb
81
f7/udplog.py
81
f7/udplog.py
@ -1,20 +1,89 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
class Udplog:
|
import time
|
||||||
|
import datetime
|
||||||
|
import traceback
|
||||||
|
import _thread
|
||||||
|
import threading
|
||||||
|
|
||||||
|
class _LocalLogNode:
|
||||||
|
def __init__(self, msg):
|
||||||
|
self.msg = msg
|
||||||
|
self.nextNode = None
|
||||||
|
|
||||||
|
class _UdpLog:
|
||||||
_instance = None
|
_instance = None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def instance(cls):
|
def instance(cls):
|
||||||
if not _instance:
|
if not cls._instance:
|
||||||
_instance = UdpLog()
|
cls._instance = _UdpLog()
|
||||||
return _instance
|
return cls._instance
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
self._topNode = None
|
||||||
|
self._botNode = None
|
||||||
|
self._workNode = None
|
||||||
|
self._msgLock = threading.Lock()
|
||||||
|
|
||||||
def init(self):
|
def init(self):
|
||||||
pass
|
_thread.start_new_thread(self._saveToFile, ())
|
||||||
|
|
||||||
def unInit(self):
|
def unInit(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def info(self, msg):
|
||||||
|
self._addLog(str(datetime.datetime.now()) + '[INFO]' + str(msg))
|
||||||
|
|
||||||
|
def debug(self, msg):
|
||||||
|
self._addLog(str(datetime.datetime.now()) + '[DEBUG]' + str(msg))
|
||||||
|
|
||||||
|
def warning(self, msg):
|
||||||
|
self._addLog(str(datetime.datetime.now()) + '[WARNING]' + str(msg))
|
||||||
|
|
||||||
|
def error(self, msg):
|
||||||
|
self._addLog(str(datetime.datetime.now()) + '[ERROR]' + str(msg))
|
||||||
|
|
||||||
|
def _addLog(self, msg):
|
||||||
|
node = _LocalLogNode(msg)
|
||||||
|
self._msgLock.acquire()
|
||||||
|
try:
|
||||||
|
if self._botNode == None:
|
||||||
|
self._topNode = node
|
||||||
|
self._botNode = node
|
||||||
|
else:
|
||||||
|
self._botNode.nextNode = node
|
||||||
|
self._botNode = node
|
||||||
|
except:
|
||||||
|
traceback.print_exc()
|
||||||
|
finally:
|
||||||
|
self._msgLock.release()
|
||||||
|
|
||||||
|
|
||||||
|
def _saveToFile(self):
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
if not self._workNode and self._topNode:
|
||||||
|
self._msgLock.acquire()
|
||||||
|
try:
|
||||||
|
self._workNode = self._topNode
|
||||||
|
self._topNode = None
|
||||||
|
self._botNode = None
|
||||||
|
finally:
|
||||||
|
self._msgLock.release()
|
||||||
|
if self._workNode:
|
||||||
|
filename = datetime.datetime.now().strftime('log%Y-%m-%d.log')
|
||||||
|
try:
|
||||||
|
with open(os.path.join(LOG_DIR, filename), 'a') as f:
|
||||||
|
while self._workNode:
|
||||||
|
nextNode = self._workNode.nextNode
|
||||||
|
f.write(self._workNode.msg + '\r\n')
|
||||||
|
self._workNode = nextNode
|
||||||
|
except Exception as e:
|
||||||
|
print(str(e), flush=True)
|
||||||
|
time.sleep(10)
|
||||||
|
except:
|
||||||
|
traceback.print_exec()
|
||||||
|
|
||||||
|
def instance():
|
||||||
|
return _UdpLog.instance()
|
||||||
|
11
test/app.py
Normal file
11
test/app.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
|
sys.path.append('..')
|
||||||
|
|
||||||
|
import f7.udplog
|
||||||
|
|
||||||
|
f7.udplog.instance().init()
|
||||||
|
time.sleep(5)
|
Loading…
x
Reference in New Issue
Block a user