Skip to content

Commit

Permalink
*version test, move to experimental
Browse files Browse the repository at this point in the history
  • Loading branch information
burner committed Aug 26, 2014
1 parent 5d7a71b commit 5db2fc4
Show file tree
Hide file tree
Showing 18 changed files with 450 additions and 37 deletions.
52 changes: 40 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
all: std/logger/*.d main.d
dmd -unittest -debug -cov -g std/logger/*.d main.d -oflog -D -w
all: std/experimental/logger/*.d main.d
dmd -unittest -debug -cov -g std/experimental/logger/*.d main.d -oflog -D -w
./log

gdc: std/logger/*.d main.d
ldc2 -unittest -O3 -g std/logger/*.d main.d -oflog
gdc: std/experimental/logger/*.d main.d
ldc2 -unittest -O3 -g std/experimental/logger/*.d main.d -oflog
./log

profile: std/logger/*.d main.d
dmd -unittest -O -release -g std/logger/*.d main.d -oflog -D -w -profile
profile: std/experimental/logger/*.d main.d
dmd -unittest -O -release -g std/experimental/logger/*.d main.d -oflog -D -w -profile
./log

concur: std/logger/*.d concur.d
dmd -unittest -debug -cov -gc std/logger/*.d concur.d -oflog -D -I.
concur: std/experimental/logger/*.d concur.d
dmd -unittest -debug -cov -gc std/experimental/logger/*.d concur.d -oflog -D -I.
./log

task: std/logger/*.d task.d
dmd -unittest -debug -cov -gc std/logger/*.d task.d -oftask -D -I.
task: std/experimental/logger/*.d task.d
dmd -unittest -debug -cov -gc std/experimental/logger/*.d task.d -oftask -D -I.

test1: test1.d std/experimental/logger/*.d
dmd -debug -gc test1.d -oftest1 std/experimental/logger/*.d

liblogger.so: std/experimental/logger/*.d
dmd -lib -fPIC -release -w -ofliblogger.so std/experimental/logger/*.d -unittest

trace: tracedisable.d std/experimental/logger/*.d liblogger.so
dmd -debug -version=StdLoggerDisableTrace -gc tracedisable.d -oftrace -Istd/experimental/logger/ -main -unittest liblogger.so
./trace

info: infodisable.d std/experimental/logger/*.d liblogger.so
dmd -debug -version=StdLoggerDisableInfo -gc infodisable.d -ofinfo -Istd/experimental/logger/ -main -unittest liblogger.so
./info

warning: warningdisable.d std/experimental/logger/*.d liblogger.so
dmd -debug -version=StdLoggerDisableWarning -gc warningdisable.d -ofwarning -Istd/experimental/logger/ -main -unittest liblogger.so
./warning

error: errordisable.d std/experimental/logger/*.d liblogger.so
dmd -debug -version=StdLoggerDisableError -gc errordisable.d -oferror -Istd/experimental/logger/ -main -unittest liblogger.so
./error

critical: criticaldisable.d std/experimental/logger/*.d liblogger.so
dmd -debug -version=StdLoggerDisableCritical -gc criticaldisable.d -ofcritical -Istd/experimental/logger/ -main -unittest liblogger.so
./critical

fatal: fataldisable.d std/experimental/logger/*.d liblogger.so
dmd -debug -version=StdLoggerDisableFatal -gc fataldisable.d -offatal -Istd/experimental/logger/ -main -unittest liblogger.so
./fatal

test1: test1.d std/logger/*.d
dmd -debug -gc test1.d -oftest1 std/logger/*.d
25 changes: 25 additions & 0 deletions arraytest.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import std.stdio;
import std.container;

abstract class A {
void write() {}
}

class B : A {
override void write() { writeln("Hello from B"); }
}

struct Pair {
string name;
A logger;
}

void main() {
Array!Pair arr;
arr.insertBack(Pair("a", new B));
arr.insertBack(Pair("a", new B));

foreach(it; arr) {
it.logger.write();
}
}
26 changes: 26 additions & 0 deletions criticaldisable.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import std.experimental.logger;

/**
This will not work
*/

unittest {
import std.conv : to;
auto tl = new TestLogger(LogLevel.trace);
tl.info(); int line = __LINE__;
tl.critical();

assert(tl.line == line, to!string(tl.line));
}

unittest
{
static assert( isLoggingActive!(LogLevel.trace));
static assert( isLoggingActive!(LogLevel.info));
static assert( isLoggingActive!(LogLevel.warning));
static assert( isLoggingActive!(LogLevel.error));
static assert(!isLoggingActive!(LogLevel.critical));
static assert( isLoggingActive!(LogLevel.fatal));
}
4 changes: 2 additions & 2 deletions dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"copyright": "Copyright © 2014, burner",
"authors": ["Robert burner Schadek"],
"license": "BSD",
"importPaths": [".", "std", "std/logger"],
"sourcePaths": ["std", "std/logger"]
"importPaths": [".", "std", "std/experimental/logger"],
"sourcePaths": ["std", "std/experimental/logger"]
}
26 changes: 26 additions & 0 deletions errordisable.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import std.experimental.logger;

/**
This will not work
*/

unittest {
import std.conv : to;
auto tl = new TestLogger(LogLevel.trace);
tl.info(); int line = __LINE__;
tl.error();

assert(tl.line == line, to!string(tl.line));
}

unittest
{
static assert( isLoggingActive!(LogLevel.trace));
static assert( isLoggingActive!(LogLevel.info));
static assert( isLoggingActive!(LogLevel.warning));
static assert(!isLoggingActive!(LogLevel.error));
static assert( isLoggingActive!(LogLevel.critical));
static assert( isLoggingActive!(LogLevel.fatal));
}
26 changes: 26 additions & 0 deletions fataldisable.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import std.experimental.logger;

/**
This will not work
*/

unittest {
import std.conv : to;
auto tl = new TestLogger(LogLevel.trace);
tl.info(); int line = __LINE__;
tl.fatal();

assert(tl.line == line, to!string(tl.line));
}

unittest
{
static assert( isLoggingActive!(LogLevel.trace));
static assert( isLoggingActive!(LogLevel.info));
static assert( isLoggingActive!(LogLevel.warning));
static assert( isLoggingActive!(LogLevel.error));
static assert( isLoggingActive!(LogLevel.critical));
static assert(!isLoggingActive!(LogLevel.fatal));
}
26 changes: 26 additions & 0 deletions infodisable.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import std.experimental.logger;

/**
This will not work
*/

unittest {
import std.conv : to;
auto tl = new TestLogger(LogLevel.trace);
tl.trace(); int line = __LINE__;
tl.info();

assert(tl.line == line, to!string(tl.line));
}

unittest
{
static assert( isLoggingActive!(LogLevel.trace));
static assert(!isLoggingActive!(LogLevel.info));
static assert( isLoggingActive!(LogLevel.warning));
static assert( isLoggingActive!(LogLevel.error));
static assert( isLoggingActive!(LogLevel.critical));
static assert( isLoggingActive!(LogLevel.fatal));
}
2 changes: 1 addition & 1 deletion main.d
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std.logger;
import std.experimental.logger;

void main() {
log("Hello World");
Expand Down
104 changes: 104 additions & 0 deletions martin.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
private struct MakeType(LogLevel.Value v) { enum val = v; alias val this; }

/// LogLevel
struct LogLevel
{
// make one type per log level to allow compile time disabling of logging
// each loglevel value implicitly converts to the runtime enum value
enum debug_ = MakeType!(Value.debug_)();
enum info = MakeType!(Value.info)();
enum error = MakeType!(Value.error)();

this(Value v)(MakeType!v val) { this(val.val); }
this(Value val) { _val = val; }

private:
// runtime loglevel value
enum Value : ubyte {
debug_,
info,
error,
}

Value _val;
alias _val this;
}

/// Logger concept
enum isLogger(T) = is(typeof({
LogLevel v = T.init.minLogLevel;
LogEntry e;
T.init.write(e);
}));

/// Logger interface for dynamic binding
interface Logger {
LogLevel minLogLevel() const;
void write(in LogEntry);
}
static assert(isLogger!Logger);

/// create a Logger interface shim
Logger loggerObject(T)(T t) if (isLogger!T) {
static class Impl {
T t;
LogLevel minLogLevel() const { return T.minLogLevel; }
void write(in LogEntry e) { t.write(e); }
}
return new Impl(t);
}

/// a log entry
struct LogEntry { string file; int line; string msg; }

/// log with compile-time LogLevel
void log(Logger, LogLevel.Value level, int line = __LINE__, string file = __FILE__, Args...)(ref Logger logger, MakeType!level, lazy Args args) if (level >= logger.minLogLevel) {
import std.conv : text;
logger.write(LogEntry(file, line, text(args)));
}
/// log with compile-time LogLevel, optimized out
void log(Logger, LogLevel.Value level, int line = __LINE__, string file = __FILE__, Args...)(ref Logger logger, MakeType!level, lazy Args args) if (level < logger.minLogLevel) {
}

// runtime LogLevel
void log(Logger, string file=__FILE__, int line=__LINE__, Args...)(ref Logger logger, LogLevel level, lazy Args args) {
if (level < logger.minLogLevel) return;
import std.conv : text;
logger.write(LogEntry(file, line, text(args)));
}

unittest
{
static struct TestLogger
{
LogLevel ll = LogLevel.error;
LogLevel minLogLevel() @property {
return ll;
}
void write(in LogEntry e) { _entries ~= e; }

const(LogEntry)[] _entries;
}
static assert(isLogger!TestLogger);

TestLogger logger;
// compile time log level
logger.log(LogLevel.debug_, "bar", 0);
assert(!logger._entries.length);
logger.log(LogLevel.error, "foo", 1);
assert(logger._entries == [LogEntry(__FILE__, __LINE__ - 1, "foo1")]);

logger._entries.clear();

// runtime log level
LogLevel level = LogLevel.debug_;
logger.log(level, "bar", 2);
assert(!logger._entries.length);
level = __ctfe ? LogLevel.info : LogLevel.error;
logger.log(level, "foo", 3);
assert(logger._entries == [LogEntry(__FILE__, __LINE__ - 1, "foo3")]);
}

void main()
{
}

0 comments on commit 5db2fc4

Please sign in to comment.