updated example and added more creation functions

This commit is contained in:
gabi 2014-10-31 03:17:40 +02:00
parent c7b8c762fb
commit 31971bf63b
14 changed files with 272 additions and 165 deletions

View File

@ -1,27 +1,27 @@
======================================================================== ========================================================================
CONSOLE APPLICATION : spitlogtest Project Overview CONSOLE APPLICATION : spdlogtest Project Overview
======================================================================== ========================================================================
AppWizard has created this spitlogtest application for you. AppWizard has created this spdlogtest application for you.
This file contains a summary of what you will find in each of the files that This file contains a summary of what you will find in each of the files that
make up your spitlogtest application. make up your spdlogtest application.
spitlogtest.vcxproj spdlogtest.vcxproj
This is the main project file for VC++ projects generated using an Application Wizard. This is the main project file for VC++ projects generated using an Application Wizard.
It contains information about the version of Visual C++ that generated the file, and It contains information about the version of Visual C++ that generated the file, and
information about the platforms, configurations, and project features selected with the information about the platforms, configurations, and project features selected with the
Application Wizard. Application Wizard.
spitlogtest.vcxproj.filters spdlogtest.vcxproj.filters
This is the filters file for VC++ projects generated using an Application Wizard. This is the filters file for VC++ projects generated using an Application Wizard.
It contains information about the association between the files in your project It contains information about the association between the files in your project
and the filters. This association is used in the IDE to show grouping of files with and the filters. This association is used in the IDE to show grouping of files with
similar extensions under a specific node (for e.g. ".cpp" files are associated with the similar extensions under a specific node (for e.g. ".cpp" files are associated with the
"Source Files" filter). "Source Files" filter).
spitlogtest.cpp spdlogtest.cpp
This is the main application source file. This is the main application source file.
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
@ -29,7 +29,7 @@ Other standard files:
StdAfx.h, StdAfx.cpp StdAfx.h, StdAfx.cpp
These files are used to build a precompiled header (PCH) file These files are used to build a precompiled header (PCH) file
named spitlogtest.pch and a precompiled types file named StdAfx.obj. named spdlogtest.pch and a precompiled types file named StdAfx.obj.
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
Other notes: Other notes:

View File

@ -13,7 +13,7 @@
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<ProjectGuid>{DC7F2F3E-0D0E-4324-A278-410DBC4045AD}</ProjectGuid> <ProjectGuid>{DC7F2F3E-0D0E-4324-A278-410DBC4045AD}</ProjectGuid>
<Keyword>Win32Proj</Keyword> <Keyword>Win32Proj</Keyword>
<RootNamespace>spitlogtest</RootNamespace> <RootNamespace>spdlogtest</RootNamespace>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
@ -85,28 +85,30 @@
<Text Include="ReadMe.txt" /> <Text Include="ReadMe.txt" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\include\spitlog\common.h" /> <ClInclude Include="..\..\include\spdlog\common.h" />
<ClInclude Include="..\..\include\spitlog\details\blocking_queue.h" /> <ClInclude Include="..\..\include\spdlog\details\blocking_queue.h" />
<ClInclude Include="..\..\include\spitlog\details\fast_oss.h" /> <ClInclude Include="..\..\include\spdlog\details\fast_oss.h" />
<ClInclude Include="..\..\include\spitlog\details\fast_istostr.h" /> <ClInclude Include="..\..\include\spdlog\details\fast_istostr.h" />
<ClInclude Include="..\..\include\spitlog\details\file_helper.h" /> <ClInclude Include="..\..\include\spdlog\details\file_helper.h" />
<ClInclude Include="..\..\include\spitlog\details\line_logger.h" /> <ClInclude Include="..\..\include\spdlog\details\line_logger.h" />
<ClInclude Include="..\..\include\spitlog\details\logger_impl.h" /> <ClInclude Include="..\..\include\spdlog\details\logger_impl.h" />
<ClInclude Include="..\..\include\spitlog\details\log_msg.h" /> <ClInclude Include="..\..\include\spdlog\details\log_msg.h" />
<ClInclude Include="..\..\include\spitlog\details\null_mutex.h" /> <ClInclude Include="..\..\include\spdlog\details\null_mutex.h" />
<ClInclude Include="..\..\include\spitlog\details\os.h" /> <ClInclude Include="..\..\include\spdlog\details\os.h" />
<ClInclude Include="..\..\include\spitlog\details\pattern_formatter_impl.h" /> <ClInclude Include="..\..\include\spdlog\details\pattern_formatter_impl.h" />
<ClInclude Include="..\..\include\spitlog\details\registry.h" /> <ClInclude Include="..\..\include\spdlog\details\registry.h" />
<ClInclude Include="..\..\include\spitlog\details\stack_buf.h" /> <ClInclude Include="..\..\include\spdlog\details\spdlog_impl.h" />
<ClInclude Include="..\..\include\spitlog\formatter.h" /> <ClInclude Include="..\..\include\spdlog\details\stack_buf.h" />
<ClInclude Include="..\..\include\spitlog\logger.h" /> <ClInclude Include="..\..\include\spdlog\formatter.h" />
<ClInclude Include="..\..\include\spitlog\sinks\async_sink.h" /> <ClInclude Include="..\..\include\spdlog\logger.h" />
<ClInclude Include="..\..\include\spitlog\sinks\base_sink.h" /> <ClInclude Include="..\..\include\spdlog\sinks\async_sink.h" />
<ClInclude Include="..\..\include\spitlog\sinks\ostream_sink.h" /> <ClInclude Include="..\..\include\spdlog\sinks\base_sink.h" />
<ClInclude Include="..\..\include\spitlog\sinks\file_sinks.h" /> <ClInclude Include="..\..\include\spdlog\sinks\ostream_sink.h" />
<ClInclude Include="..\..\include\spitlog\sinks\null_sink.h" /> <ClInclude Include="..\..\include\spdlog\sinks\file_sinks.h" />
<ClInclude Include="..\..\include\spitlog\sinks\sink.h" /> <ClInclude Include="..\..\include\spdlog\sinks\null_sink.h" />
<ClInclude Include="..\..\include\spitlog\sinks\stdout_sinks.h" /> <ClInclude Include="..\..\include\spdlog\sinks\sink.h" />
<ClInclude Include="..\..\include\spdlog\sinks\stdout_sinks.h" />
<ClInclude Include="..\..\include\spdlog\spdlog.h" />
<ClInclude Include="stdafx.h" /> <ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" /> <ClInclude Include="targetver.h" />
</ItemGroup> </ItemGroup>

View File

@ -13,13 +13,13 @@
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter> </Filter>
<Filter Include="Header Files\c11log"> <Filter Include="Header Files\spdlog">
<UniqueIdentifier>{bad4e9e3-82bd-4b02-9b4a-43198cc65844}</UniqueIdentifier> <UniqueIdentifier>{bad4e9e3-82bd-4b02-9b4a-43198cc65844}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="Header Files\c11log\details"> <Filter Include="Header Files\spdlog\details">
<UniqueIdentifier>{a4cc3c0e-8713-4146-98c7-1e7ab4ed95f8}</UniqueIdentifier> <UniqueIdentifier>{a4cc3c0e-8713-4146-98c7-1e7ab4ed95f8}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="Header Files\c11log\sinks"> <Filter Include="Header Files\spdlog\sinks">
<UniqueIdentifier>{87170f3b-5292-49b8-8581-7db7cb9abd29}</UniqueIdentifier> <UniqueIdentifier>{87170f3b-5292-49b8-8581-7db7cb9abd29}</UniqueIdentifier>
</Filter> </Filter>
</ItemGroup> </ItemGroup>
@ -33,71 +33,77 @@
<ClInclude Include="targetver.h"> <ClInclude Include="targetver.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\include\spitlog\common.h"> <ClInclude Include="..\..\include\spdlog\common.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\include\spitlog\details\blocking_queue.h"> <ClInclude Include="..\..\include\spdlog\formatter.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\include\spitlog\details\fast_oss.h"> <ClInclude Include="..\..\include\spdlog\logger.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\include\spitlog\details\fast_istostr.h"> <ClInclude Include="..\..\include\spdlog\sinks\async_sink.h">
<Filter>Header Files\spdlog\sinks</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spdlog\sinks\base_sink.h">
<Filter>Header Files\spdlog\sinks</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spdlog\sinks\null_sink.h">
<Filter>Header Files\spdlog\sinks</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spdlog\sinks\ostream_sink.h">
<Filter>Header Files\spdlog\sinks</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spdlog\sinks\sink.h">
<Filter>Header Files\spdlog\sinks</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spdlog\sinks\stdout_sinks.h">
<Filter>Header Files\spdlog\sinks</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spdlog\details\blocking_queue.h">
<Filter>Header Files\spdlog\details</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spdlog\details\fast_istostr.h">
<Filter>Header Files\spdlog\details</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spdlog\details\fast_oss.h">
<Filter>Header Files\spdlog\details</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spdlog\details\file_helper.h">
<Filter>Header Files\spdlog\details</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spdlog\sinks\file_sinks.h">
<Filter>Header Files\spdlog\sinks</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spdlog\details\line_logger.h">
<Filter>Header Files\spdlog\details</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spdlog\details\log_msg.h">
<Filter>Header Files\spdlog\details</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spdlog\details\logger_impl.h">
<Filter>Header Files\spdlog\details</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spdlog\details\os.h">
<Filter>Header Files\spdlog\details</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spdlog\details\null_mutex.h">
<Filter>Header Files\spdlog\details</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spdlog\details\pattern_formatter_impl.h">
<Filter>Header Files\spdlog\details</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spdlog\details\registry.h">
<Filter>Header Files\spdlog\details</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spdlog\details\stack_buf.h">
<Filter>Header Files\spdlog\details</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spdlog\spdlog.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\include\spitlog\details\file_helper.h"> <ClInclude Include="..\..\include\spdlog\details\spdlog_impl.h">
<Filter>Header Files</Filter> <Filter>Header Files\spdlog\details</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spitlog\details\line_logger.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spitlog\details\logger_impl.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spitlog\details\log_msg.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spitlog\details\null_mutex.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spitlog\details\os.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spitlog\details\pattern_formatter_impl.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spitlog\details\registry.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spitlog\details\stack_buf.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spitlog\formatter.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spitlog\logger.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spitlog\sinks\async_sink.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spitlog\sinks\base_sink.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spitlog\sinks\ostream_sink.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spitlog\sinks\file_sinks.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spitlog\sinks\null_sink.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spitlog\sinks\sink.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\spitlog\sinks\stdout_sinks.h">
<Filter>Header Files</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,5 +1,5 @@
// stdafx.cpp : source file that includes just the standard includes // stdafx.cpp : source file that includes just the standard includes
// spitlogtest.pch will be the pre-compiled header // spdlogtest.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information // stdafx.obj will contain the pre-compiled type information
#include "stdafx.h" #include "stdafx.h"

View File

@ -1,33 +1,39 @@
// example.cpp : Simple logger example // example.cpp : Simple logger example
// //
#include "spitlog/logger.h" #include "spdlog/spdlog.h"
#include "spitlog/sinks/async_sink.h" #include "spdlog/sinks/file_sinks.h"
#include "spitlog/sinks/file_sinks.h" #include "spdlog/sinks/stdout_sinks.h"
#include "spitlog/sinks/stdout_sinks.h" #include "spdlog/sinks/null_sink.h"
#include "spitlog/sinks/null_sink.h"
#include "utils.h" #include "utils.h"
#include "spitlog/details/registry.h"
using namespace std;
using namespace std::chrono; using namespace std::chrono;
using namespace spitlog; using namespace spdlog;
using namespace utils; using namespace utils;
int main(int argc, char* argv[]) int main_(int argc, char* argv[])
{ {
try { try {
using namespace spdlog::sinks;
spdlog::create<daily_file_sink_st>("mylog", "dailylog", "txt");
const unsigned int howmany = argc <= 1 ? 1500000 : atoi(argv[1]); const unsigned int howmany = argc <= 1 ? 1500000 : atoi(argv[1]);
//spitlog::set_format("%t"); spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %t");
auto console = spitlog::create<sinks::stdout_sink_st>("reporter");
//console->set_format("[%n %l] %t");
console->set_level(spitlog::level::INFO); auto console = spdlog::create<sinks::stdout_sink_st>("reporter");
console->info("Starting bench with", howmany, "iterations.."); console->info("Starting bench with", howmany, "iterations..");
console->log() << "Streams are also supprted: " << std::hex << 255;
spdlog::stop();
auto bench = spitlog::create<sinks::rotating_file_sink_st>("bench", "myrotating", "txt", 1024 * 1024 * 1, 10, 0); //return 0;
auto bench = spdlog::create<sinks::rotating_file_sink_st>("bench", "myrotating", "txt", 1024 * 1024 * 1, 10, 0);
//auto bench = spitlog::create<sinks::simple_file_sink_st>("bench", "simplelog.txt", 1); //auto bench = spdlog::create<sinks::simple_file_sink_st>("bench", "simplelog.txt", 1);
//auto bench = spitlog::create<sinks::null_sink_st>("bench"); //auto bench = spdlog::create<sinks::null_sink_st>("bench");
auto start = system_clock::now(); auto start = system_clock::now();
for (unsigned int i = 0; i < howmany; ++i) for (unsigned int i = 0; i < howmany; ++i)
{ {
@ -37,9 +43,9 @@ int main(int argc, char* argv[])
auto delta = system_clock::now() - start; auto delta = system_clock::now() - start;
auto delta_d = duration_cast<duration<double>> (delta).count(); auto delta_d = duration_cast<duration<double>> (delta).count();
console->info("Total:") << format(howmany); cout << "Total:" << format(howmany) << endl;
console->info("Delta:") << format(delta_d); cout << "Delta:" << format(delta_d) << endl;
console->info("Rate:") << format(howmany / delta_d) << "/sec"; cout << "Rate:" << format(howmany / delta_d) << "/sec\n";
} }
catch (std::exception &ex) catch (std::exception &ex)

View File

@ -1,27 +1,45 @@
// example.cpp : Simple logger example
// //
#define FFLOG_ENABLE_TRACE // example.cpp : spdlog usage example
//
#include <iostream> #include <iostream>
#include "spitlog/logger.h" #include "spdlog/spdlog.h"
#include "spitlog/sinks/stdout_sinks.h"
#include "spitlog/sinks/file_sinks.h"
using namespace std;
using namespace spitlog;
details::fast_oss f(const std::string& what) int main(int, char* [])
{
details::fast_oss oss;
oss << what;
return oss;
}
int main_(int, char* [])
{ {
auto foss = f("test2"); namespace spd = spdlog;
foss.str(); try
{
std::string filename = "spdlog_example";
auto console = spd::stderr_logger_mt("console");
console->info("Welcome to spdlog!");
console->info() << "Creating file " << filename << "..";
auto file_logger = spd::rotating_logger_mt("file_logger", filename, 1024 * 1024 * 5, 3);
file_logger->info("Log file message number", 1);
for (int i = 0; i < 100; ++i)
{
auto square = i*i;
file_logger->info() << i << '*' << i << '=' << square << " (" << "0x" << std::hex << square << ")";
}
// Change log level to all loggers to warning and above
spd::set_level(spd::level::WARN);
console->info("This should not be displayed");
console->warn("This should!");
// Change format pattern to all loggers
spd::set_pattern(" **** %Y-%m-%d %H:%M:%S.%e %l **** %t");
spd::get("console")->warn("This is another message with different format");
}
catch (const spd::spdlog_ex& ex)
{
std::cout << "Log failed: " << ex.what() << std::endl;
}
return 0;
} }

View File

@ -41,10 +41,10 @@ inline const char* to_str(spdlog::level::level_enum l)
// //
// Log exception // Log exception
// //
class fflog_exception : public std::exception class spdlog_ex : public std::exception
{ {
public: public:
fflog_exception(const std::string& msg) :_msg(msg) {}; spdlog_ex(const std::string& msg) :_msg(msg) {};
const char* what() const throw() override { const char* what() const throw() override {
return _msg.c_str(); return _msg.c_str();
} }

View File

@ -54,7 +54,7 @@ public:
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms_bewteen_tries)); std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms_bewteen_tries));
} }
throw fflog_exception("Failed opening file " + filename + " for writing"); throw spdlog_ex("Failed opening file " + filename + " for writing");
} }
void close() void close()
@ -71,7 +71,7 @@ public:
auto& buf = msg.formatted.buf(); auto& buf = msg.formatted.buf();
size_t size = buf.size(); size_t size = buf.size();
if(std::fwrite(buf.data(), sizeof(char), size, _fd) != size) if(std::fwrite(buf.data(), sizeof(char), size, _fd) != size)
throw fflog_exception("Failed writing to file " + _filename); throw spdlog_ex("Failed writing to file " + _filename);
if(--_flush_countdown == 0) if(--_flush_countdown == 0)
{ {

View File

@ -116,8 +116,16 @@ inline void spdlog::logger::stop_logging()
inline void spdlog::logger::_variadic_log(spdlog::details::line_logger&) {} inline void spdlog::logger::_variadic_log(spdlog::details::line_logger&) {}
template <typename Last>
inline void spdlog::logger::_variadic_log(spdlog::details::line_logger& l, const Last& last)
{
l.write(last);
}
template <typename First, typename... Rest> template <typename First, typename... Rest>
void spdlog::logger::_variadic_log(spdlog::details::line_logger& l, const First& first, const Rest&... rest) inline void spdlog::logger::_variadic_log(spdlog::details::line_logger& l, const First& first, const Rest&... rest)
{ {
l.write(first); l.write(first);
l.write(' '); l.write(' ');

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
// Loggers registy of unique name->logger pointer // Loggers registy of unique name->logger pointer
// If 2 loggers with same name are added, the second will be overrun the first // An attempt to create a logger with an alreasy existing name will be ignored
// If user requests a non existing logger, nullptr will be returned // If user requests a non existing logger, nullptr will be returned
// This class is thread safe // This class is thread safe
@ -16,10 +16,10 @@ namespace details {
class registry { class registry {
public: public:
std::shared_ptr<logger> get(const std::string& name) std::shared_ptr<logger> get(const std::string& logger_name)
{ {
std::lock_guard<std::mutex> lock(_mutex); std::lock_guard<std::mutex> lock(_mutex);
auto found = _loggers.find(name); auto found = _loggers.find(logger_name);
return found == _loggers.end() ? nullptr : found->second; return found == _loggers.end() ? nullptr : found->second;
} }
@ -27,6 +27,11 @@ 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<std::mutex> lock(_mutex); std::lock_guard<std::mutex> lock(_mutex);
//If already exists, just return it
auto found = _loggers.find(logger_name);
if (found != _loggers.end())
return found->second;
auto new_logger = std::make_shared<logger>(logger_name, sinks_begin, sinks_end); auto new_logger = std::make_shared<logger>(logger_name, sinks_begin, sinks_end);
new_logger->set_formatter(_formatter); new_logger->set_formatter(_formatter);
new_logger->set_level(_level); new_logger->set_level(_level);

View File

@ -4,12 +4,62 @@
// Global registry functions // Global registry functions
// //
#include "registry.h" #include "registry.h"
#include "../sinks/file_sinks.h"
#include "../sinks/stdout_sinks.h"
inline std::shared_ptr<spdlog::logger> spdlog::get(const std::string& name) inline std::shared_ptr<spdlog::logger> spdlog::get(const std::string& name)
{ {
return details::registry::instance().get(name); return details::registry::instance().get(name);
} }
// Create multi/single threaded rotating file logger
inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_mt(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, size_t flush_inverval)
{
return create<spdlog::sinks::rotating_file_sink_mt>(logger_name, filename, "txt", max_file_size, max_files, flush_inverval);
}
inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_st(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, size_t flush_inverval)
{
return create<spdlog::sinks::rotating_file_sink_st>(logger_name, filename, "txt", max_file_size, max_files, flush_inverval);
}
// Create file logger which creates new file at midnight):
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_mt(const std::string& logger_name, const std::string& filename, size_t flush_inverval)
{
return create<spdlog::sinks::daily_file_sink_mt>(logger_name, filename, "txt", flush_inverval);
}
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string& logger_name, const std::string& filename, size_t flush_inverval)
{
return create<spdlog::sinks::daily_file_sink_st>(logger_name, filename, "txt", flush_inverval);
}
// Create stdout/stderr loggers
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt(const std::string& logger_name)
{
return create<spdlog::sinks::stdout_sink_mt>(logger_name);
}
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st(const std::string& logger_name)
{
return create<spdlog::sinks::stdout_sink_st>(logger_name);
}
inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt(const std::string& logger_name)
{
return create<spdlog::sinks::stderr_sink_mt>(logger_name);
}
inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st(const std::string& logger_name)
{
return create<spdlog::sinks::stderr_sink_st>(logger_name);
}
inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, spdlog::sinks_init_list sinks) inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, spdlog::sinks_init_list sinks)
{ {
return details::registry::instance().create(logger_name, sinks); return details::registry::instance().create(logger_name, sinks);

View File

@ -64,6 +64,8 @@ private:
std::vector<sink_ptr> _sinks; std::vector<sink_ptr> _sinks;
std::atomic_int _level; std::atomic_int _level;
void _variadic_log(details::line_logger& l); void _variadic_log(details::line_logger& l);
template <typename Last>
inline void _variadic_log(spdlog::details::line_logger& l, const Last& last);
template <typename First, typename... Rest> template <typename First, typename... Rest>
void _variadic_log(details::line_logger&l, const First& first, const Rest&... rest); void _variadic_log(details::line_logger&l, const First& first, const Rest&... rest);
void _log_msg(details::log_msg& msg); void _log_msg(details::log_msg& msg);

View File

@ -48,8 +48,8 @@ class rotating_file_sink : public base_sink<Mutex>
{ {
public: public:
rotating_file_sink(const std::string &base_filename, const std::string &extension, rotating_file_sink(const std::string &base_filename, const std::string &extension,
const std::size_t max_size, const std::size_t max_files, std::size_t max_size, std::size_t max_files,
const std::size_t flush_inverval=0): std::size_t flush_inverval=0):
_base_filename(base_filename), _base_filename(base_filename),
_extension(extension), _extension(extension),
_max_size(max_size), _max_size(max_size),
@ -104,7 +104,7 @@ private:
std::remove(target.c_str()); std::remove(target.c_str());
if (details::file_helper::file_exists(src) && std::rename(src.c_str(), target.c_str())) if (details::file_helper::file_exists(src) && std::rename(src.c_str(), target.c_str()))
{ {
throw fflog_exception("rotating_file_sink: failed renaming " + src + " to " + target); throw spdlog_ex("rotating_file_sink: failed renaming " + src + " to " + target);
} }
} }
auto cur_name = _file_helper.filename(); auto cur_name = _file_helper.filename();

View File

@ -29,47 +29,57 @@ namespace spdlog
std::shared_ptr<logger> get(const std::string& name); std::shared_ptr<logger> get(const std::string& name);
// Example:
// auto logger = spdlog::create("mylog", {sink1, sink2});
std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks);
// Example (create a logger with daily rotating file):
// using namespace spdlog::sinks;
// spdlog::create<daily_file_sink_st>("mylog", "dailylog_filename", "txt");
template <typename Sink, typename... Args>
std::shared_ptr<spdlog::logger> create(const std::string& logger_name, const Args&... args);
// Example:
// using namespace spdlog::sinks;
// std::vector<spdlog::sink_ptr> mySinks;
// mySinks.push_back(std::make_shared<rotating_file_sink_mt>("filename", "txt", 1024 * 1024 * 5, 10));
// mySinks.push_back(std::make_shared<stdout_sink_mt>());
// spdlog::create("mylog", mySinks.begin(), mySinks.end());
template<class It>
std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end);
// Set global formatting // Set global formatting
// Example:
// spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %t"); // spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %t");
void set_pattern(const std::string& format_string); void set_pattern(const std::string& format_string);
// Set global formatter object //Set global logging level
void set_formatter(formatter_ptr f);
//Set global active logging level
void set_level(level::level_enum log_level); void set_level(level::level_enum log_level);
// Create multi/single threaded rotating file logger
std::shared_ptr<logger> rotating_logger_mt(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, size_t flush_inverval = 0);
std::shared_ptr<logger> rotating_logger_st(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, size_t flush_inverval = 0);
// Create file logger which creates new file at midnight):
std::shared_ptr<logger> daily_logger_mt(const std::string& logger_name, const std::string& filename, size_t flush_inverval = 0);
std::shared_ptr<logger> daily_logger_st(const std::string& logger_name, const std::string& filename, size_t flush_inverval = 0);
// Create stdout/stderr loggers
std::shared_ptr<logger> stdout_logger_mt(const std::string& logger_name);
std::shared_ptr<logger> stdout_logger_st(const std::string& logger_name);
std::shared_ptr<logger> stderr_logger_mt(const std::string& logger_name);
std::shared_ptr<logger> stderr_logger_st(const std::string& logger_name);
//
// Create a logger with multiple sinks
//
std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks);
template<class It>
std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end);
// Create a logger with templated sink type
// Example: spdlog::create<daily_file_sink_st>("mylog", "dailylog_filename", "txt");
template <typename Sink, typename... Args>
std::shared_ptr<spdlog::logger> create(const std::string& logger_name, const Args&... args);
// Set global formatter object
void set_formatter(formatter_ptr f);
//Stop all loggers //Stop all loggers
void stop(); void stop();
// //
// Trace macro to turn on/off at compile time // Trace macro enabled only at debug compile
// Example: SPDLOG_TRACE(my_logger, "Some trace message"); // Example: SPDLOG_TRACE(my_logger, "Some trace message");
// //
#ifdef _DEBUG #ifdef _DEBUG