trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
trantor::Logger Class Reference

This class implements log functions. More...

#include <trantor/utils/Logger.h>

Inheritance diagram for trantor::Logger:
Collaboration diagram for trantor::Logger:

Classes

class  SourceFile
 Calculate of basename of source files in compile time. More...

Public Types

enum  LogLevel {
  kTrace = 0 , kDebug , kInfo , kWarn ,
  kError , kFatal , kNumberOfLogLevels
}

Public Member Functions

 Logger (SourceFile file, int line)
 Logger (SourceFile file, int line, LogLevel level)
 Logger (SourceFile file, int line, bool isSysErr)
 Logger (SourceFile file, int line, LogLevel level, const char *func)
 Logger (LogLevel level)
 Logger (bool isSysErr)
Logger & setIndex (int index)
LogStreamstream ()

Static Public Member Functions

static void setOutputFunction (std::function< void(const char *msg, const uint64_t len)> outputFunc, std::function< void()> flushFunc, int index=-1)
 Set the output function.
static void setLogLevel (LogLevel level)
 Set the log level. Logs below the level are not printed.
static LogLevel logLevel ()
 Get the current log level.
static bool displayLocalTime ()
 Check whether it shows local time or UTC time.
static void setDisplayLocalTime (bool showLocalTime)
 Set whether it shows local time or UTC time. the default is UTC.
static bool hasSpdLogSupport ()
 Check whether trantor was build with spdlog support.
static void enableSpdLog (int index, std::shared_ptr< spdlog::logger > logger={})
 Enable logging with spdlog for the specified channel.
static void enableSpdLog (std::shared_ptr< spdlog::logger > logger={})
 Enable logging with spdlog for the default channel.
static void disableSpdLog (int index)
 Disable logging with spdlog for the specified channel.
static void disableSpdLog ()
 Disable logging with spdlog for the default channel.
static std::shared_ptr< spdlog::logger > getSpdLogger (int index=-1)
 Get the spdlog::logger set on the specified channel.
static std::shared_ptr< spdlog::logger > getDefaultSpdLogger (int index)
 Get a default spdlog::logger for the specified channel.

Protected Member Functions

void formatTime ()
Protected Member Functions inherited from trantor::NonCopyable
 NonCopyable (const NonCopyable &)=delete
NonCopyable & operator= (const NonCopyable &)=delete
 NonCopyable (NonCopyable &&) noexcept(true)=default
NonCopyable & operator= (NonCopyable &&) noexcept(true)=default

Static Protected Member Functions

static void defaultOutputFunction (const char *msg, const uint64_t len)
static void defaultFlushFunction ()
static bool & displayLocalTime_ ()
static LogLevel & logLevel_ ()
static std::function< void(const char *msg, const uint64_t len)> & outputFunc_ ()
static std::function< void()> & flushFunc_ ()
static std::function< void(const char *msg, const uint64_t len)> & outputFunc_ (size_t index)
static std::function< void()> & flushFunc_ (size_t index)

Protected Attributes

LogStream logStream_
Date date_ {Date::now()}
SourceFile sourceFile_
int fileLine_
LogLevel level_
int index_ {-1}
const char * func_ {nullptr}
std::size_t spdLogMessageOffset_ {0}

Friends

class RawLogger

Detailed Description

This class implements log functions.

Member Function Documentation

◆ disableSpdLog() [1/2]

void trantor::Logger::disableSpdLog ( )
inlinestatic

Disable logging with spdlog for the default channel.

Remarks
The spdlog::logger object is unregistered and destroyed only if it was created by getDefaultSpdLogger(). Custom loggers are only unset.

◆ disableSpdLog() [2/2]

void trantor::Logger::disableSpdLog ( int index)
static

Disable logging with spdlog for the specified channel.

Parameters
[in]channelindex (-1 = default channel).
Remarks
The spdlog::logger object is unregistered and destroyed only if it was created by getDefaultSpdLogger(index). Custom loggers are only unset.

◆ enableSpdLog() [1/2]

void trantor::Logger::enableSpdLog ( int index,
std::shared_ptr< spdlog::logger > logger = {} )
static

Enable logging with spdlog for the specified channel.

Parameters
indexchannel index (-1 = default channel).
loggerspdlog::logger object to use. If none given, defaults to getDefaultSpdLogger(index).
Remarks
If provided, it is not registered with the spdlog logger registry, it's up to you to register/drop it.

◆ enableSpdLog() [2/2]

void trantor::Logger::enableSpdLog ( std::shared_ptr< spdlog::logger > logger = {})
inlinestatic

Enable logging with spdlog for the default channel.

Parameters
loggerspdlog::logger object to use. If none given, defaults to getDefaultSpdLogger().
Remarks
If provided, it is not registered with the spdlog logger registry, it's up to you to register/drop it.

◆ getDefaultSpdLogger()

std::shared_ptr< spdlog::logger > trantor::Logger::getDefaultSpdLogger ( int index)
static

Get a default spdlog::logger for the specified channel.

This helper function provides a default spdlog::logger with a similar output format as the existing non-spdlog trantor::Logger format.

If a default logger was already created for the channel, it is returned as-is.

Otherwise, a new spdlog::logger object named "trantor" (for index < 0) or "trantor<channel>" is created, registered with spdlog, and configured as follows:

  • it has the same sinks as the lowest (index) enabled channel, or those of the spdlog::default_logger(), which by defaults outputs to stdout (spdlog::sinks::stdout_color_mt),
  • its format pattern is set to resemble to the existing non-spdlog trantor::Logger format ("%Y%m%d %T.%f %6t %^%=8l%$ [%!] %v - %s:%#"),
  • the logging level is set to unfiltered (spdlog::level::trace) since the internal trantor/drogon level filtering is still managed by trantor:::Logger,
  • the flush level is set to spdlog::level::error.
    Note
    To add custom sinks to all the channels, you can do that this way:

(optional) add your sinks to spdlog::default_logger(),

  1. create the default logger for the default channel using getDefaultSpdLogger(-1),
  2. if not done at step 1., add your sinks to this logger,
  3. enable the logger with enableSpdLog(),
  4. for the other channels, invoke enableSpdLog(index).
    Remarks
    The created spdlog::logger is automatically registered with the spdlog logger registry.
    Parameters
    [in]channelindex (-1 = default channel).
    Returns
    the default spdlog logger for the channel.

◆ getSpdLogger()

std::shared_ptr< spdlog::logger > trantor::Logger::getSpdLogger ( int index = -1)
static

Get the spdlog::logger set on the specified channel.

Parameters
[in]channelindex (-1 = default channel).
Returns
the logger, if set, else a null pointer.

◆ hasSpdLogSupport()

bool trantor::Logger::hasSpdLogSupport ( )
static

Check whether trantor was build with spdlog support.

Return values
trueif yes
falseif not - in this case, all the spdlog functions are noop functions

◆ logLevel()

LogLevel trantor::Logger::logLevel ( )
inlinestatic

Get the current log level.

Returns
LogLevel

◆ setLogLevel()

void trantor::Logger::setLogLevel ( LogLevel level)
inlinestatic

Set the log level. Logs below the level are not printed.

Parameters
level

◆ setOutputFunction()

void trantor::Logger::setOutputFunction ( std::function< void(const char *msg, const uint64_t len)> outputFunc,
std::function< void()> flushFunc,
int index = -1 )
inlinestatic

Set the output function.

Parameters
outputFuncThe function to output a log message.
flushFuncThe function to flush.
indexThe channel index.
Note
Logs are output to the standard output by default.

The documentation for this class was generated from the following file: