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 <iostream>
@ -30,22 +31,24 @@ namespace spd = spdlog;
int main(int, char *[])
{
try
{
// Console logger with color
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");
for (int i = 0; i < 10000; i++)
{
console->info("CONSOLE1");
console2->warn("CONSOLE2");
console1->info(111111);
console2->warn(222222);
}
spdlog::drop_all();
return 0;
auto console = spdlog::stdout_color_st("console");
console->info("Welcome to spdlog!");
console->info("Welcome to spdlog!");
@ -173,7 +176,7 @@ struct my_type
#include "spdlog/fmt/ostr.h" // must be included
void user_defined_example()
{
spd::get("console")->info("user defined type: {}", my_type{14});
spd::get("console")->info("user defined type: {}", my_type{ 14 });
}
//

View File

@ -1,10 +1,76 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
# Visual Studio 15
VisualStudioVersion = 15.0.27428.2037
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example", "example.vcxproj", "{9E5AB93A-0CCE-4BAC-9FCB-0FC9CB5EB8D2}"
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
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
@ -23,4 +89,13 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
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

View File

@ -13,40 +13,6 @@
<ItemGroup>
<ClCompile Include="example.cpp" />
</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">
<ProjectGuid>{9E5AB93A-0CCE-4BAC-9FCB-0FC9CB5EB8D2}</ProjectGuid>
<Keyword>Win32Proj</Keyword>

View File

@ -20,8 +20,8 @@
#include <memory>
namespace spdlog {
// async logger factory- creates a-synchronous loggers
// creates a global thread pool with default queue size of 8192 items and single thread.
// async logger factory - creates async loggers backed with thread pool.
// if a global thread pool doesn't already exist, create it with default queue size of 8192 items and single thread.
struct create_async
{
template<typename Sink, typename... SinkArgs>
@ -45,7 +45,7 @@ struct create_async
};
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)...);
}

View File

@ -134,12 +134,11 @@ private:
bool should_do_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 stderr_color_st = ansicolor_sink<details::console_stderr_trait, details::console_null_mutex_trait>;
#endif
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>;
} // namespace sinks
} // namespace spdlog

View File

@ -117,14 +117,12 @@ private:
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 stdout_color_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_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_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 spdlog

View File

@ -14,11 +14,6 @@
#include "common.h"
#include "logger.h"
#if defined _WIN32
#include "sinks/wincolor_sink.h"
#else
#include "sinks/ansicolor_sink.h"
#endif
#ifdef __ANDROID__
#include "sinks/android_sink.h"
@ -32,7 +27,7 @@
namespace spdlog {
// Default logger factory- creates synchronous loggers
struct default_factory
struct create_synchronous
{
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
// The logger's level, formatter and flush level will be set according the global settings.
// Example:
@ -52,7 +82,7 @@ struct default_factory
template<typename Sink, typename... SinkArgs>
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.
// 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)
{
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)
{
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
//
template<typename Factory = default_factory>
template<typename Factory = create_synchronous>
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>
template<typename Factory = create_synchronous>
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)
{
@ -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):
//
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)
{
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)
{
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
// Create and register a syslog logger
template<typename Factory = default_factory>
template<typename Factory = create_synchronous>
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))
{
@ -240,7 +219,7 @@ inline std::shared_ptr<logger> syslog_logger(
#if defined(__ANDROID__)
// 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")
{
return return Factory::template create<sinks::android_sink>(logger_name, tag);