Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 39 additions & 23 deletions std/experimental/logger/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,28 @@ Source: $(PHOBOSSRC std/experimental/logger/core.d)
module std.experimental.logger.core;

import core.sync.mutex : Mutex;
import core.thread : Thread, ThreadID;
import std.datetime.date : DateTime;
import std.datetime.systime : Clock, SysTime;
import std.range.primitives;
import std.traits;

import std.experimental.logger.filelogger;

static this()
{
version(linux)
{
// see https://github.com/dlang/druntime/pull/2820
import core.sys.linux.unistd;
logThreadID = syscall(SYS.gettid);
}
else
{
logThreadID = Thread.getThis.id;
}
}

/** This template evaluates if the passed `LogLevel` is active.
The previously described version statements are used to decide if the
`LogLevel` is active. The version statements only influence the compile
Expand Down Expand Up @@ -711,7 +726,6 @@ flexibility.
abstract class Logger
{
import std.array : appender, Appender;
import std.concurrency : thisTid, Tid;

/** LogEntry is a aggregation combining all information associated
with a log message. This aggregation will be passed to the method
Expand All @@ -733,7 +747,7 @@ abstract class Logger
/// the `LogLevel` associated with the log message
LogLevel logLevel;
/// thread id of the log message
Tid threadId;
ThreadID threadId;
/// the time the message was logged
SysTime timestamp;
/// the message of the log message
Expand Down Expand Up @@ -784,7 +798,7 @@ abstract class Logger
{
override void beginLogMsg(string file, int line, string funcName,
string prettyFuncName, string moduleName, LogLevel logLevel,
Tid threadId, SysTime timestamp)
ThreadID threadId, SysTime timestamp)
{
... logic here
}
Expand Down Expand Up @@ -813,7 +827,7 @@ abstract class Logger
*/
protected void beginLogMsg(string file, int line, string funcName,
string prettyFuncName, string moduleName, LogLevel logLevel,
Tid threadId, SysTime timestamp, Logger logger)
ThreadID threadId, SysTime timestamp, Logger logger)
@safe
{
static if (isLoggingActive)
Expand Down Expand Up @@ -950,7 +964,7 @@ abstract class Logger
if (isLoggingEnabled(ll, this.logLevel_, globalLogLevel))
{
this.beginLogMsg(file, line, funcName, prettyFuncName,
moduleName, ll, thisTid, Clock.currTime, this);
moduleName, ll, logThreadID, Clock.currTime, this);

auto writer = MsgRange(this);
formatString(writer, args);
Expand Down Expand Up @@ -998,7 +1012,7 @@ abstract class Logger
condition))
{
this.beginLogMsg(file, line, funcName, prettyFuncName,
moduleName, ll, thisTid, Clock.currTime, this);
moduleName, ll, logThreadID, Clock.currTime, this);

auto writer = MsgRange(this);
formatString(writer, args);
Expand Down Expand Up @@ -1049,7 +1063,7 @@ abstract class Logger
condition))
{
this.beginLogMsg(file, line, funcName, prettyFuncName,
moduleName, ll, thisTid, Clock.currTime, this);
moduleName, ll, logThreadID, Clock.currTime, this);

auto writer = MsgRange(this);
formattedWrite(writer, msg, args);
Expand Down Expand Up @@ -1096,7 +1110,7 @@ abstract class Logger
if (isLoggingEnabled(ll, this.logLevel_, globalLogLevel))
{
this.beginLogMsg(file, line, funcName, prettyFuncName,
moduleName, ll, thisTid, Clock.currTime, this);
moduleName, ll, logThreadID, Clock.currTime, this);

auto writer = MsgRange(this);
formattedWrite(writer, msg, args);
Expand Down Expand Up @@ -1166,7 +1180,7 @@ abstract class Logger
if (isLoggingEnabled(ll, this.logLevel_, globalLogLevel, condition))
{
this.beginLogMsg(file, line, funcName, prettyFuncName,
moduleName, ll, thisTid, Clock.currTime, this);
moduleName, ll, logThreadID, Clock.currTime, this);

auto writer = MsgRange(this);
formatString(writer, args);
Expand All @@ -1191,7 +1205,7 @@ abstract class Logger
condition) && ll >= moduleLogLevel!moduleName)
{
this.beginLogMsg(file, line, funcName, prettyFuncName,
moduleName, ll, thisTid, Clock.currTime, this);
moduleName, ll, logThreadID, Clock.currTime, this);
auto writer = MsgRange(this);
formatString(writer, args);

Expand Down Expand Up @@ -1235,7 +1249,7 @@ abstract class Logger
if (isLoggingEnabled(ll, this.logLevel_, globalLogLevel))
{
this.beginLogMsg(file, line, funcName, prettyFuncName,
moduleName, ll, thisTid, Clock.currTime, this);
moduleName, ll, logThreadID, Clock.currTime, this);

auto writer = MsgRange(this);
formatString(writer, args);
Expand All @@ -1259,7 +1273,7 @@ abstract class Logger
if (isLoggingEnabled(ll, this.logLevel_, globalLogLevel))
{
this.beginLogMsg(file, line, funcName, prettyFuncName,
moduleName, ll, thisTid, Clock.currTime, this);
moduleName, ll, logThreadID, Clock.currTime, this);
auto writer = MsgRange(this);
formatString(writer, args);

Expand Down Expand Up @@ -1305,7 +1319,7 @@ abstract class Logger
globalLogLevel, condition))
{
this.beginLogMsg(file, line, funcName, prettyFuncName,
moduleName, this.logLevel_, thisTid, Clock.currTime, this);
moduleName, this.logLevel_, logThreadID, Clock.currTime, this);

auto writer = MsgRange(this);
formatString(writer, args);
Expand All @@ -1330,7 +1344,7 @@ abstract class Logger
condition))
{
this.beginLogMsg(file, line, funcName, prettyFuncName,
moduleName, this.logLevel_, thisTid, Clock.currTime, this);
moduleName, this.logLevel_, logThreadID, Clock.currTime, this);
auto writer = MsgRange(this);
formatString(writer, args);

Expand Down Expand Up @@ -1377,7 +1391,7 @@ abstract class Logger
globalLogLevel))
{
this.beginLogMsg(file, line, funcName, prettyFuncName,
moduleName, this.logLevel_, thisTid, Clock.currTime, this);
moduleName, this.logLevel_, logThreadID, Clock.currTime, this);
auto writer = MsgRange(this);
formatString(writer, args);

Expand All @@ -1400,7 +1414,7 @@ abstract class Logger
if (isLoggingEnabled(this.logLevel_, this.logLevel_, globalLogLevel))
{
this.beginLogMsg(file, line, funcName, prettyFuncName,
moduleName, this.logLevel_, thisTid, Clock.currTime, this);
moduleName, this.logLevel_, logThreadID, Clock.currTime, this);
auto writer = MsgRange(this);
formatString(writer, arg);

Expand Down Expand Up @@ -1449,7 +1463,7 @@ abstract class Logger
if (isLoggingEnabled(ll, this.logLevel_, globalLogLevel, condition))
{
this.beginLogMsg(file, line, funcName, prettyFuncName,
moduleName, ll, thisTid, Clock.currTime, this);
moduleName, ll, logThreadID, Clock.currTime, this);

auto writer = MsgRange(this);
formattedWrite(writer, msg, args);
Expand Down Expand Up @@ -1497,7 +1511,7 @@ abstract class Logger
if (isLoggingEnabled(ll, this.logLevel_, globalLogLevel))
{
this.beginLogMsg(file, line, funcName, prettyFuncName,
moduleName, ll, thisTid, Clock.currTime, this);
moduleName, ll, logThreadID, Clock.currTime, this);

auto writer = MsgRange(this);
formattedWrite(writer, msg, args);
Expand Down Expand Up @@ -1547,7 +1561,7 @@ abstract class Logger
condition))
{
this.beginLogMsg(file, line, funcName, prettyFuncName,
moduleName, this.logLevel_, thisTid, Clock.currTime, this);
moduleName, this.logLevel_, logThreadID, Clock.currTime, this);

auto writer = MsgRange(this);
formattedWrite(writer, msg, args);
Expand Down Expand Up @@ -1593,7 +1607,7 @@ abstract class Logger
globalLogLevel))
{
this.beginLogMsg(file, line, funcName, prettyFuncName,
moduleName, this.logLevel_, thisTid, Clock.currTime, this);
moduleName, this.logLevel_, logThreadID, Clock.currTime, this);

auto writer = MsgRange(this);
formattedWrite(writer, msg, args);
Expand Down Expand Up @@ -1752,6 +1766,8 @@ The thread local `Logger` will use this `LogLevel` to filter log calls
every same way as presented earlier.
*/
//public LogLevel threadLogLevel = LogLevel.all;
/// cache the thread ID in TLS as core.thread.Thread.getThis.id is synchronized.
private ThreadID logThreadID;
private Logger stdLoggerThreadLogger;
private Logger stdLoggerDefaultThreadLogger;

Expand Down Expand Up @@ -3007,17 +3023,17 @@ private void trustedStore(T)(ref shared T dst, ref T src) @trusted

class TestLog : Logger
{
Tid tid;
ThreadID tid;

this()
{
super (LogLevel.trace);
this.tid = thisTid;
this.tid = logThreadID;
}

override void writeLogMsg(ref LogEntry payload) @trusted
{
assert(thisTid == this.tid);
assert(logThreadID == this.tid);
atomicOp!"+="(logged_count, 1);
}
}
Expand Down
6 changes: 3 additions & 3 deletions std/experimental/logger/filelogger.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ is already present new log messages will be append at its end.
*/
class FileLogger : Logger
{
import std.concurrency : Tid;
import core.thread : ThreadID;
import std.datetime.systime : SysTime;
import std.format : formattedWrite;

Expand Down Expand Up @@ -122,7 +122,7 @@ class FileLogger : Logger
*/
override protected void beginLogMsg(string file, int line, string funcName,
string prettyFuncName, string moduleName, LogLevel logLevel,
Tid threadId, SysTime timestamp, Logger logger)
ThreadID threadId, SysTime timestamp, Logger logger)
@safe
{
import std.string : lastIndexOf;
Expand All @@ -132,7 +132,7 @@ class FileLogger : Logger
auto lt = this.file_.lockingTextWriter();
systimeToISOString(lt, timestamp);
import std.conv : to;
formattedWrite(lt, " [%s] %s:%u:%s ", logLevel.to!string,
formattedWrite(lt, " %s %s %s:%u:%s ", threadId, logLevel.to!string,
file[fnIdx .. $], line, funcName[funIdx .. $]);
}

Expand Down