Name Date Size

..19-Aug-20204 KiB

.clang-formatH A D19-Aug-20203 KiB

.gitignoreH A D19-Aug-2020784

.travis.ymlH A D19-Aug-20202.6 KiB

appveyor.ymlH A D19-Aug-2020763

bench/H19-Aug-20204 KiB

cmake/H19-Aug-20204 KiB

CMakeLists.txtH A D19-Aug-20204 KiB

example/H19-Aug-20204 KiB

format.shH A D19-Aug-2020275

include/spdlog/H19-Aug-20204 KiB

INSTALLH A D19-Aug-2020323

LICENSEH A D19-Aug-20201.1 KiB

README.mdH A D19-Aug-20209.5 KiB

tests/H19-Aug-20204 KiB

README.md

1# spdlog
2
3Very fast, header only, C++ logging library. [![Build Status](https://travis-ci.org/gabime/spdlog.svg?branch=master)](https://travis-ci.org/gabime/spdlog)  [![Build status](https://ci.appveyor.com/api/projects/status/d2jnxclg20vd0o50?svg=true)](https://ci.appveyor.com/project/gabime/spdlog)
4
5
6## Install
7#### Just copy the headers:
8
9* Copy the source [folder](https://github.com/gabime/spdlog/tree/v1.x/include/spdlog) to your build tree and use a C++11 compiler.
10
11#### Or use your favorite package manager:
12
13* Ubuntu: `apt-get install libspdlog-dev`
14* Homebrew: `brew install spdlog`
15* FreeBSD:  `cd /usr/ports/devel/spdlog/ && make install clean`
16* Fedora: `yum install spdlog`
17* Gentoo: `emerge dev-libs/spdlog`
18* Arch Linux: `yaourt -S spdlog-git`
19* vcpkg: `vcpkg install spdlog`
20 
21
22## Platforms
23 * Linux, FreeBSD, Solaris, AIX
24 * Windows (vc 2013+, cygwin)
25 * Mac OSX (clang 3.5+)
26 * Android
27
28## Features
29* Very fast - performance is the primary goal (see [benchmarks](#benchmarks) below).
30* Headers only, just copy and use.
31* Feature rich using the excellent [fmt](https://github.com/fmtlib/fmt) library.
32* Fast asynchronous mode (optional)
33* [Custom](https://github.com/gabime/spdlog/wiki/3.-Custom-formatting) formatting.
34* Conditional Logging
35* Multi/Single threaded loggers.
36* Various log targets:
37    * Rotating log files.
38    * Daily log files.
39    * Console logging (colors supported).
40    * syslog.
41    * Windows debugger (```OutputDebugString(..)```)
42    * Easily extendable with custom log targets  (just implement a single function in the [sink](include/spdlog/sinks/sink.h) interface).
43* Severity based filtering - threshold levels can be modified in runtime as well as in compile time.
44
45
46
47## Benchmarks
48
49Below are some [benchmarks](https://github.com/gabime/spdlog/blob/v1.x/bench/bench.cpp) done in Ubuntu 64 bit, Intel i7-4770 CPU @ 3.40GHz
50
51#### Synchronous mode
52```
53*******************************************************************************
54Single thread, 1,000,000 iterations
55*******************************************************************************
56basic_st...		    Elapsed: 0.226664	4,411,806/sec
57rotating_st...	 	    Elapsed: 0.214339	4,665,499/sec
58daily_st...		    Elapsed: 0.211292	4,732,797/sec
59null_st...		    Elapsed: 0.102815	9,726,227/sec
60
61*******************************************************************************
6210 threads sharing same logger, 1,000,000 iterations
63*******************************************************************************
64basic_mt...		    Elapsed: 0.882268	1,133,441/sec
65rotating_mt...              Elapsed: 0.875515	1,142,184/sec
66daily_mt...		    Elapsed: 0.879573	1,136,915/sec
67null_mt...		    Elapsed: 0.220114	4,543,105/sec
68``` 
69#### Asynchronous mode
70```
71*******************************************************************************
7210 threads sharing same logger, 1,000,000 iterations 
73*******************************************************************************
74async...		Elapsed: 0.429088	2,330,524/sec
75async...		Elapsed: 0.411501	2,430,126/sec
76async...		Elapsed: 0.428979	2,331,116/sec
77
78```
79
80## Usage samples
81
82```c++
83#include "spdlog/spdlog.h"
84#include "spdlog/sinks/stdout_color_sinks.h"
85void stdout_example()
86{
87    // create color multi threaded logger
88    auto console = spdlog::stdout_color_mt("console");
89    console->info("Welcome to spdlog!");
90    console->error("Some error message with arg: {}", 1);
91
92    auto err_logger = spdlog::stderr_color_mt("stderr");
93    err_logger->error("Some error message");
94
95    // Formatting examples
96    console->warn("Easy padding in numbers like {:08d}", 12);
97    console->critical("Support for int: {0:d};  hex: {0:x};  oct: {0:o}; bin: {0:b}", 42);
98    console->info("Support for floats {:03.2f}", 1.23456);
99    console->info("Positional args are {1} {0}..", "too", "supported");
100    console->info("{:<30}", "left aligned");
101
102    spdlog::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name)");
103
104    // Runtime log levels
105    spdlog::set_level(spdlog::level::info); // Set global log level to info
106    console->debug("This message should not be displayed!");
107    console->set_level(spdlog::level::trace); // Set specific logger's log level
108    console->debug("This message should be displayed..");
109
110    // Customize msg format for all loggers
111    spdlog::set_pattern("[%H:%M:%S %z] [%n] [%^---%L---%$] [thread %t] %v");
112    console->info("This an info message with custom format");
113
114    // Compile time log levels
115    // define SPDLOG_DEBUG_ON or SPDLOG_TRACE_ON
116    SPDLOG_TRACE(console, "Enabled only #ifdef SPDLOG_TRACE_ON..{} ,{}", 1, 3.23);
117    SPDLOG_DEBUG(console, "Enabled only #ifdef SPDLOG_DEBUG_ON.. {} ,{}", 1, 3.23);
118}
119```
120---
121#### Basic file logger
122```c++
123#include "spdlog/sinks/basic_file_sink.h"
124void basic_logfile_example()
125{
126    try 
127    {
128        auto my_logger = spdlog::basic_logger_mt("basic_logger", "logs/basic-log.txt");
129    }
130    catch (const spdlog::spdlog_ex &ex)
131    {
132        std::cout << "Log init failed: " << ex.what() << std::endl;
133        return 1;
134    }
135}
136```
137---
138#### Rotating files
139```c++
140#include "spdlog/sinks/rotating_file_sink.h"
141void rotating_example()
142{
143    // Create a file rotating logger with 5mb size max and 3 rotated files
144    auto rotating_logger = spdlog::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3);
145}
146```
147
148---
149#### Daily files
150```c++
151
152#include "spdlog/sinks/daily_file_sink.h"
153void daily_example()
154{
155    // Create a daily logger - a new file is created every day on 2:30am
156    auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
157}
158
159```
160
161---
162#### Periodic flush
163```c++
164// periodically flush all *registered* loggers every 3 seconds:
165// warning: only use if all your loggers are thread safe!
166spdlog::flush_every(std::chrono::seconds(3));
167
168```
169
170
171---
172#### Logger with multi sinks - each with different format and log level
173```c++
174
175// create logger with 2 targets with different log levels and formats.
176// the console will show only warnings or errors, while the file will log all.
177void multi_sink_example()
178{
179    auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
180    console_sink->set_level(spdlog::level::warn);
181    console_sink->set_pattern("[multi_sink_example] [%^%l%$] %v");
182
183    auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/multisink.txt", true);
184    file_sink->set_level(spdlog::level::trace);
185
186    spdlog::logger logger("multi_sink", {console_sink, file_sink});
187    logger.set_level(spdlog::level::debug);
188    logger.warn("this should appear in both console and file");
189    logger.info("this message should not appear in the console, only in the file");
190}
191```
192
193---
194#### Asynchronous logging
195```c++
196#include "spdlog/async.h"
197#include "spdlog/sinks/basic_file_sink.h"
198void async_example()
199{
200    // default thread pool settings can be modified *before* creating the async logger:
201    // spdlog::init_thread_pool(8192, 1); // queue with 8k items and 1 backing thread.
202    auto async_file = spdlog::basic_logger_mt<spdlog::async_factory>("async_file_logger", "logs/async_log.txt");
203    // alternatively:
204    // auto async_file = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("async_file_logger", "logs/async_log.txt");   
205}
206
207```
208
209---
210#### Asynchronous logger with multi sinks  
211```c++
212#include "spdlog/sinks/stdout_color_sinks.h"
213#include "spdlog/sinks/rotating_file_sink.h"
214
215void multi_sink_example2()
216{
217    spdlog::init_thread_pool(8192, 1);
218    auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt >();
219    auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("mylog.txt", 1024*1024*10, 3);
220    std::vector<spdlog::sink_ptr> sinks {stdout_sink, rotating_sink};
221    auto logger = std::make_shared<spdlog::async_logger>("loggername", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
222    spdlog::register_logger(logger);
223}
224```
225 
226---
227#### User defined types
228```c++
229// user defined types logging by implementing operator<<
230#include "spdlog/fmt/ostr.h" // must be included
231struct my_type
232{
233    int i;
234    template<typename OStream>
235    friend OStream &operator<<(OStream &os, const my_type &c)
236    {
237        return os << "[my_type i=" << c.i << "]";
238    }
239};
240
241void user_defined_example()
242{
243    spdlog::get("console")->info("user defined type: {}", my_type{14});
244}
245
246```
247---
248#### Custom error handler
249```c++
250void err_handler_example()
251{
252    // can be set globally or per logger(logger->set_error_handler(..))
253    spdlog::set_error_handler([](const std::string &msg) { spdlog::get("console")->error("*** LOGGER ERROR ***: {}", msg); });
254    spdlog::get("console")->info("some invalid message to trigger an error {}{}{}{}", 3);
255}
256
257```
258---
259#### syslog 
260```c++
261#include "spdlog/sinks/syslog_sink.h"
262void syslog_example()
263{
264    std::string ident = "spdlog-example";
265    auto syslog_logger = spdlog::syslog_logger_mt("syslog", ident, LOG_PID);
266    syslog_logger->warn("This is warning that will end up in syslog.");
267}
268```
269---
270#### Android example 
271```c++
272#incude "spdlog/sinks/android_sink.h"
273void android_example()
274{
275    std::string tag = "spdlog-android";
276    auto android_logger = spdlog::android_logger("android", tag);
277    android_logger->critical("Use \"adb shell logcat\" to view this message.");
278}
279```
280
281## Documentation
282Documentation can be found in the [wiki](https://github.com/gabime/spdlog/wiki/1.-QuickStart) pages.
283