1
0
mirror of https://github.com/gabime/spdlog.git synced 2025-05-01 04:33:54 +00:00
This commit is contained in:
gabime 2016-04-03 02:14:54 +03:00
parent 9230d9e63d
commit 495ecaeaee
48 changed files with 10700 additions and 10556 deletions

View File

@ -11,21 +11,21 @@ int g_fd;
static void output_callback(zf_log_message *msg) static void output_callback(zf_log_message *msg)
{ {
*msg->p = '\n'; *msg->p = '\n';
write(g_fd, msg->buf, msg->p - msg->buf + 1); write(g_fd, msg->buf, msg->p - msg->buf + 1);
} }
using namespace std; using namespace std;
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
g_fd = open(g_path, O_APPEND|O_CREAT|O_WRONLY); g_fd = open(g_path, O_APPEND|O_CREAT|O_WRONLY);
if (0 > g_fd) if (0 > g_fd)
{ {
ZF_LOGE_AUX(ZF_LOG_STDERR, "Failed to open log file: %s", g_path); ZF_LOGE_AUX(ZF_LOG_STDERR, "Failed to open log file: %s", g_path);
return -1; return -1;
} }
zf_log_set_output_callback(ZF_LOG_PUT_STD, output_callback); zf_log_set_output_callback(ZF_LOG_PUT_STD, output_callback);
int thread_count = 10; int thread_count = 10;
if(argc > 1) if(argc > 1)
@ -42,7 +42,7 @@ int main(int argc, char* argv[])
{ {
int counter = ++msg_counter; int counter = ++msg_counter;
if (counter > howmany) break; if (counter > howmany) break;
ZF_LOGI("zf_log message #%i: This is some text for your pleasure", counter); ZF_LOGI("zf_log message #%i: This is some text for your pleasure", counter);
} }
})); }));
} }
@ -51,6 +51,6 @@ int main(int argc, char* argv[])
{ {
t.join(); t.join();
}; };
close(g_fd); close(g_fd);
return 0; return 0;
} }

View File

@ -6,22 +6,23 @@ static FILE *g_f;
static void output_callback(zf_log_message *msg) static void output_callback(zf_log_message *msg)
{ {
*msg->p = '\n'; *msg->p = '\n';
fwrite(msg->buf, msg->p - msg->buf + 1, 1, g_f); fwrite(msg->buf, msg->p - msg->buf + 1, 1, g_f);
} }
int main(int, char* []) int main(int, char* [])
{ {
g_f = fopen(g_path, "wb"); g_f = fopen(g_path, "wb");
if (!g_f) { if (!g_f)
ZF_LOGE_AUX(ZF_LOG_STDERR, "Failed to open log file: %s", g_path); {
return -1; ZF_LOGE_AUX(ZF_LOG_STDERR, "Failed to open log file: %s", g_path);
} return -1;
zf_log_set_output_callback(ZF_LOG_PUT_STD, output_callback); }
zf_log_set_output_callback(ZF_LOG_PUT_STD, output_callback);
const int howmany = 1000000; const int howmany = 1000000;
for(int i = 0 ; i < howmany; ++i) for(int i = 0 ; i < howmany; ++i)
ZF_LOGI("zf_log message #%i: This is some text for your pleasure", i); ZF_LOGI("zf_log message #%i: This is some text for your pleasure", i);
fclose(g_f); fclose(g_f);
return 0; return 0;
} }

View File

@ -35,7 +35,7 @@ int main(int, char*[])
console->info("{:>30}", "right aligned"); console->info("{:>30}", "right aligned");
console->info("{:^30}", "centered"); console->info("{:^30}", "centered");
spd::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name) function"); spd::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name) function");
// Runtime log levels // Runtime log levels
spd::set_level(spd::level::info); //Set global log level to info spd::set_level(spd::level::info); //Set global log level to info
@ -63,10 +63,10 @@ int main(int, char*[])
// Asynchronous logging is very fast.. // Asynchronous logging is very fast..
// Just call spdlog::set_async_mode(q_size) and all created loggers from now on will be asynchronous.. // Just call spdlog::set_async_mode(q_size) and all created loggers from now on will be asynchronous..
async_example(); async_example();
// syslog example. linux/osx only.. // syslog example. linux/osx only..
syslog_example(); syslog_example();
// Release and close all loggers // Release and close all loggers
@ -84,20 +84,20 @@ int main(int, char*[])
void async_example() void async_example()
{ {
size_t q_size = 4096; //queue size must be power of 2 size_t q_size = 4096; //queue size must be power of 2
spdlog::set_async_mode(q_size); spdlog::set_async_mode(q_size);
auto async_file = spd::daily_logger_st("async_file_logger", "logs/async_log.txt"); auto async_file = spd::daily_logger_st("async_file_logger", "logs/async_log.txt");
for (int i = 0; i < 100; ++i) for (int i = 0; i < 100; ++i)
async_file->info("Async message #{}", i); async_file->info("Async message #{}", i);
} }
//syslog example (linux/osx only) //syslog example (linux/osx only)
void syslog_example() void syslog_example()
{ {
#if defined (__linux__) || defined(__APPLE__) #if defined (__linux__) || defined(__APPLE__)
std::string ident = "spdlog-example"; std::string ident = "spdlog-example";
auto syslog_logger = spd::syslog_logger("syslog", ident, LOG_PID); auto syslog_logger = spd::syslog_logger("syslog", ident, LOG_PID);
syslog_logger->warn("This is warning that will end up in syslog. This is Linux only!"); syslog_logger->warn("This is warning that will end up in syslog. This is Linux only!");
#endif #endif
} }

File diff suppressed because it is too large Load Diff

View File

@ -33,9 +33,9 @@ public:
void register_logger(std::shared_ptr<logger> logger) void register_logger(std::shared_ptr<logger> logger)
{ {
std::lock_guard<Mutex> lock(_mutex); std::lock_guard<Mutex> lock(_mutex);
auto logger_name = logger->name(); auto logger_name = logger->name();
throw_if_exists(logger_name); throw_if_exists(logger_name);
_loggers[logger_name] = logger; _loggers[logger_name] = logger;
} }
@ -50,8 +50,8 @@ public:
std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end) std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end)
{ {
std::lock_guard<Mutex> lock(_mutex); std::lock_guard<Mutex> lock(_mutex);
throw_if_exists(logger_name); throw_if_exists(logger_name);
std::shared_ptr<logger> new_logger; std::shared_ptr<logger> new_logger;
if (_async_mode) if (_async_mode)
new_logger = std::make_shared<async_logger>(logger_name, sinks_begin, sinks_end, _async_q_size, _overflow_policy, _worker_warmup_cb, _flush_interval_ms); new_logger = std::make_shared<async_logger>(logger_name, sinks_begin, sinks_end, _async_q_size, _overflow_policy, _worker_warmup_cb, _flush_interval_ms);
else else
@ -61,8 +61,8 @@ public:
new_logger->set_formatter(_formatter); new_logger->set_formatter(_formatter);
new_logger->set_level(_level); new_logger->set_level(_level);
//Add to registry //Add to registry
_loggers[logger_name] = new_logger; _loggers[logger_name] = new_logger;
return new_logger; return new_logger;
} }
@ -139,11 +139,11 @@ private:
registry_t<Mutex>(const registry_t<Mutex>&) = delete; registry_t<Mutex>(const registry_t<Mutex>&) = delete;
registry_t<Mutex>& operator=(const registry_t<Mutex>&) = delete; registry_t<Mutex>& operator=(const registry_t<Mutex>&) = delete;
void throw_if_exists(const std::string &logger_name) void throw_if_exists(const std::string &logger_name)
{ {
if (_loggers.find(logger_name) != _loggers.end()) if (_loggers.find(logger_name) != _loggers.end())
throw spdlog_ex("logger with name '" + logger_name + "' already exists"); throw spdlog_ex("logger with name '" + logger_name + "' already exists");
} }
Mutex _mutex; Mutex _mutex;
std::unordered_map <std::string, std::shared_ptr<logger>> _loggers; std::unordered_map <std::string, std::shared_ptr<logger>> _loggers;
formatter_ptr _formatter; formatter_ptr _formatter;

View File

@ -59,29 +59,29 @@ inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string
// Create stdout/stderr loggers (with optinal color support) // Create stdout/stderr loggers (with optinal color support)
inline std::shared_ptr<spdlog::logger> create_console_logger(const std::string& logger_name, spdlog::sink_ptr sink, bool color) inline std::shared_ptr<spdlog::logger> create_console_logger(const std::string& logger_name, spdlog::sink_ptr sink, bool color)
{ {
if (color) //use color wrapper sink if (color) //use color wrapper sink
sink = std::make_shared<spdlog::sinks::ansicolor_sink>(sink); sink = std::make_shared<spdlog::sinks::ansicolor_sink>(sink);
return spdlog::details::registry::instance().create(logger_name, sink); return spdlog::details::registry::instance().create(logger_name, sink);
} }
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt(const std::string& logger_name, bool color) inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt(const std::string& logger_name, bool color)
{ {
return create_console_logger(logger_name, sinks::stdout_sink_mt::instance(), color); return create_console_logger(logger_name, sinks::stdout_sink_mt::instance(), color);
} }
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st(const std::string& logger_name, bool color) inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st(const std::string& logger_name, bool color)
{ {
return create_console_logger(logger_name, sinks::stdout_sink_st::instance(), color); return create_console_logger(logger_name, sinks::stdout_sink_st::instance(), color);
} }
inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt(const std::string& logger_name, bool color) inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt(const std::string& logger_name, bool color)
{ {
return create_console_logger(logger_name, sinks::stderr_sink_mt::instance(), color); return create_console_logger(logger_name, sinks::stderr_sink_mt::instance(), color);
} }
inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st(const std::string& logger_name, bool color) inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st(const std::string& logger_name, bool color)
{ {
return create_console_logger(logger_name, sinks::stderr_sink_st::instance(), color); return create_console_logger(logger_name, sinks::stderr_sink_st::instance(), color);
} }
#if defined(__linux__) || defined(__APPLE__) #if defined(__linux__) || defined(__APPLE__)

View File

@ -11,15 +11,18 @@
#include <string> #include <string>
#include <map> #include <map>
namespace spdlog { namespace spdlog
namespace sinks { {
namespace sinks
{
/** /**
* @brief The ansi_color_sink is a decorator around another sink and prefixes * @brief The ansi_color_sink is a decorator around another sink and prefixes
* the output with an ANSI escape sequence color code depending on the severity * the output with an ANSI escape sequence color code depending on the severity
* of the message. * of the message.
*/ */
class ansicolor_sink : public sink { class ansicolor_sink : public sink
{
public: public:
ansicolor_sink(sink_ptr wrapped_sink); ansicolor_sink(sink_ptr wrapped_sink);
virtual ~ansicolor_sink(); virtual ~ansicolor_sink();
@ -104,7 +107,7 @@ inline void ansicolor_sink::set_color(level::level_enum level, const std::string
inline ansicolor_sink::~ansicolor_sink() inline ansicolor_sink::~ansicolor_sink()
{ {
flush(); flush();
} }
} // namespace sinks } // namespace sinks

View File

@ -26,7 +26,7 @@ template<class Mutex>
class msvc_sink : public base_sink < Mutex > class msvc_sink : public base_sink < Mutex >
{ {
public: public:
explicit msvc_sink() explicit msvc_sink()
{ {
} }
@ -37,7 +37,7 @@ public:
protected: protected:
void _sink_it(const details::log_msg& msg) override void _sink_it(const details::log_msg& msg) override
{ {
OutputDebugStringA(msg.formatted.c_str()); OutputDebugStringA(msg.formatted.c_str());
} }
}; };