From cb51dc04b1ec736d37b11e0c99d89193df431bee Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Mon, 21 Mar 2016 13:44:29 +0300 Subject: [PATCH] fix #213 - logging broken by commit e89b684274cf9ad158b9bf1f051479af57fb6a56 Solution: added methods Log.fv, fd, fi, fw, fe, ff which are format line versions of Log.v,d,i,w,e,f --- src/dlangui/core/logger.d | 56 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/src/dlangui/core/logger.d b/src/dlangui/core/logger.d index 877e1ed3..c371fd6e 100644 --- a/src/dlangui/core/logger.d +++ b/src/dlangui/core/logger.d @@ -21,6 +21,8 @@ setLogLevel(LogLeve.Debug); // log debug message Log.d("mouse clicked at ", x, ",", y); +// or with format string: +Log.fd("mouse clicked at %d,%d", x, y); // log error message Log.e("exception while reading file", e); ---- @@ -33,6 +35,7 @@ module dlangui.core.logger; import std.stdio; import std.datetime : SysTime, Clock; +import core.sync.mutex; /// Log levels enum LogLevel : int { @@ -77,8 +80,6 @@ Log.e("exception while reading file", e); */ -import core.sync.mutex; - class Log { static __gshared private LogLevel logLevel = LogLevel.Info; static __gshared private std.stdio.File * logFile = null; @@ -141,6 +142,15 @@ class Log { } /// Log message with arbitrary log level static public void log(S...)(LogLevel level, S args) { + if (logLevel >= level && logFile !is null && logFile.isOpen) { + SysTime ts = Clock.currTime(); + logFile.writef("%04d-%02d-%02d %02d:%02d:%02d.%03d %s ", ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.fracSecs.split!("msecs").msecs, logLevelName(level)); + logFile.writeln(args); + logFile.flush(); + } + } + /// Log message with arbitrary log level with format string + static public void logf(S...)(LogLevel level, S args) { if (logLevel >= level && logFile !is null && logFile.isOpen) { SysTime ts = Clock.currTime(); logFile.writef("%04d-%02d-%02d %02d:%02d:%02d.%03d %s ", ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.fracSecs.split!("msecs").msecs, logLevelName(level)); @@ -155,6 +165,13 @@ class Log { log(LogLevel.Trace, args); } } + /// Log verbose / trace message with format string + static public void fv(S...)(S args) { + synchronized(mutex) { + if (logLevel >= LogLevel.Trace && logFile !is null && logFile.isOpen) + logf(LogLevel.Trace, args); + } + } /// Log debug message static public void d(S...)(S args) { synchronized(mutex) { @@ -162,6 +179,13 @@ class Log { log(LogLevel.Debug, args); } } + /// Log debug message with format string + static public void fd(S...)(S args) { + synchronized(mutex) { + if (logLevel >= LogLevel.Debug && logFile !is null && logFile.isOpen) + logf(LogLevel.Debug, args); + } + } /// Log info message static public void i(S...)(S args) { synchronized(mutex) { @@ -169,6 +193,13 @@ class Log { log(LogLevel.Info, args); } } + /// Log info message + static public void fi(S...)(S args) { + synchronized(mutex) { + if (logLevel >= LogLevel.Info && logFile !is null && logFile.isOpen) + logf(LogLevel.Info, args); + } + } /// Log warn message static public void w(S...)(S args) { synchronized(mutex) { @@ -176,6 +207,13 @@ class Log { log(LogLevel.Warn, args); } } + /// Log warn message + static public void fw(S...)(S args) { + synchronized(mutex) { + if (logLevel >= LogLevel.Warn && logFile !is null && logFile.isOpen) + logf(LogLevel.Warn, args); + } + } /// Log error message static public void e(S...)(S args) { synchronized(mutex) { @@ -183,6 +221,13 @@ class Log { log(LogLevel.Error, args); } } + /// Log error message + static public void fe(S...)(S args) { + synchronized(mutex) { + if (logLevel >= LogLevel.Error && logFile !is null && logFile.isOpen) + logf(LogLevel.Error, args); + } + } /// Log fatal error message static public void f(S...)(S args) { synchronized(mutex) { @@ -190,6 +235,13 @@ class Log { log(LogLevel.Fatal, args); } } + /// Log fatal error message + static public void ff(S...)(S args) { + synchronized(mutex) { + if (logLevel >= LogLevel.Fatal && logFile !is null && logFile.isOpen) + logf(LogLevel.Fatal, args); + } + } } debug {