From d006349fc3e568ff6477f7834324f1754ef966ed Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 18 Dec 2017 10:03:54 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=E6=96=B0=E5=A2=9E=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=8D=95=E4=BE=8B=E6=A8=A1=E5=BC=8F=E7=9A=84?= =?UTF-8?q?VtSingleton=E7=B1=BB=EF=BC=8C=E4=BF=AE=E6=94=B9LogEngine?= =?UTF-8?q?=E7=9A=84=E5=8D=95=E4=BE=8B=E6=A8=A1=E5=BC=8F=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/CtaTrading/VT_setting.json | 2 +- vnpy/trader/vtEngine.py | 13 +++---------- vnpy/trader/vtObject.py | 17 ++++++++++++++++- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/examples/CtaTrading/VT_setting.json b/examples/CtaTrading/VT_setting.json index b5b81f40..a00ff35e 100644 --- a/examples/CtaTrading/VT_setting.json +++ b/examples/CtaTrading/VT_setting.json @@ -9,7 +9,7 @@ "darkStyle": true, "language": "chinese", - "logActive": false, + "logActive": true, "logLevel": "debug", "logConsole": true, "logFile": true, diff --git a/vnpy/trader/vtEngine.py b/vnpy/trader/vtEngine.py index 1d717795..58181d83 100644 --- a/vnpy/trader/vtEngine.py +++ b/vnpy/trader/vtEngine.py @@ -538,22 +538,15 @@ class DataEngine(object): class LogEngine(object): """日志引擎""" + # 单例模式 + __metaclass__ = VtSingleton + # 日志级别 LEVEL_DEBUG = logging.DEBUG LEVEL_INFO = logging.INFO LEVEL_WARN = logging.WARN LEVEL_ERROR = logging.ERROR 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): diff --git a/vnpy/trader/vtObject.py b/vnpy/trader/vtObject.py index a0881b7a..2a3a4cb9 100644 --- a/vnpy/trader/vtObject.py +++ b/vnpy/trader/vtObject.py @@ -325,8 +325,23 @@ class VtCancelOrderReq(object): self.orderID = EMPTY_STRING # 报单号 self.frontID = 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]