stdout and color logger factories

This commit is contained in:
gabime 2018-04-20 01:50:09 +03:00
parent d6cb447667
commit ba7c4c0530
7 changed files with 231 additions and 211 deletions

View File

@ -12,9 +12,10 @@
#include "spdlog/sinks/color_sinks.h"
#include "spdlog/async.h"
#include "spdlog/color_logger.h"
#include "spdlog/stdout_logger.h"
#include "spdlog/async.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include <iostream> #include <iostream>
@ -30,122 +31,124 @@ namespace spd = spdlog;
int main(int, char *[]) int main(int, char *[])
{ {
try
{
// Console logger with color try
{
// Console logger with color
spd::init_thread_pool(8192, 3); spd::init_thread_pool(8192, 3);
auto console = spdlog::stdout_color_mt<spdlog::create_async>("console1");
auto console2 = spdlog::stderr_logger_mt<spdlog::create_async>("console2"); auto console1 = spdlog::stdout_color_mt("console111");
auto console2 = spdlog::stdout_logger_mt<spdlog::create_async>("console2");
//auto console3 = spdlog::stdout_color_mt<spdlog::create_async>("console3"); //auto console3 = spdlog::stdout_color_mt<spdlog::create_async>("console3");
for (int i = 0; i < 10000; i++) for (int i = 0; i < 10000; i++)
{ {
console->info("CONSOLE1"); console1->info(111111);
console2->warn("CONSOLE2"); console2->warn(222222);
} }
spdlog::drop_all(); spdlog::drop_all();
return 0; return 0;
auto console = spdlog::stdout_color_st("console");
console->info("Welcome to spdlog!"); console->info("Welcome to spdlog!");
console->info("Welcome to spdlog!"); console->info("Welcome to spdlog!");
console->error("Some error message with arg: {}", 1); console->error("Some error message with arg: {}", 1);
err_handler_example(); err_handler_example();
// Formatting examples // Formatting examples
console->warn("Easy padding in numbers like {:08d}", 12); console->warn("Easy padding in numbers like {:08d}", 12);
console->critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42); console->critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);
console->info("Support for floats {:03.2f}", 1.23456); console->info("Support for floats {:03.2f}", 1.23456);
console->info("Positional args are {1} {0}..", "too", "supported"); console->info("Positional args are {1} {0}..", "too", "supported");
console->info("{:<30}", "left aligned"); console->info("{:<30}", "left aligned");
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");
// Create basic file logger (not rotated) // Create basic file logger (not rotated)
auto my_logger = spd::basic_logger_mt("basic_logger", "logs/basic-log.txt"); auto my_logger = spd::basic_logger_mt("basic_logger", "logs/basic-log.txt");
my_logger->info("Some log message"); my_logger->info("Some log message");
// Create a file rotating logger with 5mb size max and 3 rotated files // Create a file rotating logger with 5mb size max and 3 rotated files
auto rotating_logger = spd::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3); auto rotating_logger = spd::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3);
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
{ {
rotating_logger->info("{} * {} equals {:>10}", i, i, i * i); rotating_logger->info("{} * {} equals {:>10}", i, i, i * i);
} }
// Create a daily logger - a new file is created every day on 2:30am // Create a daily logger - a new file is created every day on 2:30am
auto daily_logger = spd::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30); auto daily_logger = spd::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
// trigger flush if the log severity is error or higher // trigger flush if the log severity is error or higher
daily_logger->flush_on(spd::level::err); daily_logger->flush_on(spd::level::err);
daily_logger->info(123.44); daily_logger->info(123.44);
// Customize msg format for all messages // Customize msg format for all messages
//spd::set_pattern("[%^+++%$] [%H:%M:%S %z] [thread %t] %v"); //crash //spd::set_pattern("[%^+++%$] [%H:%M:%S %z] [thread %t] %v"); //crash
console->info("This an info message with custom format"); console->info("This an info message with custom format");
console->error("This an error message with custom format"); console->error("This an error message with custom format");
// 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
console->debug("This message should not be displayed!"); console->debug("This message should not be displayed!");
console->set_level(spd::level::debug); // Set specific logger's log level console->set_level(spd::level::debug); // Set specific logger's log level
console->debug("This message should be displayed.."); console->debug("This message should be displayed..");
// Compile time log levels // Compile time log levels
// define SPDLOG_DEBUG_ON or SPDLOG_TRACE_ON // define SPDLOG_DEBUG_ON or SPDLOG_TRACE_ON
SPDLOG_TRACE(console, "Enabled only #ifdef SPDLOG_TRACE_ON..{} ,{}", 1, 3.23); SPDLOG_TRACE(console, "Enabled only #ifdef SPDLOG_TRACE_ON..{} ,{}", 1, 3.23);
SPDLOG_DEBUG(console, "Enabled only #ifdef SPDLOG_DEBUG_ON.. {} ,{}", 1, 3.23); SPDLOG_DEBUG(console, "Enabled only #ifdef SPDLOG_DEBUG_ON.. {} ,{}", 1, 3.23);
// 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();
// android example. compile with NDK // android example. compile with NDK
android_example(); android_example();
// Log user-defined types example // Log user-defined types example
user_defined_example(); user_defined_example();
// Change default log error handler // Change default log error handler
err_handler_example(); err_handler_example();
// Apply a function on all registered loggers // Apply a function on all registered loggers
spd::apply_all([&](std::shared_ptr<spdlog::logger> l) { l->info("End of example."); }); spd::apply_all([&](std::shared_ptr<spdlog::logger> l) { l->info("End of example."); });
// Release and close all loggers // Release and close all loggers
spdlog::drop_all(); spdlog::drop_all();
} }
// Exceptions will only be thrown upon failed logger or sink construction (not during logging) // Exceptions will only be thrown upon failed logger or sink construction (not during logging)
catch (const spd::spdlog_ex &ex) catch (const spd::spdlog_ex &ex)
{ {
std::cout << "Log init failed: " << ex.what() << std::endl; std::cout << "Log init failed: " << ex.what() << std::endl;
return 1; return 1;
} }
} }
// must be included to use async logger // must be included to use async logger
#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: // optional change thread pool settings *before* creating the logger:
// spdlog::init_thread_pool(8192, 1); // spdlog::init_thread_pool(8192, 1);
// if not called a defaults are: 8192 queue size and 1 worker thread. // if not called a defaults are: 8192 queue size and 1 worker thread.
} }
// syslog example (linux/osx/freebsd) // syslog example (linux/osx/freebsd)
void syslog_example() void syslog_example()
{ {
#ifdef SPDLOG_ENABLE_SYSLOG #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
} }
@ -153,27 +156,27 @@ void syslog_example()
void android_example() void android_example()
{ {
#if defined(__ANDROID__) #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
{ {
int i; int i;
template<typename OStream> template<typename OStream>
friend OStream &operator<<(OStream &os, const my_type &c) friend OStream &operator<<(OStream &os, const my_type &c)
{ {
return os << "[my_type i=" << c.i << "]"; return os << "[my_type i=" << c.i << "]";
} }
}; };
#include "spdlog/fmt/ostr.h" // must be included #include "spdlog/fmt/ostr.h" // must be included
void user_defined_example() void user_defined_example()
{ {
spd::get("console")->info("user defined type: {}", my_type{14}); spd::get("console")->info("user defined type: {}", my_type{ 14 });
} }
// //
@ -181,8 +184,8 @@ void user_defined_example()
// //
void err_handler_example() void err_handler_example()
{ {
// can be set globaly or per logger(logger->set_error_handler(..)) // can be set globaly or per logger(logger->set_error_handler(..))
spdlog::set_error_handler([](const std::string &msg) { spd::get("console")->error("*******my err handler: {}", msg); }); spdlog::set_error_handler([](const std::string &msg) { spd::get("console")->error("*******my err handler: {}", msg); });
spd::get("console")->info("some invalid message to trigger an error {}{}{}{}", 3); spd::get("console")->info("some invalid message to trigger an error {}{}{}{}", 3);
//spd::get("console")->info("some invalid message to trigger an error {}{}{}{}", 3); //spd::get("console")->info("some invalid message to trigger an error {}{}{}{}", 3);
} }

View File

@ -1,10 +1,76 @@
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14 # Visual Studio 15
VisualStudioVersion = 14.0.25420.1 VisualStudioVersion = 15.0.27428.2037
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example", "example.vcxproj", "{9E5AB93A-0CCE-4BAC-9FCB-0FC9CB5EB8D2}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example", "example.vcxproj", "{9E5AB93A-0CCE-4BAC-9FCB-0FC9CB5EB8D2}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "spdlog", "spdlog", "{7FC6AB76-AD88-4135-888C-0568E81475AF}"
ProjectSection(SolutionItems) = preProject
..\include\spdlog\async.h = ..\include\spdlog\async.h
..\include\spdlog\async_logger.h = ..\include\spdlog\async_logger.h
..\include\spdlog\common.h = ..\include\spdlog\common.h
..\include\spdlog\formatter.h = ..\include\spdlog\formatter.h
..\include\spdlog\logger.h = ..\include\spdlog\logger.h
..\include\spdlog\spdlog.h = ..\include\spdlog\spdlog.h
..\include\spdlog\tweakme.h = ..\include\spdlog\tweakme.h
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "details", "details", "{08E93803-E650-42D9-BBB4-3C16979F850E}"
ProjectSection(SolutionItems) = preProject
..\include\spdlog\details\async_log_helper.h = ..\include\spdlog\details\async_log_helper.h
..\include\spdlog\details\async_logger_impl.h = ..\include\spdlog\details\async_logger_impl.h
..\include\spdlog\details\file_helper.h = ..\include\spdlog\details\file_helper.h
..\include\spdlog\details\log_msg.h = ..\include\spdlog\details\log_msg.h
..\include\spdlog\details\logger_impl.h = ..\include\spdlog\details\logger_impl.h
..\include\spdlog\details\mpmc_bounded_q.h = ..\include\spdlog\details\mpmc_bounded_q.h
..\include\spdlog\details\null_mutex.h = ..\include\spdlog\details\null_mutex.h
..\include\spdlog\details\os.h = ..\include\spdlog\details\os.h
..\include\spdlog\details\pattern_formatter_impl.h = ..\include\spdlog\details\pattern_formatter_impl.h
..\include\spdlog\details\registry.h = ..\include\spdlog\details\registry.h
..\include\spdlog\details\spdlog_impl.h = ..\include\spdlog\details\spdlog_impl.h
..\include\spdlog\details\thread_pool.h = ..\include\spdlog\details\thread_pool.h
..\include\spdlog\details\traits.h = ..\include\spdlog\details\traits.h
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "fmt", "fmt", "{82378DE1-8463-4F91-91A0-C2C40E2AEA2A}"
ProjectSection(SolutionItems) = preProject
..\include\spdlog\fmt\fmt.h = ..\include\spdlog\fmt\fmt.h
..\include\spdlog\fmt\ostr.h = ..\include\spdlog\fmt\ostr.h
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "bundled", "bundled", "{D9CA4494-80D1-48D1-A897-D3564F7B27FF}"
ProjectSection(SolutionItems) = preProject
..\include\spdlog\fmt\bundled\format.cc = ..\include\spdlog\fmt\bundled\format.cc
..\include\spdlog\fmt\bundled\format.h = ..\include\spdlog\fmt\bundled\format.h
..\include\spdlog\fmt\bundled\LICENSE.rst = ..\include\spdlog\fmt\bundled\LICENSE.rst
..\include\spdlog\fmt\bundled\ostream.cc = ..\include\spdlog\fmt\bundled\ostream.cc
..\include\spdlog\fmt\bundled\ostream.h = ..\include\spdlog\fmt\bundled\ostream.h
..\include\spdlog\fmt\bundled\posix.cc = ..\include\spdlog\fmt\bundled\posix.cc
..\include\spdlog\fmt\bundled\posix.h = ..\include\spdlog\fmt\bundled\posix.h
..\include\spdlog\fmt\bundled\printf.cc = ..\include\spdlog\fmt\bundled\printf.cc
..\include\spdlog\fmt\bundled\printf.h = ..\include\spdlog\fmt\bundled\printf.h
..\include\spdlog\fmt\bundled\time.h = ..\include\spdlog\fmt\bundled\time.h
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sinks", "sinks", "{27D16BB9-2B81-4F61-80EC-0C7A777248E4}"
ProjectSection(SolutionItems) = preProject
..\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\base_sink.h = ..\include\spdlog\sinks\base_sink.h
..\include\spdlog\color_logger.h = ..\include\spdlog\color_logger.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\msvc_sink.h = ..\include\spdlog\sinks\msvc_sink.h
..\include\spdlog\sinks\null_sink.h = ..\include\spdlog\sinks\null_sink.h
..\include\spdlog\sinks\ostream_sink.h = ..\include\spdlog\sinks\ostream_sink.h
..\include\spdlog\sinks\sink.h = ..\include\spdlog\sinks\sink.h
..\include\spdlog\sinks\stdout_sinks.h = ..\include\spdlog\sinks\stdout_sinks.h
..\include\spdlog\sinks\syslog_sink.h = ..\include\spdlog\sinks\syslog_sink.h
..\include\spdlog\sinks\wincolor_sink.h = ..\include\spdlog\sinks\wincolor_sink.h
..\include\spdlog\sinks\windebug_sink.h = ..\include\spdlog\sinks\windebug_sink.h
EndProjectSection
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32 Debug|Win32 = Debug|Win32
@ -23,4 +89,13 @@ Global
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
EndGlobalSection EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{08E93803-E650-42D9-BBB4-3C16979F850E} = {7FC6AB76-AD88-4135-888C-0568E81475AF}
{82378DE1-8463-4F91-91A0-C2C40E2AEA2A} = {7FC6AB76-AD88-4135-888C-0568E81475AF}
{D9CA4494-80D1-48D1-A897-D3564F7B27FF} = {82378DE1-8463-4F91-91A0-C2C40E2AEA2A}
{27D16BB9-2B81-4F61-80EC-0C7A777248E4} = {7FC6AB76-AD88-4135-888C-0568E81475AF}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1BF53532-C5DC-4236-B195-9E17CBE40A48}
EndGlobalSection
EndGlobal EndGlobal

View File

@ -13,40 +13,6 @@
<ItemGroup> <ItemGroup>
<ClCompile Include="example.cpp" /> <ClCompile Include="example.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\spdlog\async_logger.h" />
<ClInclude Include="..\include\spdlog\common.h" />
<ClInclude Include="..\include\spdlog\details\async_logger_impl.h" />
<ClInclude Include="..\include\spdlog\details\async_log_helper.h" />
<ClInclude Include="..\include\spdlog\details\file_helper.h" />
<ClInclude Include="..\include\spdlog\details\logger_impl.h" />
<ClInclude Include="..\include\spdlog\details\log_msg.h" />
<ClInclude Include="..\include\spdlog\details\mpmc_bounded_q.h" />
<ClInclude Include="..\include\spdlog\details\null_mutex.h" />
<ClInclude Include="..\include\spdlog\details\os.h" />
<ClInclude Include="..\include\spdlog\details\pattern_formatter_impl.h" />
<ClInclude Include="..\include\spdlog\details\registry.h" />
<ClInclude Include="..\include\spdlog\details\spdlog_impl.h" />
<ClInclude Include="..\include\spdlog\details\thread_pool.h" />
<ClInclude Include="..\include\spdlog\fmt\fmt.h" />
<ClInclude Include="..\include\spdlog\fmt\ostr.h" />
<ClInclude Include="..\include\spdlog\formatter.h" />
<ClInclude Include="..\include\spdlog\logger.h" />
<ClInclude Include="..\include\spdlog\sinks\android_sink.h" />
<ClInclude Include="..\include\spdlog\sinks\ansicolor_sink.h" />
<ClInclude Include="..\include\spdlog\sinks\base_sink.h" />
<ClInclude Include="..\include\spdlog\sinks\dist_sink.h" />
<ClInclude Include="..\include\spdlog\sinks\file_sinks.h" />
<ClInclude Include="..\include\spdlog\sinks\msvc_sink.h" />
<ClInclude Include="..\include\spdlog\sinks\null_sink.h" />
<ClInclude Include="..\include\spdlog\sinks\ostream_sink.h" />
<ClInclude Include="..\include\spdlog\sinks\sink.h" />
<ClInclude Include="..\include\spdlog\sinks\stdout_sinks.h" />
<ClInclude Include="..\include\spdlog\sinks\syslog_sink.h" />
<ClInclude Include="..\include\spdlog\sinks\wincolor_sink.h" />
<ClInclude Include="..\include\spdlog\spdlog.h" />
<ClInclude Include="..\include\spdlog\tweakme.h" />
</ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<ProjectGuid>{9E5AB93A-0CCE-4BAC-9FCB-0FC9CB5EB8D2}</ProjectGuid> <ProjectGuid>{9E5AB93A-0CCE-4BAC-9FCB-0FC9CB5EB8D2}</ProjectGuid>
<Keyword>Win32Proj</Keyword> <Keyword>Win32Proj</Keyword>

View File

@ -20,8 +20,8 @@
#include <memory> #include <memory>
namespace spdlog { namespace spdlog {
// async logger factory- creates a-synchronous loggers // async logger factory - creates async loggers backed with thread pool.
// creates a global thread pool with default queue size of 8192 items and single thread. // if a global thread pool doesn't already exist, create it with default queue size of 8192 items and single thread.
struct create_async struct create_async
{ {
template<typename Sink, typename... SinkArgs> template<typename Sink, typename... SinkArgs>
@ -45,7 +45,7 @@ struct create_async
}; };
template<typename Sink, typename... SinkArgs> template<typename Sink, typename... SinkArgs>
inline std::shared_ptr<spdlog::logger> create_as(const std::string &logger_name, SinkArgs &&... sink_args) inline std::shared_ptr<spdlog::logger> create_async_logger(const std::string &logger_name, SinkArgs &&... sink_args)
{ {
return create_async::create<Sink>(logger_name, std::forward<SinkArgs>(sink_args)...); return create_async::create<Sink>(logger_name, std::forward<SinkArgs>(sink_args)...);
} }

View File

@ -134,12 +134,11 @@ 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_;
}; };
#ifndef _WIN32
using stdout_color_mt = ansicolor_sink<details::console_stdout_trait, details::console_mutex_trait>;
using stdout_color_st = ansicolor_sink<details::console_stdout_trait, details::console_null_mutex_trait>;
using stderr_color_mt = ansicolor_sink<details::console_stderr_trait, details::console_mutex_trait>; using ansicolor_stdout_sink_mt = ansicolor_sink<details::console_stdout_trait, details::console_mutex_trait>;
using stderr_color_st = ansicolor_sink<details::console_stderr_trait, details::console_null_mutex_trait>; using ansicolor_stdout_sink_st = ansicolor_sink<details::console_stdout_trait, details::console_null_mutex_trait>;
#endif 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 sinks
} // namespace spdlog } // namespace spdlog

View File

@ -117,14 +117,12 @@ private:
std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_; std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_;
}; };
//
// windows color console to stdout
//
using stdout_color_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 stdout_color_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 stderr_color_mt = wincolor_sink<details::console_stderr_trait, details::console_mutex_trait>;
using stderr_color_st = wincolor_sink<details::console_stderr_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_st = wincolor_sink<details::console_stderr_trait, details::console_null_mutex_trait>;
} // namespace sinks } // namespace sinks
} // namespace spdlog } // namespace spdlog

View File

@ -14,11 +14,6 @@
#include "common.h" #include "common.h"
#include "logger.h" #include "logger.h"
#if defined _WIN32
#include "sinks/wincolor_sink.h"
#else
#include "sinks/ansicolor_sink.h"
#endif
#ifdef __ANDROID__ #ifdef __ANDROID__
#include "sinks/android_sink.h" #include "sinks/android_sink.h"
@ -32,7 +27,7 @@
namespace spdlog { namespace spdlog {
// Default logger factory- creates synchronous loggers // Default logger factory- creates synchronous loggers
struct default_factory struct create_synchronous
{ {
template<typename Sink, typename... SinkArgs> template<typename Sink, typename... SinkArgs>
@ -45,6 +40,41 @@ struct default_factory
} }
}; };
//
// 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.
// Example: // Example:
@ -52,7 +82,7 @@ struct default_factory
template<typename Sink, typename... SinkArgs> template<typename Sink, typename... SinkArgs>
inline std::shared_ptr<spdlog::logger> create(const std::string &logger_name, SinkArgs &&... sink_args) inline std::shared_ptr<spdlog::logger> create(const std::string &logger_name, SinkArgs &&... sink_args)
{ {
return default_factory::create<Sink>(logger_name, std::forward<SinkArgs>(sink_args)...); return create_synchronous::create<Sink>(logger_name, std::forward<SinkArgs>(sink_args)...);
} }
// //
@ -132,13 +162,13 @@ inline void drop_all()
// Create and register multi/single threaded basic file logger. // Create and register multi/single threaded basic file logger.
// Basic logger simply writes to given file without any limitations or rotations. // Basic logger simply writes to given file without any limitations or rotations.
// //
template<typename Factory = default_factory> template<typename Factory = create_synchronous>
inline std::shared_ptr<logger> basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false) 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); return Factory::template create<sinks::simple_file_sink_mt>(logger_name, filename, truncate);
} }
template<typename Factory = default_factory> template<typename Factory = create_synchronous>
inline std::shared_ptr<logger> basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false) 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); return Factory::template create<sinks::simple_file_sink_st>(logger_name, filename, truncate);
@ -147,14 +177,14 @@ inline std::shared_ptr<logger> basic_logger_st(const std::string &logger_name, c
// //
// Create and register multi/single threaded rotating file logger // Create and register multi/single threaded rotating file logger
// //
template<typename Factory = default_factory> template<typename Factory = create_synchronous>
inline std::shared_ptr<logger> rotating_logger_mt( 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) 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); return Factory::template create<sinks::rotating_file_sink_mt>(logger_name, filename, max_file_size, max_files);
} }
template<typename Factory = default_factory> template<typename Factory = create_synchronous>
inline std::shared_ptr<logger> rotating_logger_st( 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) const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files)
{ {
@ -164,73 +194,22 @@ inline std::shared_ptr<logger> rotating_logger_st(
// //
// Create file logger which creates new file on the given time (default in midnight): // Create file logger which creates new file on the given time (default in midnight):
// //
template<typename Factory = default_factory> template<typename Factory = create_synchronous>
inline std::shared_ptr<logger> daily_logger_mt(const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0) 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); return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute);
} }
template<typename Factory = default_factory> template<typename Factory = create_synchronous>
inline std::shared_ptr<logger> daily_logger_st(const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0) 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); return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute);
} }
//
// color console logger
//
template<typename Factory = default_factory>
inline std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name)
{
return Factory::template create<sinks::stdout_color_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<sinks::stdout_color_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<sinks::stderr_color_mt>(logger_name);
}
template<typename Factory = default_factory>
inline std::shared_ptr<logger> stderr_color_st(const std::string &logger_name)
{
return Factory::template create<sinks::stderr_color_st>(logger_name);
}
// console loggers (no colors)
template<typename Factory = default_factory>
inline std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name)
{
return Factory::template create<sinks::stdout_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<sinks::stdout_sink_st>(logger_name);
}
template<typename Factory = default_factory>
inline std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name)
{
return Factory::template create<sinks::stderr_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<sinks::stderr_sink_st>(logger_name);
}
#ifdef SPDLOG_ENABLE_SYSLOG #ifdef SPDLOG_ENABLE_SYSLOG
// Create and register a syslog logger // Create and register a syslog logger
template<typename Factory = default_factory> template<typename Factory = create_synchronous>
inline std::shared_ptr<logger> syslog_logger( 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)) const std::string &logger_name, const std::string &ident = "", int syslog_option = 0, int syslog_facilty = (1 << 3))
{ {
@ -240,7 +219,7 @@ inline std::shared_ptr<logger> syslog_logger(
#if defined(__ANDROID__) #if defined(__ANDROID__)
// Create and register android syslog logger // Create and register android syslog logger
template<typename Factory = default_factory> template<typename Factory = create_synchronous>
inline std::shared_ptr<logger> android_logger(const std::string &logger_name, const std::string &tag = "spdlog") 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); return return Factory::template create<sinks::android_sink>(logger_name, tag);