$darkmode
logger.hpp File Reference
#include <functional>
#include <memory>
#include <string>
#include <fable/enum.hpp>
#include <spdlog/spdlog.h>
Include dependency graph for logger.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

 FABLE_ENUM_SERIALIZATION (spdlog::level::level_enum,({ {spdlog::level::level_enum::trace, "trace"}, {spdlog::level::level_enum::debug, "debug"}, {spdlog::level::level_enum::info, "info"}, {spdlog::level::level_enum::warn, "warning"}, {spdlog::level::level_enum::err, "error"}, {spdlog::level::level_enum::critical, "fatal"}, {spdlog::level::level_enum::off, "off"}, })) namespace cloe
 

Detailed Description

See also
cloe/core/logger.cpp
cloe/core.hpp

This file provides method for retrieving loggers for different namespaces within the cloe library.

The typical use-case of the functions in this file look like this:

auto log = logger::get("utility");
log->info("this is an informational message");

Function Documentation

◆ FABLE_ENUM_SERIALIZATION()

FABLE_ENUM_SERIALIZATION ( spdlog::level::level_enum  ,
({ {spdlog::level::level_enum::trace, "trace"}, {spdlog::level::level_enum::debug, "debug"}, {spdlog::level::level_enum::info, "info"}, {spdlog::level::level_enum::warn, "warning"}, {spdlog::level::level_enum::err, "error"}, {spdlog::level::level_enum::critical, "fatal"}, {spdlog::level::level_enum::off, "off"},})   
)

Logger is a shared_ptr to an spdlog::logger.

By default, this prints log messages to the console like so:

2017-04-20 08:37:03.787] [NAME] [LEVEL] MESSAGE

where NAME is the name of the logger, LEVEL is the logging level, and MESSAGE is the message to be printed.

Levels available are:

  • trace
  • debug
  • info
  • warn
  • error
  • critical

Each of these levels can be used via a method of the same name. For example:

auto log = logger::get("cloe")
log->set_level(spdlog::level::warning); // only show warnings or higher
log->info("This is an informational message.") // will not be shown
try {
...
} catch (exception& e) {
log->error("Something bad happened: {}", e);
}

Formatting available for these functions is via the fmt library, see http://fmtlib.net for an overview of the formatting supported.

For a more detailed look at what the logger provides, see <spdlog/logger.h>. Alternatively, the Github site has more documentation.

LogLevel represents the various severity levels of messages. See Logger for more details on the levels.

The individual levels can be accessed through spdlog::level. For example,

LogLevel l1 = spdlog::level::info;
auto l2 = spdlog::level::info;
assert(l1 == l2);

Return a logger for the given namespace name.

If the logger does not exist, a new one will be created with the logger factory. See the documentation on the Logger type for information on how to use it.

Sets the logger factory for loggers that do not exist yet.

The default implementation uses an spdlog::stdout_logger_mt. You can pass this function a lambda where you can use whatever logic you like. If this function registers a logger with a name, then this function will not be called again when getting that name, otherwise it will. That makes voodoo like the following possible:

void main() {
int count{}, created{};
logger::set_factory([&](std::string name) {
// Mangle all names so *I* have the control! Mwuahaha
count++;
name += "_mangled";
auto l = spdlog::get(name);
if (l) {
// I already created it, so return this
return l;
}
// Logger does not exist yet, so create it how I want.
auto log = spdlog::stdout_logger_mt(name);
log->set_level(spdlog::level::error); // only show errors or more severe
log->set_pattern(">>> OWNED <<< [%H:%M:%S] %v");
created++;
return log;
})
// ...
// do stuff like get a logger and use it:
auto log = logger::get("cloe");
log->info("Super informational!");
// ...
auto my_log = spdlog::stdout_logger_mt("special"); // bypass above
my_log->info("Special logger factory called {} times and created {} loggers.", count, created);
return 0;
}

You can pass in any function, function object, lambda, etc.

Set the acceptable level of output for all loggers.

This can be overridden on a per-logger basis. For example:

logger:set_level(spdlog::level::error);
logger::get("cloe/webserver")->set_level(spdlog::level::info);

Convert the strings trace, debug, info, warn|warning, err|error, critical|fatal, and off|disable into a logging level.

Convert a logging level to one of the strings trace, debug, info, warn, err, critical, and off.