2021-12-23 13:14:51 +00:00
|
|
|
/**
|
2024-04-09 17:56:33 +01:00
|
|
|
* Default logger
|
|
|
|
*
|
|
|
|
* Authors: Tristan Brice Velloza Kildaire (deavmi)
|
|
|
|
*/
|
2021-12-23 13:16:31 +00:00
|
|
|
module dlog.defaults;
|
2021-12-23 13:14:51 +00:00
|
|
|
|
2024-04-09 16:26:55 +01:00
|
|
|
import dlog.core;
|
2024-04-09 18:14:18 +01:00
|
|
|
import dlog.basic : BasicMessage, FileHandler, Level, BasicLogger;
|
2024-04-09 16:26:55 +01:00
|
|
|
import std.stdio : stdout;
|
2023-03-01 14:30:49 +00:00
|
|
|
import std.conv : to;
|
2024-04-09 16:26:55 +01:00
|
|
|
import dlog.utilities : flatten;
|
|
|
|
import std.array :join;
|
2024-04-09 17:53:32 +01:00
|
|
|
import std.datetime.systime : Clock, SysTime;
|
2021-12-23 10:14:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DefaultLogger
|
|
|
|
*
|
2024-04-09 17:53:32 +01:00
|
|
|
* The default logger logs using
|
|
|
|
* a pretty stock-standard (non-colored)
|
|
|
|
* message transformation and supports
|
|
|
|
* the basic levels of logging.
|
2021-12-23 10:14:36 +00:00
|
|
|
*/
|
2024-04-09 18:14:18 +01:00
|
|
|
public final class DefaultLogger : BasicLogger
|
2021-12-23 10:14:36 +00:00
|
|
|
{
|
2024-04-09 16:26:55 +01:00
|
|
|
/**
|
|
|
|
* The joiner for multi-argument
|
|
|
|
* log messages
|
|
|
|
*/
|
|
|
|
private string multiArgJoiner;
|
|
|
|
|
2023-03-03 13:21:41 +00:00
|
|
|
/**
|
|
|
|
* Constructs a new default logger
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* multiArgJoiner = the joiner to use
|
|
|
|
*/
|
2023-01-07 19:10:15 +00:00
|
|
|
this(string multiArgJoiner = " ")
|
2021-12-23 10:14:36 +00:00
|
|
|
{
|
2024-04-09 16:26:55 +01:00
|
|
|
this.multiArgJoiner = multiArgJoiner;
|
|
|
|
|
|
|
|
addTransform(new DefaultTransform());
|
|
|
|
addHandler(new FileHandler(stdout));
|
2021-12-23 10:14:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 13:26:33 +00:00
|
|
|
/**
|
2024-04-09 16:26:55 +01:00
|
|
|
* Logs the given message of an arbitrary amount of
|
|
|
|
* arguments and specifically sets the level to ERROR
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* segments = the arbitrary argumnets (alias sequence)
|
2023-03-03 13:26:33 +00:00
|
|
|
*/
|
2024-04-09 16:26:55 +01:00
|
|
|
public void error(TextType...)(TextType segments)
|
2021-12-23 10:14:36 +00:00
|
|
|
{
|
2024-04-09 16:26:55 +01:00
|
|
|
doLog(segments, Level.ERROR);
|
2021-12-23 10:14:36 +00:00
|
|
|
}
|
2024-04-09 16:26:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs the given message of an arbitrary amount of
|
|
|
|
* arguments and specifically sets the level to INFO
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* segments = the arbitrary argumnets (alias sequence)
|
|
|
|
*/
|
|
|
|
public void info(TextType...)(TextType segments)
|
|
|
|
{
|
|
|
|
doLog(segments, Level.INFO);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs the given message of an arbitrary amount of
|
|
|
|
* arguments and specifically sets the level to WARN
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* segments = the arbitrary argumnets (alias sequence)
|
|
|
|
*/
|
|
|
|
public void warn(TextType...)(TextType segments)
|
|
|
|
{
|
|
|
|
doLog(segments, Level.WARN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs the given message of an arbitrary amount of
|
|
|
|
* arguments and specifically sets the level to DEBUG
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* segments = the arbitrary argumnets (alias sequence)
|
|
|
|
*/
|
|
|
|
public void debug_(TextType...)(TextType segments)
|
|
|
|
{
|
|
|
|
doLog(segments, Level.DEBUG);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs the actual logging
|
|
|
|
* by packing up everything before
|
|
|
|
* sending it to the `log(Message)`
|
|
|
|
* method
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* segments = the compile-time segments
|
|
|
|
* level = the log level to use
|
|
|
|
*/
|
|
|
|
private void doLog(TextType...)(TextType segments, Level level)
|
|
|
|
{
|
|
|
|
/* Create a new basic message */
|
|
|
|
BasicMessage message = new BasicMessage();
|
|
|
|
|
|
|
|
/* Set the level */
|
|
|
|
message.setLevel(level);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Grab all compile-time arguments and make them
|
|
|
|
* into an array, then join them together and
|
|
|
|
* set that text as the message's text
|
|
|
|
*/
|
|
|
|
message.setText(join(flatten(segments), multiArgJoiner));
|
|
|
|
|
|
|
|
/* Log this message */
|
|
|
|
log(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias for debug_
|
|
|
|
*/
|
|
|
|
public alias dbg = debug_;
|
2021-12-23 10:14:36 +00:00
|
|
|
}
|
2023-02-27 17:07:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DefaultTransform
|
|
|
|
*
|
|
|
|
* Provides a transformation of the kind
|
|
|
|
*
|
2024-04-09 17:53:32 +01:00
|
|
|
* [date+time] (level): message `\n`
|
2023-02-27 17:07:09 +00:00
|
|
|
*/
|
2024-04-09 17:53:32 +01:00
|
|
|
private final class DefaultTransform : Transform
|
2023-02-27 17:07:09 +00:00
|
|
|
{
|
|
|
|
/**
|
2024-04-09 17:53:32 +01:00
|
|
|
* Performs the default transformation.
|
|
|
|
* If the message is not a `BasicMessage`
|
|
|
|
* then no transformation occurs.
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* message = the message to transform
|
|
|
|
* Returns: the transformed message
|
2023-02-27 17:07:09 +00:00
|
|
|
*/
|
2024-04-09 16:26:55 +01:00
|
|
|
public Message transform(Message message)
|
2023-02-27 17:07:09 +00:00
|
|
|
{
|
2024-04-09 16:26:55 +01:00
|
|
|
// Only handle BasicMessage(s)
|
|
|
|
BasicMessage bmesg = cast(BasicMessage)message;
|
|
|
|
if(bmesg is null)
|
|
|
|
{
|
|
|
|
return message;
|
|
|
|
}
|
2023-03-01 14:30:49 +00:00
|
|
|
|
2024-04-09 16:26:55 +01:00
|
|
|
string text;
|
2023-02-27 17:07:09 +00:00
|
|
|
|
|
|
|
/* Date and time */
|
|
|
|
SysTime currTime = Clock.currTime();
|
|
|
|
string timestamp = to!(string)(currTime);
|
2024-04-09 16:26:55 +01:00
|
|
|
text = "["~timestamp~"]";
|
2023-02-27 17:07:09 +00:00
|
|
|
|
2024-04-09 16:26:55 +01:00
|
|
|
/* Level */
|
|
|
|
text = text ~ "\t(";
|
|
|
|
text = text ~ to!(string)(bmesg.getLevel());
|
|
|
|
text = text ~ "): "~bmesg.getText();
|
2023-02-27 17:07:09 +00:00
|
|
|
|
|
|
|
/* Add trailing newline */
|
2024-04-09 16:26:55 +01:00
|
|
|
text = text ~ '\n';
|
2023-02-27 17:07:09 +00:00
|
|
|
|
2024-04-09 16:26:55 +01:00
|
|
|
/* Store the updated text */
|
|
|
|
bmesg.setText(text);
|
|
|
|
|
2023-02-27 17:07:09 +00:00
|
|
|
return message;
|
|
|
|
}
|
2024-04-09 16:26:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
version(unittest)
|
|
|
|
{
|
|
|
|
import std.meta : AliasSeq;
|
|
|
|
import std.stdio : writeln;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the DefaultLogger
|
|
|
|
*/
|
|
|
|
unittest
|
|
|
|
{
|
|
|
|
DefaultLogger logger = new DefaultLogger();
|
|
|
|
|
2024-04-09 18:14:18 +01:00
|
|
|
// Set logging level to at least INFO
|
|
|
|
logger.setLevel(Level.INFO);
|
|
|
|
|
2024-04-09 16:26:55 +01:00
|
|
|
alias testParameters = AliasSeq!("This is a log message", 1.1, true, [1,2,3], 'f', logger);
|
|
|
|
|
|
|
|
|
|
|
|
// Test various types one-by-one
|
|
|
|
static foreach(testParameter; testParameters)
|
|
|
|
{
|
|
|
|
logger.info(testParameter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test various parameters (of various types) all at once
|
|
|
|
logger.info(testParameters);
|
|
|
|
|
|
|
|
// Same as above but with a custom joiner set
|
|
|
|
logger = new DefaultLogger("(-)");
|
2024-04-09 18:14:18 +01:00
|
|
|
|
|
|
|
// Set logging level to at least INFO
|
|
|
|
logger.setLevel(Level.INFO);
|
|
|
|
|
2024-04-09 16:26:55 +01:00
|
|
|
logger.info(testParameters);
|
|
|
|
|
|
|
|
writeln();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Printing out some mixed data-types, also using a DEFAULT context
|
|
|
|
*/
|
|
|
|
unittest
|
|
|
|
{
|
2024-04-09 18:14:18 +01:00
|
|
|
// Create a default logger with the default joiner
|
2024-04-09 16:26:55 +01:00
|
|
|
DefaultLogger logger = new DefaultLogger();
|
|
|
|
|
2024-04-09 18:14:18 +01:00
|
|
|
// Set logging level to at least INFO
|
|
|
|
logger.setLevel(Level.INFO);
|
|
|
|
|
|
|
|
// Log some stuff
|
2024-04-09 16:26:55 +01:00
|
|
|
logger.info(["a", "b", "c", "d"], [1, 2], true);
|
|
|
|
|
|
|
|
writeln();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Printing out some mixed data-types, also using a DEFAULT context
|
|
|
|
* but also testing out the `error()`, `warn()`, `info()` and `debug()`
|
|
|
|
*/
|
|
|
|
unittest
|
|
|
|
{
|
2024-04-09 18:14:18 +01:00
|
|
|
// Create a default logger with the default joiner
|
2024-04-09 16:26:55 +01:00
|
|
|
DefaultLogger logger = new DefaultLogger();
|
|
|
|
|
2024-04-09 18:14:18 +01:00
|
|
|
// Set logging level to at least DEBUG
|
|
|
|
logger.setLevel(Level.DEBUG);
|
2024-04-09 16:26:55 +01:00
|
|
|
|
|
|
|
// Test out `error()`
|
|
|
|
logger.error(["woah", "LEVELS!"], 69.420);
|
|
|
|
|
|
|
|
// Test out `info()`
|
|
|
|
logger.info(["woah", "LEVELS!"], 69.420);
|
|
|
|
|
|
|
|
// Test out `warn()`
|
|
|
|
logger.warn(["woah", "LEVELS!"], 69.420);
|
|
|
|
|
|
|
|
// Test out `debug_()`
|
|
|
|
logger.debug_(["woah", "LEVELS!"], 69.420);
|
|
|
|
|
2024-04-09 18:14:18 +01:00
|
|
|
// Should not be able to see this
|
|
|
|
logger.setLevel(Level.INFO);
|
|
|
|
logger.debug_("Can't see me!");
|
|
|
|
|
2024-04-09 16:26:55 +01:00
|
|
|
writeln();
|
2023-02-27 17:07:09 +00:00
|
|
|
}
|