experimental.logger: safe function and stdou

2015-11-18 Thread Andre via Digitalmars-d-learn

Hi,

I want to write log entries to stdout in JSON format. Therefore I 
created
a customer logger. Unfortunatelly there are 2 errors with 
following implementation:


module logger;

import std.experimental.logger;
import std.stdio: stdout;

class MyCustomLogger : Logger
{
private FileLogger _stdOutLogger;

this(LogLevel lv = LogLevel.all) @safe
{
super(lv);
_stdOutLogger = new FileLogger(stdout);
}

override void writeLogMsg(ref LogEntry payload)
{
with (payload)
{
_stdOutLogger.logf(logLevel, `{
"file":"%s", "line":%s, "funcName":"%s", 
"prettyFuncName":"%s",
"moduleName":"%s", "logLevel":%s, "threadId":%s, 
"timestamp":"%s", "msg":"%s"}`,
file, line, funcName, prettyFuncName, moduleName, logLevel, 
threadId, timestamp);

}
}
}

static this()
{
stdThreadLocalLog = new MyCustomLogger();
}

source\logger.d(13,34): Error: safe function 'this' cannot access 
__gshared data 'stdout'


source\logger.d(20,22): Error: safe function 
'logger.MyCustomLogger.writeLogMsg' cannot call system function 
'std.experimental.logger.core.Logger.logf!(20, 
"source\\logger.d", "logger.MyCustomLogger.writeLogMsg", "void 
logger.MyCustomLogger...



Also the example for creating an user defined logger is wrong:
http://dlang.org/phobos-prerelease/std_experimental_logger.html
There is a compilation error regarding variable newName in super.
newName seems not to be expected here.

Kind regards
André




Re: experimental.logger: safe function and stdou

2015-11-18 Thread Andre via Digitalmars-d-learn

On Thursday, 19 November 2015 at 06:27:58 UTC, Andre wrote:


override void writeLogMsg(ref LogEntry payload)
{
with (payload)
{
_stdOutLogger.logf(logLevel, `{
"file":"%s", "line":%s, "funcName":"%s", 
"prettyFuncName":"%s",
"moduleName":"%s", "logLevel":%s, "threadId":%s, 
"timestamp":"%s", "msg":"%s"}`,
file, line, funcName, prettyFuncName, moduleName, logLevel, 
threadId, timestamp);

}
}


I changed the implementation of writeLogMsg and attached the 
trusted attribute.

Is this a valid solution?

override void writeLogMsg(ref LogEntry payload) @trusted
{
with (payload)
{
			 writefln(`{"file":"%s", "line":%s, "funcName":"%s", 
"prettyFuncName":"%s",`~
	` "moduleName":"%s", "logLevel":%s, "threadId":%s, 
"timestamp":"%s", "msg":"%s"}`,
file, line, funcName, prettyFuncName, moduleName, logLevel, 
threadId, timestamp, msg);

stdout.flush();
}
}