The library defines Logger interface to log messages.
The library defines Logger interface to log messages. The library knows when and what to log, but how it leaves on library user. The default GlobalLogger is dummy implementation, that ignores all messages on all logging levels. The client can set global Logger for whole library.
-
c++
class ConsoleLogger : public Logger
{
void LogInfo(LogMessage& msg) final
{
std::cout << " " << msg.Compose() << std::endl;
}
void LogError(LogMessage& msg) final
{
std::cout << " error: " << msg.Compose() << std::endl;
}
void LogDebug(LogMessage& msg) final
{
std::cout << " debug: " << msg.Compose() << std::endl;
}
};
static void Log()
{
}
-
java
class ConsoleLogger extends Logger {
public void LogInfo(LogMessage msg) {
System.out.println(
" native: " + msg.
Compose());
}
public void LogError(LogMessage msg) {
System.out.println(
" native:error: " + msg.
Compose());
}
public void LogDebug(LogMessage msg) {
System.out.println(
" native:debug: " + msg.
Compose());
}
}
public void Log() {
Logger log = new ConsoleLogger();
GlobalLogger.Set(log);
}
-
csharp
class ConsoleLogger : Logger
{
public override void LogInfo(LogMessage msg)
{
System.Console.WriteLine(
" native: " + msg.
Compose());
}
public override void LogError(LogMessage msg)
{
System.Console.WriteLine(
" native:error: " + msg.
Compose());
}
public override void LogDebug(LogMessage msg)
{
System.Console.WriteLine(
" native:debug: " + msg.
Compose());
}
}
public void Log()
{
Logger log = new ConsoleLogger();
GlobalLogger.Set(log);
}
The Logger can be implemented also in Java / CSharp bindings.
The logger has 3 levels
- Info - on interface boundaries.
- Error - when error is reported by used libraries of in wrong arguments
- Debug - messages that helps with bug hunting in this library
Client decides which level is logged. The LogMessage is used to avoid unnecessary composition of logger message. LogMessage is composed only when client wants to log it.
- Warning
- Client MUST NOT store the LogMessage and use it outside Logger::LogInfo, Logger::LogError, Logger::LogDebug call. The library stores only references to data in LogMessage. The references can be invalid when Logger functions end.
- Note
- Only for C++ the library provides also FormattingComposer, FormattingLogger that extends (and use) Logger with functionality similar as printf function.
For more details see Logging overview.