Commit e4d3eb64 authored by gabime's avatar gabime

Udpated example and spdlog.h

parent 0969118c
...@@ -11,19 +11,13 @@ ...@@ -11,19 +11,13 @@
#define SPDLOG_DEBUG_ON #define SPDLOG_DEBUG_ON
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/file_sinks.h"
#include "spdlog/color_logger.h"
//#include "spdlog/stdout_logger.h"
//#include "spdlog/async.h"
#include "spdlog/spdlog.h"
#include <iostream> #include <iostream>
#include <memory> #include <memory>
void async_example(); void async_example();
void syslog_example();
void android_example();
void user_defined_example(); void user_defined_example();
void err_handler_example(); void err_handler_example();
...@@ -31,23 +25,8 @@ namespace spd = spdlog; ...@@ -31,23 +25,8 @@ namespace spd = spdlog;
int main(int, char *[]) int main(int, char *[])
{ {
try try
{ {
// Console logger with color
//spd::init_thread_pool(8192, 3);
auto console1 = spdlog::stdout_color_mt("console111");
auto console2 = spdlog::stdout_color_mt<>("console2");
//auto console3 = spdlog::stdout_color_mt<spdlog::create_async>("console3");
for (int i = 0; i < 10000; i++)
{
console1->info(111111);
console2->warn(222222);
}
spdlog::drop_all();
return 0;
auto console = spdlog::stdout_color_st("console"); auto console = spdlog::stdout_color_st("console");
console->info("Welcome to spdlog!"); console->info("Welcome to spdlog!");
...@@ -101,11 +80,6 @@ int main(int, char *[]) ...@@ -101,11 +80,6 @@ int main(int, char *[])
// 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();
// android example. compile with NDK
android_example();
// Log user-defined types example // Log user-defined types example
user_defined_example(); user_defined_example();
...@@ -130,38 +104,40 @@ int main(int, char *[]) ...@@ -130,38 +104,40 @@ int main(int, char *[])
#include "spdlog/async.h" #include "spdlog/async.h"
void async_example() void async_example()
{ {
auto async_file = spd::basic_logger_mt<spdlog::create_async>("async_file_logger", "logs/async_log.txt"); auto async_file = spd::basic_logger_mt<spdlog::create_async>("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);
} }
// optional change thread pool settings *before* creating the logger: // you can also modify thread pool settings *before* creating the logger:
// spdlog::init_thread_pool(8192, 1); // spdlog::init_thread_pool(32768, 4); // queue with 32k of pre allocated items and 4 backing threads.
// if not called a defaults are: 8192 queue size and 1 worker thread. // if not called a defaults are: preallocated 8192 queue items and 1 worker thread.
} }
// syslog example (linux/osx/freebsd) // syslog example (linux/osx/freebsd)
#ifndef _WIN32
#incude "spdlog/sinks/syslog_sink.h"
void syslog_example() void syslog_example()
{ {
#ifdef SPDLOG_ENABLE_SYSLOG
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."); syslog_logger->warn("This is warning that will end up in syslog.");
#endif
} }
#endif
// Android example // Android example
#if defined(__ANDROID__)
#incude "spdlog/sinks/android_sink.h"
void android_example() void android_example()
{ {
#if defined(__ANDROID__)
std::string tag = "spdlog-android"; std::string tag = "spdlog-android";
auto android_logger = spd::android_logger("android", tag); auto android_logger = spd::android_logger("android", tag);
android_logger->critical("Use \"adb shell logcat\" to view this message."); android_logger->critical("Use \"adb shell logcat\" to view this message.");
#endif
} }
#endif
// user defined types logging by implementing operator<< // user defined types logging by implementing operator<<
struct my_type struct my_type
{ {
......
...@@ -58,7 +58,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sinks", "sinks", "{27D16BB9 ...@@ -58,7 +58,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sinks", "sinks", "{27D16BB9
..\include\spdlog\sinks\android_sink.h = ..\include\spdlog\sinks\android_sink.h ..\include\spdlog\sinks\android_sink.h = ..\include\spdlog\sinks\android_sink.h
..\include\spdlog\sinks\ansicolor_sink.h = ..\include\spdlog\sinks\ansicolor_sink.h ..\include\spdlog\sinks\ansicolor_sink.h = ..\include\spdlog\sinks\ansicolor_sink.h
..\include\spdlog\sinks\base_sink.h = ..\include\spdlog\sinks\base_sink.h ..\include\spdlog\sinks\base_sink.h = ..\include\spdlog\sinks\base_sink.h
..\include\spdlog\color_logger.h = ..\include\spdlog\color_logger.h ..\include\spdlog\sinks\stdout_color_sinks.h = ..\include\spdlog\sinks\stdout_color_sinks.h
..\include\spdlog\sinks\dist_sink.h = ..\include\spdlog\sinks\dist_sink.h ..\include\spdlog\sinks\dist_sink.h = ..\include\spdlog\sinks\dist_sink.h
..\include\spdlog\sinks\file_sinks.h = ..\include\spdlog\sinks\file_sinks.h ..\include\spdlog\sinks\file_sinks.h = ..\include\spdlog\sinks\file_sinks.h
..\include\spdlog\sinks\msvc_sink.h = ..\include\spdlog\sinks\msvc_sink.h ..\include\spdlog\sinks\msvc_sink.h = ..\include\spdlog\sinks\msvc_sink.h
......
...@@ -21,15 +21,15 @@ ...@@ -21,15 +21,15 @@
#endif #endif
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
/* /*
* Android sink (logging using __android_log_write) * Android sink (logging using __android_log_write)
* __android_log_write is thread-safe. No lock is needed. * __android_log_write is thread-safe. No lock is needed.
*/ */
class android_sink : public sink class android_sink : public sink
{ {
public: public:
explicit android_sink(const std::string &tag = "spdlog", bool use_raw_msg = false) explicit android_sink(const std::string &tag = "spdlog", bool use_raw_msg = false)
: _tag(tag) : _tag(tag)
, _use_raw_msg(use_raw_msg) , _use_raw_msg(use_raw_msg)
...@@ -59,7 +59,7 @@ public: ...@@ -59,7 +59,7 @@ public:
void flush() override {} void flush() override {}
private: private:
static android_LogPriority convert_to_android(spdlog::level::level_enum level) static android_LogPriority convert_to_android(spdlog::level::level_enum level)
{ {
switch (level) switch (level)
...@@ -83,9 +83,20 @@ private: ...@@ -83,9 +83,20 @@ private:
std::string _tag; std::string _tag;
bool _use_raw_msg; bool _use_raw_msg;
}; };
} // namespace sinks
// Create and register android syslog logger
template<typename Factory = default_factory>
inline std::shared_ptr<logger> android_logger(const std::string &logger_name, const std::string &tag = "spdlog")
{
return return Factory::template create<sinks::android_sink>(logger_name, tag);
}
} // namespace sinks
} // namespace spdlog } // namespace spdlog
#endif #endif
...@@ -15,17 +15,17 @@ ...@@ -15,17 +15,17 @@
#include <mutex> #include <mutex>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
/** /**
* This sink prefixes the output with an ANSI escape sequence color code depending on the severity * This sink prefixes the output with an ANSI escape sequence color code depending on the severity
* of the message. * of the message.
* If no color terminal detected, omit the escape codes. * If no color terminal detected, omit the escape codes.
*/ */
template<class StdoutTrait, class ConsoleMutexTrait> template<class StdoutTrait, class ConsoleMutexTrait>
class ansicolor_sink : public sink class ansicolor_sink : public sink
{ {
public: public:
using mutex_t = typename ConsoleMutexTrait::mutex_t; using mutex_t = typename ConsoleMutexTrait::mutex_t;
ansicolor_sink() ansicolor_sink()
: target_file_(StdoutTrait::stream()), : target_file_(StdoutTrait::stream()),
...@@ -117,7 +117,7 @@ public: ...@@ -117,7 +117,7 @@ public:
} }
private: private:
void _print_ccode(const std::string &color_code) void _print_ccode(const std::string &color_code)
{ {
...@@ -133,12 +133,13 @@ private: ...@@ -133,12 +133,13 @@ private:
bool should_do_colors_; bool should_do_colors_;
std::unordered_map<level::level_enum, std::string, level::level_hasher> colors_; std::unordered_map<level::level_enum, std::string, level::level_hasher> colors_;
}; };
using ansicolor_stdout_sink_mt = ansicolor_sink<details::console_stdout_trait, details::console_mutex_trait>;
using ansicolor_stdout_sink_st = ansicolor_sink<details::console_stdout_trait, details::console_null_mutex_trait>;
using ansicolor_stderr_sink_mt = ansicolor_sink<details::console_stderr_trait, details::console_mutex_trait>;
using ansicolor_stderr_sink_st = ansicolor_sink<details::console_stderr_trait, details::console_null_mutex_trait>;
using ansicolor_stdout_sink_mt = ansicolor_sink<details::console_stdout_trait, details::console_mutex_trait>; } // namespace sinks
using ansicolor_stdout_sink_st = ansicolor_sink<details::console_stdout_trait, details::console_null_mutex_trait>;
using ansicolor_stderr_sink_mt = ansicolor_sink<details::console_stderr_trait, details::console_mutex_trait>;
using ansicolor_stderr_sink_st = ansicolor_sink<details::console_stderr_trait, details::console_null_mutex_trait>;
} // namespace sinks
} // namespace spdlog } // namespace spdlog
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
#ifdef _WIN32
#include "wincolor_sink.h"
#else
#include "ansicolor_sink.h"
#endif
...@@ -19,14 +19,14 @@ ...@@ -19,14 +19,14 @@
#include <string> #include <string>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
/* /*
* Trivial file sink with single file as target * Trivial file sink with single file as target
*/ */
template<class Mutex> template<class Mutex>
class simple_file_sink SPDLOG_FINAL : public base_sink<Mutex> class simple_file_sink SPDLOG_FINAL : public base_sink<Mutex>
{ {
public: public:
explicit simple_file_sink(const filename_t &filename, bool truncate = false) explicit simple_file_sink(const filename_t &filename, bool truncate = false)
: _force_flush(false) : _force_flush(false)
{ {
...@@ -38,7 +38,7 @@ public: ...@@ -38,7 +38,7 @@ public:
_force_flush = force_flush; _force_flush = force_flush;
} }
protected: protected:
void _sink_it(const details::log_msg &msg) override void _sink_it(const details::log_msg &msg) override
{ {
_file_helper.write(msg); _file_helper.write(msg);
...@@ -53,21 +53,21 @@ protected: ...@@ -53,21 +53,21 @@ protected:
_file_helper.flush(); _file_helper.flush();
} }
private: private:
details::file_helper _file_helper; details::file_helper _file_helper;
bool _force_flush; bool _force_flush;
}; };
using simple_file_sink_mt = simple_file_sink<std::mutex>; using simple_file_sink_mt = simple_file_sink<std::mutex>;
using simple_file_sink_st = simple_file_sink<details::null_mutex>; using simple_file_sink_st = simple_file_sink<details::null_mutex>;
/* /*
* Rotating file sink based on size * Rotating file sink based on size
*/ */
template<class Mutex> template<class Mutex>
class rotating_file_sink SPDLOG_FINAL : public base_sink<Mutex> class rotating_file_sink SPDLOG_FINAL : public base_sink<Mutex>
{ {
public: public:
rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files) rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files)
: _base_filename(std::move(base_filename)) : _base_filename(std::move(base_filename))
, _max_size(max_size) , _max_size(max_size)
...@@ -95,7 +95,7 @@ public: ...@@ -95,7 +95,7 @@ public:
return w.str(); return w.str();
} }
protected: protected:
void _sink_it(const details::log_msg &msg) override void _sink_it(const details::log_msg &msg) override
{ {
_current_size += msg.formatted.size(); _current_size += msg.formatted.size();
...@@ -112,7 +112,7 @@ protected: ...@@ -112,7 +112,7 @@ protected:
_file_helper.flush(); _file_helper.flush();
} }
private: private:
// Rotate files: // Rotate files:
// log.txt -> log.1.txt // log.txt -> log.1.txt
// log.1.txt -> log.2.txt // log.1.txt -> log.2.txt
...@@ -147,16 +147,16 @@ private: ...@@ -147,16 +147,16 @@ private:
std::size_t _max_files; std::size_t _max_files;
std::size_t _current_size; std::size_t _current_size;
details::file_helper _file_helper; details::file_helper _file_helper;
}; };
using rotating_file_sink_mt = rotating_file_sink<std::mutex>; using rotating_file_sink_mt = rotating_file_sink<std::mutex>;
using rotating_file_sink_st = rotating_file_sink<details::null_mutex>; using rotating_file_sink_st = rotating_file_sink<details::null_mutex>;
/* /*
* Default generator of daily log file names. * Default generator of daily log file names.
*/ */
struct default_daily_file_name_calculator struct default_daily_file_name_calculator
{ {
// Create filename for the form filename.YYYY-MM-DD_hh-mm.ext // Create filename for the form filename.YYYY-MM-DD_hh-mm.ext
static filename_t calc_filename(const filename_t &filename) static filename_t calc_filename(const filename_t &filename)
{ {
...@@ -168,13 +168,13 @@ struct default_daily_file_name_calculator ...@@ -168,13 +168,13 @@ struct default_daily_file_name_calculator
tm.tm_hour, tm.tm_min, ext); tm.tm_hour, tm.tm_min, ext);
return w.str(); return w.str();
} }
}; };
/* /*
* Generator of daily log file names in format basename.YYYY-MM-DD.ext * Generator of daily log file names in format basename.YYYY-MM-DD.ext
*/ */
struct dateonly_daily_file_name_calculator struct dateonly_daily_file_name_calculator
{ {
// Create filename for the form basename.YYYY-MM-DD // Create filename for the form basename.YYYY-MM-DD
static filename_t calc_filename(const filename_t &filename) static filename_t calc_filename(const filename_t &filename)
{ {
...@@ -185,15 +185,15 @@ struct dateonly_daily_file_name_calculator ...@@ -185,15 +185,15 @@ struct dateonly_daily_file_name_calculator
w.write(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, ext); w.write(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, ext);
return w.str(); return w.str();
} }
}; };
/* /*
* Rotating file sink based on date. rotates at midnight * Rotating file sink based on date. rotates at midnight
*/ */
template<class Mutex, class FileNameCalc = default_daily_file_name_calculator> template<class Mutex, class FileNameCalc = default_daily_file_name_calculator>
class daily_file_sink SPDLOG_FINAL : public base_sink<Mutex> class daily_file_sink SPDLOG_FINAL : public base_sink<Mutex>
{ {
public: public:
// create daily file sink which rotates on given time // create daily file sink which rotates on given time
daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute) daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute)
: _base_filename(std::move(base_filename)) : _base_filename(std::move(base_filename))
...@@ -208,7 +208,7 @@ public: ...@@ -208,7 +208,7 @@ public:
_file_helper.open(FileNameCalc::calc_filename(_base_filename)); _file_helper.open(FileNameCalc::calc_filename(_base_filename));
} }
protected: protected:
void _sink_it(const details::log_msg &msg) override void _sink_it(const details::log_msg &msg) override
{ {
if (std::chrono::system_clock::now() >= _rotation_tp) if (std::chrono::system_clock::now() >= _rotation_tp)
...@@ -224,7 +224,7 @@ protected: ...@@ -224,7 +224,7 @@ protected:
_file_helper.flush(); _file_helper.flush();
} }
private: private:
std::chrono::system_clock::time_point _next_rotation_tp() std::chrono::system_clock::time_point _next_rotation_tp()
{ {
auto now = std::chrono::system_clock::now(); auto now = std::chrono::system_clock::now();
...@@ -238,7 +238,7 @@ private: ...@@ -238,7 +238,7 @@ private:
{ {
return rotation_time; return rotation_time;
} }
return {rotation_time + std::chrono::hours(24)}; return { rotation_time + std::chrono::hours(24) };
} }
filename_t _base_filename; filename_t _base_filename;
...@@ -246,10 +246,59 @@ private: ...@@ -246,10 +246,59 @@ private:
int _rotation_m; int _rotation_m;
std::chrono::system_clock::time_point _rotation_tp; std::chrono::system_clock::time_point _rotation_tp;
details::file_helper _file_helper; details::file_helper _file_helper;
}; };
using daily_file_sink_mt = daily_file_sink<std::mutex>;
using daily_file_sink_st = daily_file_sink<details::null_mutex>;
} // namespace sinks
//
// factory functions to create and register file loggers
//
// Basic logger simply writes to given file without any limitations or rotations.
template<typename Factory = default_factory>
inline std::shared_ptr<logger> basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false)
{
return Factory::template create<sinks::simple_file_sink_mt>(logger_name, filename, truncate);
}
template<typename Factory = default_factory>
inline std::shared_ptr<logger> basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false)
{
return Factory::template create<sinks::simple_file_sink_st>(logger_name, filename, truncate);
}
//
// Create and register multi/single threaded rotating file logger
//
template<typename Factory = default_factory>
inline 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)
{
return Factory::template create<sinks::rotating_file_sink_mt>(logger_name, filename, max_file_size, max_files);
}
template<typename Factory = default_factory>
inline 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)
{
return Factory::template create<sinks::rotating_file_sink_st>(logger_name, filename, max_file_size, max_files);
}
using daily_file_sink_mt = daily_file_sink<std::mutex>; //
using daily_file_sink_st = daily_file_sink<details::null_mutex>; // Create file logger which creates new file on the given time (default in midnight):
//
template<typename Factory = default_factory>
inline std::shared_ptr<logger> daily_logger_mt(const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0)
{
return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute);
}
} // namespace sinks template<typename Factory = default_factory>
inline std::shared_ptr<logger> daily_logger_st(const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0)
{
return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute);
}
} // namespace spdlog } // namespace spdlog
//
// Copyright(c) 2018 spdlog
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
#include "../spdlog.h"
#ifdef _WIN32
#include "wincolor_sink.h"
#else
#include "ansicolor_sink.h"
#endif
namespace {
using namespace spdlog::sinks;
#ifdef _WIN32
using stdout_color_sink_mt = wincolor_stdout_sink_mt;
using stdout_color_sink_st = wincolor_stdout_sink_st;
using stderr_color_sink_mt = wincolor_stderr_sink_mt;
using stderr_color_sink_st = wincolor_stderr_sink_st;
#else
using stdout_color_sink_mt = ansicolor_stdout_sink_mt;
using stdout_color_sink_st = ansicolor_stdout_sink_st;
using stderr_color_sink_mt = ansicolor_stderr_sink_mt;
using stderr_color_sink_st = ansicolor_stderr_sink_st;
#endif
}
namespace spdlog
{
template<typename Factory = default_factory>
inline std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name)
{
return Factory::template create<stdout_color_sink_mt>(logger_name);
}
template<typename Factory = default_factory>
inline std::shared_ptr<logger> stdout_color_st(const std::string &logger_name)
{
return Factory::template create<stdout_color_sink_st>(logger_name);
}
template<typename Factory = default_factory>
inline std::shared_ptr<logger> stderr_color_mt(const std::string &logger_name)
{
return Factory::template create<stderr_color_sink_mt>(logger_name);
}
template<typename Factory = default_factory>
inline std::shared_ptr<logger> stderr_color_st(const std::string &logger_name)
{
return Factory::template createstderr_color_sink_mt>(logger_name);
}
}
\ No newline at end of file
...@@ -13,12 +13,12 @@ ...@@ -13,12 +13,12 @@
#include <mutex> #include <mutex>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
template<class StdoutTrait, class ConsoleMutexTrait> template<class StdoutTrait, class ConsoleMutexTrait>
class stdout_sink : public sink class stdout_sink : public sink
{ {
public: public:
using mutex_t = typename ConsoleMutexTrait::mutex_t; using mutex_t = typename ConsoleMutexTrait::mutex_t;
stdout_sink() : stdout_sink() :
_mutex(ConsoleMutexTrait::console_mutex()), _mutex(ConsoleMutexTrait::console_mutex()),
...@@ -40,15 +40,40 @@ public: ...@@ -40,15 +40,40 @@ public:
std::lock_guard<mutex_t> lock(_mutex); std::lock_guard<mutex_t> lock(_mutex);
fflush(StdoutTrait::stream()); fflush(StdoutTrait::stream());
} }
private: private:
typename mutex_t& _mutex; typename mutex_t& _mutex;
FILE* _file; FILE* _file;
}; };
using stdout_sink_mt = stdout_sink<details::console_stdout_trait, details::console_mutex_trait>;
using stdout_sink_st = stdout_sink<details::console_stdout_trait, details::console_null_mutex_trait>;
using stderr_sink_mt = stdout_sink<details::console_stderr_trait, details::console_mutex_trait>;
using stderr_sink_st = stdout_sink<details::console_stderr_trait, details::console_null_mutex_trait>;
using stdout_sink_mt = stdout_sink<details::console_stdout_trait, details::console_mutex_trait>; } // namespace sinks
using stdout_sink_st = stdout_sink<details::console_stdout_trait, details::console_null_mutex_trait>;
using stderr_sink_mt = stdout_sink<details::console_stderr_trait, details::console_mutex_trait>;
using stderr_sink_st = stdout_sink<details::console_stderr_trait, details::console_null_mutex_trait>;
} // namespace sinks // factory methods
template<typename Factory = default_factory>
inline std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name)
{
return Factory::template create<stdout_color_sink_mt>(logger_name);
}
template<typename Factory = default_factory>
inline std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name)
{
return Factory::template create<stdout_color_sink_mt>(logger_name);
}
template<typename Factory = default_factory>
inline std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name)
{
return Factory::template create<stderr_color_sink_mt>(logger_name);
}
template<typename Factory = default_factory>
inline std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name)
{
return Factory::template create<stderr_logger_sink_mt>(logger_name);
}
} // namespace spdlog } // namespace spdlog
...@@ -17,15 +17,15 @@ ...@@ -17,15 +17,15 @@
#include <syslog.h> #include <syslog.h>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
/** /**
* Sink that write to syslog using the `syscall()` library call. * Sink that write to syslog using the `syscall()` library call.
* *
* Locking is not needed, as `syslog()` itself is thread-safe. * Locking is not needed, as `syslog()` itself is thread-safe.
*/ */
class syslog_sink : public sink class syslog_sink : public sink
{ {
public: public:
// //
syslog_sink(const std::string &ident = "", int syslog_option = 0, int syslog_facility = LOG_USER) syslog_sink(const std::string &ident = "", int syslog_option = 0, int syslog_facility = LOG_USER)
: _ident(ident) : _ident(ident)
...@@ -57,7 +57,7 @@ public: ...@@ -57,7 +57,7 @@ public:
void flush() override {} void flush() override {}
private: private:
std::array<int, 7> _priorities; std::array<int, 7> _priorities;
// must store the ident because the man says openlog might use the pointer as is and not a string copy // must store the ident because the man says openlog might use the pointer as is and not a string copy
const std::string _ident; const std::string _ident;
...@@ -69,8 +69,16 @@ private: ...@@ -69,8 +69,16 @@ private:
{ {
return _priorities[static_cast<size_t>(msg.level)]; return _priorities[static_cast<size_t>(msg.level)];
} }
}; };
} // namespace sinks } // namespace sinks
// Create and register a syslog logger
template<typename Factory = default_factory>
inline std::shared_ptr<logger> syslog_logger(
const std::string &logger_name, const std::string &ident = "", int syslog_option = 0, int syslog_facilty = (1 << 3))
{
return return Factory::template create<sinks::syslog_sink>(logger_name, syslog_ident, syslog_option, syslog_facility);
}
} // namespace spdlog } // namespace spdlog
#endif #endif
...@@ -18,14 +18,14 @@ ...@@ -18,14 +18,14 @@
#include <wincon.h> #include <wincon.h>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
/* /*
* Windows color console sink. Uses WriteConsoleA to write to the console with colors * Windows color console sink. Uses WriteConsoleA to write to the console with colors
*/ */
template<class HandleTrait, class ConsoleMutexTrait> template<class HandleTrait, class ConsoleMutexTrait>
class wincolor_sink : public sink class wincolor_sink : public sink
{ {
public: public:
const WORD BOLD = FOREGROUND_INTENSITY; const WORD BOLD = FOREGROUND_INTENSITY;
const WORD RED = FOREGROUND_RED; const WORD RED = FOREGROUND_RED;
...@@ -90,7 +90,7 @@ public: ...@@ -90,7 +90,7 @@ public:
// windows console always flushed? // windows console always flushed?
} }
private: private:
using mutex_t = typename ConsoleMutexTrait::mutex_t; using mutex_t = typename ConsoleMutexTrait::mutex_t;
// set color and return the orig console attributes (for resetting later) // set color and return the orig console attributes (for resetting later)
WORD set_console_attribs(WORD attribs) WORD set_console_attribs(WORD attribs)
...@@ -115,14 +115,14 @@ private: ...@@ -115,14 +115,14 @@ private:
HANDLE out_handle_; HANDLE out_handle_;
mutex_t &_mutex; mutex_t &_mutex;
std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_; std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_;
}; };
using wincolor_stdout_sink_mt = wincolor_sink<details::console_stdout_trait, details::console_mutex_trait>; using wincolor_stdout_sink_mt = wincolor_sink<details::console_stdout_trait, details::console_mutex_trait>;
using wincolor_stdout_sink_st = wincolor_sink<details::console_stdout_trait, details::console_null_mutex_trait>; using wincolor_stdout_sink_st = wincolor_sink<details::console_stdout_trait, details::console_null_mutex_trait>;
using wincolor_stderr_sink_mt = wincolor_sink<details::console_stderr_trait, details::console_mutex_trait>; using wincolor_stderr_sink_mt = wincolor_sink<details::console_stderr_trait, details::console_mutex_trait>;
using wincolor_stderr_sink_st = wincolor_sink<details::console_stderr_trait, details::console_null_mutex_trait>; using wincolor_stderr_sink_st = wincolor_sink<details::console_stderr_trait, details::console_null_mutex_trait>;
} // namespace sinks } // namespace sinks
} // namespace spdlog } // namespace spdlog
...@@ -8,17 +8,9 @@ ...@@ -8,17 +8,9 @@
#pragma once #pragma once
#include "details/registry.h" #include "details/registry.h"
#include "sinks/file_sinks.h"
#include "sinks/stdout_sinks.h"
#include "common.h" #include "common.h"
#include "logger.h" #include "logger.h"
#ifdef __ANDROID__
#include "sinks/android_sink.h"
#endif
#include <chrono> #include <chrono>
#include <functional> #include <functional>
#include <memory> #include <memory>
...@@ -27,7 +19,7 @@ ...@@ -27,7 +19,7 @@
namespace spdlog { namespace spdlog {
// Default logger factory- creates synchronous loggers // Default logger factory- creates synchronous loggers
struct create_synchronous struct default_factory
{ {
template<typename Sink, typename... SinkArgs> template<typename Sink, typename... SinkArgs>
...@@ -40,41 +32,6 @@ struct create_synchronous ...@@ -40,41 +32,6 @@ struct create_synchronous
} }
}; };
using default_factory = create_synchronous;
//
// color console loggers
//
// you must include "spdlog/sinks/color_sinks.h" before creating color loggers
//
// #include "spdlog/color_console.h"
//
// using namespace spdlog::sinks
// auto logger = spdlog::console<stdout_color_mt>("logger_name1");
// auto logger = spdlog::console<stdout_color_st>("logger_name2");
// auto looger = spdlog::console<stderr_color_mt>("logger_name3");
//
//
// create asynchrounous color logger
// you must include "spdlog/asynch.h" before creating async loggers
//
// #include "spdlog/asynch."
// #include "spdlog/sinks/color_sinks.h"
// auto async_console = spdlog::console<stderr_color_st, spdlog::create_async>("some_name");
// or
// auto async_console = spdlog::create_async_logger<stdout_color_mt>("Console2");
//template<typename console_type, typename Factory = create_synchronous>
//inline std::shared_ptr<logger> console(const std::string &logger_name)
//{
// return Factory::template create<console_type>(logger_name);
//}
// Create and register a logger with a templated sink type // Create and register a logger with a templated sink type
// The logger's level, formatter and flush level will be set according the global settings. // The logger's level, formatter and flush level will be set according the global settings.
...@@ -159,73 +116,6 @@ inline void drop_all() ...@@ -159,73 +116,6 @@ inline void drop_all()
details::registry::instance().drop_all(); details::registry::instance().drop_all();
} }
//
// Create and register multi/single threaded basic file logger.
// Basic logger simply writes to given file without any limitations or rotations.
//
template<typename Factory = default_factory>
inline std::shared_ptr<logger> basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false)
{
return Factory::template create<sinks::simple_file_sink_mt>(logger_name, filename, truncate);
}
template<typename Factory = default_factory>
inline std::shared_ptr<logger> basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false)
{
return Factory::template create<sinks::simple_file_sink_st>(logger_name, filename, truncate);
}
//
// Create and register multi/single threaded rotating file logger
//
template<typename Factory = default_factory>
inline 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)
{
return Factory::template create<sinks::rotating_file_sink_mt>(logger_name, filename, max_file_size, max_files);
}
template<typename Factory = default_factory>
inline 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)
{
return Factory::template create<sinks::rotating_file_sink_st>(logger_name, filename, max_file_size, max_files);
}
//
// Create file logger which creates new file on the given time (default in midnight):
//
template<typename Factory = default_factory>
inline std::shared_ptr<logger> daily_logger_mt(const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0)
{
return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute);
}
template<typename Factory = default_factory>
inline std::shared_ptr<logger> daily_logger_st(const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0)
{
return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute);
}
#ifdef SPDLOG_ENABLE_SYSLOG
// Create and register a syslog logger
template<typename Factory = default_factory>
inline std::shared_ptr<logger> syslog_logger(
const std::string &logger_name, const std::string &ident = "", int syslog_option = 0, int syslog_facilty = (1 << 3))
{
return return Factory::template create<sinks::syslog_sink>(logger_name, syslog_ident, syslog_option, syslog_facility);
}
#endif
#if defined(__ANDROID__)
// Create and register android syslog logger
template<typename Factory = default_factory>
inline std::shared_ptr<logger> android_logger(const std::string &logger_name, const std::string &tag = "spdlog")
{
return return Factory::template create<sinks::android_sink>(logger_name, tag);
}
#endif
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// //
......
...@@ -102,12 +102,6 @@ ...@@ -102,12 +102,6 @@
// #define SPDLOG_FMT_PRINTF // #define SPDLOG_FMT_PRINTF
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Uncomment to enable syslog (disabled by default)
//
// #define SPDLOG_ENABLE_SYSLOG
///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Uncomment to enable wchar_t support (convert to utf8) // Uncomment to enable wchar_t support (convert to utf8)
// //
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment