[Add]新增用于实现单例模式的VtSingleton类,修改LogEngine的单例模式实现

This commit is contained in:
vn.py 2017-12-18 10:03:54 +08:00
parent 65bc568888
commit d006349fc3
3 changed files with 20 additions and 12 deletions

View File

@ -9,7 +9,7 @@
"darkStyle": true, "darkStyle": true,
"language": "chinese", "language": "chinese",
"logActive": false, "logActive": true,
"logLevel": "debug", "logLevel": "debug",
"logConsole": true, "logConsole": true,
"logFile": true, "logFile": true,

View File

@ -538,22 +538,15 @@ class DataEngine(object):
class LogEngine(object): class LogEngine(object):
"""日志引擎""" """日志引擎"""
# 单例模式
__metaclass__ = VtSingleton
# 日志级别 # 日志级别
LEVEL_DEBUG = logging.DEBUG LEVEL_DEBUG = logging.DEBUG
LEVEL_INFO = logging.INFO LEVEL_INFO = logging.INFO
LEVEL_WARN = logging.WARN LEVEL_WARN = logging.WARN
LEVEL_ERROR = logging.ERROR LEVEL_ERROR = logging.ERROR
LEVEL_CRITICAL = logging.CRITICAL LEVEL_CRITICAL = logging.CRITICAL
# 单例对象
instance = None
#----------------------------------------------------------------------
def __new__(cls, *args, **kwargs):
"""创建对象,保证单例"""
if not cls.instance:
cls.instance = super(LogEngine, cls).__new__(cls, *args, **kwargs)
return cls.instance
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def __init__(self): def __init__(self):

View File

@ -325,8 +325,23 @@ class VtCancelOrderReq(object):
self.orderID = EMPTY_STRING # 报单号 self.orderID = EMPTY_STRING # 报单号
self.frontID = EMPTY_STRING # 前置机号 self.frontID = EMPTY_STRING # 前置机号
self.sessionID = EMPTY_STRING # 会话号 self.sessionID = EMPTY_STRING # 会话号
########################################################################
class VtSingleton(type):
"""
单例应用方式:静态变量 __metaclass__ = Singleton
"""
_instances = {}
#----------------------------------------------------------------------
def __call__(cls, *args, **kwargs):
"""调用"""
if cls not in cls._instances:
cls._instances[cls] = super(VtSingleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]