diff --git a/std/logger/core.d b/std/logger/core.d index 14dce69e169..a375d622b30 100644 --- a/std/logger/core.d +++ b/std/logger/core.d @@ -197,7 +197,6 @@ import std.logger.multilogger; import std.logger.filelogger; import std.logger.nulllogger; -/+ private pure string logLevelToParameterString(const LogLevel lv) { switch(lv) @@ -266,9 +265,7 @@ private pure string logLevelToDisable(const LogLevel lv) assert(false, to!string(cast(int)lv)); } } -+/ -/+ private string genDocComment(const bool asMemberFunction, const bool asConditional, const bool asPrintf, const LogLevel lv, const bool specificLogLevel = false) @@ -372,14 +369,27 @@ private string genDocComment(const bool asMemberFunction, return ret ~ " */\n"; } -+/ - -//pragma(msg, genDocComment(false, true, true, LogLevel.unspecific, true)); +//pragma(msg, genDocComment(false, false, false, LogLevel.unspecific, true)); //pragma(msg, buildLogFunction(false, false, false, LogLevel.unspecific)); //pragma(msg, buildLogFunction(false, false, false, LogLevel.unspecific)); //pragma(msg, buildLogFunction(false, false, false, LogLevel.trace)); //pragma(msg, buildLogFunction(false, false, true, LogLevel.info)); +/** This function logs data. + +In order for the data to be processed the $(D LogLevel) of the defaultLogger +must be greater equal to the global $(D LogLevel). + +Params: +args = The data that should be logged. + +Returns: The logger used by the logging function as reference. + +Examples: +-------------------- +log("Hello World", 3.1415); +-------------------- +*/ public ref Logger log(int line = __LINE__, string file = __FILE__, string funcName = __FUNCTION__, string prettyFuncName = __PRETTY_FUNCTION__, string moduleName = __MODULE__, A...)(lazy A args) @trusted @@ -395,6 +405,24 @@ public ref Logger log(int line = __LINE__, string file = __FILE__, return LogManager.defaultLogger; } +/** This function logs data depending on a $(D LogLevel) passed +explicitly. + +This function takes a $(D LogLevel) as first argument. In order for the +data to be processed the $(D LogLevel) must be greater equal to the +$(D LogLevel) of the used logger, and the global $(D LogLevel). + +Params: +logLevel = The $(D LogLevel) used for logging the message. +args = The data that should be logged. + +Returns: The logger used by the logging function as reference. + +Examples: +-------------------- +logl(LogLevel.error, "Hello World"); +-------------------- +*/ public ref Logger logl(int line = __LINE__, string file = __FILE__, string funcName = __FUNCTION__, string prettyFuncName = __PRETTY_FUNCTION__, string moduleName = __MODULE__, A...)(const LogLevel logLevel, lazy A args) @@ -412,6 +440,24 @@ public ref Logger logl(int line = __LINE__, string file = __FILE__, return LogManager.defaultLogger; } +/** This function logs data depending on a $(D condition) passed +explicitly. + +This function takes a $(D bool) as first argument. In order for the +data to be processed the $(D bool) must be $(D true) and the $(D LogLevel) of +the defaultLogger must be greater equal to the global $(D LogLevel). + +Params: +cond = Only if this $(D bool) is $(D true) will the data be logged. +args = The data that should be logged. + +Returns: The logger used by the logging function as reference. + +Examples: +-------------------- +logl(false, 1337); +-------------------- +*/ public ref Logger logc(int line = __LINE__, string file = __FILE__, string funcName = __FUNCTION__, string prettyFuncName = __PRETTY_FUNCTION__, string moduleName = __MODULE__, A...)(const bool cond, lazy A args) @trusted @@ -427,6 +473,25 @@ public ref Logger logc(int line = __LINE__, string file = __FILE__, return LogManager.defaultLogger; } +/** This function logs data depending on a $(D condition) and a $(D LogLevel) +passed explicitly. + +This function takes a $(D bool) as first argument and a $(D bool) as second +argument. In order for the +data to be processed the $(D bool) must be $(D true) and the $(D LogLevel) of +the defaultLogger must be greater equal to the global $(D LogLevel). + +Params: +cond = Only if this $(D bool) is $(D true) will the data be logged. +args = The data that should be logged. + +Returns: The logger used by the logging function as reference. + +Examples: +-------------------- +loglc(LogLevel.info, someCondition, 13, 37, "Hello World"); +-------------------- +*/ public ref Logger loglc(int line = __LINE__, string file = __FILE__, string funcName = __FUNCTION__, string prettyFuncName = __PRETTY_FUNCTION__, string moduleName = __MODULE__, A...)(const LogLevel logLevel, const bool cond, @@ -444,6 +509,22 @@ public ref Logger loglc(int line = __LINE__, string file = __FILE__, return LogManager.defaultLogger; } +/** This function logs data in a printf style manner. + +In order for the data to be processed the $(D LogLevel) of the defaultLogger +must be greater equal to the global $(D LogLevel). + +Params: +msg = The $(D string) that is used to format the additional data. +args = The data that should be logged. + +Returns: The logger used by the logging function as reference. + +Examples: +-------------------- +logf("Hello World %f", 3.1415); +-------------------- +*/ public ref Logger logf(int line = __LINE__, string file = __FILE__, string funcName = __FUNCTION__, string prettyFuncName = __PRETTY_FUNCTION__, string moduleName = __MODULE__, A...)(string msg, @@ -460,6 +541,25 @@ public ref Logger logf(int line = __LINE__, string file = __FILE__, return LogManager.defaultLogger; } +/** This function logs data in a printf style manner depending on a +$(D condition) and a $(D LogLevel) passed explicitly. + +This function takes a $(D LogLevel) as first argument. In order for the +data to be processed the $(D LogLevel) must be greater equal to the +$(D LogLevel) of the used logger, and the global $(D LogLevel). + +Params: +logLevel = The $(D LogLevel) used for logging the message. +msg = The $(D string) that is used to format the additional data. +args = The data that should be logged. + +Returns: The logger used by the logging function as reference. + +Examples: +-------------------- +loglf(LogLevel.critical, "%d", 1337); +-------------------- +*/ public ref Logger loglf(int line = __LINE__, string file = __FILE__, string funcName = __FUNCTION__, string prettyFuncName = __PRETTY_FUNCTION__, string moduleName = __MODULE__, A...)(const LogLevel logLevel, string msg, @@ -477,6 +577,25 @@ public ref Logger loglf(int line = __LINE__, string file = __FILE__, return LogManager.defaultLogger; } +/** This function logs data in a print style manner depending on a +$(D condition) passed explicitly + +This function takes a $(D bool) as first argument. In order for the +data to be processed the $(D bool) must be $(D true) and the $(D LogLevel) of +the defaultLogger must be greater equal to the global $(D LogLevel). + +Params: +cond = Only if this $(D bool) is $(D true) will the data be logged. +msg = The $(D string) that is used to format the additional data. +args = The data that should be logged. + +Returns: The logger used by the logging function as reference. + +Examples: +-------------------- +logcf(false, "%d", 1337); +-------------------- +*/ public ref Logger logcf(int line = __LINE__, string file = __FILE__, string funcName = __FUNCTION__, string prettyFuncName = __PRETTY_FUNCTION__, string moduleName = __MODULE__, A...)(const bool cond, string msg, lazy A args) @@ -493,6 +612,27 @@ public ref Logger logcf(int line = __LINE__, string file = __FILE__, return LogManager.defaultLogger; } +/** This function logs data in a print style manner depending on a $(D +LogLevel) and a $(D condition) passed explicitly + +This function takes a $(D LogLevel) as first argument This function takes a +$(D bool) as second argument. In order for the data to be processed the +$(D bool) must be $(D true) and the $(D LogLevel) of the defaultLogger must be +greater equal to the global $(D LogLevel). + +Params: +logLevel = The $(D LogLevel) used for logging the message. +cond = Only if this $(D bool) is $(D true) will the data be logged. +msg = The $(D string) that is used to format the additional data. +args = The data that should be logged. + +Returns: The logger used by the logging function as reference. + +Examples: +-------------------- +loglcf(LogLevel.trace, false, "%d %s", 1337, "is number"); +-------------------- +*/ public ref Logger loglcf(int line = __LINE__, string file = __FILE__, string funcName = __FUNCTION__, string prettyFuncName = __PRETTY_FUNCTION__, string moduleName = __MODULE__, A...)(const LogLevel logLevel, bool cond, diff --git a/std/logger/stdiologger.d b/std/logger/stdiologger.d index 3f890112d94..130ddbbbf00 100644 --- a/std/logger/stdiologger.d +++ b/std/logger/stdiologger.d @@ -1,20 +1,36 @@ module std.logger.stdiologger; import std.stdio; +import core.sync.mutex; import std.string; import std.logger.templatelogger; +private struct StdioOutputRange { + static __gshared Mutex mu; + + void put(T)(ref T t) { + mu.lock(); + scope(exit) mu.unlock(); + write(t); + } + + static StdioOutputRange opCall() { + StdioOutputRange ret; + StdioOutputRange.mu = new Mutex(); + return ret; + } +} + /** This $(D Logger) implementation writes log messages to the systems standard output. The format of the output is: $(D FileNameWithoutPath:FunctionNameWithoutModulePath:LineNumber Message). */ -class StdIOLogger : TemplateLogger!(File.LockingTextWriter, defaultFormatter, +class StdIOLogger : TemplateLogger!(StdioOutputRange, defaultFormatter, (a) => true) { static @trusted this() { this("", LogLevel.info); - //StdIOLogger.stdioMutex = new Mutex(); } /** Default constructor for the $(D StdIOLogger) Logger. @@ -49,7 +65,7 @@ class StdIOLogger : TemplateLogger!(File.LockingTextWriter, defaultFormatter, */ public this(string name, const LogLevel lv = LogLevel.info) @trusted { - super(stdout.lockingTextWriter(), name, lv); + super(StdioOutputRange(), name, lv); } } diff --git a/std/logger/templatelogger.d b/std/logger/templatelogger.d index 6d38a7419e4..001ad572d9a 100644 --- a/std/logger/templatelogger.d +++ b/std/logger/templatelogger.d @@ -14,7 +14,8 @@ public void defaultFormatter(T,F)(ref T t, ref F payload) @trusted { size_t fnIdx = payload.file.lastIndexOf('/') + 1; size_t funIdx = payload.funcName.lastIndexOf('.') + 1; - formattedWrite(t, "%s:%s:%s:%u %s\n",payload.timestamp.toISOExtString(), + auto time = payload.timestamp.toISOExtString(); + formattedWrite(t, "%s:%s:%s:%u %s\n", time[0 .. 25], payload.file[fnIdx .. $], payload.funcName[funIdx .. $], payload.line, payload.msg); }