mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-12 00:40:26 +08:00
spdlog
This commit is contained in:
parent
cbddc8796a
commit
c7b8c762fb
@ -3,7 +3,7 @@
|
||||
#include<initializer_list>
|
||||
#include<chrono>
|
||||
|
||||
namespace spitlog
|
||||
namespace spdlog
|
||||
{
|
||||
class formatter;
|
||||
namespace sinks {
|
||||
@ -14,7 +14,7 @@ class sink;
|
||||
using log_clock = std::chrono::system_clock;
|
||||
using sink_ptr = std::shared_ptr < sinks::sink > ;
|
||||
using sinks_init_list = std::initializer_list < sink_ptr > ;
|
||||
using formatter_ptr = std::shared_ptr<spitlog::formatter>;
|
||||
using formatter_ptr = std::shared_ptr<spdlog::formatter>;
|
||||
|
||||
//Log level enum
|
||||
namespace level
|
||||
@ -27,11 +27,12 @@ typedef enum
|
||||
WARN,
|
||||
ERR,
|
||||
CRITICAL,
|
||||
NONE = 99
|
||||
ALWAYS,
|
||||
OFF
|
||||
} level_enum;
|
||||
|
||||
static const char* level_names[] { "trace", "debug", "info", "warning", "error", "critical", "fatal" };
|
||||
inline const char* to_str(spitlog::level::level_enum l)
|
||||
static const char* level_names[] { "trace", "debug", "info", "warning", "error", "critical", "", ""};
|
||||
inline const char* to_str(spdlog::level::level_enum l)
|
||||
{
|
||||
return level_names[l];
|
||||
}
|
||||
@ -52,4 +53,4 @@ private:
|
||||
|
||||
};
|
||||
|
||||
} //spitlog
|
||||
} //spdlog
|
@ -10,7 +10,7 @@
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
|
||||
namespace spitlog
|
||||
namespace spdlog
|
||||
{
|
||||
namespace details
|
||||
{
|
@ -5,7 +5,7 @@
|
||||
//Source: http://stackoverflow.com/a/4351484/192001
|
||||
//Modified version to pad zeros according to padding arg
|
||||
|
||||
namespace spitlog {
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
|
||||
const char digit_pairs[201] = {
|
@ -9,7 +9,7 @@
|
||||
#include "stack_buf.h"
|
||||
#include<iostream>
|
||||
|
||||
namespace spitlog
|
||||
namespace spdlog
|
||||
{
|
||||
namespace details
|
||||
{
|
@ -16,7 +16,7 @@
|
||||
|
||||
|
||||
|
||||
namespace spitlog
|
||||
namespace spdlog
|
||||
{
|
||||
namespace details
|
||||
{
|
||||
@ -36,16 +36,14 @@ public:
|
||||
|
||||
~file_helper()
|
||||
{
|
||||
if (_fd)
|
||||
std::fclose(_fd);
|
||||
close();
|
||||
}
|
||||
|
||||
|
||||
void open(const std::string& filename)
|
||||
{
|
||||
|
||||
if (_fd)
|
||||
std::fclose(_fd);
|
||||
close();
|
||||
|
||||
_filename = filename;
|
||||
for (int tries = 0; tries < open_max_tries; ++tries)
|
||||
@ -61,8 +59,11 @@ public:
|
||||
|
||||
void close()
|
||||
{
|
||||
std::fclose(_fd);
|
||||
_fd = nullptr;
|
||||
if (_fd)
|
||||
{
|
||||
std::fclose(_fd);
|
||||
_fd = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void write(const log_msg& msg)
|
@ -8,7 +8,7 @@
|
||||
// Line logger class - aggregates operator<< calls to fast ostream
|
||||
// and logs upon destruction
|
||||
|
||||
namespace spitlog
|
||||
namespace spdlog
|
||||
{
|
||||
namespace details
|
||||
{
|
||||
@ -76,4 +76,4 @@ private:
|
||||
bool _enabled;
|
||||
};
|
||||
} //Namespace details
|
||||
} // Namespace spitlog
|
||||
} // Namespace spdlog
|
@ -3,7 +3,7 @@
|
||||
#include "../common.h"
|
||||
#include "./fast_oss.h"
|
||||
|
||||
namespace spitlog
|
||||
namespace spdlog
|
||||
{
|
||||
namespace details
|
||||
{
|
136
include/spdlog/details/logger_impl.h
Normal file
136
include/spdlog/details/logger_impl.h
Normal file
@ -0,0 +1,136 @@
|
||||
#pragma once
|
||||
//
|
||||
// Logger implementation
|
||||
//
|
||||
|
||||
|
||||
#include "./line_logger.h"
|
||||
|
||||
|
||||
inline spdlog::logger::logger(const std::string& logger_name, sinks_init_list sinks_list) :
|
||||
_name(logger_name),
|
||||
_sinks(sinks_list)
|
||||
{
|
||||
// no support under vs2013 for member initialization for std::atomic
|
||||
_level = level::INFO;
|
||||
}
|
||||
|
||||
template<class It>
|
||||
inline spdlog::logger::logger(const std::string& logger_name, const It& begin, const It& end) :
|
||||
_name(logger_name),
|
||||
_sinks(begin, end)
|
||||
{}
|
||||
|
||||
|
||||
inline void spdlog::logger::set_formatter(spdlog::formatter_ptr msg_formatter)
|
||||
{
|
||||
_formatter = msg_formatter;
|
||||
}
|
||||
|
||||
inline void spdlog::logger::set_pattern(const std::string& pattern)
|
||||
{
|
||||
_formatter = std::make_shared<pattern_formatter>(pattern);
|
||||
}
|
||||
|
||||
inline spdlog::formatter_ptr spdlog::logger::get_formatter() const
|
||||
{
|
||||
return _formatter;
|
||||
}
|
||||
|
||||
|
||||
template <typename... Args>
|
||||
inline spdlog::details::line_logger spdlog::logger::log(level::level_enum lvl, const Args&... args) {
|
||||
bool msg_enabled = should_log(lvl);
|
||||
details::line_logger l(this, lvl, msg_enabled);
|
||||
if (msg_enabled)
|
||||
_variadic_log(l, args...);
|
||||
return l;
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline spdlog::details::line_logger spdlog::logger::log(const Args&... args) {
|
||||
return log(level::ALWAYS, args...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline spdlog::details::line_logger spdlog::logger::trace(const Args&... args)
|
||||
{
|
||||
return log(level::TRACE, args...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline spdlog::details::line_logger spdlog::logger::debug(const Args&... args)
|
||||
{
|
||||
return log(level::DEBUG, args...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline spdlog::details::line_logger spdlog::logger::info(const Args&... args)
|
||||
{
|
||||
return log(level::INFO, args...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline spdlog::details::line_logger spdlog::logger::warn(const Args&... args)
|
||||
{
|
||||
return log(level::WARN, args...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline spdlog::details::line_logger spdlog::logger::error(const Args&... args)
|
||||
{
|
||||
return log(level::ERR, args...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline spdlog::details::line_logger spdlog::logger::critical(const Args&... args)
|
||||
{
|
||||
return log(level::CRITICAL, args...);
|
||||
}
|
||||
|
||||
inline const std::string& spdlog::logger::name() const
|
||||
{
|
||||
return _name;
|
||||
}
|
||||
|
||||
inline void spdlog::logger::set_level(spdlog::level::level_enum log_level)
|
||||
{
|
||||
_level.store(log_level);
|
||||
}
|
||||
|
||||
inline spdlog::level::level_enum spdlog::logger::level() const
|
||||
{
|
||||
return static_cast<spdlog::level::level_enum>(_level.load());
|
||||
}
|
||||
|
||||
inline bool spdlog::logger::should_log(spdlog::level::level_enum msg_level) const
|
||||
{
|
||||
return msg_level >= _level.load();
|
||||
}
|
||||
|
||||
inline void spdlog::logger::stop_logging()
|
||||
{
|
||||
set_level(level::OFF);
|
||||
}
|
||||
|
||||
|
||||
inline void spdlog::logger::_variadic_log(spdlog::details::line_logger&) {}
|
||||
|
||||
template <typename First, typename... Rest>
|
||||
void spdlog::logger::_variadic_log(spdlog::details::line_logger& l, const First& first, const Rest&... rest)
|
||||
{
|
||||
l.write(first);
|
||||
l.write(' ');
|
||||
_variadic_log(l, rest...);
|
||||
}
|
||||
|
||||
inline void spdlog::logger::_log_msg(details::log_msg& msg)
|
||||
{
|
||||
//Use default formatter if not set
|
||||
if (!_formatter)
|
||||
_formatter = std::make_shared<pattern_formatter>("%+");
|
||||
_formatter->format(msg);
|
||||
for (auto &sink : _sinks)
|
||||
sink->log(msg);
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
// null, no cost mutex
|
||||
|
||||
namespace spitlog {
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
struct null_mutex
|
||||
{
|
@ -7,7 +7,7 @@
|
||||
#include <Windows.h>
|
||||
#endif
|
||||
|
||||
namespace spitlog
|
||||
namespace spdlog
|
||||
{
|
||||
namespace details
|
||||
{
|
||||
@ -123,7 +123,7 @@ inline int utc_minutes_offset(const std::tm& tm = localtime())
|
||||
|
||||
} //os
|
||||
} //details
|
||||
} //spitlog
|
||||
} //spdlog
|
||||
|
||||
|
||||
|
@ -10,7 +10,7 @@
|
||||
#include "./fast_oss.h"
|
||||
#include "./os.h"
|
||||
|
||||
namespace spitlog
|
||||
namespace spdlog
|
||||
{
|
||||
namespace details {
|
||||
class flag_formatter
|
||||
@ -377,12 +377,12 @@ class full_formatter :public flag_formatter
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// pattern_formatter inline impl
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
inline spitlog::pattern_formatter::pattern_formatter(const std::string& pattern)
|
||||
inline spdlog::pattern_formatter::pattern_formatter(const std::string& pattern)
|
||||
{
|
||||
compile_pattern(pattern);
|
||||
}
|
||||
|
||||
inline void spitlog::pattern_formatter::compile_pattern(const std::string& pattern)
|
||||
inline void spdlog::pattern_formatter::compile_pattern(const std::string& pattern)
|
||||
{
|
||||
auto end = pattern.end();
|
||||
std::unique_ptr<details::aggregate_formatter> user_chars;
|
||||
@ -411,7 +411,7 @@ inline void spitlog::pattern_formatter::compile_pattern(const std::string& patte
|
||||
}
|
||||
|
||||
}
|
||||
inline void spitlog::pattern_formatter::handle_flag(char flag)
|
||||
inline void spdlog::pattern_formatter::handle_flag(char flag)
|
||||
{
|
||||
switch (flag)
|
||||
{
|
||||
@ -523,7 +523,7 @@ inline void spitlog::pattern_formatter::handle_flag(char flag)
|
||||
}
|
||||
|
||||
|
||||
inline void spitlog::pattern_formatter::format(details::log_msg& msg)
|
||||
inline void spdlog::pattern_formatter::format(details::log_msg& msg)
|
||||
{
|
||||
for (auto &f : _formatters)
|
||||
{
|
@ -7,58 +7,77 @@
|
||||
#include <string>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "../logger.h"
|
||||
#include "../common.h"
|
||||
|
||||
namespace spitlog {
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
|
||||
class registry {
|
||||
public:
|
||||
std::shared_ptr<logger> get(const std::string& name)
|
||||
{
|
||||
std::lock_guard<std::mutex> l(_mutex);
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
auto found = _loggers.find(name);
|
||||
return found == _loggers.end() ? nullptr : found->second;
|
||||
}
|
||||
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)
|
||||
{
|
||||
std::lock_guard<std::mutex> l(_mutex);
|
||||
auto new_logger = std::make_shared<logger>(logger_name, sinks);
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
auto new_logger = std::make_shared<logger>(logger_name, sinks_begin, sinks_end);
|
||||
new_logger->set_formatter(_formatter);
|
||||
new_logger->set_level(_level);
|
||||
_loggers[logger_name] = new_logger;
|
||||
return new_logger;
|
||||
}
|
||||
|
||||
|
||||
std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks)
|
||||
{
|
||||
return create(logger_name, sinks.begin(), sinks.end());
|
||||
}
|
||||
|
||||
std::shared_ptr<logger> create(const std::string& logger_name, sink_ptr sink)
|
||||
{
|
||||
return create(logger_name, { sink });
|
||||
}
|
||||
|
||||
|
||||
template<class It>
|
||||
std::shared_ptr<logger> create (const std::string& logger_name, const It& sinks_begin, const It& sinks_end)
|
||||
{
|
||||
std::lock_guard<std::mutex> l(_mutex);
|
||||
auto new_logger = std::make_shared<logger>(logger_name, sinks_begin, sinks_end);
|
||||
new_logger->set_formatter(_formatter);
|
||||
_loggers[logger_name] = new_logger;
|
||||
return new_logger;
|
||||
|
||||
}
|
||||
|
||||
void formatter(formatter_ptr f)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
_formatter = f;
|
||||
for (auto& l : _loggers)
|
||||
l.second->set_formatter(_formatter);
|
||||
}
|
||||
|
||||
formatter_ptr formatter()
|
||||
|
||||
void set_pattern(const std::string& pattern)
|
||||
{
|
||||
return _formatter;
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
_formatter = std::make_shared<pattern_formatter>(pattern);
|
||||
for (auto& l : _loggers)
|
||||
l.second->set_formatter(_formatter);
|
||||
|
||||
}
|
||||
|
||||
void set_format(const std::string& format_string)
|
||||
void set_level(level::level_enum log_level)
|
||||
{
|
||||
_formatter = std::make_shared<pattern_formatter>(format_string);
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
for (auto& l : _loggers)
|
||||
l.second->set_level(log_level);
|
||||
|
||||
}
|
||||
|
||||
void stop_all()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
_level = level::OFF;
|
||||
for (auto& l : _loggers)
|
||||
l.second->stop_logging();
|
||||
}
|
||||
|
||||
|
||||
@ -74,7 +93,7 @@ private:
|
||||
std::mutex _mutex;
|
||||
std::unordered_map <std::string, std::shared_ptr<logger>> _loggers;
|
||||
formatter_ptr _formatter;
|
||||
|
||||
level::level_enum _level = level::INFO;
|
||||
};
|
||||
}
|
||||
}
|
51
include/spdlog/details/spdlog_impl.h
Normal file
51
include/spdlog/details/spdlog_impl.h
Normal file
@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
//
|
||||
// Global registry functions
|
||||
//
|
||||
#include "registry.h"
|
||||
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::get(const std::string& name)
|
||||
{
|
||||
return details::registry::instance().get(name);
|
||||
}
|
||||
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, spdlog::sinks_init_list sinks)
|
||||
{
|
||||
return details::registry::instance().create(logger_name, sinks);
|
||||
}
|
||||
|
||||
|
||||
template <typename Sink, typename... Args>
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, const Args&... args)
|
||||
{
|
||||
sink_ptr sink = std::make_shared<Sink>(args...);
|
||||
return details::registry::instance().create(logger_name, { sink });
|
||||
}
|
||||
|
||||
|
||||
template<class It>
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end)
|
||||
{
|
||||
return details::registry::instance().create(logger_name, sinks_begin, sinks_end);
|
||||
}
|
||||
|
||||
inline void spdlog::set_formatter(spdlog::formatter_ptr f)
|
||||
{
|
||||
details::registry::instance().formatter(f);
|
||||
}
|
||||
|
||||
inline void spdlog::set_pattern(const std::string& format_string)
|
||||
{
|
||||
return details::registry::instance().set_pattern(format_string);
|
||||
}
|
||||
|
||||
inline void spdlog::set_level(level::level_enum log_level)
|
||||
{
|
||||
return details::registry::instance().set_level(log_level);
|
||||
}
|
||||
|
||||
inline void spdlog::stop()
|
||||
{
|
||||
return details::registry::instance().stop_all();
|
||||
}
|
@ -7,7 +7,7 @@
|
||||
|
||||
|
||||
// Fast memory storage on the stack when possible or in std::vector
|
||||
namespace spitlog
|
||||
namespace spdlog
|
||||
{
|
||||
namespace details
|
||||
{
|
||||
@ -108,4 +108,4 @@ private:
|
||||
};
|
||||
|
||||
}
|
||||
} //namespace spitlog { namespace details {
|
||||
} //namespace spdlog { namespace details {
|
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "details/log_msg.h"
|
||||
namespace spitlog
|
||||
namespace spdlog
|
||||
{
|
||||
namespace details {
|
||||
class flag_formatter;
|
||||
@ -29,5 +29,5 @@ private:
|
||||
};
|
||||
}
|
||||
|
||||
#include "./details/pattern_formatter_impl.h"
|
||||
#include "details/pattern_formatter_impl.h"
|
||||
|
@ -15,7 +15,7 @@
|
||||
#include "sinks/base_sink.h"
|
||||
#include "common.h"
|
||||
|
||||
namespace spitlog
|
||||
namespace spdlog
|
||||
{
|
||||
|
||||
namespace details
|
||||
@ -31,7 +31,7 @@ public:
|
||||
template<class It>
|
||||
logger(const std::string& name, const It& begin, const It& end);
|
||||
|
||||
void set_format(const std::string&);
|
||||
void set_pattern(const std::string&);
|
||||
void set_formatter(formatter_ptr);
|
||||
formatter_ptr get_formatter() const;
|
||||
|
||||
@ -45,7 +45,10 @@ public:
|
||||
const std::string& name() const;
|
||||
bool should_log(level::level_enum) const;
|
||||
|
||||
void stop_logging();
|
||||
|
||||
template <typename... Args> details::line_logger log(level::level_enum lvl, const Args&... args);
|
||||
template <typename... Args> details::line_logger log(const Args&... args);
|
||||
template <typename... Args> details::line_logger trace(const Args&... args);
|
||||
template <typename... Args> details::line_logger debug(const Args&... args);
|
||||
template <typename... Args> details::line_logger info(const Args&... args);
|
||||
@ -67,35 +70,20 @@ private:
|
||||
};
|
||||
|
||||
|
||||
//
|
||||
// Registry functions for easy loggers creation and retrieval
|
||||
// example
|
||||
// auto console_logger = spitlog::create("my_logger", spitlog::sinks<stdout_sink_mt>);
|
||||
// auto same_logger = spitlog::get("my_logger");
|
||||
// auto file_logger = c11
|
||||
//
|
||||
std::shared_ptr<logger> get(const std::string& name);
|
||||
std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks);
|
||||
template <typename Sink, typename... Args>
|
||||
std::shared_ptr<spitlog::logger> create(const std::string& logger_name, const Args&... args);
|
||||
template<class It>
|
||||
std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end);
|
||||
|
||||
void set_formatter(formatter_ptr f);
|
||||
void set_format(const std::string& format_string);
|
||||
}
|
||||
|
||||
//
|
||||
// Trace & debug macros
|
||||
//
|
||||
#ifdef FFLOG_ENABLE_TRACE
|
||||
#define FFLOG_TRACE(logger, ...) logger->log(spitlog::level::TRACE, __FILE__, " #", __LINE__,": " __VA_ARGS__)
|
||||
#define FFLOG_TRACE(logger, ...) logger->log(spdlog::level::TRACE, __FILE__, " #", __LINE__,": " __VA_ARGS__)
|
||||
#else
|
||||
#define FFLOG_TRACE(logger, ...) {}
|
||||
#endif
|
||||
|
||||
#ifdef FFLOG_ENABLE_DEBUG
|
||||
#define FFLOG_DEBUG(logger, ...) logger->log(spitlog::level::DEBUG, __VA_ARGS__)
|
||||
#define FFLOG_DEBUG(logger, ...) logger->log(spdlog::level::DEBUG, __VA_ARGS__)
|
||||
#else
|
||||
#define FFLOG_DEBUG(logger, ...) {}
|
||||
#endif
|
@ -13,7 +13,7 @@
|
||||
|
||||
#include<iostream>
|
||||
|
||||
namespace spitlog
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
||||
@ -53,26 +53,26 @@ private:
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// async_sink class implementation
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
inline spitlog::sinks::async_sink::async_sink(const q_type::size_type max_queue_size)
|
||||
inline spdlog::sinks::async_sink::async_sink(const q_type::size_type max_queue_size)
|
||||
:_sinks(),
|
||||
_active(true),
|
||||
_q(max_queue_size),
|
||||
_back_thread(&async_sink::_thread_loop, this)
|
||||
{}
|
||||
|
||||
inline spitlog::sinks::async_sink::~async_sink()
|
||||
inline spdlog::sinks::async_sink::~async_sink()
|
||||
{
|
||||
_shutdown();
|
||||
}
|
||||
|
||||
inline void spitlog::sinks::async_sink::_sink_it(const details::log_msg& msg)
|
||||
inline void spdlog::sinks::async_sink::_sink_it(const details::log_msg& msg)
|
||||
{
|
||||
if(!_active)
|
||||
return;
|
||||
_q.push(msg);
|
||||
}
|
||||
|
||||
inline void spitlog::sinks::async_sink::_thread_loop()
|
||||
inline void spdlog::sinks::async_sink::_thread_loop()
|
||||
{
|
||||
static std::chrono::seconds pop_timeout { 1 };
|
||||
while (_active)
|
||||
@ -90,27 +90,27 @@ inline void spitlog::sinks::async_sink::_thread_loop()
|
||||
}
|
||||
}
|
||||
|
||||
inline void spitlog::sinks::async_sink::add_sink(spitlog::sink_ptr s)
|
||||
inline void spdlog::sinks::async_sink::add_sink(spdlog::sink_ptr s)
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(_mutex);
|
||||
_sinks.push_back(s);
|
||||
}
|
||||
|
||||
|
||||
inline void spitlog::sinks::async_sink::remove_sink(spitlog::sink_ptr s)
|
||||
inline void spdlog::sinks::async_sink::remove_sink(spdlog::sink_ptr s)
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(_mutex);
|
||||
_sinks.erase(std::remove(_sinks.begin(), _sinks.end(), s), _sinks.end());
|
||||
}
|
||||
|
||||
|
||||
inline spitlog::sinks::async_sink::q_type& spitlog::sinks::async_sink::q()
|
||||
inline spdlog::sinks::async_sink::q_type& spdlog::sinks::async_sink::q()
|
||||
{
|
||||
return _q;
|
||||
}
|
||||
|
||||
|
||||
inline void spitlog::sinks::async_sink::shutdown(const std::chrono::milliseconds& timeout)
|
||||
inline void spdlog::sinks::async_sink::shutdown(const std::chrono::milliseconds& timeout)
|
||||
{
|
||||
if(timeout > std::chrono::milliseconds::zero())
|
||||
{
|
||||
@ -124,7 +124,7 @@ inline void spitlog::sinks::async_sink::shutdown(const std::chrono::milliseconds
|
||||
}
|
||||
|
||||
|
||||
inline void spitlog::sinks::async_sink::_shutdown()
|
||||
inline void spdlog::sinks::async_sink::_shutdown()
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(_mutex);
|
||||
if(_active)
|
@ -14,7 +14,7 @@
|
||||
#include "../details/log_msg.h"
|
||||
|
||||
|
||||
namespace spitlog
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
@ -10,7 +10,7 @@
|
||||
|
||||
|
||||
|
||||
namespace spitlog
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
||||
@ -159,7 +159,7 @@ private:
|
||||
using namespace std::chrono;
|
||||
auto now = system_clock::now();
|
||||
time_t tnow = std::chrono::system_clock::to_time_t(now);
|
||||
tm date = spitlog::details::os::localtime(tnow);
|
||||
tm date = spdlog::details::os::localtime(tnow);
|
||||
date.tm_hour = date.tm_min = date.tm_sec = 0;
|
||||
auto midnight = std::chrono::system_clock::from_time_t(std::mktime(&date));
|
||||
return system_clock::time_point(midnight + hours(24));
|
||||
@ -168,8 +168,8 @@ private:
|
||||
//Create filename for the form basename.YYYY-MM-DD.extension
|
||||
static std::string calc_filename(const std::string& basename, const std::string& extension)
|
||||
{
|
||||
std::tm tm = spitlog::details::os::localtime();
|
||||
fast_oss oss;
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
details::fast_oss oss;
|
||||
oss << basename << '.';
|
||||
oss << tm.tm_year + 1900 << '-' << std::setw(2) << std::setfill('0') << tm.tm_mon + 1 << '-' << tm.tm_mday;
|
||||
oss << '.' << extension;
|
@ -4,7 +4,7 @@
|
||||
#include "../details/null_mutex.h"
|
||||
|
||||
|
||||
namespace spitlog {
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
|
||||
template <class Mutex>
|
@ -7,7 +7,7 @@
|
||||
#include "../details/null_mutex.h"
|
||||
#include "./base_sink.h"
|
||||
|
||||
namespace spitlog
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
@ -2,7 +2,7 @@
|
||||
|
||||
#include "../details/log_msg.h"
|
||||
|
||||
namespace spitlog
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
@ -5,7 +5,7 @@
|
||||
#include "./ostream_sink.h"
|
||||
#include "../details/null_mutex.h"
|
||||
|
||||
namespace spitlog
|
||||
namespace spdlog
|
||||
{
|
||||
namespace sinks
|
||||
{
|
84
include/spdlog/spdlog.h
Normal file
84
include/spdlog/spdlog.h
Normal file
@ -0,0 +1,84 @@
|
||||
//
|
||||
// This is spdlog - an extremely fast and easy to use c++11 logging library
|
||||
//
|
||||
|
||||
// example code (create multi threaded daily logger):
|
||||
// auto my_logger = spdlog::create<daily_file_sink_st>("mylog", "dailylog_filename", "txt");
|
||||
// ..
|
||||
// auto mylog = spdlog::get("mylog");
|
||||
// mylog->info("Hello logger.", "This is message number", 1, "!!") ;
|
||||
// mylog->info() << "std streams are also supprted: " << std::hex << 255;
|
||||
|
||||
// see example.cpp for more examples
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "logger.h"
|
||||
#include "details/registry.h"
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
|
||||
// Return an existing logger or nullptr if a logger with such name doesn't exist.
|
||||
// Examples:
|
||||
//
|
||||
// spdlog::get("mylog")->info("Hello");
|
||||
// auto logger = spdlog::get("mylog");
|
||||
// logger.info("This is another message" , x, y, z);
|
||||
// logger.info() << "This is another message" << x << y << z;
|
||||
std::shared_ptr<logger> get(const std::string& name);
|
||||
|
||||
|
||||
// Example:
|
||||
// auto logger = spdlog::create("mylog", {sink1, sink2});
|
||||
std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks);
|
||||
|
||||
|
||||
// Example (create a logger with daily rotating file):
|
||||
// using namespace spdlog::sinks;
|
||||
// spdlog::create<daily_file_sink_st>("mylog", "dailylog_filename", "txt");
|
||||
template <typename Sink, typename... Args>
|
||||
std::shared_ptr<spdlog::logger> create(const std::string& logger_name, const Args&... args);
|
||||
|
||||
// Example:
|
||||
// using namespace spdlog::sinks;
|
||||
// std::vector<spdlog::sink_ptr> mySinks;
|
||||
// mySinks.push_back(std::make_shared<rotating_file_sink_mt>("filename", "txt", 1024 * 1024 * 5, 10));
|
||||
// mySinks.push_back(std::make_shared<stdout_sink_mt>());
|
||||
// spdlog::create("mylog", mySinks.begin(), mySinks.end());
|
||||
template<class It>
|
||||
std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end);
|
||||
|
||||
|
||||
// Set global formatting
|
||||
// Example:
|
||||
// spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %t");
|
||||
void set_pattern(const std::string& format_string);
|
||||
|
||||
|
||||
// Set global formatter object
|
||||
void set_formatter(formatter_ptr f);
|
||||
|
||||
|
||||
//Set global active logging level
|
||||
void set_level(level::level_enum log_level);
|
||||
|
||||
|
||||
//Stop all loggers
|
||||
void stop();
|
||||
|
||||
|
||||
//
|
||||
// Trace macro to turn on/off at compile time
|
||||
// Example: SPDLOG_TRACE(my_logger, "Some trace message");
|
||||
//
|
||||
#ifdef _DEBUG
|
||||
#define SPDLOG_TRACE(logger, ...) logger->log(__FILE__, " #", __LINE__,": " __VA_ARGS__)
|
||||
#else
|
||||
#define SPDLOG_TRACE(logger, ...) {}
|
||||
#endif
|
||||
|
||||
|
||||
}
|
||||
|
||||
#include "details/spdlog_impl.h"
|
@ -1,163 +0,0 @@
|
||||
#pragma once
|
||||
//
|
||||
// Logger implementation
|
||||
//
|
||||
|
||||
|
||||
#include "./line_logger.h"
|
||||
|
||||
|
||||
inline spitlog::logger::logger(const std::string& logger_name, sinks_init_list sinks_list) :
|
||||
_name(logger_name),
|
||||
_sinks(sinks_list)
|
||||
{
|
||||
// no support under vs2013 for member initialization for std::atomic
|
||||
_level = level::INFO;
|
||||
}
|
||||
|
||||
template<class It>
|
||||
inline spitlog::logger::logger(const std::string& logger_name, const It& begin, const It& end) :
|
||||
_name(logger_name),
|
||||
_sinks(begin, end)
|
||||
{}
|
||||
|
||||
|
||||
inline void spitlog::logger::set_formatter(spitlog::formatter_ptr msg_formatter)
|
||||
{
|
||||
_formatter = msg_formatter;
|
||||
}
|
||||
|
||||
inline void spitlog::logger::set_format(const std::string& format)
|
||||
{
|
||||
_formatter = std::make_shared<pattern_formatter>(format);
|
||||
}
|
||||
|
||||
inline spitlog::formatter_ptr spitlog::logger::get_formatter() const
|
||||
{
|
||||
return _formatter;
|
||||
}
|
||||
|
||||
|
||||
template <typename... Args>
|
||||
inline spitlog::details::line_logger spitlog::logger::log(level::level_enum lvl, const Args&... args) {
|
||||
bool msg_enabled = should_log(lvl);
|
||||
details::line_logger l(this, lvl, msg_enabled);
|
||||
if (msg_enabled)
|
||||
_variadic_log(l, args...);
|
||||
return l;
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline spitlog::details::line_logger spitlog::logger::trace(const Args&... args)
|
||||
{
|
||||
return log(level::TRACE, args...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline spitlog::details::line_logger spitlog::logger::debug(const Args&... args)
|
||||
{
|
||||
return log(level::DEBUG, args...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline spitlog::details::line_logger spitlog::logger::info(const Args&... args)
|
||||
{
|
||||
return log(level::INFO, args...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline spitlog::details::line_logger spitlog::logger::warn(const Args&... args)
|
||||
{
|
||||
return log(level::WARN, args...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline spitlog::details::line_logger spitlog::logger::error(const Args&... args)
|
||||
{
|
||||
return log(level::ERR, args...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline spitlog::details::line_logger spitlog::logger::critical(const Args&... args)
|
||||
{
|
||||
return log(level::CRITICAL, args...);
|
||||
}
|
||||
|
||||
inline const std::string& spitlog::logger::name() const
|
||||
{
|
||||
return _name;
|
||||
}
|
||||
|
||||
inline void spitlog::logger::set_level(spitlog::level::level_enum log_level)
|
||||
{
|
||||
_level.store(log_level);
|
||||
}
|
||||
|
||||
inline spitlog::level::level_enum spitlog::logger::level() const
|
||||
{
|
||||
return static_cast<spitlog::level::level_enum>(_level.load());
|
||||
}
|
||||
|
||||
inline bool spitlog::logger::should_log(spitlog::level::level_enum msg_level) const
|
||||
{
|
||||
return msg_level >= _level.load();
|
||||
}
|
||||
|
||||
inline void spitlog::logger::_variadic_log(spitlog::details::line_logger&) {}
|
||||
|
||||
template <typename First, typename... Rest>
|
||||
void spitlog::logger::_variadic_log(spitlog::details::line_logger& l, const First& first, const Rest&... rest)
|
||||
{
|
||||
l.write(first);
|
||||
l.write(' ');
|
||||
_variadic_log(l, rest...);
|
||||
}
|
||||
|
||||
inline void spitlog::logger::_log_msg(details::log_msg& msg)
|
||||
{
|
||||
//Use default formatter if not set
|
||||
if (!_formatter)
|
||||
_formatter = std::make_shared<pattern_formatter>("%+");
|
||||
_formatter->format(msg);
|
||||
for (auto &sink : _sinks)
|
||||
sink->log(msg);
|
||||
}
|
||||
|
||||
//
|
||||
// Global registry functions
|
||||
//
|
||||
#include "./registry.h"
|
||||
inline std::shared_ptr<spitlog::logger> spitlog::get(const std::string& name)
|
||||
{
|
||||
return details::registry::instance().get(name);
|
||||
}
|
||||
|
||||
inline std::shared_ptr<spitlog::logger> spitlog::create(const std::string& logger_name, spitlog::sinks_init_list sinks)
|
||||
{
|
||||
return details::registry::instance().create(logger_name, sinks);
|
||||
}
|
||||
|
||||
|
||||
template <typename Sink, typename... Args>
|
||||
inline std::shared_ptr<spitlog::logger> spitlog::create(const std::string& logger_name, const Args&... args)
|
||||
{
|
||||
sink_ptr sink = std::make_shared<Sink>(args...);
|
||||
return details::registry::instance().create(logger_name, { sink });
|
||||
}
|
||||
|
||||
|
||||
template<class It>
|
||||
inline std::shared_ptr<spitlog::logger> spitlog::create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end)
|
||||
{
|
||||
return details::registry::instance().create(logger_name, std::forward(sinks_begin), std::forward(sinks_end));
|
||||
}
|
||||
|
||||
inline void spitlog::set_formatter(spitlog::formatter_ptr f)
|
||||
{
|
||||
details::registry::instance().formatter(f);
|
||||
}
|
||||
|
||||
inline void spitlog::set_format(const std::string& format_string)
|
||||
{
|
||||
return details::registry::instance().set_format(format_string);
|
||||
}
|
Loading…
Reference in New Issue
Block a user