spdlog/include/spdlog/spdlog.h

191 lines
7.9 KiB
C
Raw Normal View History

2016-04-20 04:57:49 -04:00
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
// spdlog main header file.
// see example.cpp for usage example
#pragma once
2017-02-03 08:27:06 -05:00
#include "common.h"
#include "logger.h"
2016-04-20 04:57:49 -04:00
#include <memory>
#include <functional>
#include <chrono>
#include <string>
namespace spdlog
{
2016-05-14 18:49:15 -04:00
2016-09-14 17:38:21 -04:00
//
2016-04-20 04:57:49 -04:00
// Return an existing logger or nullptr if a logger with such name doesn't exist.
2016-09-03 07:08:55 -04:00
// example: spdlog::get("my_logger")->info("hello {}", "world");
2016-04-20 04:57:49 -04:00
//
std::shared_ptr<logger> get(const std::string& name);
2016-09-03 07:08:55 -04:00
2016-04-20 04:57:49 -04:00
//
// Set global formatting
// example: spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v");
//
void set_pattern(const std::string& format_string);
void set_formatter(formatter_ptr f);
//
2018-01-03 10:19:56 -05:00
// Set global logging level
2016-04-20 04:57:49 -04:00
//
void set_level(level::level_enum log_level);
2018-01-03 10:19:56 -05:00
//
// Set global flush level
//
void flush_on(level::level_enum log_level);
2016-04-20 04:57:49 -04:00
2016-08-04 20:56:40 -04:00
//
// Set global error handler
//
2018-02-25 06:39:37 -05:00
void set_error_handler(log_err_handler handler);
2016-08-04 20:56:40 -04:00
2016-04-20 04:57:49 -04:00
//
// Turn on async mode (off by default) and set the queue size for each async_logger.
// effective only for loggers created after this call.
// queue_size: size of queue (must be power of 2):
// Each logger will pre-allocate a dedicated queue with queue_size entries upon construction.
//
// async_overflow_policy (optional, block_retry by default):
// async_overflow_policy::block_retry - if queue is full, block until queue has room for the new log entry.
2018-02-24 20:43:26 -05:00
// async_overflow_policy::discard_log_msg - never block and discard any new messages when queue overflows.
2016-04-20 04:57:49 -04:00
//
// worker_warmup_cb (optional):
// callback function that will be called in worker thread upon start (can be used to init stuff like thread affinity)
//
// worker_teardown_cb (optional):
// callback function that will be called in worker thread upon exit
//
void set_async_mode(size_t queue_size, const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, const std::function<void()>& worker_warmup_cb = nullptr, const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), const std::function<void()>& worker_teardown_cb = nullptr);
2016-04-20 04:57:49 -04:00
// Turn off async mode
void set_sync_mode();
//
2016-09-14 17:38:21 -04:00
// Create and register multi/single threaded basic file logger.
2017-08-19 08:49:16 -04:00
// Basic logger simply writes to given file without any limitations or rotations.
//
std::shared_ptr<logger> basic_logger_mt(const std::string& logger_name, const filename_t& filename, bool truncate = false);
std::shared_ptr<logger> basic_logger_st(const std::string& logger_name, const filename_t& filename, bool truncate = false);
2016-04-20 04:57:49 -04:00
//
// Create and register multi/single threaded rotating file logger
//
std::shared_ptr<logger> rotating_logger_mt(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files);
std::shared_ptr<logger> rotating_logger_st(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files);
2016-04-20 04:57:49 -04:00
//
2018-02-24 20:43:26 -05:00
// Create file logger which creates new file on the given time (default in midnight):
2016-04-20 04:57:49 -04:00
//
std::shared_ptr<logger> daily_logger_mt(const std::string& logger_name, const filename_t& filename, int hour=0, int minute=0);
std::shared_ptr<logger> daily_logger_st(const std::string& logger_name, const filename_t& filename, int hour=0, int minute=0);
2016-04-20 04:57:49 -04:00
//
// Create and register stdout/stderr loggers
//
std::shared_ptr<logger> stdout_logger_mt(const std::string& logger_name);
std::shared_ptr<logger> stdout_logger_st(const std::string& logger_name);
std::shared_ptr<logger> stderr_logger_mt(const std::string& logger_name);
std::shared_ptr<logger> stderr_logger_st(const std::string& logger_name);
//
// Create and register colored stdout/stderr loggers
//
std::shared_ptr<logger> stdout_color_mt(const std::string& logger_name);
std::shared_ptr<logger> stdout_color_st(const std::string& logger_name);
std::shared_ptr<logger> stderr_color_mt(const std::string& logger_name);
std::shared_ptr<logger> stderr_color_st(const std::string& logger_name);
2016-04-20 04:57:49 -04:00
//
// Create and register a syslog logger
//
#ifdef SPDLOG_ENABLE_SYSLOG
2017-12-04 07:03:40 -05:00
std::shared_ptr<logger> syslog_logger(const std::string& logger_name, const std::string& ident = "", int syslog_option = 0, int syslog_facilty = (1<<3));
2016-04-20 04:57:49 -04:00
#endif
#if defined(__ANDROID__)
std::shared_ptr<logger> android_logger(const std::string& logger_name, const std::string& tag = "spdlog");
#endif
2016-04-20 04:57:49 -04:00
// Create and register a logger with a single sink
2016-06-17 01:29:12 -04:00
std::shared_ptr<logger> create(const std::string& logger_name, const sink_ptr& sink);
2016-04-20 04:57:49 -04:00
// Create and register a logger with multiple sinks
std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks);
template<class It>
std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end);
// Create and register a logger with templated sink type
2016-08-20 06:55:50 -04:00
// Example:
// spdlog::create<daily_file_sink_st>("mylog", "dailylog_filename");
2016-04-20 04:57:49 -04:00
template <typename Sink, typename... Args>
2018-02-25 06:39:37 -05:00
std::shared_ptr<spdlog::logger> create(const std::string& logger_name, Args... args);
2016-04-20 04:57:49 -04:00
// Create and register an async logger with a single sink
std::shared_ptr<logger> create_async(const std::string& logger_name, const sink_ptr& sink, size_t queue_size, const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, const std::function<void()>& worker_warmup_cb = nullptr, const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), const std::function<void()>& worker_teardown_cb = nullptr);
// Create and register an async logger with multiple sinks
std::shared_ptr<logger> create_async(const std::string& logger_name, sinks_init_list sinks, size_t queue_size, const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, const std::function<void()>& worker_warmup_cb = nullptr, const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), const std::function<void()>& worker_teardown_cb = nullptr);
template<class It>
std::shared_ptr<logger> create_async(const std::string& logger_name, const It& sinks_begin, const It& sinks_end, size_t queue_size, const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, const std::function<void()>& worker_warmup_cb = nullptr, const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), const std::function<void()>& worker_teardown_cb = nullptr);
2016-04-20 04:57:49 -04:00
// Register the given logger with the given name
void register_logger(std::shared_ptr<logger> logger);
// Apply a user defined function on all registered loggers
2016-08-20 06:55:50 -04:00
// Example:
// spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) {l->flush();});
void apply_all(std::function<void(std::shared_ptr<logger>)> fun);
2016-04-20 04:57:49 -04:00
// Drop the reference to the given logger
void drop(const std::string &name);
// Drop all references from the registry
2016-04-20 04:57:49 -04:00
void drop_all();
///////////////////////////////////////////////////////////////////////////////
//
// Trace & Debug can be switched on/off at compile time for zero cost debug statements.
2017-10-24 06:34:58 -04:00
// Uncomment SPDLOG_DEBUG_ON/SPDLOG_TRACE_ON in tweakme.h to enable.
// SPDLOG_TRACE(..) will also print current file and line.
2016-04-20 04:57:49 -04:00
//
// Example:
2016-07-15 10:55:34 -04:00
// spdlog::set_level(spdlog::level::trace);
// SPDLOG_TRACE(my_logger, "some trace message");
// SPDLOG_TRACE(my_logger, "another trace message {} {}", 1, 2);
// SPDLOG_DEBUG(my_logger, "some debug message {} {}", 3, 4);
2016-04-20 04:57:49 -04:00
///////////////////////////////////////////////////////////////////////////////
#ifdef SPDLOG_TRACE_ON
2017-10-16 14:32:16 -04:00
# define SPDLOG_STR_H(x) #x
# define SPDLOG_STR_HELPER(x) SPDLOG_STR_H(x)
# ifdef _MSC_VER
# define SPDLOG_TRACE(logger, ...) logger->trace("[ " __FILE__ "(" SPDLOG_STR_HELPER(__LINE__) ") ] " __VA_ARGS__)
# else
# define SPDLOG_TRACE(logger, ...) logger->trace("[ " __FILE__ ":" SPDLOG_STR_HELPER(__LINE__) " ] " __VA_ARGS__)
# endif
2017-08-25 11:19:29 -04:00
#else
2017-10-16 14:32:16 -04:00
# define SPDLOG_TRACE(logger, ...) (void)0
2016-04-20 04:57:49 -04:00
#endif
#ifdef SPDLOG_DEBUG_ON
2017-10-16 14:32:16 -04:00
# define SPDLOG_DEBUG(logger, ...) logger->debug(__VA_ARGS__)
2016-04-20 04:57:49 -04:00
#else
2017-10-16 14:32:16 -04:00
# define SPDLOG_DEBUG(logger, ...) (void)0
2016-04-20 04:57:49 -04:00
#endif
}
#include "details/spdlog_impl.h"