Function Dune::XT::Common::TimedLogger

Function Dune::XT::Common::TimedLogger

inline TimedLogging &Dune::XT::Common::TimedLogger()

Global instance of the timed logger.

This global logger instance is intended to be used in two ways: - Many classes or functions use this instance to log info, debug or warning messages. You can do so in your code by calling the TimedLogging::get() method, providing an identifier that should resemble the current scope:

void user_function()
{
  TimedLogger().get("user_function").info() << "some information" << std::endl;
  for (size_t ii = 0; ii < 100; ++ii)
    TimedLogger().get("user_function").debug() << "debug output number " << ii << std::endl;
}

You can also hold a TimedLogManager object within the current scope or class, if wished:

class UserClass
{
public:
  UserClass()
    : logger_(TimedLogger().get("UserClass"))
  {}

  void some_method()
  {
    logger_.warn() << "something is severly wrong!" << std::endl;
  }

private:
  TimedLogManager logger_;
}

Each time a new TimedLogManager is created using TimedLogging::get() the loglevel is increased, each time such a logger goes out of scope the loglevel is decreased. - You can use this instance to control the level (and style) of logging you want to have enabled in your application. You should call TimedLogging::create() as soon as possible (and only once!), until then all logging (execpt warnings) is disabled:

void silent_function()
{
  auto logger = TimedLogger().get("silent_function");
  logger.info() << "This will never show!" << std::endl;
  const bool all_is_well = false;
  if (!all_is_well)
    logger.warn() << "But this warning will!" << std::endl;
}

int main()
{
  TimedLogger().create(0,   // max info level (only the first)
                       -1,  // max debug level (disabled)
                       true // warnings are enabled
                       );

  auto logger = TimedLogger().get("main");
  logger.info()  << "Welcome to my application!" << std::endl;
  logger.debug() << "This will never show!" << std::endl;

  silent_function();
}

In addition you can enable coloring of the streams (see TimedPrefixedLogStream) and give their respective colors, if wished (see the implementation of color_map() or the foreground colors of Colors for available colors):

int main()
{
  TimedLogger().create(10,         // max info level
                       2,          // max debug level
                       true,       // warnings are enabled (the default)
                       true,       // colors are enabled (the default)
                       "blue",     // info color (the default)
                       "darkgrey", // debug color (the default)
                       "red"       // warning color (the default)
                      );

  auto logger = TimedLogger().get("main");
  logger.info() << "<- The 'main' prefix left of this should be blue!" << std::endl;
  logger.warn() << "<- The 'warn' prefix left of this should be red!"  << std::endl;
}

Note

Debug logging is only enabled if DUNE_XT_COMMON_TIMEDLOGGING_ENABLE_DEBUG is true (which is by default the case if NDEBUG is not defined) but you might still want to guard calls to logger.debug() for performance reasons.