Skip to content
This repository has been archived by the owner on Apr 6, 2019. It is now read-only.
Simon Ninon edited this page Dec 11, 2016 · 1 revision

tacopie provides a flexible way to setup logging.

By default, the library logs nothing. However, it is possible to set a logger of your choice (either provided by the library or a custom one).

Enable Logging

First of all, you have to enable logging by defining the appropriate CMake variable.

Please refer to this part of the wiki.

tacopie::active_logger

tacopie::active_logger is the variable that contains the instance of the current logger.

active_logger is defined as follows: extern std::unique_ptr<logger_iface> active_logger, within includes/tacopie/logger.hpp.

This variable can be reset to store the instance of your logger.

Please note that:

  • Setting and getting the value of active_logger is not thread_safe. Thus, it is preferable to access and modify active_logger before using cpp_redis inside your program.
  • By default, active_logger is set to nullptr, meaning that no logger is used.

Using the provided logger

tacopie provides a default logger: tacopie::logger.

You can use it by using this line of code:

tacopie::active_logger = std::unique_ptr<tacopie::logger>(new tacopie::logger);

Default logger supports different log_level to filter what you want to see. By default, the log_level is set to info, but can be changed using the constructor parameter:

tacopie::active_logger = std::unique_ptr<cpp_http_server::logger>(new tacopie::logger(tacopie::logger::log_level::debug));

The following levels are available:

  enum class log_level {
    error = 0,
    warn  = 1,
    info  = 2,
    debug = 3
  };

This logger uses a mutex to provide clean output in the multithreaded context of the library. This is really useful for debug and reporting issues, but it might not be what you are looking for if your primary concern is performance.

Using your own logger

You can create and use your custom logger.

tacopie provides an interface logger_iface, defined in includes/tacopie/logger.hpp:

class logger_iface {

  // ...

  virtual void debug(const std::string& msg, const std::string& file, unsigned int line) = 0;
  virtual void info(const std::string& msg, const std::string& file, unsigned int line) = 0;
  virtual void warn(const std::string& msg, const std::string& file, unsigned int line) = 0;
  virtual void error(const std::string& msg, const std::string& file, unsigned int line) = 0;

  // ...

};

Your custom logger just has to inherit from that interface and implement the debug, info, warn and error functions.

class my_logger : public tacopie::logger_iface {

  // ...

  void debug(const std::string& msg, const std::string& file, unsigned int line) { ... }
  void info(const std::string& msg, const std::string& file, unsigned int line) { ... }
  void warn(const std::string& msg, const std::string& file, unsigned int line) { ... }
  void error(const std::string& msg, const std::string& file, unsigned int line) { ... }

  // ...

};

Then, just set tacopie::active_logger with an instance of your custom logger: your custom logger will automatically be used:

tacopie::active_logger = std::unique_ptr<my_logger>(new my_logger);