query/include/matador/logger/log_manager.hpp

304 lines
7.6 KiB
C++

#ifndef MATADOR_LOG_MANAGER_HPP
#define MATADOR_LOG_MANAGER_HPP
#include "matador/utils/singleton.hpp"
#include "matador/logger/logger.hpp"
#include "matador/logger/log_sink.hpp"
#include "matador/logger/file_sink.hpp"
#include "matador/logger/rotating_file_sink.hpp"
#include <memory>
namespace matador::logger {
/**
* @brief Manages all log domains
*
* The log_manager class is a singleton and
* manages all available log_domains
*
* There ist always a default log domain
* with the name "default"
* available for which sinks can be added
* and loggers can be created.
*/
class log_manager final : public utils::singleton<log_manager>
{
public:
/**
* Creates a logger with the given source name
* for the default log domain
*
* @param source Name of the source
* @return The created logger
*/
[[nodiscard]] logger create_logger(std::string source) const;
/**
* Creates a logger with the given source name
* for the log domain identified by the given
* log domain name.
*
* If the log domain with the given name doesn't exist,
* the domain is created
*
* @param source Name of the source
* @param domain_name The name of the log domain to execute to
* @return The created logger
*/
logger create_logger(std::string source, const std::string &domain_name);
/**
* Adds a log sink to the default log_domain
*
* @param sink Sink to add to the default log_domain
*/
void add_sink(sink_ptr sink) const;
/**
* Adds a log sink to the log_domain with the given name.
* If the log domain doesn't exist, it is automatically created.
*
* @param sink Sink to add
* @param domain_name Name of the log domain
*/
void add_sink(sink_ptr sink, const std::string &domain_name);
/**
* Clears all sinks from the default log domain
*/
void clear_all_sinks() const;
/**
* Clears all sinks from the log domain
* with the given name
*
* @param domain_name Domain name to clear all sinks from
*/
void clear_all_sinks(const std::string &domain_name);
/**
* Remove all log domains but the default log domain.
* Clears all sinks from the default log domain.
*/
void clear();
/**
* Sets the max default log level. The default
* max leven is LVL_FATAL. All log domains
* will start with this default max log range
*
* @param max_level max log level
*/
static void max_default_log_level(log_level max_level);
/**
* Returns the default max log level
*
* @return The max log level
*/
static log_level max_default_log_level();
/**
* Sets the default min log level. The default
* min leven is LVL_INFO. All log domains
* will start with this default max log range
*
* @param min_level min log level
*/
static void min_default_log_level(log_level min_level);
/**
* Returns the default min log level
*
* @return The min log level
*/
static log_level min_default_log_level();
/// @cond MATADOR_DEV
std::shared_ptr<log_domain> find_domain(const std::string &name);
void log_default(log_level lvl, const std::string &source, const char *message) const;
/// @endcond
protected:
/// @cond MATADOR_DEV
log_manager()
{
default_log_domain_ = log_domain_map_.insert(std::make_pair("default", std::make_shared<log_domain>("default", default_log_level_range_))).first->second;
}
/// @endcond
private:
std::shared_ptr<log_domain> acquire_domain(const std::string &name);
private:
friend class utils::singleton<log_manager>;
std::shared_ptr<log_domain> default_log_domain_;
std::map<std::string, std::shared_ptr<log_domain>> log_domain_map_;
static log_level_range default_log_level_range_;
};
/**
* Shortcut to create a file log sink
* with the given path. If the path doesn't
* exist, it is created.
*
* @param logfile Path to the logfile
* @return A shared_ptr to the file_sink
*/
std::shared_ptr<file_sink> create_file_sink(const std::string &logfile);
/**
* Shortcut to create a stderr log sink.
*
* @return A shared_ptr to the stderr_sink
*/
std::shared_ptr<stderr_sink> create_stderr_sink();
/**
* Shortcut to create a stdout log sink.
*
* @return A shared_ptr to the stdout_sink
*/
std::shared_ptr<stdout_sink> create_stdout_sink();
/**
* Shortcut to create a rotating file log sink
* with the given path, max log files and max
* log file size. If the path doesn't
* exist, it is created.
*
* @param logfile Path to the log file
* @param max_size Max log file size
* @param file_count Max number of log files
* @return A shared_ptr to the rotating_file_sink
*/
std::shared_ptr<rotating_file_sink> create_rotating_file_sink(const std::string &logfile, size_t max_size, size_t file_count);
/**
* Sets the default min log level.
*
* @param min_lvl Default min log level
*/
void default_min_log_level(log_level min_lvl);
/**
* Sets the default max log level.
*
* @param max_lvl Default max log level
*/
void default_max_log_level(log_level max_lvl);
/**
* Sets the domain min log level for the
* domain with the given name.
*
* @param name Log domain name
* @param min_lvl Default min log level
*/
void domain_min_log_level(const std::string &name, log_level min_lvl);
/**
* Sets the default max log level for the
* domain with the given name.
*
* @param name Log domain name
* @param max_lvl Default max log level
*/
void domain_max_log_level(const std::string &name, log_level max_lvl);
/**
* Adds a log sink to the default log domain
*
* @param sink The log sink to add
*/
void add_log_sink(sink_ptr sink);
/**
* Adds a log sink to the log domain
* with the given name. If the domain
* doesn't exist, it is created.
*
* @param sink The log sink to add
* @param domain The log domain name to add
*/
void add_log_sink(sink_ptr sink, const std::string &domain);
/**
* Removes all sinks from the
* default domain
*/
void clear_all_log_sinks();
/**
* Removes all sinks from the log domain
* with the given domain name
*
* @param domain Domain name to clear all sinks
*/
void clear_all_log_sinks(const std::string &domain);
/**
* Creates a logger with the given source name
* connected to the default log domain.
*
* @param source The name of the source
* @return The logger instance
*/
logger create_logger(std::string source);
/**
* Creates a logger with the given source name
* connected to the log domain with the given
* name. If the domain doesn't exist, it is created
*
* @param source The name of the source
* @param domain The name of the log domain
* @return The logger instance
*/
logger create_logger(std::string source, const std::string &domain);
/**
* Logs the given message for the given source and log level
* to the default log domain.
*
* @param lvl Log level
* @param source Source of the log message
* @param message Message to log
*/
void log_default(log_level lvl, const std::string &source, const char *message);
/**
* Log the given message with source and log level
* to the default domain. The message will be created
* from the what-argument and the args while the preprocessed
* message uses the printf style to add the arguments.
*
* @tparam ARGS Type of the arguments
* @param lvl Log level
* @param source Source of the log message
* @param what The printf style message
* @param args The arguments for the message
*/
template<typename... ARGS>
void log(const log_level lvl, const std::string &source, const char *what, ARGS const &... args)
{
char message_buffer[16384];
#ifdef _MSC_VER
sprintf_s(message_buffer, 912, what, args...);
#else
sprintf(message_buffer, what, args...);
#endif
log_default(lvl, source, message_buffer);
}
}
#endif //MATADOR_LOG_MANAGER_HPP