Compare commits

...

107 Commits

Author SHA1 Message Date
gabime
93d84e5d59 v0.12.0 2017-02-17 16:40:59 +02:00
Gabi Melman
311937815e Merge pull request #370 from jcelerier/fix/remove_static_global_strings
Replace static global std::string arrays by Meyer singletons.
2017-02-16 15:39:15 +02:00
Jean-Michaël Celerier
ef665e959f Don't deduce return types for days / months
Required for C++11 support
2017-02-16 13:17:13 +01:00
Gabi Melman
20f4428696 Update README.md 2017-02-16 02:12:50 +02:00
Gabi Melman
b65c7bad9f Update README.md 2017-02-16 02:10:18 +02:00
Jean-Michaël Celerier
06b8193a14 Add missing consts 2017-02-15 14:41:57 +01:00
Jean-Michaël Celerier
a626ebbbec Replace static global std::string arrays by Meyer singletons. This improves thread-safety. 2017-02-15 14:31:51 +01:00
Gabi Melman
054eb555ca Moved SPDLOG_VERSION macro to spdlog.h 2017-02-03 15:28:32 +02:00
Gabi Melman
4f50c5d143 Update spdlog.h 2017-02-03 15:27:06 +02:00
Gabi Melman
7481a8ecf7 Update common.h 2017-02-03 15:26:47 +02:00
Gabi Melman
a76a5cfc9c Update README.md 2017-02-03 15:19:23 +02:00
Gabi Melman
904bed92c3 Added SPDLOG_VERSION macro to common.h 2017-02-03 15:11:08 +02:00
Gabi Melman
8650c15749 Updated install options section 2017-02-03 14:44:03 +02:00
Gabi Melman
35865ee54e Merge pull request #348 from w0land/cmake_prefix
Add prefix for BUILD_TESTING cmake option
2017-01-11 11:29:15 +02:00
Bartosz Taczała
50c181ea4b Add prefix for BUILD_TESTING cmake option
This is helpful when using spdlog as a dependency (git submodule) when a
master project is not interested in spdlog tests. Using
"BUILD_TESTING" name may create a confusion.
Extra: BUILD_EXAMPLE variable already have a prefix.
2017-01-11 09:32:55 +01:00
Gabi Melman
2ec188041f Update README.md 2017-01-06 12:39:12 +02:00
Gabi Melman
e7ec922c0a Update async_log_helper.h
removed empty lines
2017-01-06 12:32:25 +02:00
Gabi Melman
38456118d0 Update null_sink.h 2016-12-31 22:38:00 +02:00
gabime
0a3a3f0ee2 Updated comment on thread safety 2016-12-31 17:54:37 +02:00
Gabi Melman
58853d9c95 Update README.md 2016-12-30 12:16:44 +02:00
Gabi Melman
5ee14906c5 Update README.md 2016-12-30 12:16:09 +02:00
gabime
2d873785a5 astyle 2016-12-10 02:05:05 +02:00
gabime
fec467da7b extra blank line between comments 2016-12-10 02:04:20 +02:00
gabime
b5f34c5320 typo in comment 2016-12-10 02:03:13 +02:00
gabime
92db8115b7 option to prevent child processes from inheriting log file desciptors (#define SPDLOG_PREVENT_CHILD_FD) 2016-12-10 01:43:43 +02:00
Gabi Melman
af35f9c086 Merge pull request #330 from PMExtra/master
Use the feature checking macros to detect thread_local support in clang.
2016-12-09 21:47:58 +02:00
Gabi Melman
0e016882d9 Update os.h 2016-12-06 19:47:33 +02:00
PM_Extra
b1a55ca3a4 Use the feature checking macros to detect thread_local support in clang. 2016-12-04 13:42:52 +08:00
Gabi Melman
68cc3e6856 Merge pull request #329 from PMExtra/master
fixed compilation error in clang before version 8
2016-12-02 20:24:12 +02:00
PM_Extra
f7574eb4c7 fixed compilation error in clang before version 8 (does not support thread_local keyword) 2016-12-03 01:40:52 +08:00
gabime
33494049a8 fixed compilation error in 2013 (does not support thread_local keyword) 2016-12-02 17:48:10 +02:00
gabime
5d23e88c16 astyle 2016-12-02 17:40:40 +02:00
gabime
6d8efa8d7f store thread_id in tls 2016-12-02 17:33:49 +02:00
gabime
343904b56d add missing include to widnows _getpid 2016-12-02 17:12:24 +02:00
gabime
cf73f02eaf pid support (added the %P option to formatter) 2016-12-02 17:09:00 +02:00
Gabi Melman
cda27d2bff Merge pull request #322 from onnodb/FixIssue321
Fix compilation errors in "wincolor_sink.h"
2016-11-25 17:13:55 +02:00
Onno Broekmans
b61be7320a Fix compilation errors in "wincolor_sink.h" 2016-11-25 15:47:21 +01:00
Gabi Melman
b2ce64625d Merge pull request #319 from jktjkt/cmake
cmake: misc improvements
2016-11-22 13:39:32 +02:00
Jan Kundrát
f058d3aa74 cmake: use -Wall on GCC and Clang
These checks come from [1]. The `MATCHES` operator is used for clang
because of Apple's special string.

[1] http://stackoverflow.com/questions/10046114/in-cmake-how-can-i-test-if-the-compiler-is-clang/10055571#10055571
2016-11-22 10:31:01 +01:00
Jan Kundrát
1c31800210 cmake: Use a standard option for controlling the tests
As per the docs [1], there's a standard variable for this purpose. This
introduces a behavior change, the tests are now being built by default.

[1] https://cmake.org/cmake/help/v3.0/module/CTest.html
2016-11-22 10:20:13 +01:00
Jan Kundrát
61cdd170fd cmake: List spdlog's content in IDEs
This is a usual CMake way of ensuring that IDEs have a way of showing
all source files which comprise this header-only library. It works in
the Qt Creator, for example.
2016-11-22 10:10:52 +01:00
gabime
d6b34d7b5c Updated to fmt version 796beaaddb5226162fe00c2c55e322d80d26f3d8 2016-11-19 23:43:39 +02:00
gabime
bd6fe569b5 astyle previous commits 2016-11-18 17:17:09 +02:00
gabime
d142f13551 Updated fmto to version def687462c32ec40757e49eb6069f109d50236d6 2016-11-18 17:13:53 +02:00
gabime
e12916c070 Fixed issue #317 (Build error on raspberry pi) 2016-11-18 16:58:57 +02:00
gabime
f6cece206a Merge 2016-11-18 16:58:30 +02:00
gabime
817371b225 Fix issue #317 (Build error on raspberry pi) 2016-11-18 16:56:25 +02:00
Gabi Melman
9eee823041 Fix issue #315 2016-11-14 14:58:10 +02:00
Gabi Melman
4e768c146b Merge pull request #314 from osx2000/master
Compatibility with Oracle Developer Studio 12.5 on  Solaris
2016-11-11 15:38:32 +02:00
osx2000
3cd497ee95 extended conditional compilation to __SUNPRO_CC 2016-11-11 14:28:45 +01:00
osx2000
e9fc4ac095 Fully qualified std::this_thread::yield() 2016-11-11 14:27:07 +01:00
Gabi Melman
9ccb6af2bd Merge pull request #305 from chenhayat/vs2013_fix
Fix Klockwork compilation warning
2016-11-03 16:30:25 +02:00
Chen Hayat
5259b3dbf4 Fix Klockwork compilation warning 2016-11-03 14:19:02 +02:00
Gabi Melman
8c67d6e05e Merge pull request #304 from chenhayat/vs2013_fix
Fix compilation error C2664  on VS2013
2016-11-02 15:59:34 +02:00
Chen Hayat
b4cb1febf2 removed external library changes 2016-11-02 15:43:30 +02:00
Chen Hayat
0c16b9ae1e Remove casting from previous commit and fix the following Klockwork issues:
1. Removing "return" from void functions.
2. Using "const" for operator= argument.
2016-11-01 17:16:07 +02:00
Chen Hayat
83d192b1f1 Fix compilation error C2664 on VS2013
No converting constructor
2016-10-30 17:11:45 +02:00
gabime
87ddb9a6c1 astyle 2016-10-20 12:14:25 +03:00
gabime
6128a87da2 Fix issue #300 2016-10-20 12:11:31 +03:00
Gabi Melman
698783861c Fixed vc warning on x64 build 2016-10-17 12:50:38 +03:00
gabime
f14d1c002b astyle 2016-10-12 23:08:44 +03:00
gabime
0cfdad4d0b Windows console color support. Replaced color param in API with new functions 2016-10-12 15:10:10 +03:00
Gabi Melman
94dbefe9b6 Merge pull request #296 from guoxiao/fix
Fix several compiler warnings
2016-10-09 11:39:49 +03:00
Guo Xiao
73e53c7cb6 Remove extra ';' 2016-10-09 15:36:19 +08:00
Guo Xiao
9b218d4d12 Convert off_t to size_t 2016-10-09 15:36:19 +08:00
Gabi Melman
541dd88a97 Update common.h 2016-10-09 01:55:47 +03:00
Gabi Melman
ca928bc1b0 Fix issue #295
vs 2013 not supports std::atomic_int
2016-10-09 00:41:29 +03:00
Gabi Melman
b642482432 removed empty line 2016-10-08 20:58:29 +03:00
gabime
703a1d9736 added g3log crush example 2016-10-01 18:15:21 +03:00
gabime
6ce507eceb bench: added latency comparison with g3log 2016-10-01 16:55:13 +03:00
gabime
56678a5f6a added set_force_flush(bool) to simple file sink for performance benchmarks 2016-10-01 16:37:33 +03:00
gabime
12800ac466 fixed shadow warnings in gcc 2016-09-30 14:08:31 +03:00
gabime
faa184ce24 Added #ifdef __ANDROID__ to spllog_impl.h 2016-09-29 23:49:03 +03:00
Gabi Melman
fa175d6300 Merge pull request #289 from thomas-frantz/expose_sinks
Exposed logger sinks.
2016-09-25 03:47:36 +03:00
Therenall
66b08294ca Exposed logger sinks. 2016-09-24 15:14:05 -04:00
Gabi Melman
9e37f5cef9 Merge pull request #285 from azadkuh/issue280
update os.h to fix filesize() on older win32
2016-09-20 13:14:44 +03:00
amir zamani
811eeef7a6 update os.h to fix filesize() on older win32
_fstat() always fails under older 32bit WinXP/Win2003 targets.

_filelength() just works for both WinXP SDK and later Win7+ 32bit targets.
2016-09-20 14:13:15 +04:30
gabime
1f1f6a5f3b Support bench under OSX 2016-09-18 02:28:41 +03:00
gabime
214c67788f add async with null sink bench 2016-09-18 01:51:53 +03:00
gabime
e97621d61d Removed force_flush arg from everywhere. Use flush_on(level) instead 2016-09-18 00:43:42 +03:00
gabime
b0f8230783 Fixed bug in basic_logger creating (passed truncate to force_flush arg) 2016-09-18 00:14:56 +03:00
Gabi Melman
9c04504881 Update README.md 2016-09-16 02:58:02 +03:00
gabime
5653e5c9d7 astyle 2016-09-15 00:38:21 +03:00
gabime
6312748cc7 updated bundled fmt to version 1fb0586b065c4202e976528a6bdc6384dc56dc04 2016-09-15 00:35:51 +03:00
Gabi Melman
87d79eb95d Merge pull request #277 from hghwng/master
Fix compilation on Android.
2016-09-14 17:55:43 +03:00
Hugh Wang
96267654e6 Add Android examples. 2016-09-14 17:39:15 +08:00
Hugh Wang
72a6fd65da Support direct creation of android logger. 2016-09-14 17:37:06 +08:00
Hugh Wang
bf02f57475 Fix Android sink.
1. Remove lock.
2. Improve error detection.
3. Remove unsupported log levels.
2016-09-14 17:37:06 +08:00
Hugh Wang
f2c9692438 Fix compilation on Android. 2016-09-14 17:37:06 +08:00
Gabi Melman
59bf37f769 Merge pull request #279 from facontidavide/loglevel_per_sink
Loglevel per sink. Thanks @facontidavide
2016-09-13 00:04:59 +03:00
davide
d79af47a28 added example 2016-09-12 22:28:37 +02:00
davide
b67076fc85 added level_t to sink. Improves the flexibility of loggers with multiple sinks 2016-09-12 22:26:38 +02:00
Gabi Melman
c69df8ae44 Update spdlog.h 2016-09-03 14:15:09 +03:00
Gabi Melman
b18d235b63 Update spdlog.h 2016-09-03 14:08:55 +03:00
Gabi Melman
01ef3d3114 Update async_logger_impl.h 2016-09-03 01:35:10 +03:00
Gabi Melman
d01e288afc fix flush async flush 2016-09-03 01:33:07 +03:00
gabime
3ee1bab316 added std:: to vector decl in bench 2016-09-02 17:24:40 +03:00
gabime
c3757c99d5 remoed zf_log from bench 2016-09-02 17:23:40 +03:00
gabime
2d64a1de1c fixed bench compilation 2016-09-02 17:16:02 +03:00
gabime
0d35df584c removed zf_log from bench 2016-09-02 17:14:48 +03:00
gabime
3afabcd17e astyle 2016-09-02 17:06:00 +03:00
gabime
d2a367fa7c Fixed tests after remove of force_flush arg 2016-09-02 16:59:55 +03:00
gabime
e562e001cf Removed force_flush arg from API (use flush_on(level) instead) 2016-09-02 16:59:15 +03:00
gabime
1df30a0733 support flush_on(..) in async loggers too 2016-09-02 16:19:29 +03:00
gabime
3a12f3c560 fix typo in example 2016-09-02 15:00:51 +03:00
Gabi Melman
f310cc460f Update README.md 2016-08-26 15:39:00 +03:00
Gabi Melman
2fa29987ca Update README.md 2016-08-26 14:16:36 +03:00
51 changed files with 2633 additions and 1494 deletions

2
.gitignore vendored
View File

@@ -45,6 +45,8 @@ example/*
!example/example.sln !example/example.sln
!example/example.vcxproj !example/example.vcxproj
!example/CMakeLists.txt !example/CMakeLists.txt
!example/multisink.cpp
!example/jni
# generated files # generated files
generated generated

View File

@@ -5,14 +5,19 @@
cmake_minimum_required(VERSION 3.1) cmake_minimum_required(VERSION 3.1)
project(spdlog VERSION 1.0.0) project(spdlog VERSION 1.0.0)
include(CTest)
set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
set(CMAKE_CXX_FLAGS "-Wall ${CMAKE_CXX_FLAGS}")
endif()
add_library(spdlog INTERFACE) add_library(spdlog INTERFACE)
option(SPDLOG_BUILD_EXAMPLES "Build examples" OFF) option(SPDLOG_BUILD_EXAMPLES "Build examples" OFF)
option(SPDLOG_BUILD_TESTS "Build tests" OFF) option(SPDLOG_BUILD_TESTING "Build spdlog tests" ON)
target_include_directories( target_include_directories(
spdlog spdlog
@@ -23,12 +28,11 @@ target_include_directories(
set(HEADER_BASE "${CMAKE_CURRENT_SOURCE_DIR}/include") set(HEADER_BASE "${CMAKE_CURRENT_SOURCE_DIR}/include")
include(CTest)
if(SPDLOG_BUILD_EXAMPLES) if(SPDLOG_BUILD_EXAMPLES)
add_subdirectory(example) add_subdirectory(example)
endif() endif()
if(SPDLOG_BUILD_TESTS) if(SPDLOG_BUILD_TESTING)
add_subdirectory(tests) add_subdirectory(tests)
endif() endif()
@@ -78,3 +82,6 @@ install(
NAMESPACE "${namespace}" NAMESPACE "${namespace}"
DESTINATION "${config_install_dir}" DESTINATION "${config_install_dir}"
) )
file(GLOB_RECURSE spdlog_include_SRCS "${HEADER_BASE}/*.h")
add_custom_target(spdlog_headers_for_ide SOURCES ${spdlog_include_SRCS})

View File

@@ -4,13 +4,25 @@ Very fast, header only, C++ logging library. [![Build Status](https://travis-ci.
## Install ## Install
Just copy the source [folder](https://github.com/gabime/spdlog/tree/master/include/spdlog) to your build tree and use a C++11 compiler #### Just copy or clone the headers:
* Copy the source [folder](https://github.com/gabime/spdlog/tree/master/include/spdlog) to your build tree and use a C++11 compiler.
#### Or use your favourite package manager:
* Ubuntu: `apt-get install libspdlog-dev`
* Homebrew: `brew install spdlog`
* FreeBSD: `cd /usr/ports/devel/spdlog/ && make install clean`
* Fedora: `yum install spdlog`
* Arch Linux: `pacman -S spdlog-git`
* vcpkg: `vcpkg install spdlog`
## Platforms ## Platforms
* Linux (gcc 4.8.1+, clang 3.5+) * Linux, FreeBSD, Solaris
* Windows (visual studio 2013+, cygwin/mingw with g++ 4.9.1+) * Windows (vc 2013+, cygwin/mingw)
* Mac OSX (clang 3.5+) * Mac OSX (clang 3.5+)
* Solaris (gcc 5.2.0+) * Android
##Features ##Features
* Very fast - performance is the primary goal (see [benchmarks](#benchmarks) below). * Very fast - performance is the primary goal (see [benchmarks](#benchmarks) below).
@@ -24,6 +36,7 @@ Just copy the source [folder](https://github.com/gabime/spdlog/tree/master/inclu
* Daily log files. * Daily log files.
* Console logging (colors supported). * Console logging (colors supported).
* syslog. * syslog.
* Windows debugger (```OutputDebugString(..)```)
* Easily extendable with custom log targets (just implement a single function in the [sink](include/spdlog/sinks/sink.h) interface). * Easily extendable with custom log targets (just implement a single function in the [sink](include/spdlog/sinks/sink.h) interface).
* Severity based filtering - threshold levels can be modified in runtime as well as in compile time. * Severity based filtering - threshold levels can be modified in runtime as well as in compile time.
@@ -57,13 +70,7 @@ Time needed to log 1,000,000 lines in asynchronous mode, i.e. the time it takes
## Usage Example ## Usage Example
```c++ ```c++
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
//
// spdlog usage example
//
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include <iostream> #include <iostream>
@@ -79,34 +86,25 @@ int main(int, char*[])
{ {
try try
{ {
// Multithreaded color console // Console logger with color
auto console = spd::stdout_logger_mt("console", true); auto console = spd::stdout_color_mt("console");
console->info("Welcome to spdlog!"); console->info("Welcome to spdlog!");
console->error("An info message example {}..", 1); console->error("Some error message with arg{}..", 1);
// 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");
console->info("{:>30}", "right aligned");
console->info("{:^30}", "centered");
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");
// Runtime log levels
spd::set_level(spd::level::info); //Set global log level to info
console->debug("This message shold not be displayed!");
console->set_level(spd::level::debug); // Set specific logger's log level
console->debug("This message shold be displayed..");
// Create basic file logger (not rotated) // Create basic file logger (not rotated)
auto my_logger = spd::basic_logger_mt("basic_logger", "logs/basic.txt"); auto my_logger = spd::basic_logger_mt("basic_logger", "logs/basic.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/mylogfile", 1048576 * 5, 3); auto rotating_logger = spd::rotating_logger_mt("some_logger_name", "logs/mylogfile", 1048576 * 5, 3);
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
@@ -114,14 +112,23 @@ int main(int, char*[])
// 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", 2, 30); auto daily_logger = spd::daily_logger_mt("daily_logger", "logs/daily", 2, 30);
// trigger flush if the log severity is error or higher
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 ***"); spd::set_pattern("*** [%H:%M:%S %z] [thread %t] %v ***");
rotating_logger->info("This is another message with custom format"); rotating_logger->info("This is another message with custom format");
// Compile time debug or trace macros.
// Enabled #ifdef SPDLOG_DEBUG_ON or #ifdef SPDLOG_TRACE_ON // Runtime log levels
spd::set_level(spd::level::info); //Set global log level to info
console->debug("This message shold not be displayed!");
console->set_level(spd::level::debug); // Set specific logger's log level
console->debug("This message shold be displayed..");
// Compile time log levels
// 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);
@@ -129,17 +136,23 @@ 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. linux/osx only
syslog_example(); syslog_example();
// Log user-defined types example.. // android example. compile with NDK
android_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();

View File

@@ -1,9 +1,9 @@
CXX ?= g++ CXX ?= g++
CXXFLAGS = -march=native -Wall -Wextra -pedantic -std=c++11 -pthread -Wl,--no-as-needed -I../include CXXFLAGS = -march=native -Wall -Wextra -pedantic -std=c++11 -pthread -I../include
CXX_RELEASE_FLAGS = -O3 -flto -DNDEBUG CXX_RELEASE_FLAGS = -O3 -flto -DNDEBUG
binaries=spdlog-bench spdlog-bench-mt spdlog-async zf_log-bench zf_log-bench-mt boost-bench boost-bench-mt glog-bench glog-bench-mt g2log-async easylogging-bench easylogging-bench-mt binaries=spdlog-bench spdlog-bench-mt spdlog-async spdlog-null-async boost-bench boost-bench-mt glog-bench glog-bench-mt g2log-async easylogging-bench easylogging-bench-mt
all: $(binaries) all: $(binaries)
@@ -17,12 +17,9 @@ spdlog-async: spdlog-async.cpp
$(CXX) spdlog-async.cpp -o spdlog-async $(CXXFLAGS) $(CXX_RELEASE_FLAGS) $(CXX) spdlog-async.cpp -o spdlog-async $(CXXFLAGS) $(CXX_RELEASE_FLAGS)
ZF_LOG_FLAGS = -I../../zf_log.git/zf_log/ spdlog-null-async: spdlog-null-async.cpp
zf_log-bench: zf_log-bench.cpp $(CXX) spdlog-null-async.cpp -o spdlog-null-async $(CXXFLAGS) $(CXX_RELEASE_FLAGS)
$(CXX) zf_log-bench.cpp -o zf_log-bench $(CXXFLAGS) $(CXX_RELEASE_FLAGS) $(ZF_LOG_FLAGS)
zf_log-bench-mt: zf_log-bench-mt.cpp
$(CXX) zf_log-bench-mt.cpp -o zf_log-bench-mt $(CXXFLAGS) $(CXX_RELEASE_FLAGS) $(ZF_LOG_FLAGS)
BOOST_FLAGS = -DBOOST_LOG_DYN_LINK -I/usr/include -lboost_log -lboost_log_setup -lboost_filesystem -lboost_system -lboost_thread -lboost_regex -lboost_date_time -lboost_chrono BOOST_FLAGS = -DBOOST_LOG_DYN_LINK -I/usr/include -lboost_log -lboost_log_setup -lboost_filesystem -lboost_system -lboost_thread -lboost_regex -lboost_date_time -lboost_chrono

View File

@@ -2,7 +2,6 @@
// Copyright(c) 2015 Gabi Melman. // Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT) // Distributed under the MIT License (http://opensource.org/licenses/MIT)
// //
#include <boost/log/core.hpp> #include <boost/log/core.hpp>
#include <boost/log/trivial.hpp> #include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp> #include <boost/log/expressions.hpp>

32
bench/latency/Makefile Normal file
View File

@@ -0,0 +1,32 @@
CXX ?= g++
CXXFLAGS = -march=native -Wall -std=c++11 -pthread
CXX_RELEASE_FLAGS = -O2 -DNDEBUG
binaries=spdlog-latency g3log-latency g3log-crush
all: $(binaries)
spdlog-latency: spdlog-latency.cpp
$(CXX) spdlog-latency.cpp -o spdlog-latency $(CXXFLAGS) $(CXX_RELEASE_FLAGS) -I../../include
g3log-latency: g3log-latency.cpp
$(CXX) g3log-latency.cpp -o g3log-latency $(CXXFLAGS) $(CXX_RELEASE_FLAGS) -I../../../g3log/src -L. -lg3logger
g3log-crush: g3log-crush.cpp
$(CXX) g3log-crush.cpp -o g3log-crush $(CXXFLAGS) $(CXX_RELEASE_FLAGS) -I../../../g3log/src -L. -lg3logger
.PHONY: clean
clean:
rm -f *.o *.log $(binaries)
rebuild: clean all

13
bench/latency/compare.sh Executable file
View File

@@ -0,0 +1,13 @@
#!/bin/bash
echo "running spdlog and g3log tests 10 time with ${1:-10} threads each (total 1,000,000 entries).."
rm -f *.log
for i in {1..10}
do
echo
sleep 0.5
./spdlog-latency ${1:-10} 2>/dev/null || exit
sleep 0.5
./g3log-latency ${1:-10} 2>/dev/null || exit
done

View File

@@ -0,0 +1,37 @@
#include <iostream>
#include <g3log/g3log.hpp>
#include <g3log/logworker.hpp>
void CrusherLoop()
{
size_t counter = 0;
while (true)
{
LOGF(INFO, "Some text to crush you machine. thread:");
if(++counter % 1000000 == 0)
{
std::cout << "Wrote " << counter << " entries" << std::endl;
}
}
}
int main(int argc, char** argv)
{
std::cout << "WARNING: This test will exaust all your machine memory and will crush it!" << std::endl;
std::cout << "Are you sure you want to continue ? " << std::endl;
char c;
std::cin >> c;
if (toupper( c ) != 'Y')
return 0;
auto worker = g3::LogWorker::createLogWorker();
auto handle= worker->addDefaultLogger(argv[0], "g3log.txt");
g3::initializeLogging(worker.get());
CrusherLoop();
return 0;
}

View File

@@ -0,0 +1,129 @@
#include <thread>
#include <vector>
#include <atomic>
#include <iostream>
#include <chrono>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <map>
#include <numeric>
#include <functional>
#include <thread>
#include "utils.h"
#include <g3log/g3log.hpp>
#include <g3log/logworker.hpp>
namespace
{
const uint64_t g_iterations = 1000000;
std::atomic<size_t> g_counter = {0};
void MeasurePeakDuringLogWrites(const size_t id, std::vector<uint64_t>& result)
{
while (true)
{
const size_t value_now = ++g_counter;
if (value_now > g_iterations)
{
return;
}
auto start_time = std::chrono::high_resolution_clock::now();
LOGF(INFO, "Some text to log for thread: %ld", id);
auto stop_time = std::chrono::high_resolution_clock::now();
uint64_t time_us = std::chrono::duration_cast<std::chrono::microseconds>(stop_time - start_time).count();
result.push_back(time_us);
}
}
void PrintResults(const std::map<size_t, std::vector<uint64_t>>& threads_result, size_t total_us)
{
std::vector<uint64_t> all_measurements;
all_measurements.reserve(g_iterations);
for (auto& t_result : threads_result)
{
all_measurements.insert(all_measurements.end(), t_result.second.begin(), t_result.second.end());
}
// calc worst latenct
auto worst = *std::max_element(all_measurements.begin(), all_measurements.end());
// calc avg
auto total = accumulate(begin(all_measurements), end(all_measurements), 0, std::plus<uint64_t>());
auto avg = double(total)/all_measurements.size();
std::cout << "[g3log] worst: " << std::setw(10) << std::right << worst << "\tAvg: " << avg << "\tTotal: " << utils::format(total_us) << " us" << std::endl;
}
}// anonymous
// The purpose of this test is NOT to see how fast
// each thread can possibly write. It is to see what
// the worst latency is for writing a log entry
//
// In the test 1 million log entries will be written
// an atomic counter is used to give each thread what
// it is to write next. The overhead of atomic
// synchronization between the threads are not counted in the worst case latency
int main(int argc, char** argv)
{
size_t number_of_threads {0};
if (argc == 2)
{
number_of_threads = atoi(argv[1]);
}
if (argc != 2 || number_of_threads == 0)
{
std::cerr << "USAGE is: " << argv[0] << " number_threads" << std::endl;
return 1;
}
std::vector<std::thread> threads(number_of_threads);
std::map<size_t, std::vector<uint64_t>> threads_result;
for (size_t idx = 0; idx < number_of_threads; ++idx)
{
// reserve to 1 million for all the result
// it's a test so let's not care about the wasted space
threads_result[idx].reserve(g_iterations);
}
const std::string g_path = "./" ;
const std::string g_prefix_log_name = "g3log-performance-";
const std::string g_measurement_dump = g_path + g_prefix_log_name + "_RESULT.txt";
auto worker = g3::LogWorker::createLogWorker();
auto handle= worker->addDefaultLogger(argv[0], "g3log.txt");
g3::initializeLogging(worker.get());
auto start_time_application_total = std::chrono::high_resolution_clock::now();
for (uint64_t idx = 0; idx < number_of_threads; ++idx)
{
threads[idx] = std::thread(MeasurePeakDuringLogWrites, idx, std::ref(threads_result[idx]));
}
for (size_t idx = 0; idx < number_of_threads; ++idx)
{
threads[idx].join();
}
auto stop_time_application_total = std::chrono::high_resolution_clock::now();
uint64_t total_time_in_us = std::chrono::duration_cast<std::chrono::microseconds>(stop_time_application_total - start_time_application_total).count();
PrintResults(threads_result, total_time_in_us);
return 0;
}

View File

@@ -0,0 +1,128 @@
#include <thread>
#include <vector>
#include <atomic>
#include <iostream>
#include <chrono>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <functional>
#include "utils.h"
#include <thread>
#include "spdlog/spdlog.h"
namespace spd = spdlog;
namespace
{
const uint64_t g_iterations = 1000000;
std::atomic<size_t> g_counter = {0};
void MeasurePeakDuringLogWrites(const size_t id, std::vector<uint64_t>& result)
{
auto logger = spd::get("file_logger");
while (true)
{
const size_t value_now = ++g_counter;
if (value_now > g_iterations)
{
return;
}
auto start_time = std::chrono::high_resolution_clock::now();
logger->info("Some text to log for thread: [somemore text...............................] {}", id);
auto stop_time = std::chrono::high_resolution_clock::now();
uint64_t time_us = std::chrono::duration_cast<std::chrono::microseconds>(stop_time - start_time).count();
result.push_back(time_us);
}
}
void PrintResults(const std::map<size_t, std::vector<uint64_t>>& threads_result, size_t total_us)
{
std::vector<uint64_t> all_measurements;
all_measurements.reserve(g_iterations);
for (auto& t_result : threads_result)
{
all_measurements.insert(all_measurements.end(), t_result.second.begin(), t_result.second.end());
}
// calc worst latenct
auto worst = *std::max_element(all_measurements.begin(), all_measurements.end());
// calc avg
auto total = accumulate(begin(all_measurements), end(all_measurements), 0, std::plus<uint64_t>());
auto avg = double(total)/all_measurements.size();
std::cout << "[spdlog] worst: " << std::setw(10) << std::right << worst << "\tAvg: " << avg << "\tTotal: " << utils::format(total_us) << " us" << std::endl;
}
}// anonymous
// The purpose of this test is NOT to see how fast
// each thread can possibly write. It is to see what
// the worst latency is for writing a log entry
//
// In the test 1 million log entries will be written
// an atomic counter is used to give each thread what
// it is to write next. The overhead of atomic
// synchronization between the threads are not counted in the worst case latency
int main(int argc, char** argv)
{
size_t number_of_threads {0};
if (argc == 2)
{
number_of_threads = atoi(argv[1]);
}
if (argc != 2 || number_of_threads == 0)
{
std::cerr << "usage: " << argv[0] << " number_threads" << std::endl;
return 1;
}
std::vector<std::thread> threads(number_of_threads);
std::map<size_t, std::vector<uint64_t>> threads_result;
for (size_t idx = 0; idx < number_of_threads; ++idx)
{
// reserve to 1 million for all the result
// it's a test so let's not care about the wasted space
threads_result[idx].reserve(g_iterations);
}
int queue_size = 1048576; // 2 ^ 20
spdlog::set_async_mode(queue_size);
auto logger = spdlog::create<spd::sinks::simple_file_sink_mt>("file_logger", "spdlog.log", true);
//force flush on every call to compare with g3log
auto s = (spd::sinks::simple_file_sink_mt*)logger->sinks()[0].get();
s->set_force_flush(true);
auto start_time_application_total = std::chrono::high_resolution_clock::now();
for (uint64_t idx = 0; idx < number_of_threads; ++idx)
{
threads[idx] = std::thread(MeasurePeakDuringLogWrites, idx, std::ref(threads_result[idx]));
}
for (size_t idx = 0; idx < number_of_threads; ++idx)
{
threads[idx].join();
}
auto stop_time_application_total = std::chrono::high_resolution_clock::now();
uint64_t total_time_in_us = std::chrono::duration_cast<std::chrono::microseconds>(stop_time_application_total - start_time_application_total).count();
PrintResults(threads_result, total_time_in_us);
return 0;
}

35
bench/latency/utils.h Normal file
View File

@@ -0,0 +1,35 @@
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
#include <sstream>
#include <iomanip>
#include <locale>
namespace utils
{
template<typename T>
inline std::string format(const T& value)
{
static std::locale loc("");
std::stringstream ss;
ss.imbue(loc);
ss << value;
return ss.str();
}
template<>
inline std::string format(const double & value)
{
static std::locale loc("");
std::stringstream ss;
ss.imbue(loc);
ss << std::fixed << std::setprecision(1) << value;
return ss.str();
}
}

View File

@@ -28,7 +28,7 @@ int main(int argc, char* argv[])
logger->set_pattern("[%Y-%b-%d %T.%e]: %v"); logger->set_pattern("[%Y-%b-%d %T.%e]: %v");
std::atomic<int > msg_counter {0}; std::atomic<int > msg_counter {0};
vector<thread> threads; std::vector<thread> threads;
for (int t = 0; t < thread_count; ++t) for (int t = 0; t < thread_count; ++t)
{ {

112
bench/spdlog-null-async.cpp Normal file
View File

@@ -0,0 +1,112 @@
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
//
// bench.cpp : spdlog benchmarks
//
#include <atomic>
#include <cstdlib> // EXIT_FAILURE
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include "spdlog/spdlog.h"
#include "spdlog/async_logger.h"
#include "spdlog/sinks/null_sink.h"
#include "utils.h"
using namespace std;
using namespace std::chrono;
using namespace spdlog;
using namespace spdlog::sinks;
using namespace utils;
size_t bench_as(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count);
int main(int argc, char* argv[])
{
int queue_size = 1048576;
int howmany = 1000000;
int threads = 10;
int iters = 10;
try
{
if(argc > 1)
howmany = atoi(argv[1]);
if (argc > 2)
threads = atoi(argv[2]);
if (argc > 3)
queue_size = atoi(argv[3]);
cout << "\n*******************************************************************************\n";
cout << "async logging.. " << threads << " threads sharing same logger, " << format(howmany) << " messages " << endl;
cout << "*******************************************************************************\n";
spdlog::set_async_mode(queue_size);
size_t total_rate = 0;
for(int i = 0; i < iters; ++i)
{
//auto as = spdlog::daily_logger_st("as", "logs/daily_async");
auto as = spdlog::create<null_sink_st>("async(null-sink)");
total_rate+= bench_as(howmany, as, threads);
spdlog::drop("async(null-sink)");
}
std::cout << endl;
std::cout << "Avg rate: " << format(total_rate/iters) << "/sec" <<std::endl;
}
catch (std::exception &ex)
{
std::cerr << "Error: " << ex.what() << std::endl;
perror("Last error");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
//return rate/sec
size_t bench_as(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count)
{
cout << log->name() << "...\t\t" << flush;
std::atomic<int > msg_counter {0};
vector<thread> threads;
auto start = system_clock::now();
for (int t = 0; t < thread_count; ++t)
{
threads.push_back(std::thread([&]()
{
for(;;)
{
int counter = ++msg_counter;
if (counter > howmany) break;
log->info("Hello logger: msg number {}", counter);
}
}));
}
for(auto &t:threads)
{
t.join();
};
auto delta = system_clock::now() - start;
auto delta_d = duration_cast<duration<double>> (delta).count();
auto per_sec = size_t(howmany / delta_d);
cout << format(per_sec) << "/sec" << endl;
return per_sec;
}

35
bench/utils.h Normal file
View File

@@ -0,0 +1,35 @@
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
#include <sstream>
#include <iomanip>
#include <locale>
namespace utils
{
template<typename T>
inline std::string format(const T& value)
{
static std::locale loc("");
std::stringstream ss;
ss.imbue(loc);
ss << value;
return ss.str();
}
template<>
inline std::string format(const double & value)
{
static std::locale loc("");
std::stringstream ss;
ss.imbue(loc);
ss << std::fixed << std::setprecision(1) << value;
return ss.str();
}
}

View File

@@ -1,56 +0,0 @@
#include <thread>
#include <vector>
#include <atomic>
#include <cstdlib>
#include <unistd.h>
#include <fcntl.h>
#include <zf_log.c>
const char g_path[] = "logs/zf_log.txt";
int g_fd;
static void output_callback(zf_log_message *msg)
{
*msg->p = '\n';
write(g_fd, msg->buf, msg->p - msg->buf + 1);
}
using namespace std;
int main(int argc, char* argv[])
{
g_fd = open(g_path, O_APPEND|O_CREAT|O_WRONLY);
if (0 > g_fd)
{
ZF_LOGE_AUX(ZF_LOG_STDERR, "Failed to open log file: %s", g_path);
return -1;
}
zf_log_set_output_callback(ZF_LOG_PUT_STD, output_callback);
int thread_count = 10;
if(argc > 1)
thread_count = std::atoi(argv[1]);
int howmany = 1000000;
std::atomic<int > msg_counter {0};
vector<thread> threads;
for (int t = 0; t < thread_count; ++t)
{
threads.push_back(std::thread([&]()
{
while (true)
{
int counter = ++msg_counter;
if (counter > howmany) break;
ZF_LOGI("zf_log message #%i: This is some text for your pleasure", counter);
}
}));
}
for (auto &t:threads)
{
t.join();
};
close(g_fd);
return 0;
}

View File

@@ -1,28 +0,0 @@
#include <stdio.h>
#include <zf_log.c>
const char g_path[] = "logs/zf_log.txt";
static FILE *g_f;
static void output_callback(zf_log_message *msg)
{
*msg->p = '\n';
fwrite(msg->buf, msg->p - msg->buf + 1, 1, g_f);
}
int main(int, char* [])
{
g_f = fopen(g_path, "wb");
if (!g_f)
{
ZF_LOGE_AUX(ZF_LOG_STDERR, "Failed to open log file: %s", g_path);
return -1;
}
zf_log_set_output_callback(ZF_LOG_PUT_STD, output_callback);
const int howmany = 1000000;
for(int i = 0 ; i < howmany; ++i)
ZF_LOGI("zf_log message #%i: This is some text for your pleasure", i);
fclose(g_f);
return 0;
}

View File

@@ -40,6 +40,9 @@ target_link_libraries(example spdlog::spdlog ${CMAKE_THREAD_LIBS_INIT})
add_executable(benchmark bench.cpp) add_executable(benchmark bench.cpp)
target_link_libraries(benchmark spdlog::spdlog ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries(benchmark spdlog::spdlog ${CMAKE_THREAD_LIBS_INIT})
add_executable(multisink multisink.cpp)
target_link_libraries(multisink spdlog::spdlog ${CMAKE_THREAD_LIBS_INIT})
enable_testing() enable_testing()
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs") file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs")
add_test(NAME RunExample COMMAND example) add_test(NAME RunExample COMMAND example)

View File

@@ -35,7 +35,6 @@ int main(int argc, char* argv[])
int queue_size = 1048576; int queue_size = 1048576;
int howmany = 1000000; int howmany = 1000000;
int threads = 10; int threads = 10;
bool auto_flush = false;
int file_size = 30 * 1024 * 1024; int file_size = 30 * 1024 * 1024;
int rotating_files = 5; int rotating_files = 5;
@@ -51,29 +50,29 @@ int main(int argc, char* argv[])
cout << "*******************************************************************************\n"; cout << "*******************************************************************************\n";
cout << "Single thread, " << format(howmany) << " iterations, auto flush=" << auto_flush << endl; cout << "Single thread, " << format(howmany) << " iterations" << endl;
cout << "*******************************************************************************\n"; cout << "*******************************************************************************\n";
auto rotating_st = spdlog::rotating_logger_st("rotating_st", "logs/rotating_st", file_size, rotating_files, auto_flush); auto rotating_st = spdlog::rotating_logger_st("rotating_st", "logs/rotating_st", file_size, rotating_files);
bench(howmany, rotating_st); bench(howmany, rotating_st);
auto daily_st = spdlog::daily_logger_st("daily_st", "logs/daily_st", auto_flush); auto daily_st = spdlog::daily_logger_st("daily_st", "logs/daily_st");
bench(howmany, daily_st); bench(howmany, daily_st);
bench(howmany, spdlog::create<null_sink_st>("null_st")); bench(howmany, spdlog::create<null_sink_st>("null_st"));
cout << "\n*******************************************************************************\n"; cout << "\n*******************************************************************************\n";
cout << threads << " threads sharing same logger, " << format(howmany) << " iterations, auto_flush=" << auto_flush << endl; cout << threads << " threads sharing same logger, " << format(howmany) << " iterations" << endl;
cout << "*******************************************************************************\n"; cout << "*******************************************************************************\n";
auto rotating_mt = spdlog::rotating_logger_mt("rotating_mt", "logs/rotating_mt", file_size, rotating_files, auto_flush); auto rotating_mt = spdlog::rotating_logger_mt("rotating_mt", "logs/rotating_mt", file_size, rotating_files);
bench_mt(howmany, rotating_mt, threads); bench_mt(howmany, rotating_mt, threads);
auto daily_mt = spdlog::daily_logger_mt("daily_mt", "logs/daily_mt", auto_flush); auto daily_mt = spdlog::daily_logger_mt("daily_mt", "logs/daily_mt");
bench_mt(howmany, daily_mt, threads); bench_mt(howmany, daily_mt, threads);
bench(howmany, spdlog::create<null_sink_st>("null_mt")); bench(howmany, spdlog::create<null_sink_st>("null_mt"));
cout << "\n*******************************************************************************\n"; cout << "\n*******************************************************************************\n";
cout << "async logging.. " << threads << " threads sharing same logger, " << format(howmany) << " iterations, auto_flush=" << auto_flush << endl; cout << "async logging.. " << threads << " threads sharing same logger, " << format(howmany) << " iterations " << endl;
cout << "*******************************************************************************\n"; cout << "*******************************************************************************\n";
@@ -81,7 +80,7 @@ int main(int argc, char* argv[])
for(int i = 0; i < 3; ++i) for(int i = 0; i < 3; ++i)
{ {
auto as = spdlog::daily_logger_st("as", "logs/daily_async", auto_flush); auto as = spdlog::daily_logger_st("as", "logs/daily_async");
bench_mt(howmany, as, threads); bench_mt(howmany, as, threads);
spdlog::drop("as"); spdlog::drop("as");
} }

View File

@@ -13,6 +13,7 @@
void async_example(); void async_example();
void syslog_example(); void syslog_example();
void android_example();
void user_defined_example(); void user_defined_example();
void err_handler_example(); void err_handler_example();
@@ -21,34 +22,26 @@ int main(int, char*[])
{ {
try try
{ {
// Multithreaded color console // Console logger with color
auto console = spd::stdout_logger_mt("console", true); auto console = spd::stdout_color_mt("console");
console->info("Welcome to spdlog!"); console->info("Welcome to spdlog!");
console->error("An info message example {}..", 1); console->error("Some error message with arg{}..", 1);
// 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");
console->info("{:>30}", "right aligned");
console->info("{:^30}", "centered");
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");
// Runtime log levels
spd::set_level(spd::level::info); //Set global log level to info
console->debug("This message shold not be displayed!");
console->set_level(spd::level::debug); // Set specific logger's log level
console->debug("This message shold be displayed..");
// Create basic file logger (not rotated) // Create basic file logger (not rotated)
auto my_logger = spd::basic_logger_mt("basic_logger", "logs/basic.txt"); auto my_logger = spd::basic_logger_mt("basic_logger", "logs/basic.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/mylogfile", 1048576 * 5, 3); auto rotating_logger = spd::rotating_logger_mt("some_logger_name", "logs/mylogfile", 1048576 * 5, 3);
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
@@ -56,14 +49,23 @@ int main(int, char*[])
// 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", 2, 30); auto daily_logger = spd::daily_logger_mt("daily_logger", "logs/daily", 2, 30);
// trigger flush if the log severity is error or higher
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 ***"); spd::set_pattern("*** [%H:%M:%S %z] [thread %t] %v ***");
rotating_logger->info("This is another message with custom format"); rotating_logger->info("This is another message with custom format");
// Compile time debug or trace macros.
// Enabled #ifdef SPDLOG_DEBUG_ON or #ifdef SPDLOG_TRACE_ON // Runtime log levels
spd::set_level(spd::level::info); //Set global log level to info
console->debug("This message shold not be displayed!");
console->set_level(spd::level::debug); // Set specific logger's log level
console->debug("This message shold be displayed..");
// Compile time log levels
// 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);
@@ -74,6 +76,9 @@ int main(int, char*[])
// syslog example. linux/osx only // syslog example. linux/osx only
syslog_example(); 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();
@@ -102,6 +107,7 @@ void async_example()
size_t q_size = 4096; //queue size must be power of 2 size_t q_size = 4096; //queue size must be power of 2
spdlog::set_async_mode(q_size); spdlog::set_async_mode(q_size);
auto async_file = spd::daily_logger_st("async_file_logger", "logs/async_log.txt"); auto async_file = spd::daily_logger_st("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);
} }
@@ -116,6 +122,16 @@ void syslog_example()
#endif #endif
} }
// Android example
void android_example()
{
#if defined(__ANDROID__)
std::string tag = "spdlog-android";
auto android_logger = spd::android_logger("android", tag);
android_logger->critical("Use \"adb shell logcat\" to view this message.");
#endif
}
// user defined types logging by implementing operator<< // user defined types logging by implementing operator<<
struct my_type struct my_type
{ {
@@ -145,4 +161,3 @@ void err_handler_example()
}); });
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

@@ -42,6 +42,7 @@
<ClInclude Include="..\include\spdlog\sinks\sink.h" /> <ClInclude Include="..\include\spdlog\sinks\sink.h" />
<ClInclude Include="..\include\spdlog\sinks\stdout_sinks.h" /> <ClInclude Include="..\include\spdlog\sinks\stdout_sinks.h" />
<ClInclude Include="..\include\spdlog\sinks\syslog_sink.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\spdlog.h" />
<ClInclude Include="..\include\spdlog\tweakme.h" /> <ClInclude Include="..\include\spdlog\tweakme.h" />
</ItemGroup> </ItemGroup>
@@ -55,13 +56,13 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset> <PlatformToolset>v120</PlatformToolset>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries> <UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset> <PlatformToolset>v120</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization> <WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>

15
example/jni/Android.mk Normal file
View File

@@ -0,0 +1,15 @@
# Setup a project
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := example
LOCAL_SRC_FILES := example.cpp
LOCAL_CPPFLAGS += -Wall -Wshadow -Wextra -pedantic -std=c++11 -fPIE -pie
LOCAL_LDFLAGS += -fPIE -pie
# Add exception support and set path for spdlog's headers
LOCAL_CPPFLAGS += -fexceptions -I../include
# Use android's log library
LOCAL_LDFLAGS += -llog
include $(BUILD_EXECUTABLE)

View File

@@ -0,0 +1,2 @@
# Exceptions are used in spdlog. Link to an exception-ready C++ runtime.
APP_STL = gnustl_static

1
example/jni/example.cpp Symbolic link
View File

@@ -0,0 +1 @@
../example.cpp

47
example/multisink.cpp Normal file
View File

@@ -0,0 +1,47 @@
#include "spdlog/spdlog.h"
#include <iostream>
#include <memory>
namespace spd = spdlog;
int main(int, char*[])
{
bool enable_debug = true;
try
{
// This other example use a single logger with multiple sinks.
// This means that the same log_msg is forwarded to multiple sinks;
// Each sink can have it's own log level and a message will be logged.
std::vector<spdlog::sink_ptr> sinks;
sinks.push_back( std::make_shared<spdlog::sinks::stdout_sink_mt>() );
sinks.push_back( std::make_shared<spdlog::sinks::simple_file_sink_mt>("./log_regular_file.txt") );
sinks.push_back( std::make_shared<spdlog::sinks::simple_file_sink_mt>("./log_debug_file.txt") );
spdlog::logger console_multisink("multisink", sinks.begin(), sinks.end() );
console_multisink.set_level( spdlog::level::warn);
sinks[0]->set_level( spdlog::level::trace); // console. Allow everything. Default value
sinks[1]->set_level( spdlog::level::trace); // regular file. Allow everything. Default value
sinks[2]->set_level( spdlog::level::off); // regular file. Ignore everything.
console_multisink.warn("warn: will print only on console and regular file");
if( enable_debug )
{
console_multisink.set_level( spdlog::level::debug); // level of the logger
sinks[1]->set_level( spdlog::level::debug); // regular file
sinks[2]->set_level( spdlog::level::debug); // debug file
}
console_multisink.debug("Debug: you should see this on console and both files");
// Release and close all loggers
spdlog::drop_all();
}
// Exceptions will only be thrown upon failed logger or sink construction (not during logging)
catch (const spd::spdlog_ex& ex)
{
std::cout << "Log init failed: " << ex.what() << std::endl;
return 1;
}
}

View File

@@ -30,11 +30,11 @@
#endif #endif
#if defined(__GNUC__) || defined(__clang__) #if defined(__GNUC__) || defined(__clang__)
#define DEPRECATED __attribute__((deprecated)) #define SPDLOG_DEPRECATED __attribute__((deprecated))
#elif defined(_MSC_VER) #elif defined(_MSC_VER)
#define DEPRECATED __declspec(deprecated) #define SPDLOG_DEPRECATED __declspec(deprecated)
#else #else
#define DEPRECATED #define SPDLOG_DEPRECATED
#endif #endif
@@ -57,7 +57,7 @@ using formatter_ptr = std::shared_ptr<spdlog::formatter>;
#if defined(SPDLOG_NO_ATOMIC_LEVELS) #if defined(SPDLOG_NO_ATOMIC_LEVELS)
using level_t = details::null_atomic_int; using level_t = details::null_atomic_int;
#else #else
using level_t = std::atomic_int; using level_t = std::atomic<int>;
#endif #endif
using log_err_handler = std::function<void(const std::string &err_msg)>; using log_err_handler = std::function<void(const std::string &err_msg)>;

View File

@@ -67,7 +67,7 @@ async_msg(async_msg&& other) SPDLOG_NOEXCEPT:
{} {}
async_msg(async_msg_type m_type) :msg_type(m_type) async_msg(async_msg_type m_type) :msg_type(m_type)
{}; {}
async_msg& operator=(async_msg&& other) SPDLOG_NOEXCEPT async_msg& operator=(async_msg&& other) SPDLOG_NOEXCEPT
{ {
@@ -82,7 +82,7 @@ async_msg(async_msg&& other) SPDLOG_NOEXCEPT:
// never copy or assign. should only be moved.. // never copy or assign. should only be moved..
async_msg(const async_msg&) = delete; async_msg(const async_msg&) = delete;
async_msg& operator=(async_msg& other) = delete; async_msg& operator=(const async_msg& other) = delete;
// construct from log_msg // construct from log_msg
async_msg(const details::log_msg& m) : async_msg(const details::log_msg& m) :
@@ -133,7 +133,7 @@ public:
void set_formatter(formatter_ptr); void set_formatter(formatter_ptr);
void flush(); void flush(bool wait_for_q);
private: private:
@@ -229,8 +229,6 @@ inline spdlog::details::async_log_helper::~async_log_helper()
inline void spdlog::details::async_log_helper::log(const details::log_msg& msg) inline void spdlog::details::async_log_helper::log(const details::log_msg& msg)
{ {
push_msg(async_msg(msg)); push_msg(async_msg(msg));
} }
inline void spdlog::details::async_log_helper::push_msg(details::async_log_helper::async_msg&& new_msg) inline void spdlog::details::async_log_helper::push_msg(details::async_log_helper::async_msg&& new_msg)
@@ -246,15 +244,14 @@ inline void spdlog::details::async_log_helper::push_msg(details::async_log_helpe
} }
while (!_q.enqueue(std::move(new_msg))); while (!_q.enqueue(std::move(new_msg)));
} }
} }
//wait for the queue be empty and request flush from its sinks // optionally wait for the queue be empty and request flush from the sinks
inline void spdlog::details::async_log_helper::flush() inline void spdlog::details::async_log_helper::flush(bool wait_for_q)
{ {
wait_empty_q();
push_msg(async_msg(async_msg_type::flush)); push_msg(async_msg(async_msg_type::flush));
wait_empty_q(); //make sure the above flush message was processed if(wait_for_q)
wait_empty_q(); //return only make after the above flush message was processed
} }
inline void spdlog::details::async_log_helper::worker_loop() inline void spdlog::details::async_log_helper::worker_loop()
@@ -281,10 +278,8 @@ inline void spdlog::details::async_log_helper::worker_loop()
// return true if this thread should still be active (while no terminate msg was received) // return true if this thread should still be active (while no terminate msg was received)
inline bool spdlog::details::async_log_helper::process_next_msg(log_clock::time_point& last_pop, log_clock::time_point& last_flush) inline bool spdlog::details::async_log_helper::process_next_msg(log_clock::time_point& last_pop, log_clock::time_point& last_flush)
{ {
async_msg incoming_async_msg; async_msg incoming_async_msg;
if (_q.dequeue(incoming_async_msg)) if (_q.dequeue(incoming_async_msg))
{ {
last_pop = details::os::now(); last_pop = details::os::now();
@@ -304,8 +299,13 @@ inline bool spdlog::details::async_log_helper::process_next_msg(log_clock::time_
incoming_async_msg.fill_log_msg(incoming_log_msg); incoming_async_msg.fill_log_msg(incoming_log_msg);
_formatter->format(incoming_log_msg); _formatter->format(incoming_log_msg);
for (auto &s : _sinks) for (auto &s : _sinks)
{
if(s->should_log( incoming_log_msg.level))
{
s->log(incoming_log_msg); s->log(incoming_log_msg);
} }
}
}
return true; return true;
} }
@@ -317,7 +317,6 @@ inline bool spdlog::details::async_log_helper::process_next_msg(log_clock::time_
handle_flush_interval(now, last_flush); handle_flush_interval(now, last_flush);
sleep_or_yield(now, last_pop); sleep_or_yield(now, last_pop);
return !_terminate_requested; return !_terminate_requested;
} }
} }
@@ -355,8 +354,7 @@ inline void spdlog::details::async_log_helper::sleep_or_yield(const spdlog::log_
// yield upto 150 micros // yield upto 150 micros
if (time_since_op <= microseconds(100)) if (time_since_op <= microseconds(100))
return yield(); return std::this_thread::yield();
// sleep for 20 ms upto 200 ms // sleep for 20 ms upto 200 ms
if (time_since_op <= milliseconds(200)) if (time_since_op <= milliseconds(200))
@@ -374,13 +372,7 @@ inline void spdlog::details::async_log_helper::wait_empty_q()
{ {
sleep_or_yield(details::os::now(), last_op); sleep_or_yield(details::os::now(), last_op);
} }
} }

View File

@@ -31,13 +31,13 @@ inline spdlog::async_logger::async_logger(const std::string& logger_name,
} }
inline spdlog::async_logger::async_logger(const std::string& logger_name, inline spdlog::async_logger::async_logger(const std::string& logger_name,
sinks_init_list sinks, sinks_init_list sinks_list,
size_t queue_size, size_t queue_size,
const async_overflow_policy overflow_policy, const async_overflow_policy overflow_policy,
const std::function<void()>& worker_warmup_cb, const std::function<void()>& worker_warmup_cb,
const std::chrono::milliseconds& flush_interval_ms, const std::chrono::milliseconds& flush_interval_ms,
const std::function<void()>& worker_teardown_cb) : const std::function<void()>& worker_teardown_cb) :
async_logger(logger_name, sinks.begin(), sinks.end(), queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb) {} async_logger(logger_name, sinks_list.begin(), sinks_list.end(), queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb) {}
inline spdlog::async_logger::async_logger(const std::string& logger_name, inline spdlog::async_logger::async_logger(const std::string& logger_name,
sink_ptr single_sink, sink_ptr single_sink,
@@ -54,8 +54,7 @@ inline spdlog::async_logger::async_logger(const std::string& logger_name,
inline void spdlog::async_logger::flush() inline void spdlog::async_logger::flush()
{ {
_async_log_helper->flush(true);
_async_log_helper->flush();
} }
inline void spdlog::async_logger::_set_formatter(spdlog::formatter_ptr msg_formatter) inline void spdlog::async_logger::_set_formatter(spdlog::formatter_ptr msg_formatter)
@@ -76,6 +75,8 @@ inline void spdlog::async_logger::_sink_it(details::log_msg& msg)
try try
{ {
_async_log_helper->log(msg); _async_log_helper->log(msg);
if (_should_flush_on(msg))
_async_log_helper->flush(false); // do async flush
} }
catch (const std::exception &ex) catch (const std::exception &ex)
{ {

View File

@@ -31,9 +31,8 @@ public:
const int open_tries = 5; const int open_tries = 5;
const int open_interval = 10; const int open_interval = 10;
explicit file_helper(bool force_flush) : explicit file_helper() :
_fd(nullptr), _fd(nullptr)
_force_flush(force_flush)
{} {}
file_helper(const file_helper&) = delete; file_helper(const file_helper&) = delete;
@@ -91,9 +90,6 @@ public:
auto data = msg.formatted.data(); auto data = msg.formatted.data();
if (std::fwrite(data, 1, msg_size, _fd) != msg_size) if (std::fwrite(data, 1, msg_size, _fd) != msg_size)
throw spdlog_ex("Failed writing to file " + os::filename_to_str(_filename), errno); throw spdlog_ex("Failed writing to file " + os::filename_to_str(_filename), errno);
if (_force_flush)
std::fflush(_fd);
} }
size_t size() size_t size()
@@ -117,7 +113,6 @@ public:
private: private:
FILE* _fd; FILE* _fd;
filename_t _filename; filename_t _filename;
bool _force_flush;
}; };
} }
} }

View File

@@ -244,13 +244,16 @@ inline bool spdlog::logger::should_log(spdlog::level::level_enum msg_level) cons
// //
inline void spdlog::logger::_sink_it(details::log_msg& msg) inline void spdlog::logger::_sink_it(details::log_msg& msg)
{ {
_formatter->format(msg); _formatter->format(msg);
for (auto &sink : _sinks) for (auto &sink : _sinks)
{
if( sink->should_log( msg.level))
{
sink->log(msg); sink->log(msg);
}
}
const auto flush_level = _flush_level.load(std::memory_order_relaxed); if(_should_flush_on(msg))
if (msg.level >= flush_level)
flush(); flush();
} }
@@ -282,3 +285,14 @@ inline void spdlog::logger::_default_err_handler(const std::string &msg)
sinks::stderr_sink_mt::instance()->log(err_msg); sinks::stderr_sink_mt::instance()->log(err_msg);
_last_err_time = now; _last_err_time = now;
} }
inline bool spdlog::logger::_should_flush_on(const details::log_msg &msg)
{
const auto flush_level = _flush_level.load(std::memory_order_relaxed);
return (msg.level >= flush_level) && (msg.level != level::off);
}
inline const std::vector<spdlog::sink_ptr>& spdlog::logger::sinks() const
{
return _sinks;
}

View File

@@ -10,10 +10,12 @@
#include <ctime> #include <ctime>
#include <functional> #include <functional>
#include <string> #include <string>
#include <chrono>
#include <thread>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h>
#ifdef _WIN32 #ifdef _WIN32
@@ -25,27 +27,32 @@
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#endif #endif
#include <windows.h> #include <windows.h>
#include <process.h> // _get_pid support
#include <io.h> // _get_osfhandle support
#ifdef __MINGW32__ #ifdef __MINGW32__
#include <share.h> #include <share.h>
#endif #endif
#include <sys/types.h> #else // unix
#elif __linux__
#include <sys/syscall.h> //Use gettid() syscall under linux to get thread id
#include <unistd.h> #include <unistd.h>
#include <chrono> #include <fcntl.h>
#ifdef __linux__
#include <sys/syscall.h> //Use gettid() syscall under linux to get thread id
#elif __FreeBSD__ #elif __FreeBSD__
#include <sys/thr.h> //Use thr_self() syscall under FreeBSD to get thread id #include <sys/thr.h> //Use thr_self() syscall under FreeBSD to get thread id
#else
#include <thread>
#endif #endif
#endif //unix
#ifndef __has_feature // Clang - feature checking macros.
#define __has_feature(x) 0 // Compatibility with non-clang compilers.
#endif
namespace spdlog namespace spdlog
{ {
namespace details namespace details
@@ -135,6 +142,18 @@ inline bool operator!=(const std::tm& tm1, const std::tm& tm2)
SPDLOG_CONSTEXPR static const char* eol = SPDLOG_EOL; SPDLOG_CONSTEXPR static const char* eol = SPDLOG_EOL;
SPDLOG_CONSTEXPR static int eol_size = sizeof(SPDLOG_EOL) - 1; SPDLOG_CONSTEXPR static int eol_size = sizeof(SPDLOG_EOL) - 1;
inline void prevent_child_fd(FILE *f)
{
#ifdef _WIN32
auto file_handle = (HANDLE)_get_osfhandle(_fileno(f));
if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0))
throw spdlog_ex("SetHandleInformation failed", errno);
#else
auto fd = fileno(f);
if(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
throw spdlog_ex("fcntl with FD_CLOEXEC failed", errno);
#endif
}
//fopen_s on non windows for writing //fopen_s on non windows for writing
@@ -146,13 +165,18 @@ inline int fopen_s(FILE** fp, const filename_t& filename, const filename_t& mode
#else #else
*fp = _fsopen((filename.c_str()), mode.c_str(), _SH_DENYWR); *fp = _fsopen((filename.c_str()), mode.c_str(), _SH_DENYWR);
#endif #endif
return *fp == nullptr; #else //unix
#else
*fp = fopen((filename.c_str()), mode.c_str()); *fp = fopen((filename.c_str()), mode.c_str());
return *fp == nullptr;
#endif #endif
#ifdef SPDLOG_PREVENT_CHILD_FD
if(*fp != nullptr)
prevent_child_fd(*fp);
#endif
return *fp == nullptr;
} }
inline int remove(const filename_t &filename) inline int remove(const filename_t &filename)
{ {
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
@@ -204,9 +228,9 @@ inline size_t filesize(FILE *f)
return st.st_size; return st.st_size;
#else //windows 32 bits #else //windows 32 bits
struct _stat st; long ret = _filelength(fd);
if (_fstat(fd, &st) == 0) if (ret >= 0)
return st.st_size; return static_cast<size_t>(ret);
#endif #endif
#else // unix #else // unix
@@ -215,11 +239,11 @@ inline size_t filesize(FILE *f)
#if !defined(__FreeBSD__) && !defined(__APPLE__) && (defined(__x86_64__) || defined(__ppc64__)) #if !defined(__FreeBSD__) && !defined(__APPLE__) && (defined(__x86_64__) || defined(__ppc64__))
struct stat64 st; struct stat64 st;
if (fstat64(fd, &st) == 0) if (fstat64(fd, &st) == 0)
return st.st_size; return static_cast<size_t>(st.st_size);
#else // unix 32 bits or osx #else // unix 32 bits or osx
struct stat st; struct stat st;
if (fstat(fd, &st) == 0) if (fstat(fd, &st) == 0)
return st.st_size; return static_cast<size_t>(st.st_size);
#endif #endif
#endif #endif
throw spdlog_ex("Failed getting file size from fd", errno); throw spdlog_ex("Failed getting file size from fd", errno);
@@ -292,7 +316,7 @@ inline int utc_minutes_offset(const std::tm& tm = details::os::localtime())
//Return current thread id as size_t //Return current thread id as size_t
//It exists because the std::this_thread::get_id() is much slower(espcially under VS 2013) //It exists because the std::this_thread::get_id() is much slower(espcially under VS 2013)
inline size_t thread_id() inline size_t _thread_id()
{ {
#ifdef _WIN32 #ifdef _WIN32
return static_cast<size_t>(::GetCurrentThreadId()); return static_cast<size_t>(::GetCurrentThreadId());
@@ -308,10 +332,21 @@ inline size_t thread_id()
#else //Default to standard C++11 (OSX and other Unix) #else //Default to standard C++11 (OSX and other Unix)
return static_cast<size_t>(std::hash<std::thread::id>()(std::this_thread::get_id())); return static_cast<size_t>(std::hash<std::thread::id>()(std::this_thread::get_id()));
#endif #endif
} }
//Return current thread id as size_t (from thread local storage)
inline size_t thread_id()
{
#if defined(_MSC_VER) && (_MSC_VER < 1900) || defined(__clang__) && !__has_feature(cxx_thread_local)
return _thread_id();
#else
static thread_local const size_t tid = _thread_id();
return tid;
#endif
}
// wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined) // wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined)
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
@@ -342,7 +377,9 @@ inline std::string errno_str(int err_num)
else else
return "Unkown error"; return "Unkown error";
#elif defined(__FreeBSD__) || defined(__APPLE__) || ((_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE) // posix version #elif defined(__FreeBSD__) || defined(__APPLE__) || defined(ANDROID) || defined(__SUNPRO_CC) || \
((_POSIX_C_SOURCE >= 200112L) && ! defined(_GNU_SOURCE)) // posix version
if (strerror_r(err_num, buf, buf_size) == 0) if (strerror_r(err_num, buf, buf_size) == 0)
return std::string(buf); return std::string(buf);
else else
@@ -353,8 +390,17 @@ inline std::string errno_str(int err_num)
#endif #endif
} }
inline int pid()
{
#ifdef _WIN32
return ::_getpid();
#else
return static_cast<int>(::getpid());
#endif
}
} //os } //os
} //details } //details
} //spdlog } //spdlog

View File

@@ -18,6 +18,7 @@
#include <thread> #include <thread>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <array>
namespace spdlog namespace spdlog
{ {
@@ -78,42 +79,60 @@ static int to12h(const tm& t)
} }
//Abbreviated weekday name //Abbreviated weekday name
static const std::string days[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; using days_array = std::array<std::string, 7>;
static const days_array& days()
{
static const days_array arr{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
return arr;
}
class a_formatter:public flag_formatter class a_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << days[tm_time.tm_wday]; msg.formatted << days()[tm_time.tm_wday];
} }
}; };
//Full weekday name //Full weekday name
static const std::string full_days[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; static const days_array& full_days()
{
static const days_array arr{ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
return arr;
}
class A_formatter:public flag_formatter class A_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << full_days[tm_time.tm_wday]; msg.formatted << full_days()[tm_time.tm_wday];
} }
}; };
//Abbreviated month //Abbreviated month
static const std::string months[] { "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" }; using months_array = std::array<std::string, 12>;
static const months_array& months()
{
static const months_array arr{ "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" };
return arr;
}
class b_formatter:public flag_formatter class b_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << months[tm_time.tm_mon]; msg.formatted << months()[tm_time.tm_mon];
} }
}; };
//Full month name //Full month name
static const std::string full_months[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; static const months_array& full_months()
{
static const months_array arr{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
return arr;
}
class B_formatter:public flag_formatter class B_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << full_months[tm_time.tm_mon]; msg.formatted << full_months()[tm_time.tm_mon];
} }
}; };
@@ -138,7 +157,7 @@ class c_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << days[tm_time.tm_wday] << ' ' << months[tm_time.tm_mon] << ' ' << tm_time.tm_mday << ' '; msg.formatted << days()[tm_time.tm_wday] << ' ' << months()[tm_time.tm_mon] << ' ' << tm_time.tm_mday << ' ';
pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, ':') << ' ' << tm_time.tm_year + 1900; pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, ':') << ' ' << tm_time.tm_year + 1900;
} }
}; };
@@ -365,6 +384,15 @@ class t_formatter:public flag_formatter
} }
}; };
// Current pid
class pid_formatter:public flag_formatter
{
void format(details::log_msg& msg, const std::tm&) override
{
msg.formatted << details::os::pid();
}
};
class v_formatter:public flag_formatter class v_formatter:public flag_formatter
{ {
@@ -453,6 +481,8 @@ class full_formatter:public flag_formatter
} }
}; };
} }
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@@ -611,6 +641,10 @@ inline void spdlog::pattern_formatter::handle_flag(char flag)
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::full_formatter())); _formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::full_formatter()));
break; break;
case ('P'):
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::pid_formatter()));
break;
default: //Unkown flag appears as is default: //Unkown flag appears as is
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::ch_formatter('%'))); _formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::ch_formatter('%')));
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::ch_formatter(flag))); _formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::ch_formatter(flag)));

View File

@@ -12,8 +12,20 @@
#include <spdlog/details/registry.h> #include <spdlog/details/registry.h>
#include <spdlog/sinks/file_sinks.h> #include <spdlog/sinks/file_sinks.h>
#include <spdlog/sinks/stdout_sinks.h> #include <spdlog/sinks/stdout_sinks.h>
#ifdef SPDLOG_ENABLE_SYSLOG
#include <spdlog/sinks/syslog_sink.h> #include <spdlog/sinks/syslog_sink.h>
#endif
#ifdef _WIN32
#include <spdlog/sinks/wincolor_sink.h>
#else
#include <spdlog/sinks/ansicolor_sink.h> #include <spdlog/sinks/ansicolor_sink.h>
#endif
#ifdef __ANDROID__
#include <spdlog/sinks/android_sink.h>
#endif
#include <chrono> #include <chrono>
#include <functional> #include <functional>
@@ -36,66 +48,118 @@ inline void spdlog::drop(const std::string &name)
} }
// Create multi/single threaded simple file logger // Create multi/single threaded simple file logger
inline std::shared_ptr<spdlog::logger> spdlog::basic_logger_mt(const std::string& logger_name, const filename_t& filename, bool force_flush, bool truncate) inline std::shared_ptr<spdlog::logger> spdlog::basic_logger_mt(const std::string& logger_name, const filename_t& filename, bool truncate)
{ {
return create<spdlog::sinks::simple_file_sink_mt>(logger_name, filename, force_flush, truncate); return create<spdlog::sinks::simple_file_sink_mt>(logger_name, filename, truncate);
} }
inline std::shared_ptr<spdlog::logger> spdlog::basic_logger_st(const std::string& logger_name, const filename_t& filename, bool force_flush, bool truncate) inline std::shared_ptr<spdlog::logger> spdlog::basic_logger_st(const std::string& logger_name, const filename_t& filename, bool truncate)
{ {
return create<spdlog::sinks::simple_file_sink_st>(logger_name, filename, force_flush, truncate); return create<spdlog::sinks::simple_file_sink_st>(logger_name, filename, truncate);
} }
// Create multi/single threaded rotating file logger // Create multi/single threaded rotating file logger
inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_mt(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files, bool force_flush) inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_mt(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files)
{ {
return create<spdlog::sinks::rotating_file_sink_mt>(logger_name, filename, SPDLOG_FILENAME_T("txt"), max_file_size, max_files, force_flush); return create<spdlog::sinks::rotating_file_sink_mt>(logger_name, filename, SPDLOG_FILENAME_T("txt"), max_file_size, max_files);
} }
inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_st(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files, bool force_flush) inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_st(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files)
{ {
return create<spdlog::sinks::rotating_file_sink_st>(logger_name, filename, SPDLOG_FILENAME_T("txt"), max_file_size, max_files, force_flush); return create<spdlog::sinks::rotating_file_sink_st>(logger_name, filename, SPDLOG_FILENAME_T("txt"), max_file_size, max_files);
} }
// Create file logger which creates new file at midnight): // 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 filename_t& filename, int hour, int minute, bool force_flush) inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_mt(const std::string& logger_name, const filename_t& filename, int hour, int minute)
{ {
return create<spdlog::sinks::daily_file_sink_mt>(logger_name, filename, SPDLOG_FILENAME_T("txt"), hour, minute, force_flush); return create<spdlog::sinks::daily_file_sink_mt>(logger_name, filename, SPDLOG_FILENAME_T("txt"), hour, minute);
} }
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string& logger_name, const filename_t& filename, int hour, int minute, bool force_flush) inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string& logger_name, const filename_t& filename, int hour, int minute)
{ {
return create<spdlog::sinks::daily_file_sink_st>(logger_name, filename, SPDLOG_FILENAME_T("txt"), hour, minute, force_flush); return create<spdlog::sinks::daily_file_sink_st>(logger_name, filename, SPDLOG_FILENAME_T("txt"), hour, minute);
} }
// Create stdout/stderr loggers (with optinal color support)
inline std::shared_ptr<spdlog::logger> create_console_logger(const std::string& logger_name, spdlog::sink_ptr sink, bool color) //
// stdout/stderr loggers
//
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt(const std::string& logger_name)
{ {
if (color) //use color wrapper sink return spdlog::details::registry::instance().create(logger_name, spdlog::sinks::stdout_sink_mt::instance());
sink = std::make_shared<spdlog::sinks::ansicolor_sink>(sink); }
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st(const std::string& logger_name)
{
return spdlog::details::registry::instance().create(logger_name, spdlog::sinks::stdout_sink_st::instance());
}
inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt(const std::string& logger_name)
{
return spdlog::details::registry::instance().create(logger_name, spdlog::sinks::stderr_sink_mt::instance());
}
inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st(const std::string& logger_name)
{
return spdlog::details::registry::instance().create(logger_name, spdlog::sinks::stderr_sink_st::instance());
}
//
// stdout/stderr color loggers
//
#ifdef _WIN32
inline std::shared_ptr<spdlog::logger> spdlog::stdout_color_mt(const std::string& logger_name)
{
auto sink = std::make_shared<spdlog::sinks::wincolor_stdout_sink_mt>();
return spdlog::details::registry::instance().create(logger_name, sink); return spdlog::details::registry::instance().create(logger_name, sink);
} }
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt(const std::string& logger_name, bool color) inline std::shared_ptr<spdlog::logger> spdlog::stdout_color_st(const std::string& logger_name)
{ {
return create_console_logger(logger_name, sinks::stdout_sink_mt::instance(), color); auto sink = std::make_shared<spdlog::sinks::wincolor_stdout_sink_st>();
return spdlog::details::registry::instance().create(logger_name, sink);
} }
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st(const std::string& logger_name, bool color) inline std::shared_ptr<spdlog::logger> spdlog::stderr_color_mt(const std::string& logger_name)
{ {
return create_console_logger(logger_name, sinks::stdout_sink_st::instance(), color); auto sink = std::make_shared<spdlog::sinks::wincolor_stderr_sink_mt>();
return spdlog::details::registry::instance().create(logger_name, sink);
} }
inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt(const std::string& logger_name, bool color)
inline std::shared_ptr<spdlog::logger> spdlog::stderr_color_st(const std::string& logger_name)
{ {
return create_console_logger(logger_name, sinks::stderr_sink_mt::instance(), color); auto sink = std::make_shared<spdlog::sinks::wincolor_stderr_sink_st>();
return spdlog::details::registry::instance().create(logger_name, sink);
} }
inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st(const std::string& logger_name, bool color) #else //ansi terminal colors
inline std::shared_ptr<spdlog::logger> spdlog::stdout_color_mt(const std::string& logger_name)
{ {
return create_console_logger(logger_name, sinks::stderr_sink_st::instance(), color); auto sink = std::make_shared<spdlog::sinks::ansicolor_sink>(spdlog::sinks::stdout_sink_mt::instance());
return spdlog::details::registry::instance().create(logger_name, sink);
} }
inline std::shared_ptr<spdlog::logger> spdlog::stdout_color_st(const std::string& logger_name)
{
auto sink = std::make_shared<spdlog::sinks::ansicolor_sink>(spdlog::sinks::stdout_sink_st::instance());
return spdlog::details::registry::instance().create(logger_name, sink);
}
inline std::shared_ptr<spdlog::logger> spdlog::stderr_color_mt(const std::string& logger_name)
{
auto sink = std::make_shared<spdlog::sinks::ansicolor_sink>(spdlog::sinks::stderr_sink_mt::instance());
return spdlog::details::registry::instance().create(logger_name, sink);
}
inline std::shared_ptr<spdlog::logger> spdlog::stderr_color_st(const std::string& logger_name)
{
auto sink = std::make_shared<spdlog::sinks::ansicolor_sink>(spdlog::sinks::stderr_sink_st::instance());
return spdlog::details::registry::instance().create(logger_name, sink);
}
#endif
#ifdef SPDLOG_ENABLE_SYSLOG #ifdef SPDLOG_ENABLE_SYSLOG
// Create syslog logger // Create syslog logger
inline std::shared_ptr<spdlog::logger> spdlog::syslog_logger(const std::string& logger_name, const std::string& syslog_ident, int syslog_option) inline std::shared_ptr<spdlog::logger> spdlog::syslog_logger(const std::string& logger_name, const std::string& syslog_ident, int syslog_option)
@@ -104,6 +168,13 @@ inline std::shared_ptr<spdlog::logger> spdlog::syslog_logger(const std::string&
} }
#endif #endif
#ifdef __ANDROID__
inline std::shared_ptr<spdlog::logger> spdlog::android_logger(const std::string& logger_name, const std::string& tag)
{
return create<spdlog::sinks::android_sink>(logger_name, tag);
}
#endif
// Create and register a logger a single sink // Create and register a logger a single sink
inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, const spdlog::sink_ptr& sink) inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, const spdlog::sink_ptr& sink)
{ {

View File

@@ -25,9 +25,7 @@
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
// Commented out by spdlog to use header only #include "format.h"
// #include "fmt/format.h"
// #include "fmt/printf.h"
#include <string.h> #include <string.h>
@@ -73,25 +71,31 @@ using fmt::internal::Arg;
// Dummy implementations of strerror_r and strerror_s called if corresponding // Dummy implementations of strerror_r and strerror_s called if corresponding
// system functions are not available. // system functions are not available.
static inline fmt::internal::Null<> strerror_r(int, char *, ...) { static inline fmt::internal::Null<> strerror_r(int, char *, ...)
{
return fmt::internal::Null<>(); return fmt::internal::Null<>();
} }
static inline fmt::internal::Null<> strerror_s(char *, std::size_t, ...) { static inline fmt::internal::Null<> strerror_s(char *, std::size_t, ...)
{
return fmt::internal::Null<>(); return fmt::internal::Null<>();
} }
namespace fmt { namespace fmt {
FMT_FUNC internal::RuntimeError::~RuntimeError() throw() {} FMT_FUNC internal::RuntimeError::~RuntimeError() FMT_DTOR_NOEXCEPT
FMT_FUNC FormatError::~FormatError() throw() {} {}
FMT_FUNC SystemError::~SystemError() throw() {} FMT_FUNC FormatError::~FormatError() FMT_DTOR_NOEXCEPT
{}
FMT_FUNC SystemError::~SystemError() FMT_DTOR_NOEXCEPT
{}
namespace { namespace {
#ifndef _MSC_VER #ifndef _MSC_VER
# define FMT_SNPRINTF snprintf # define FMT_SNPRINTF snprintf
#else // _MSC_VER #else // _MSC_VER
inline int fmt_snprintf(char *buffer, size_t size, const char *format, ...) { inline int fmt_snprintf(char *buffer, size_t size, const char *format, ...)
{
va_list args; va_list args;
va_start(args, format); va_start(args, format);
int result = vsnprintf_s(buffer, size, _TRUNCATE, format, args); int result = vsnprintf_s(buffer, size, _TRUNCATE, format, args);
@@ -121,26 +125,31 @@ typedef void (*FormatFunc)(Writer &, int, StringRef);
// other - failure // other - failure
// Buffer should be at least of size 1. // Buffer should be at least of size 1.
int safe_strerror( int safe_strerror(
int error_code, char *&buffer, std::size_t buffer_size) FMT_NOEXCEPT { int error_code, char *&buffer, std::size_t buffer_size) FMT_NOEXCEPT
{
FMT_ASSERT(buffer != 0 && buffer_size != 0, "invalid buffer"); FMT_ASSERT(buffer != 0 && buffer_size != 0, "invalid buffer");
class StrError { class StrError
{
private: private:
int error_code_; int error_code_;
char *&buffer_; char *&buffer_;
std::size_t buffer_size_; std::size_t buffer_size_;
// A noop assignment operator to avoid bogus warnings. // A noop assignment operator to avoid bogus warnings.
void operator=(const StrError &) {} void operator=(const StrError &)
{}
// Handle the result of XSI-compliant version of strerror_r. // Handle the result of XSI-compliant version of strerror_r.
int handle(int result) { int handle(int result)
{
// glibc versions before 2.13 return result in errno. // glibc versions before 2.13 return result in errno.
return result == -1 ? errno : result; return result == -1 ? errno : result;
} }
// Handle the result of GNU-specific version of strerror_r. // Handle the result of GNU-specific version of strerror_r.
int handle(char *message) { int handle(char *message)
{
// If the buffer is full then the message is probably truncated. // If the buffer is full then the message is probably truncated.
if (message == buffer_ && strlen(buffer_) == buffer_size_ - 1) if (message == buffer_ && strlen(buffer_) == buffer_size_ - 1)
return ERANGE; return ERANGE;
@@ -149,19 +158,22 @@ int safe_strerror(
} }
// Handle the case when strerror_r is not available. // Handle the case when strerror_r is not available.
int handle(internal::Null<>) { int handle(internal::Null<>)
{
return fallback(strerror_s(buffer_, buffer_size_, error_code_)); return fallback(strerror_s(buffer_, buffer_size_, error_code_));
} }
// Fallback to strerror_s when strerror_r is not available. // Fallback to strerror_s when strerror_r is not available.
int fallback(int result) { int fallback(int result)
{
// If the buffer is full then the message is probably truncated. // If the buffer is full then the message is probably truncated.
return result == 0 && strlen(buffer_) == buffer_size_ - 1 ? return result == 0 && strlen(buffer_) == buffer_size_ - 1 ?
ERANGE : result; ERANGE : result;
} }
// Fallback to strerror if strerror_r and strerror_s are not available. // Fallback to strerror if strerror_r and strerror_s are not available.
int fallback(internal::Null<>) { int fallback(internal::Null<>)
{
errno = 0; errno = 0;
buffer_ = strerror(error_code_); buffer_ = strerror(error_code_);
return errno; return errno;
@@ -169,10 +181,13 @@ int safe_strerror(
public: public:
StrError(int err_code, char *&buf, std::size_t buf_size) StrError(int err_code, char *&buf, std::size_t buf_size)
: error_code_(err_code), buffer_(buf), buffer_size_(buf_size) {} : error_code_(err_code), buffer_(buf), buffer_size_(buf_size)
{}
int run() { int run()
strerror_r(0, 0, ""); // Suppress a warning about unused strerror_r. {
// Suppress a warning about unused strerror_r.
strerror_r(0, FMT_NULL, "");
return handle(strerror_r(error_code_, buffer_, buffer_size_)); return handle(strerror_r(error_code_, buffer_, buffer_size_));
} }
}; };
@@ -180,7 +195,8 @@ int safe_strerror(
} }
void format_error_code(Writer &out, int error_code, void format_error_code(Writer &out, int error_code,
StringRef message) FMT_NOEXCEPT { StringRef message) FMT_NOEXCEPT
{
// Report error code making sure that the output fits into // Report error code making sure that the output fits into
// INLINE_BUFFER_SIZE to avoid dynamic memory allocation and potential // INLINE_BUFFER_SIZE to avoid dynamic memory allocation and potential
// bad_alloc. // bad_alloc.
@@ -203,7 +219,8 @@ void format_error_code(Writer &out, int error_code,
} }
void report_error(FormatFunc func, int error_code, void report_error(FormatFunc func, int error_code,
StringRef message) FMT_NOEXCEPT { StringRef message) FMT_NOEXCEPT
{
MemoryWriter full_message; MemoryWriter full_message;
func(full_message, error_code, message); func(full_message, error_code, message);
// Use Writer::data instead of Writer::c_str to avoid potential memory // Use Writer::data instead of Writer::c_str to avoid potential memory
@@ -218,13 +235,15 @@ namespace internal {
// This method is used to preserve binary compatibility with fmt 3.0. // This method is used to preserve binary compatibility with fmt 3.0.
// It can be removed in 4.0. // It can be removed in 4.0.
FMT_FUNC void format_system_error( FMT_FUNC void format_system_error(
Writer &out, int error_code, StringRef message) FMT_NOEXCEPT { Writer &out, int error_code, StringRef message) FMT_NOEXCEPT
{
fmt::format_system_error(out, error_code, message); fmt::format_system_error(out, error_code, message);
} }
} // namespace internal } // namespace internal
FMT_FUNC void SystemError::init( FMT_FUNC void SystemError::init(
int err_code, CStringRef format_str, ArgList args) { int err_code, CStringRef format_str, ArgList args)
{
error_code_ = err_code; error_code_ = err_code;
MemoryWriter w; MemoryWriter w;
format_system_error(w, err_code, format(format_str, args)); format_system_error(w, err_code, format(format_str, args));
@@ -235,7 +254,8 @@ FMT_FUNC void SystemError::init(
template <typename T> template <typename T>
int internal::CharTraits<char>::format_float( int internal::CharTraits<char>::format_float(
char *buffer, std::size_t size, const char *format, char *buffer, std::size_t size, const char *format,
unsigned width, int precision, T value) { unsigned width, int precision, T value)
{
if (width == 0) { if (width == 0) {
return precision < 0 ? return precision < 0 ?
FMT_SNPRINTF(buffer, size, format, value) : FMT_SNPRINTF(buffer, size, format, value) :
@@ -249,7 +269,8 @@ int internal::CharTraits<char>::format_float(
template <typename T> template <typename T>
int internal::CharTraits<wchar_t>::format_float( int internal::CharTraits<wchar_t>::format_float(
wchar_t *buffer, std::size_t size, const wchar_t *format, wchar_t *buffer, std::size_t size, const wchar_t *format,
unsigned width, int precision, T value) { unsigned width, int precision, T value)
{
if (width == 0) { if (width == 0) {
return precision < 0 ? return precision < 0 ?
FMT_SWPRINTF(buffer, size, format, value) : FMT_SWPRINTF(buffer, size, format, value) :
@@ -294,7 +315,8 @@ const uint64_t internal::BasicData<T>::POWERS_OF_10_64[] = {
ULongLong(1000000000) * ULongLong(1000000000) * 10 ULongLong(1000000000) * ULongLong(1000000000) * 10
}; };
FMT_FUNC void internal::report_unknown_type(char code, const char *type) { FMT_FUNC void internal::report_unknown_type(char code, const char *type)
{
(void)type; (void)type;
if (std::isprint(static_cast<unsigned char>(code))) { if (std::isprint(static_cast<unsigned char>(code))) {
FMT_THROW(FormatError( FMT_THROW(FormatError(
@@ -307,13 +329,14 @@ FMT_FUNC void internal::report_unknown_type(char code, const char *type) {
#if FMT_USE_WINDOWS_H #if FMT_USE_WINDOWS_H
FMT_FUNC internal::UTF8ToUTF16::UTF8ToUTF16(StringRef s) { FMT_FUNC internal::UTF8ToUTF16::UTF8ToUTF16(StringRef s)
{
static const char ERROR_MSG[] = "cannot convert string from UTF-8 to UTF-16"; static const char ERROR_MSG[] = "cannot convert string from UTF-8 to UTF-16";
if (s.size() > INT_MAX) if (s.size() > INT_MAX)
FMT_THROW(WindowsError(ERROR_INVALID_PARAMETER, ERROR_MSG)); FMT_THROW(WindowsError(ERROR_INVALID_PARAMETER, ERROR_MSG));
int s_size = static_cast<int>(s.size()); int s_size = static_cast<int>(s.size());
int length = MultiByteToWideChar( int length = MultiByteToWideChar(
CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), s_size, 0, 0); CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), s_size, FMT_NULL, 0);
if (length == 0) if (length == 0)
FMT_THROW(WindowsError(GetLastError(), ERROR_MSG)); FMT_THROW(WindowsError(GetLastError(), ERROR_MSG));
buffer_.resize(length + 1); buffer_.resize(length + 1);
@@ -324,23 +347,26 @@ FMT_FUNC internal::UTF8ToUTF16::UTF8ToUTF16(StringRef s) {
buffer_[length] = 0; buffer_[length] = 0;
} }
FMT_FUNC internal::UTF16ToUTF8::UTF16ToUTF8(WStringRef s) { FMT_FUNC internal::UTF16ToUTF8::UTF16ToUTF8(WStringRef s)
{
if (int error_code = convert(s)) { if (int error_code = convert(s)) {
FMT_THROW(WindowsError(error_code, FMT_THROW(WindowsError(error_code,
"cannot convert string from UTF-16 to UTF-8")); "cannot convert string from UTF-16 to UTF-8"));
} }
} }
FMT_FUNC int internal::UTF16ToUTF8::convert(WStringRef s) { FMT_FUNC int internal::UTF16ToUTF8::convert(WStringRef s)
{
if (s.size() > INT_MAX) if (s.size() > INT_MAX)
return ERROR_INVALID_PARAMETER; return ERROR_INVALID_PARAMETER;
int s_size = static_cast<int>(s.size()); int s_size = static_cast<int>(s.size());
int length = WideCharToMultiByte(CP_UTF8, 0, s.data(), s_size, 0, 0, 0, 0); int length = WideCharToMultiByte(
CP_UTF8, 0, s.data(), s_size, FMT_NULL, 0, FMT_NULL, FMT_NULL);
if (length == 0) if (length == 0)
return GetLastError(); return GetLastError();
buffer_.resize(length + 1); buffer_.resize(length + 1);
length = WideCharToMultiByte( length = WideCharToMultiByte(
CP_UTF8, 0, s.data(), s_size, &buffer_[0], length, 0, 0); CP_UTF8, 0, s.data(), s_size, &buffer_[0], length, FMT_NULL, FMT_NULL);
if (length == 0) if (length == 0)
return GetLastError(); return GetLastError();
buffer_[length] = 0; buffer_[length] = 0;
@@ -348,7 +374,8 @@ FMT_FUNC int internal::UTF16ToUTF8::convert(WStringRef s) {
} }
FMT_FUNC void WindowsError::init( FMT_FUNC void WindowsError::init(
int err_code, CStringRef format_str, ArgList args) { int err_code, CStringRef format_str, ArgList args)
{
error_code_ = err_code; error_code_ = err_code;
MemoryWriter w; MemoryWriter w;
internal::format_windows_error(w, err_code, format(format_str, args)); internal::format_windows_error(w, err_code, format(format_str, args));
@@ -357,15 +384,17 @@ FMT_FUNC void WindowsError::init(
} }
FMT_FUNC void internal::format_windows_error( FMT_FUNC void internal::format_windows_error(
Writer &out, int error_code, StringRef message) FMT_NOEXCEPT { Writer &out, int error_code, StringRef message) FMT_NOEXCEPT
{
FMT_TRY{ FMT_TRY{
MemoryBuffer<wchar_t, INLINE_BUFFER_SIZE> buffer; MemoryBuffer<wchar_t, INLINE_BUFFER_SIZE> buffer;
buffer.resize(INLINE_BUFFER_SIZE); buffer.resize(INLINE_BUFFER_SIZE);
for (;;) { for (;;) {
wchar_t *system_message = &buffer[0]; wchar_t *system_message = &buffer[0];
int result = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, int result = FormatMessageW(
0, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
system_message, static_cast<uint32_t>(buffer.size()), 0); FMT_NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
system_message, static_cast<uint32_t>(buffer.size()), FMT_NULL);
if (result != 0) { if (result != 0) {
UTF16ToUTF8 utf8_message; UTF16ToUTF8 utf8_message;
if (utf8_message.convert(system_message) == ERROR_SUCCESS) { if (utf8_message.convert(system_message) == ERROR_SUCCESS) {
@@ -378,14 +407,16 @@ FMT_FUNC void internal::format_windows_error(
break; // Can't get error message, report error code instead. break; // Can't get error message, report error code instead.
buffer.resize(buffer.size() * 2); buffer.resize(buffer.size() * 2);
} }
} FMT_CATCH(...) {} } FMT_CATCH(...)
{}
fmt::format_error_code(out, error_code, message); // 'fmt::' is for bcc32. fmt::format_error_code(out, error_code, message); // 'fmt::' is for bcc32.
} }
#endif // FMT_USE_WINDOWS_H #endif // FMT_USE_WINDOWS_H
FMT_FUNC void format_system_error( FMT_FUNC void format_system_error(
Writer &out, int error_code, StringRef message) FMT_NOEXCEPT { Writer &out, int error_code, StringRef message) FMT_NOEXCEPT
{
FMT_TRY{ FMT_TRY{
internal::MemoryBuffer<char, internal::INLINE_BUFFER_SIZE> buffer; internal::MemoryBuffer<char, internal::INLINE_BUFFER_SIZE> buffer;
buffer.resize(internal::INLINE_BUFFER_SIZE); buffer.resize(internal::INLINE_BUFFER_SIZE);
@@ -400,16 +431,18 @@ FMT_FUNC void format_system_error(
break; // Can't get error message, report error code instead. break; // Can't get error message, report error code instead.
buffer.resize(buffer.size() * 2); buffer.resize(buffer.size() * 2);
} }
} FMT_CATCH(...) {} } FMT_CATCH(...)
{}
fmt::format_error_code(out, error_code, message); // 'fmt::' is for bcc32. fmt::format_error_code(out, error_code, message); // 'fmt::' is for bcc32.
} }
template <typename Char> template <typename Char>
void internal::ArgMap<Char>::init(const ArgList &args) { void internal::ArgMap<Char>::init(const ArgList &args)
{
if (!map_.empty()) if (!map_.empty())
return; return;
typedef internal::NamedArg<Char> NamedArg; typedef internal::NamedArg<Char> NamedArg;
const NamedArg *named_arg = 0; const NamedArg *named_arg = FMT_NULL;
bool use_values = bool use_values =
args.type(ArgList::MAX_PACKED_ARGS - 1) == internal::Arg::NONE; args.type(ArgList::MAX_PACKED_ARGS - 1) == internal::Arg::NONE;
if (use_values) { if (use_values) {
@@ -450,12 +483,14 @@ void internal::ArgMap<Char>::init(const ArgList &args) {
} }
template <typename Char> template <typename Char>
void internal::FixedBuffer<Char>::grow(std::size_t) { void internal::FixedBuffer<Char>::grow(std::size_t)
{
FMT_THROW(std::runtime_error("buffer overflow")); FMT_THROW(std::runtime_error("buffer overflow"));
} }
FMT_FUNC Arg internal::FormatterBase::do_get_arg( FMT_FUNC Arg internal::FormatterBase::do_get_arg(
unsigned arg_index, const char *&error) { unsigned arg_index, const char *&error)
{
Arg arg = args_[arg_index]; Arg arg = args_[arg_index];
switch (arg.type) { switch (arg.type) {
case Arg::NONE: case Arg::NONE:
@@ -471,30 +506,35 @@ FMT_FUNC Arg internal::FormatterBase::do_get_arg(
} }
FMT_FUNC void report_system_error( FMT_FUNC void report_system_error(
int error_code, fmt::StringRef message) FMT_NOEXCEPT { int error_code, fmt::StringRef message) FMT_NOEXCEPT
{
// 'fmt::' is for bcc32. // 'fmt::' is for bcc32.
report_error(format_system_error, error_code, message); report_error(format_system_error, error_code, message);
} }
#if FMT_USE_WINDOWS_H #if FMT_USE_WINDOWS_H
FMT_FUNC void report_windows_error( FMT_FUNC void report_windows_error(
int error_code, fmt::StringRef message) FMT_NOEXCEPT { int error_code, fmt::StringRef message) FMT_NOEXCEPT
{
// 'fmt::' is for bcc32. // 'fmt::' is for bcc32.
report_error(internal::format_windows_error, error_code, message); report_error(internal::format_windows_error, error_code, message);
} }
#endif #endif
FMT_FUNC void print(std::FILE *f, CStringRef format_str, ArgList args) { FMT_FUNC void print(std::FILE *f, CStringRef format_str, ArgList args)
{
MemoryWriter w; MemoryWriter w;
w.write(format_str, args); w.write(format_str, args);
std::fwrite(w.data(), 1, w.size(), f); std::fwrite(w.data(), 1, w.size(), f);
} }
FMT_FUNC void print(CStringRef format_str, ArgList args) { FMT_FUNC void print(CStringRef format_str, ArgList args)
{
print(stdout, format_str, args); print(stdout, format_str, args);
} }
FMT_FUNC void print_colored(Color c, CStringRef format, ArgList args) { FMT_FUNC void print_colored(Color c, CStringRef format, ArgList args)
{
char escape[] = "\x1b[30m"; char escape[] = "\x1b[30m";
escape[3] = static_cast<char>('0' + c); escape[3] = static_cast<char>('0' + c);
std::fputs(escape, stdout); std::fputs(escape, stdout);
@@ -502,16 +542,6 @@ FMT_FUNC void print_colored(Color c, CStringRef format, ArgList args) {
std::fputs(RESET_COLOR, stdout); std::fputs(RESET_COLOR, stdout);
} }
template <typename Char>
void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format, ArgList args);
FMT_FUNC int fprintf(std::FILE *f, CStringRef format, ArgList args) {
MemoryWriter w;
printf(w, format, args);
std::size_t size = w.size();
return std::fwrite(w.data(), 1, size, f) < size ? -1 : static_cast<int>(size);
}
#ifndef FMT_HEADER_ONLY #ifndef FMT_HEADER_ONLY
template struct internal::BasicData<void>; template struct internal::BasicData<void>;
@@ -522,8 +552,6 @@ template void internal::FixedBuffer<char>::grow(std::size_t);
template void internal::ArgMap<char>::init(const ArgList &args); template void internal::ArgMap<char>::init(const ArgList &args);
template void PrintfFormatter<char>::format(CStringRef format);
template int internal::CharTraits<char>::format_float( template int internal::CharTraits<char>::format_float(
char *buffer, std::size_t size, const char *format, char *buffer, std::size_t size, const char *format,
unsigned width, int precision, double value); unsigned width, int precision, double value);
@@ -538,8 +566,6 @@ template void internal::FixedBuffer<wchar_t>::grow(std::size_t);
template void internal::ArgMap<wchar_t>::init(const ArgList &args); template void internal::ArgMap<wchar_t>::init(const ArgList &args);
template void PrintfFormatter<wchar_t>::format(WCStringRef format);
template int internal::CharTraits<wchar_t>::format_float( template int internal::CharTraits<wchar_t>::format_float(
wchar_t *buffer, std::size_t size, const wchar_t *format, wchar_t *buffer, std::size_t size, const wchar_t *format,
unsigned width, int precision, double value); unsigned width, int precision, double value);

View File

@@ -40,6 +40,9 @@
#include <vector> #include <vector>
#include <utility> #include <utility>
// The fmt library version in the form major * 10000 + minor * 100 + patch.
#define FMT_VERSION 30002
#ifdef _SECURE_SCL #ifdef _SECURE_SCL
# define FMT_SECURE_SCL _SECURE_SCL # define FMT_SECURE_SCL _SECURE_SCL
#else #else
@@ -180,20 +183,50 @@ typedef __int64 intmax_t;
# define FMT_USE_NOEXCEPT 0 # define FMT_USE_NOEXCEPT 0
#endif #endif
#ifndef FMT_NOEXCEPT
# if FMT_EXCEPTIONS
#if FMT_USE_NOEXCEPT || FMT_HAS_FEATURE(cxx_noexcept) || \ #if FMT_USE_NOEXCEPT || FMT_HAS_FEATURE(cxx_noexcept) || \
(FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \ (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \
FMT_MSC_VER >= 1900 FMT_MSC_VER >= 1900
# define FMT_NOEXCEPT noexcept # define FMT_DETECTED_NOEXCEPT noexcept
#else #else
# define FMT_NOEXCEPT throw() # define FMT_DETECTED_NOEXCEPT throw()
#endif #endif
#ifndef FMT_NOEXCEPT
# if FMT_EXCEPTIONS
# define FMT_NOEXCEPT FMT_DETECTED_NOEXCEPT
# else # else
# define FMT_NOEXCEPT # define FMT_NOEXCEPT
# endif # endif
#endif #endif
// This is needed because GCC still uses throw() in its headers when exceptions
// are disabled.
#if FMT_GCC_VERSION
# define FMT_DTOR_NOEXCEPT FMT_DETECTED_NOEXCEPT
#else
# define FMT_DTOR_NOEXCEPT FMT_NOEXCEPT
#endif
#ifndef FMT_OVERRIDE
# if (defined(FMT_USE_OVERRIDE) && FMT_USE_OVERRIDE) || FMT_HAS_FEATURE(cxx_override) || \
(FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \
FMT_MSC_VER >= 1900
# define FMT_OVERRIDE override
# else
# define FMT_OVERRIDE
# endif
#endif
#ifndef FMT_NULL
# if FMT_HAS_FEATURE(cxx_nullptr) || \
(FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \
FMT_MSC_VER >= 1600
# define FMT_NULL nullptr
# else
# define FMT_NULL NULL
# endif
#endif
// A macro to disallow the copy constructor and operator= functions // A macro to disallow the copy constructor and operator= functions
// This should be used in the private: declarations for a class // This should be used in the private: declarations for a class
#ifndef FMT_USE_DELETED_FUNCTIONS #ifndef FMT_USE_DELETED_FUNCTIONS
@@ -225,6 +258,21 @@ typedef __int64 intmax_t;
(!defined(FMT_ICC_VERSION) || FMT_ICC_VERSION >= 1500) (!defined(FMT_ICC_VERSION) || FMT_ICC_VERSION >= 1500)
#endif #endif
#ifndef FMT_USE_EXTERN_TEMPLATES
// Clang doesn't have a feature check for extern templates so we check
// for variadic templates which were introduced in the same version.
// For GCC according to cppreference.com they were introduced in 3.3.
# define FMT_USE_EXTERN_TEMPLATES \
((__clang__ && FMT_USE_VARIADIC_TEMPLATES) || \
(FMT_GCC_VERSION >= 303 && FMT_HAS_GXX_CXX11))
#endif
#ifdef FMT_HEADER_ONLY
// If header only do not use extern templates.
# undef FMT_USE_EXTERN_TEMPLATES
# define FMT_USE_EXTERN_TEMPLATES 0
#endif
#ifndef FMT_ASSERT #ifndef FMT_ASSERT
# define FMT_ASSERT(condition, message) assert((condition) && message) # define FMT_ASSERT(condition, message) assert((condition) && message)
#endif #endif
@@ -387,8 +435,11 @@ public:
static bool isnegative(double x) static bool isnegative(double x)
{ {
using namespace fmt::internal; using namespace fmt::internal;
if (const_check(sizeof(signbit(x)) == sizeof(int))) if (const_check(sizeof(signbit(x)) == sizeof(bool) ||
sizeof(signbit(x)) == sizeof(int)))
{
return signbit(x) != 0; return signbit(x) != 0;
}
if (x < 0) return true; if (x < 0) return true;
if (!isnotanumber(x)) return false; if (!isnotanumber(x)) return false;
int dec = 0, sign = 0; int dec = 0, sign = 0;
@@ -460,7 +511,8 @@ private:
public: public:
/** Constructs a string reference object from a C string and a size. */ /** Constructs a string reference object from a C string and a size. */
BasicStringRef(const Char *s, std::size_t size) : data_(s), size_(size) {} BasicStringRef(const Char *s, std::size_t size): data_(s), size_(size)
{}
/** /**
\rst \rst
@@ -469,7 +521,8 @@ public:
\endrst \endrst
*/ */
BasicStringRef(const Char *s) BasicStringRef(const Char *s)
: data_(s), size_(std::char_traits<Char>::length(s)) {} : data_(s), size_(std::char_traits<Char>::length(s))
{}
/** /**
\rst \rst
@@ -477,7 +530,8 @@ public:
\endrst \endrst
*/ */
BasicStringRef(const std::basic_string<Char> &s) BasicStringRef(const std::basic_string<Char> &s)
: data_(s.c_str()), size_(s.size()) {} : data_(s.c_str()), size_(s.size())
{}
/** /**
\rst \rst
@@ -573,14 +627,16 @@ private:
public: public:
/** Constructs a string reference object from a C string. */ /** Constructs a string reference object from a C string. */
BasicCStringRef(const Char *s) : data_(s) {} BasicCStringRef(const Char *s): data_(s)
{}
/** /**
\rst \rst
Constructs a string reference from an ``std::string`` object. Constructs a string reference from an ``std::string`` object.
\endrst \endrst
*/ */
BasicCStringRef(const std::basic_string<Char> &s) : data_(s.c_str()) {} BasicCStringRef(const std::basic_string<Char> &s): data_(s.c_str())
{}
/** Returns the pointer to a C string. */ /** Returns the pointer to a C string. */
const Char *c_str() const const Char *c_str() const
@@ -597,8 +653,11 @@ class FormatError : public std::runtime_error
{ {
public: public:
explicit FormatError(CStringRef message) explicit FormatError(CStringRef message)
: std::runtime_error(message.c_str()) {} : std::runtime_error(message.c_str())
~FormatError() throw(); {}
FormatError(const FormatError &ferr): std::runtime_error(ferr)
{}
~FormatError() FMT_DTOR_NOEXCEPT;
}; };
namespace internal namespace internal
@@ -632,7 +691,10 @@ inline typename MakeUnsigned<Int>::Type to_unsigned(Int value)
// The number of characters to store in the MemoryBuffer object itself // The number of characters to store in the MemoryBuffer object itself
// to avoid dynamic memory allocation. // to avoid dynamic memory allocation.
enum { INLINE_BUFFER_SIZE = 500 }; enum
{
INLINE_BUFFER_SIZE = 500
};
#if FMT_SECURE_SCL #if FMT_SECURE_SCL
// Use checked iterator to avoid warnings on MSVC. // Use checked iterator to avoid warnings on MSVC.
@@ -666,8 +728,9 @@ protected:
std::size_t size_; std::size_t size_;
std::size_t capacity_; std::size_t capacity_;
Buffer(T *ptr = 0, std::size_t capacity = 0) Buffer(T *ptr = FMT_NULL, std::size_t capacity = 0)
: ptr_(ptr), size_(0), capacity_(capacity) {} : ptr_(ptr), size_(0), capacity_(capacity)
{}
/** /**
\rst \rst
@@ -678,7 +741,8 @@ protected:
virtual void grow(std::size_t size) = 0; virtual void grow(std::size_t size) = 0;
public: public:
virtual ~Buffer() {} virtual ~Buffer()
{}
/** Returns the size of this buffer. */ /** Returns the size of this buffer. */
std::size_t size() const std::size_t size() const
@@ -713,7 +777,10 @@ public:
grow(capacity); grow(capacity);
} }
void clear() FMT_NOEXCEPT { size_ = 0; } void clear() FMT_NOEXCEPT
{
size_ = 0;
}
void push_back(const T &value) void push_back(const T &value)
{ {
@@ -766,11 +833,12 @@ private:
} }
protected: protected:
void grow(std::size_t size); void grow(std::size_t size) FMT_OVERRIDE;
public: public:
explicit MemoryBuffer(const Allocator &alloc = Allocator()) explicit MemoryBuffer(const Allocator &alloc = Allocator())
: Allocator(alloc), Buffer<T>(data_, SIZE) {} : Allocator(alloc), Buffer<T>(data_, SIZE)
{}
~MemoryBuffer() ~MemoryBuffer()
{ {
deallocate(); deallocate();
@@ -828,7 +896,7 @@ void MemoryBuffer<T, SIZE, Allocator>::grow(std::size_t size)
std::size_t new_capacity = this->capacity_ + this->capacity_ / 2; std::size_t new_capacity = this->capacity_ + this->capacity_ / 2;
if (size > new_capacity) if (size > new_capacity)
new_capacity = size; new_capacity = size;
T *new_ptr = this->allocate(new_capacity); T *new_ptr = this->allocate(new_capacity, FMT_NULL);
// The following code doesn't throw, so the raw pointer above doesn't leak. // The following code doesn't throw, so the raw pointer above doesn't leak.
std::uninitialized_copy(this->ptr_, this->ptr_ + this->size_, std::uninitialized_copy(this->ptr_, this->ptr_ + this->size_,
make_ptr(new_ptr, new_capacity)); make_ptr(new_ptr, new_capacity));
@@ -848,10 +916,11 @@ template <typename Char>
class FixedBuffer: public fmt::Buffer<Char> class FixedBuffer: public fmt::Buffer<Char>
{ {
public: public:
FixedBuffer(Char *array, std::size_t size) : fmt::Buffer<Char>(array, size) {} FixedBuffer(Char *array, std::size_t size): fmt::Buffer<Char>(array, size)
{}
protected: protected:
FMT_API void grow(std::size_t size); FMT_API void grow(std::size_t size) FMT_OVERRIDE;
}; };
template <typename Char> template <typename Char>
@@ -891,6 +960,15 @@ public:
const char *format, unsigned width, int precision, T value); const char *format, unsigned width, int precision, T value);
}; };
#if FMT_USE_EXTERN_TEMPLATES
extern template int CharTraits<char>::format_float<double>
(char *buffer, std::size_t size,
const char* format, unsigned width, int precision, double value);
extern template int CharTraits<char>::format_float<long double>
(char *buffer, std::size_t size,
const char* format, unsigned width, int precision, long double value);
#endif
template <> template <>
class CharTraits<wchar_t>: public BasicCharTraits<wchar_t> class CharTraits<wchar_t>: public BasicCharTraits<wchar_t>
{ {
@@ -909,6 +987,15 @@ public:
const wchar_t *format, unsigned width, int precision, T value); const wchar_t *format, unsigned width, int precision, T value);
}; };
#if FMT_USE_EXTERN_TEMPLATES
extern template int CharTraits<wchar_t>::format_float<double>
(wchar_t *buffer, std::size_t size,
const wchar_t* format, unsigned width, int precision, double value);
extern template int CharTraits<wchar_t>::format_float<long double>
(wchar_t *buffer, std::size_t size,
const wchar_t* format, unsigned width, int precision, long double value);
#endif
// Checks if a number is negative - used to avoid warnings. // Checks if a number is negative - used to avoid warnings.
template <bool IsSigned> template <bool IsSigned>
struct SignChecker struct SignChecker
@@ -972,13 +1059,7 @@ struct FMT_API BasicData
static const char DIGITS[]; static const char DIGITS[];
}; };
#ifndef FMT_USE_EXTERN_TEMPLATES #if FMT_USE_EXTERN_TEMPLATES
// Clang doesn't have a feature check for extern templates so we check
// for variadic templates which were introduced in the same version.
# define FMT_USE_EXTERN_TEMPLATES (__clang__ && FMT_USE_VARIADIC_TEMPLATES)
#endif
#if FMT_USE_EXTERN_TEMPLATES && !defined(FMT_HEADER_ONLY)
extern template struct BasicData<void>; extern template struct BasicData<void>;
#endif #endif
@@ -1027,7 +1108,8 @@ inline unsigned count_digits(uint32_t n)
struct NoThousandsSep struct NoThousandsSep
{ {
template <typename Char> template <typename Char>
void operator()(Char *) {} void operator()(Char *)
{}
}; };
// A functor that adds a thousands separator. // A functor that adds a thousands separator.
@@ -1040,7 +1122,8 @@ private:
unsigned digit_index_; unsigned digit_index_;
public: public:
explicit ThousandsSep(fmt::StringRef sep) : sep_(sep), digit_index_(0) {} explicit ThousandsSep(fmt::StringRef sep): sep_(sep), digit_index_(0)
{}
template <typename Char> template <typename Char>
void operator()(Char *&buffer) void operator()(Char *&buffer)
@@ -1087,7 +1170,8 @@ inline void format_decimal(Char *buffer, UInt value, unsigned num_digits,
template <typename UInt, typename Char> template <typename UInt, typename Char>
inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) inline void format_decimal(Char *buffer, UInt value, unsigned num_digits)
{ {
return format_decimal(buffer, value, num_digits, NoThousandsSep()); format_decimal(buffer, value, num_digits, NoThousandsSep());
return;
} }
#ifndef _WIN32 #ifndef _WIN32
@@ -1134,7 +1218,8 @@ private:
MemoryBuffer<char, INLINE_BUFFER_SIZE> buffer_; MemoryBuffer<char, INLINE_BUFFER_SIZE> buffer_;
public: public:
UTF16ToUTF8() {} UTF16ToUTF8()
{}
FMT_API explicit UTF16ToUTF8(WStringRef s); FMT_API explicit UTF16ToUTF8(WStringRef s);
operator StringRef() const operator StringRef() const
{ {
@@ -1218,9 +1303,12 @@ struct Arg : Value
template <typename Char> template <typename Char>
struct NamedArg; struct NamedArg;
template <typename Char, typename T>
struct NamedArgWithType;
template <typename T = void> template <typename T = void>
struct Null {}; struct Null
{};
// A helper class template to enable or disable overloads taking wide // A helper class template to enable or disable overloads taking wide
// characters and strings in MakeValue. // characters and strings in MakeValue.
@@ -1251,13 +1339,19 @@ No &convert(...);
template<typename T, bool ENABLE_CONVERSION> template<typename T, bool ENABLE_CONVERSION>
struct ConvertToIntImpl struct ConvertToIntImpl
{ {
enum { value = ENABLE_CONVERSION }; enum
{
value = ENABLE_CONVERSION
};
}; };
template<typename T, bool ENABLE_CONVERSION> template<typename T, bool ENABLE_CONVERSION>
struct ConvertToIntImpl2 struct ConvertToIntImpl2
{ {
enum { value = false }; enum
{
value = false
};
}; };
template<typename T> template<typename T>
@@ -1273,8 +1367,14 @@ struct ConvertToIntImpl2<T, true>
template<typename T> template<typename T>
struct ConvertToInt struct ConvertToInt
{ {
enum { enable_conversion = sizeof(convert(get<T>())) == sizeof(Yes) }; enum
enum { value = ConvertToIntImpl2<T, enable_conversion>::value }; {
enable_conversion = sizeof(fmt::internal::convert(get<T>())) == sizeof(Yes)
};
enum
{
value = ConvertToIntImpl2<T, enable_conversion>::value
};
}; };
#define FMT_DISABLE_CONVERSION_TO_INT(Type) \ #define FMT_DISABLE_CONVERSION_TO_INT(Type) \
@@ -1287,7 +1387,8 @@ FMT_DISABLE_CONVERSION_TO_INT(double);
FMT_DISABLE_CONVERSION_TO_INT(long double); FMT_DISABLE_CONVERSION_TO_INT(long double);
template<bool B, class T = void> template<bool B, class T = void>
struct EnableIf {}; struct EnableIf
{};
template<class T> template<class T>
struct EnableIf<true, T> struct EnableIf<true, T>
@@ -1311,18 +1412,34 @@ struct Conditional<false, T, F>
template <bool> template <bool>
struct Not struct Not
{ {
enum { value = 0 }; enum
{
value = 0
};
}; };
template <> template <>
struct Not<false> struct Not<false>
{ {
enum { value = 1 }; enum
{
value = 1
};
};
template <typename T>
struct False
{
enum
{
value = 0
};
}; };
template <typename T, T> struct LConvCheck template <typename T, T> struct LConvCheck
{ {
LConvCheck(int) {} LConvCheck(int)
{}
}; };
// Returns the thousands separator for the current locale. // Returns the thousands separator for the current locale.
@@ -1340,6 +1457,36 @@ inline fmt::StringRef thousands_sep(...)
return ""; return "";
} }
#define FMT_CONCAT(a, b) a##b
#if FMT_GCC_VERSION >= 303
# define FMT_UNUSED __attribute__((unused))
#else
# define FMT_UNUSED
#endif
#ifndef FMT_USE_STATIC_ASSERT
# define FMT_USE_STATIC_ASSERT 0
#endif
#if FMT_USE_STATIC_ASSERT || FMT_HAS_FEATURE(cxx_static_assert) || \
(FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1600
# define FMT_STATIC_ASSERT(cond, message) static_assert(cond, message)
#else
# define FMT_CONCAT_(a, b) FMT_CONCAT(a, b)
# define FMT_STATIC_ASSERT(cond, message) \
typedef int FMT_CONCAT_(Assert, __LINE__)[(cond) ? 1 : -1] FMT_UNUSED
#endif
template <typename Formatter, typename Char, typename T>
void format_arg(Formatter &, const Char *, const T &)
{
FMT_STATIC_ASSERT(False<T>::value,
"Cannot format argument. To enable the use of ostream "
"operator<< include fmt/ostream.h. Otherwise provide "
"an overload of format_arg.");
}
// Makes an Arg object from any type. // Makes an Arg object from any type.
template <typename Formatter> template <typename Formatter>
class MakeValue: public Arg class MakeValue: public Arg
@@ -1387,13 +1534,14 @@ private:
static void format_custom_arg( static void format_custom_arg(
void *formatter, const void *arg, void *format_str_ptr) void *formatter, const void *arg, void *format_str_ptr)
{ {
format(*static_cast<Formatter*>(formatter), format_arg(*static_cast<Formatter*>(formatter),
*static_cast<const Char**>(format_str_ptr), *static_cast<const Char**>(format_str_ptr),
*static_cast<const T*>(arg)); *static_cast<const T*>(arg));
} }
public: public:
MakeValue() {} MakeValue()
{}
#define FMT_MAKE_VALUE_(Type, field, TYPE, rhs) \ #define FMT_MAKE_VALUE_(Type, field, TYPE, rhs) \
MakeValue(Type value) { field = rhs; } \ MakeValue(Type value) { field = rhs; } \
@@ -1461,7 +1609,9 @@ public:
FMT_MAKE_VALUE(char *, string.value, CSTRING) FMT_MAKE_VALUE(char *, string.value, CSTRING)
FMT_MAKE_VALUE(const char *, string.value, CSTRING) FMT_MAKE_VALUE(const char *, string.value, CSTRING)
FMT_MAKE_VALUE(signed char *, sstring.value, CSTRING)
FMT_MAKE_VALUE(const signed char *, sstring.value, CSTRING) FMT_MAKE_VALUE(const signed char *, sstring.value, CSTRING)
FMT_MAKE_VALUE(unsigned char *, ustring.value, CSTRING)
FMT_MAKE_VALUE(const unsigned char *, ustring.value, CSTRING) FMT_MAKE_VALUE(const unsigned char *, ustring.value, CSTRING)
FMT_MAKE_STR_VALUE(const std::string &, STRING) FMT_MAKE_STR_VALUE(const std::string &, STRING)
FMT_MAKE_STR_VALUE(StringRef, STRING) FMT_MAKE_STR_VALUE(StringRef, STRING)
@@ -1510,12 +1660,22 @@ public:
{ {
pointer = &value; pointer = &value;
} }
template <typename Char_, typename T>
MakeValue(const NamedArgWithType<Char_, T> &value)
{
pointer = &value;
}
template <typename Char_> template <typename Char_>
static uint64_t type(const NamedArg<Char_> &) static uint64_t type(const NamedArg<Char_> &)
{ {
return Arg::NAMED_ARG; return Arg::NAMED_ARG;
} }
template <typename Char_, typename T>
static uint64_t type(const NamedArgWithType<Char_, T> &)
{
return Arg::NAMED_ARG;
}
}; };
template <typename Formatter> template <typename Formatter>
@@ -1542,14 +1702,26 @@ struct NamedArg : Arg
template <typename T> template <typename T>
NamedArg(BasicStringRef<Char> argname, const T &value) NamedArg(BasicStringRef<Char> argname, const T &value)
: Arg(MakeArg< BasicFormatter<Char> >(value)), name(argname) {} : Arg(MakeArg< BasicFormatter<Char> >(value)), name(argname)
{}
};
template <typename Char, typename T>
struct NamedArgWithType: NamedArg<Char>
{
NamedArgWithType(BasicStringRef<Char> argname, const T &value)
: NamedArg<Char>(argname, value)
{}
}; };
class RuntimeError: public std::runtime_error class RuntimeError: public std::runtime_error
{ {
protected: protected:
RuntimeError() : std::runtime_error("") {} RuntimeError(): std::runtime_error("")
~RuntimeError() throw(); {}
RuntimeError(const RuntimeError &rerr): std::runtime_error(rerr)
{}
~RuntimeError() FMT_DTOR_NOEXCEPT;
}; };
template <typename Char> template <typename Char>
@@ -1576,10 +1748,7 @@ private:
internal::Arg::Type type(unsigned index) const internal::Arg::Type type(unsigned index) const
{ {
unsigned shift = index * 4; return type(types_, index);
uint64_t mask = 0xf;
return static_cast<internal::Arg::Type>(
(types_ & (mask << shift)) >> shift);
} }
template <typename Char> template <typename Char>
@@ -1587,14 +1756,25 @@ private:
public: public:
// Maximum number of arguments with packed types. // Maximum number of arguments with packed types.
enum { MAX_PACKED_ARGS = 16 }; enum
{
MAX_PACKED_ARGS = 16
};
ArgList() : types_(0) {} ArgList(): types_(0)
{}
ArgList(ULongLong types, const internal::Value *values) ArgList(ULongLong types, const internal::Value *values)
: types_(types), values_(values) {} : types_(types), values_(values)
{}
ArgList(ULongLong types, const internal::Arg *args) ArgList(ULongLong types, const internal::Arg *args)
: types_(types), args_(args) {} : types_(types), args_(args)
{}
uint64_t types() const
{
return types_;
}
/** Returns the argument at specified index. */ /** Returns the argument at specified index. */
internal::Arg operator[](unsigned index) const internal::Arg operator[](unsigned index) const
@@ -1625,6 +1805,14 @@ public:
} }
return args_[index]; return args_[index];
} }
static internal::Arg::Type type(uint64_t types, unsigned index)
{
unsigned shift = index * 4;
uint64_t mask = 0xf;
return static_cast<internal::Arg::Type>(
(types & (mask << shift)) >> shift);
}
}; };
#define FMT_DISPATCH(call) static_cast<Impl*>(this)->call #define FMT_DISPATCH(call) static_cast<Impl*>(this)->call
@@ -1660,7 +1848,8 @@ private:
typedef internal::Arg Arg; typedef internal::Arg Arg;
public: public:
void report_unhandled_arg() {} void report_unhandled_arg()
{}
Result visit_unhandled_arg() Result visit_unhandled_arg()
{ {
@@ -1820,7 +2009,8 @@ enum
}; };
// An empty format specifier. // An empty format specifier.
struct EmptySpec {}; struct EmptySpec
{};
// A type specifier. // A type specifier.
template <char TYPE> template <char TYPE>
@@ -1860,7 +2050,8 @@ struct WidthSpec
// two specialization of WidthSpec and its subclasses. // two specialization of WidthSpec and its subclasses.
wchar_t fill_; wchar_t fill_;
WidthSpec(unsigned width, wchar_t fill) : width_(width), fill_(fill) {} WidthSpec(unsigned width, wchar_t fill): width_(width), fill_(fill)
{}
unsigned width() const unsigned width() const
{ {
@@ -1878,7 +2069,8 @@ struct AlignSpec : WidthSpec
Alignment align_; Alignment align_;
AlignSpec(unsigned width, wchar_t fill, Alignment align = ALIGN_DEFAULT) AlignSpec(unsigned width, wchar_t fill, Alignment align = ALIGN_DEFAULT)
: WidthSpec(width, fill), align_(align) {} : WidthSpec(width, fill), align_(align)
{}
Alignment align() const Alignment align() const
{ {
@@ -1895,7 +2087,8 @@ struct AlignSpec : WidthSpec
template <char TYPE> template <char TYPE>
struct AlignTypeSpec: AlignSpec struct AlignTypeSpec: AlignSpec
{ {
AlignTypeSpec(unsigned width, wchar_t fill) : AlignSpec(width, fill) {} AlignTypeSpec(unsigned width, wchar_t fill): AlignSpec(width, fill)
{}
bool flag(unsigned) const bool flag(unsigned) const
{ {
@@ -1916,7 +2109,8 @@ struct FormatSpec : AlignSpec
FormatSpec( FormatSpec(
unsigned width = 0, char type = 0, wchar_t fill = ' ') unsigned width = 0, char type = 0, wchar_t fill = ' ')
: AlignSpec(width, fill), flags_(0), precision_(-1), type_(type) {} : AlignSpec(width, fill), flags_(0), precision_(-1), type_(type)
{}
bool flag(unsigned f) const bool flag(unsigned f) const
{ {
@@ -1941,7 +2135,8 @@ private:
public: public:
IntFormatSpec(T val, const SpecT &spec = SpecT()) IntFormatSpec(T val, const SpecT &spec = SpecT())
: SpecT(spec), value_(val) {} : SpecT(spec), value_(val)
{}
T value() const T value() const
{ {
@@ -2116,7 +2311,7 @@ public:
if (it->first == name) if (it->first == name)
return &it->second; return &it->second;
} }
return 0; return FMT_NULL;
} }
}; };
@@ -2155,13 +2350,14 @@ protected:
void write(const char *value) void write(const char *value)
{ {
Arg::StringValue<char> str = {value, value != 0 ? std::strlen(value) : 0}; Arg::StringValue<char> str = { value, value ? std::strlen(value) : 0 };
writer_.write_str(str, spec_); writer_.write_str(str, spec_);
} }
public: public:
ArgFormatterBase(BasicWriter<Char> &w, FormatSpec &s) ArgFormatterBase(BasicWriter<Char> &w, FormatSpec &s)
: writer_(w), spec_(s) {} : writer_(w), spec_(s)
{}
template <typename T> template <typename T>
void visit_any_int(T value) void visit_any_int(T value)
@@ -2178,7 +2374,10 @@ public:
void visit_bool(bool value) void visit_bool(bool value)
{ {
if (spec_.type_) if (spec_.type_)
return visit_any_int(value); {
visit_any_int(value);
return;
}
write(value); write(value);
} }
@@ -2195,29 +2394,29 @@ public:
typedef typename BasicWriter<Char>::CharPtr CharPtr; typedef typename BasicWriter<Char>::CharPtr CharPtr;
Char fill = internal::CharTraits<Char>::cast(spec_.fill()); Char fill = internal::CharTraits<Char>::cast(spec_.fill());
CharPtr out = CharPtr(); CharPtr out = CharPtr();
const unsigned CHAR_WIDTH = 1; const unsigned CHAR_SIZE = 1;
if (spec_.width_ > CHAR_WIDTH) if (spec_.width_ > CHAR_SIZE)
{ {
out = writer_.grow_buffer(spec_.width_); out = writer_.grow_buffer(spec_.width_);
if (spec_.align_ == ALIGN_RIGHT) if (spec_.align_ == ALIGN_RIGHT)
{ {
std::uninitialized_fill_n(out, spec_.width_ - CHAR_WIDTH, fill); std::uninitialized_fill_n(out, spec_.width_ - CHAR_SIZE, fill);
out += spec_.width_ - CHAR_WIDTH; out += spec_.width_ - CHAR_SIZE;
} }
else if (spec_.align_ == ALIGN_CENTER) else if (spec_.align_ == ALIGN_CENTER)
{ {
out = writer_.fill_padding(out, spec_.width_, out = writer_.fill_padding(out, spec_.width_,
internal::const_check(CHAR_WIDTH), fill); internal::const_check(CHAR_SIZE), fill);
} }
else else
{ {
std::uninitialized_fill_n(out + CHAR_WIDTH, std::uninitialized_fill_n(out + CHAR_SIZE,
spec_.width_ - CHAR_WIDTH, fill); spec_.width_ - CHAR_SIZE, fill);
} }
} }
else else
{ {
out = writer_.grow_buffer(CHAR_WIDTH); out = writer_.grow_buffer(CHAR_SIZE);
} }
*out = internal::CharTraits<Char>::cast(value); *out = internal::CharTraits<Char>::cast(value);
} }
@@ -2342,7 +2541,8 @@ public:
BasicArgFormatter(BasicFormatter<Char, Impl> &formatter, BasicArgFormatter(BasicFormatter<Char, Impl> &formatter,
FormatSpec &spec, const Char *fmt) FormatSpec &spec, const Char *fmt)
: internal::ArgFormatterBase<Impl, Char>(formatter.writer(), spec), : internal::ArgFormatterBase<Impl, Char>(formatter.writer(), spec),
formatter_(formatter), format_(fmt) {} formatter_(formatter), format_(fmt)
{}
/** Formats an argument of a custom (user-defined) type. */ /** Formats an argument of a custom (user-defined) type. */
void visit_custom(internal::Arg::CustomValue c) void visit_custom(internal::Arg::CustomValue c)
@@ -2359,7 +2559,8 @@ public:
/** Constructs an argument formatter object. */ /** Constructs an argument formatter object. */
ArgFormatter(BasicFormatter<Char> &formatter, ArgFormatter(BasicFormatter<Char> &formatter,
FormatSpec &spec, const Char *fmt) FormatSpec &spec, const Char *fmt)
: BasicArgFormatter<ArgFormatter<Char>, Char>(formatter, spec, fmt) {} : BasicArgFormatter<ArgFormatter<Char>, Char>(formatter, spec, fmt)
{}
}; };
/** This template formats data and writes the output to a writer. */ /** This template formats data and writes the output to a writer. */
@@ -2397,7 +2598,8 @@ public:
\endrst \endrst
*/ */
BasicFormatter(const ArgList &args, BasicWriter<Char> &w) BasicFormatter(const ArgList &args, BasicWriter<Char> &w)
: internal::FormatterBase(args), writer_(w) {} : internal::FormatterBase(args), writer_(w)
{}
/** Returns a reference to the writer associated with this formatter. */ /** Returns a reference to the writer associated with this formatter. */
BasicWriter<Char> &writer() BasicWriter<Char> &writer()
@@ -2492,10 +2694,12 @@ struct ArgType
{ {
uint64_t type; uint64_t type;
ArgType() : type(0) {} ArgType(): type(0)
{}
template <typename T> template <typename T>
ArgType(const T &arg) : type(make_type(arg)) {} ArgType(const T &arg) : type(make_type(arg))
{}
}; };
# define FMT_ARG_TYPE_DEFAULT(n) ArgType t##n = ArgType() # define FMT_ARG_TYPE_DEFAULT(n) ArgType t##n = ArgType()
@@ -2622,7 +2826,8 @@ protected:
typedef char Char; // For FMT_VARIADIC_CTOR. typedef char Char; // For FMT_VARIADIC_CTOR.
SystemError() {} SystemError()
{}
public: public:
/** /**
@@ -2649,7 +2854,7 @@ public:
} }
FMT_VARIADIC_CTOR(SystemError, init, int, CStringRef) FMT_VARIADIC_CTOR(SystemError, init, int, CStringRef)
~SystemError() throw(); ~SystemError() FMT_DTOR_NOEXCEPT;
int error_code() const int error_code() const
{ {
@@ -2805,7 +3010,8 @@ private:
} }
template<typename T> template<typename T>
void append_float_length(Char *&, T) {} void append_float_length(Char *&, T)
{}
template <typename Impl, typename Char_> template <typename Impl, typename Char_>
friend class internal::ArgFormatterBase; friend class internal::ArgFormatterBase;
@@ -2817,7 +3023,8 @@ protected:
/** /**
Constructs a ``BasicWriter`` object. Constructs a ``BasicWriter`` object.
*/ */
explicit BasicWriter(Buffer<Char> &b) : buffer_(b) {} explicit BasicWriter(Buffer<Char> &b): buffer_(b)
{}
public: public:
/** /**
@@ -2825,7 +3032,8 @@ public:
Destroys a ``BasicWriter`` object. Destroys a ``BasicWriter`` object.
\endrst \endrst
*/ */
virtual ~BasicWriter() {} virtual ~BasicWriter()
{}
/** /**
Returns the total number of characters written. Returns the total number of characters written.
@@ -3001,9 +3209,15 @@ public:
return *this; return *this;
} }
void clear() FMT_NOEXCEPT { buffer_.clear(); } void clear() FMT_NOEXCEPT
{
buffer_.clear();
}
Buffer<Char> &buffer() FMT_NOEXCEPT { return buffer_; } Buffer<Char> &buffer() FMT_NOEXCEPT
{
return buffer_;
}
}; };
template <typename Char> template <typename Char>
@@ -3260,7 +3474,10 @@ void BasicWriter<Char>::write_int(T value, Spec spec)
case 'n': case 'n':
{ {
unsigned num_digits = internal::count_digits(abs_value); unsigned num_digits = internal::count_digits(abs_value);
fmt::StringRef sep = internal::thousands_sep(std::localeconv()); fmt::StringRef sep = "";
#ifndef ANDROID
sep = internal::thousands_sep(std::localeconv());
#endif
unsigned size = static_cast<unsigned>( unsigned size = static_cast<unsigned>(
num_digits + sep.size() * ((num_digits - 1) / 3)); num_digits + sep.size() * ((num_digits - 1) / 3));
CharPtr p = prepare_int_buffer(size, spec, prefix, prefix_size) + 1; CharPtr p = prepare_int_buffer(size, spec, prefix, prefix_size) + 1;
@@ -3365,7 +3582,10 @@ void BasicWriter<Char>::write_double(T value, const FormatSpec &spec)
} }
// Build format string. // Build format string.
enum { MAX_FORMAT_SIZE = 10}; // longest format: %#-*.*Lg enum
{
MAX_FORMAT_SIZE = 10
}; // longest format: %#-*.*Lg
Char format[MAX_FORMAT_SIZE]; Char format[MAX_FORMAT_SIZE];
Char *format_ptr = format; Char *format_ptr = format;
*format_ptr++ = '%'; *format_ptr++ = '%';
@@ -3396,7 +3616,7 @@ void BasicWriter<Char>::write_double(T value, const FormatSpec &spec)
// Format using snprintf. // Format using snprintf.
Char fill = internal::CharTraits<Char>::cast(spec.fill()); Char fill = internal::CharTraits<Char>::cast(spec.fill());
unsigned n = 0; unsigned n = 0;
Char *start = 0; Char *start = FMT_NULL;
for (;;) for (;;)
{ {
std::size_t buffer_size = buffer_.capacity() - offset; std::size_t buffer_size = buffer_.capacity() - offset;
@@ -3501,7 +3721,8 @@ private:
public: public:
explicit BasicMemoryWriter(const Allocator& alloc = Allocator()) explicit BasicMemoryWriter(const Allocator& alloc = Allocator())
: BasicWriter<Char>(buffer_), buffer_(alloc) {} : BasicWriter<Char>(buffer_), buffer_(alloc)
{}
#if FMT_USE_RVALUE_REFERENCES #if FMT_USE_RVALUE_REFERENCES
/** /**
@@ -3512,8 +3733,7 @@ public:
*/ */
BasicMemoryWriter(BasicMemoryWriter &&other) BasicMemoryWriter(BasicMemoryWriter &&other)
: BasicWriter<Char>(buffer_), buffer_(std::move(other.buffer_)) : BasicWriter<Char>(buffer_), buffer_(std::move(other.buffer_))
{ {}
}
/** /**
\rst \rst
@@ -3565,7 +3785,8 @@ public:
\endrst \endrst
*/ */
BasicArrayWriter(Char *array, std::size_t size) BasicArrayWriter(Char *array, std::size_t size)
: BasicWriter<Char>(buffer_), buffer_(array, size) {} : BasicWriter<Char>(buffer_), buffer_(array, size)
{}
/** /**
\rst \rst
@@ -3575,7 +3796,8 @@ public:
*/ */
template <std::size_t SIZE> template <std::size_t SIZE>
explicit BasicArrayWriter(Char(&array)[SIZE]) explicit BasicArrayWriter(Char(&array)[SIZE])
: BasicWriter<Char>(buffer_), buffer_(array, SIZE) {} : BasicWriter<Char>(buffer_), buffer_(array, SIZE)
{}
}; };
typedef BasicArrayWriter<char> ArrayWriter; typedef BasicArrayWriter<char> ArrayWriter;
@@ -3637,7 +3859,10 @@ FMT_API void report_windows_error(int error_code,
#endif #endif
enum Color { BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE }; enum Color
{
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE
};
/** /**
Formats a string and prints it to stdout using ANSI escape sequences Formats a string and prints it to stdout using ANSI escape sequences
@@ -3700,7 +3925,10 @@ class FormatInt
private: private:
// Buffer should be large enough to hold all digits (digits10 + 1), // Buffer should be large enough to hold all digits (digits10 + 1),
// a sign and a null character. // a sign and a null character.
enum {BUFFER_SIZE = std::numeric_limits<ULongLong>::digits10 + 3}; enum
{
BUFFER_SIZE = std::numeric_limits<ULongLong>::digits10 + 3
};
mutable char buffer_[BUFFER_SIZE]; mutable char buffer_[BUFFER_SIZE];
char *str_; char *str_;
@@ -3753,9 +3981,12 @@ public:
{ {
FormatSigned(value); FormatSigned(value);
} }
explicit FormatInt(unsigned value) : str_(format_decimal(value)) {} explicit FormatInt(unsigned value): str_(format_decimal(value))
explicit FormatInt(unsigned long value) : str_(format_decimal(value)) {} {}
explicit FormatInt(ULongLong value) : str_(format_decimal(value)) {} explicit FormatInt(unsigned long value): str_(format_decimal(value))
{}
explicit FormatInt(ULongLong value): str_(format_decimal(value))
{}
/** Returns the number of characters written to the output buffer. */ /** Returns the number of characters written to the output buffer. */
std::size_t size() const std::size_t size() const
@@ -3834,15 +4065,15 @@ inline void format_decimal(char *&buffer, T value)
\endrst \endrst
*/ */
template <typename T> template <typename T>
inline internal::NamedArg<char> arg(StringRef name, const T &arg) inline internal::NamedArgWithType<char, T> arg(StringRef name, const T &arg)
{ {
return internal::NamedArg<char>(name, arg); return internal::NamedArgWithType<char, T>(name, arg);
} }
template <typename T> template <typename T>
inline internal::NamedArg<wchar_t> arg(WStringRef name, const T &arg) inline internal::NamedArgWithType<wchar_t, T> arg(WStringRef name, const T &arg)
{ {
return internal::NamedArg<wchar_t>(name, arg); return internal::NamedArgWithType<wchar_t, T>(name, arg);
} }
// The following two functions are deleted intentionally to disable // The following two functions are deleted intentionally to disable
@@ -3871,7 +4102,6 @@ void arg(WStringRef, const internal::NamedArg<Char>&) FMT_DELETED_OR_UNDEFINED;
#define FMT_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N #define FMT_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
#define FMT_RSEQ_N() 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 #define FMT_RSEQ_N() 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
#define FMT_CONCAT(a, b) a##b
#define FMT_FOR_EACH_(N, f, ...) \ #define FMT_FOR_EACH_(N, f, ...) \
FMT_EXPAND(FMT_CONCAT(FMT_FOR_EACH, N)(f, __VA_ARGS__)) FMT_EXPAND(FMT_CONCAT(FMT_FOR_EACH, N)(f, __VA_ARGS__))
#define FMT_FOR_EACH(f, ...) \ #define FMT_FOR_EACH(f, ...) \
@@ -4064,7 +4294,7 @@ inline internal::Arg BasicFormatter<Char, AF>::get_arg(
template <typename Char, typename AF> template <typename Char, typename AF>
inline internal::Arg BasicFormatter<Char, AF>::parse_arg_index(const Char *&s) inline internal::Arg BasicFormatter<Char, AF>::parse_arg_index(const Char *&s)
{ {
const char *error = 0; const char *error = FMT_NULL;
internal::Arg arg = *s < '0' || *s > '9' ? internal::Arg arg = *s < '0' || *s > '9' ?
next_arg(error) : get_arg(internal::parse_nonnegative_int(s), error); next_arg(error) : get_arg(internal::parse_nonnegative_int(s), error);
if (error) if (error)
@@ -4086,7 +4316,7 @@ inline internal::Arg BasicFormatter<Char, AF>::parse_arg_name(const Char *&s)
c = *++s; c = *++s;
} }
while (internal::is_name_start(c) || ('0' <= c && c <= '9')); while (internal::is_name_start(c) || ('0' <= c && c <= '9'));
const char *error = 0; const char *error = FMT_NULL;
internal::Arg arg = get_arg(BasicStringRef<Char>(start, s - start), error); internal::Arg arg = get_arg(BasicStringRef<Char>(start, s - start), error);
if (error) if (error)
FMT_THROW(FormatError(error)); FMT_THROW(FormatError(error));
@@ -4339,7 +4569,7 @@ struct UdlArg
const Char *str; const Char *str;
template <typename T> template <typename T>
NamedArg<Char> operator=(T &&value) const NamedArgWithType<Char, T> operator=(T &&value) const
{ {
return { str, std::forward<T>(value) }; return { str, std::forward<T>(value) };
} }

View File

@@ -7,15 +7,13 @@
For the license information refer to format.h. For the license information refer to format.h.
*/ */
// Commented out by spdlog to use header only #include "ostream.h"
// #include "fmt/ostream.h"
// #include "fmt/printf.h"
namespace fmt { namespace fmt {
namespace { namespace internal {
// Write the content of w to os. FMT_FUNC void write(std::ostream &os, Writer &w)
void write(std::ostream &os, Writer &w) { {
const char *data = w.data(); const char *data = w.data();
typedef internal::MakeUnsigned<std::streamsize>::Type UnsignedStreamSize; typedef internal::MakeUnsigned<std::streamsize>::Type UnsignedStreamSize;
UnsignedStreamSize size = w.size(); UnsignedStreamSize size = w.size();
@@ -30,16 +28,10 @@ void write(std::ostream &os, Writer &w) {
} }
} }
FMT_FUNC void print(std::ostream &os, CStringRef format_str, ArgList args) { FMT_FUNC void print(std::ostream &os, CStringRef format_str, ArgList args)
{
MemoryWriter w; MemoryWriter w;
w.write(format_str, args); w.write(format_str, args);
write(os, w); internal::write(os, w);
}
FMT_FUNC int fprintf(std::ostream &os, CStringRef format, ArgList args) {
MemoryWriter w;
printf(w, format, args);
write(os, w);
return static_cast<int>(w.size());
} }
} // namespace fmt } // namespace fmt

View File

@@ -10,8 +10,8 @@
#ifndef FMT_OSTREAM_H_ #ifndef FMT_OSTREAM_H_
#define FMT_OSTREAM_H_ #define FMT_OSTREAM_H_
// Commented out by spdlog to use header only // commented out by spdlog
// #include "fmt/format.h" // #include "format.h"
#include <ostream> #include <ostream>
namespace fmt namespace fmt
@@ -77,11 +77,14 @@ struct ConvertToIntImpl<T, true>
value = sizeof(convert(get<DummyStream>() << get<T>())) == sizeof(No) value = sizeof(convert(get<DummyStream>() << get<T>())) == sizeof(No)
}; };
}; };
// Write the content of w to os.
void write(std::ostream &os, Writer &w);
} // namespace internal } // namespace internal
// Formats a value. // Formats a value.
template <typename Char, typename ArgFormatter, typename T> template <typename Char, typename ArgFormatter, typename T>
void format(BasicFormatter<Char, ArgFormatter> &f, void format_arg(BasicFormatter<Char, ArgFormatter> &f,
const Char *&format_str, const T &value) const Char *&format_str, const T &value)
{ {
internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer; internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
@@ -106,18 +109,6 @@ void format(BasicFormatter<Char, ArgFormatter> &f,
*/ */
FMT_API void print(std::ostream &os, CStringRef format_str, ArgList args); FMT_API void print(std::ostream &os, CStringRef format_str, ArgList args);
FMT_VARIADIC(void, print, std::ostream &, CStringRef) FMT_VARIADIC(void, print, std::ostream &, CStringRef)
/**
\rst
Prints formatted data to the stream *os*.
**Example**::
fprintf(cerr, "Don't %s!", "panic");
\endrst
*/
FMT_API int fprintf(std::ostream &os, CStringRef format_str, ArgList args);
FMT_VARIADIC(int, fprintf, std::ostream &, CStringRef)
} // namespace fmt } // namespace fmt
#ifdef FMT_HEADER_ONLY #ifdef FMT_HEADER_ONLY

View File

@@ -13,7 +13,7 @@
#include <algorithm> // std::fill_n #include <algorithm> // std::fill_n
#include <limits> // std::numeric_limits #include <limits> // std::numeric_limits
#include "fmt/format.h" #include "ostream.h"
namespace fmt namespace fmt
{ {
@@ -83,13 +83,19 @@ public:
template <typename T, typename U> template <typename T, typename U>
struct is_same struct is_same
{ {
enum { value = 0 }; enum
{
value = 0
};
}; };
template <typename T> template <typename T>
struct is_same<T, T> struct is_same<T, T>
{ {
enum { value = 1 }; enum
{
value = 1
};
}; };
// An argument visitor that converts an integer argument to T for printf, // An argument visitor that converts an integer argument to T for printf,
@@ -107,7 +113,8 @@ private:
public: public:
ArgConverter(internal::Arg &arg, wchar_t type) ArgConverter(internal::Arg &arg, wchar_t type)
: arg_(arg), type_(type) {} : arg_(arg), type_(type)
{}
void visit_bool(bool value) void visit_bool(bool value)
{ {
@@ -166,7 +173,8 @@ private:
FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter); FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter);
public: public:
explicit CharConverter(internal::Arg &arg) : arg_(arg) {} explicit CharConverter(internal::Arg &arg): arg_(arg)
{}
template <typename T> template <typename T>
void visit_any_int(T value) void visit_any_int(T value)
@@ -186,7 +194,8 @@ private:
FMT_DISALLOW_COPY_AND_ASSIGN(WidthHandler); FMT_DISALLOW_COPY_AND_ASSIGN(WidthHandler);
public: public:
explicit WidthHandler(FormatSpec &spec) : spec_(spec) {} explicit WidthHandler(FormatSpec &spec): spec_(spec)
{}
void report_unhandled_arg() void report_unhandled_arg()
{ {
@@ -248,8 +257,9 @@ public:
specifier information for standard argument types. specifier information for standard argument types.
\endrst \endrst
*/ */
BasicPrintfArgFormatter(BasicWriter<Char> &writer, FormatSpec &spec) BasicPrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
: internal::ArgFormatterBase<Impl, Char>(writer, spec) {} : internal::ArgFormatterBase<Impl, Char>(w, s)
{}
/** Formats an argument of type ``bool``. */ /** Formats an argument of type ``bool``. */
void visit_bool(bool value) void visit_bool(bool value)
@@ -329,7 +339,8 @@ class PrintfArgFormatter
public: public:
/** Constructs an argument formatter object. */ /** Constructs an argument formatter object. */
PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s) PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
: BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char>(w, s) {} : BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char>(w, s)
{}
}; };
/** This template formats data and writes the output to a writer. */ /** This template formats data and writes the output to a writer. */
@@ -358,8 +369,9 @@ public:
appropriate lifetimes. appropriate lifetimes.
\endrst \endrst
*/ */
explicit PrintfFormatter(const ArgList &args, BasicWriter<Char> &w) explicit PrintfFormatter(const ArgList &al, BasicWriter<Char> &w)
: FormatterBase(args), writer_(w) {} : FormatterBase(al), writer_(w)
{}
/** Formats stored arguments and writes the output to the writer. */ /** Formats stored arguments and writes the output to the writer. */
FMT_API void format(BasicCStringRef<Char> format_str); FMT_API void format(BasicCStringRef<Char> format_str);
@@ -399,7 +411,7 @@ internal::Arg PrintfFormatter<Char, AF>::get_arg(const Char *s,
unsigned arg_index) unsigned arg_index)
{ {
(void)s; (void)s;
const char *error = 0; const char *error = FMT_NULL;
internal::Arg arg = arg_index == std::numeric_limits<unsigned>::max() ? internal::Arg arg = arg_index == std::numeric_limits<unsigned>::max() ?
next_arg(error) : FormatterBase::get_arg(arg_index - 1, error); next_arg(error) : FormatterBase::get_arg(arg_index - 1, error);
if (error) if (error)
@@ -619,6 +631,28 @@ inline int printf(CStringRef format, ArgList args)
return fprintf(stdout, format, args); return fprintf(stdout, format, args);
} }
FMT_VARIADIC(int, printf, CStringRef) FMT_VARIADIC(int, printf, CStringRef)
/**
\rst
Prints formatted data to the stream *os*.
**Example**::
fprintf(cerr, "Don't %s!", "panic");
\endrst
*/
inline int fprintf(std::ostream &os, CStringRef format_str, ArgList args)
{
MemoryWriter w;
printf(w, format_str, args);
internal::write(os, w);
return static_cast<int>(w.size());
}
FMT_VARIADIC(int, fprintf, std::ostream &, CStringRef)
} // namespace fmt } // namespace fmt
#ifdef FMT_HEADER_ONLY
# include "printf.cc"
#endif
#endif // FMT_PRINTF_H_ #endif // FMT_PRINTF_H_

View File

@@ -5,7 +5,7 @@
#pragma once #pragma once
// Thread safe logger // Thread safe logger (except for set_pattern(..), set_formatter(..) and set_error_handler())
// Has name, log level, vector of std::shared sink pointers and formatter // Has name, log level, vector of std::shared sink pointers and formatter
// Upon each log write the logger: // Upon each log write the logger:
// 1. Checks if its log level is enough to log the message // 1. Checks if its log level is enough to log the message
@@ -65,8 +65,11 @@ public:
// automatically call flush() if message level >= log_level // automatically call flush() if message level >= log_level
void flush_on(level::level_enum log_level); void flush_on(level::level_enum log_level);
virtual void flush(); virtual void flush();
const std::vector<sink_ptr>& sinks() const;
protected: protected:
virtual void _sink_it(details::log_msg&); virtual void _sink_it(details::log_msg&);
virtual void _set_pattern(const std::string&); virtual void _set_pattern(const std::string&);
@@ -75,6 +78,9 @@ protected:
// default error handler: print the error to stderr with the max rate of 1 message/minute // default error handler: print the error to stderr with the max rate of 1 message/minute
virtual void _default_err_handler(const std::string &msg); virtual void _default_err_handler(const std::string &msg);
// return true if the given message level should trigger a flush
bool _should_flush_on(const details::log_msg&);
const std::string _name; const std::string _name;
std::vector<sink_ptr> _sinks; std::vector<sink_ptr> _sinks;
formatter_ptr _formatter; formatter_ptr _formatter;
@@ -86,5 +92,3 @@ protected:
} }
#include <spdlog/details/logger_impl.h> #include <spdlog/details/logger_impl.h>

View File

@@ -7,51 +7,43 @@
#if defined(__ANDROID__) #if defined(__ANDROID__)
#include <spdlog/sinks/base_sink.h> #include <spdlog/sinks/sink.h>
#include <spdlog/details/null_mutex.h>
#include <android/log.h>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <android/log.h>
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.
*/ */
template<class Mutex> class android_sink : public sink
class base_android_sink : public base_sink < Mutex >
{ {
public: public:
explicit base_android_sink(std::string tag="spdlog"): _tag(tag) explicit android_sink(const std::string& tag = "spdlog"): _tag(tag) {}
void log(const details::log_msg& msg) override
{ {
const android_LogPriority priority = convert_to_android(msg.level);
// See system/core/liblog/logger_write.c for explanation of return value
const int ret = __android_log_write(
priority, _tag.c_str(), msg.formatted.c_str()
);
if (ret < 0)
{
throw spdlog_ex("__android_log_write() failed", ret);
}
} }
void flush() override void flush() override
{ {
} }
protected:
void _sink_it(const details::log_msg& msg) override
{
const android_LogPriority priority = convert_to_android(msg.level);
const int expected_size = msg.formatted.size();
const int size = __android_log_write(
priority, _tag.c_str(), msg.formatted.c_str()
);
if (size > expected_size)
{
// Will write a little bit more than original message
}
else
{
throw spdlog_ex("Send to Android logcat failed");
}
}
private: private:
static android_LogPriority convert_to_android(spdlog::level::level_enum level) static android_LogPriority convert_to_android(spdlog::level::level_enum level)
{ {
@@ -63,29 +55,20 @@ private:
return ANDROID_LOG_DEBUG; return ANDROID_LOG_DEBUG;
case spdlog::level::info: case spdlog::level::info:
return ANDROID_LOG_INFO; return ANDROID_LOG_INFO;
case spdlog::level::notice:
return ANDROID_LOG_INFO;
case spdlog::level::warn: case spdlog::level::warn:
return ANDROID_LOG_WARN; return ANDROID_LOG_WARN;
case spdlog::level::err: case spdlog::level::err:
return ANDROID_LOG_ERROR; return ANDROID_LOG_ERROR;
case spdlog::level::critical: case spdlog::level::critical:
return ANDROID_LOG_FATAL; return ANDROID_LOG_FATAL;
case spdlog::level::alert:
return ANDROID_LOG_FATAL;
case spdlog::level::emerg:
return ANDROID_LOG_FATAL;
default: default:
throw spdlog_ex("Incorrect level value"); return ANDROID_LOG_DEFAULT;
} }
} }
std::string _tag; std::string _tag;
}; };
typedef base_android_sink<std::mutex> android_sink_mt;
typedef base_android_sink<details::null_mutex> android_sink_st;
} }
} }

View File

@@ -33,7 +33,7 @@ public:
virtual void log(const details::log_msg& msg) override; virtual void log(const details::log_msg& msg) override;
virtual void flush() override; virtual void flush() override;
void set_color(level::level_enum level, const std::string& color); void set_color(level::level_enum color_level, const std::string& color);
/// Formatting codes /// Formatting codes
const std::string reset = "\033[00m"; const std::string reset = "\033[00m";
@@ -101,9 +101,9 @@ inline void ansicolor_sink::flush()
sink_->flush(); sink_->flush();
} }
inline void ansicolor_sink::set_color(level::level_enum level, const std::string& color) inline void ansicolor_sink::set_color(level::level_enum color_level, const std::string& color)
{ {
colors_[level] = color; colors_[color_level] = color;
} }
inline ansicolor_sink::~ansicolor_sink() inline ansicolor_sink::~ansicolor_sink()

View File

@@ -36,9 +36,13 @@ protected:
void _sink_it(const details::log_msg& msg) override void _sink_it(const details::log_msg& msg) override
{ {
for (auto &sink : _sinks) for (auto &sink : _sinks)
{
if( sink->should_log( msg.level))
{
sink->log(msg); sink->log(msg);
} }
}
}
public: public:
void flush() override void flush() override

View File

@@ -29,10 +29,7 @@ template<class Mutex>
class simple_file_sink : public base_sink < Mutex > class simple_file_sink : public base_sink < Mutex >
{ {
public: public:
explicit simple_file_sink(const filename_t &filename, explicit simple_file_sink(const filename_t &filename, bool truncate = false):_force_flush(false)
bool force_flush = false,
bool truncate = false) :
_file_helper(force_flush)
{ {
_file_helper.open(filename, truncate); _file_helper.open(filename, truncate);
} }
@@ -40,14 +37,21 @@ public:
{ {
_file_helper.flush(); _file_helper.flush();
} }
void set_force_flush(bool 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);
if(_force_flush)
_file_helper.flush();
} }
private: private:
details::file_helper _file_helper; details::file_helper _file_helper;
bool _force_flush;
}; };
typedef simple_file_sink<std::mutex> simple_file_sink_mt; typedef simple_file_sink<std::mutex> simple_file_sink_mt;
@@ -61,14 +65,13 @@ class rotating_file_sink : public base_sink < Mutex >
{ {
public: public:
rotating_file_sink(const filename_t &base_filename, const filename_t &extension, rotating_file_sink(const filename_t &base_filename, const filename_t &extension,
std::size_t max_size, std::size_t max_files, std::size_t max_size, std::size_t max_files ) :
bool force_flush = false) :
_base_filename(base_filename), _base_filename(base_filename),
_extension(extension), _extension(extension),
_max_size(max_size), _max_size(max_size),
_max_files(max_files), _max_files(max_files),
_current_size(0), _current_size(0),
_file_helper(force_flush) _file_helper()
{ {
_file_helper.open(calc_filename(_base_filename, 0, _extension)); _file_helper.open(calc_filename(_base_filename, 0, _extension));
_current_size = _file_helper.size(); //expensive. called only once _current_size = _file_helper.size(); //expensive. called only once
@@ -184,12 +187,10 @@ public:
const filename_t& base_filename, const filename_t& base_filename,
const filename_t& extension, const filename_t& extension,
int rotation_hour, int rotation_hour,
int rotation_minute, int rotation_minute) : _base_filename(base_filename),
bool force_flush = false) : _base_filename(base_filename),
_extension(extension), _extension(extension),
_rotation_h(rotation_hour), _rotation_h(rotation_hour),
_rotation_m(rotation_minute), _rotation_m(rotation_minute)
_file_helper(force_flush)
{ {
if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59) if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59)
throw spdlog_ex("daily_file_sink: Invalid rotation time in ctor"); throw spdlog_ex("daily_file_sink: Invalid rotation time in ctor");

View File

@@ -27,7 +27,7 @@ protected:
}; };
typedef null_sink<details::null_mutex> null_sink_st; typedef null_sink<details::null_mutex> null_sink_st;
typedef null_sink<std::mutex> null_sink_mt; typedef null_sink<details::null_mutex> null_sink_mt;
} }
} }

View File

@@ -15,10 +15,39 @@ namespace sinks
class sink class sink
{ {
public: public:
sink()
{
_level = level::trace;
}
virtual ~sink() {} virtual ~sink() {}
virtual void log(const details::log_msg& msg) = 0; virtual void log(const details::log_msg& msg) = 0;
virtual void flush() = 0; virtual void flush() = 0;
bool should_log(level::level_enum msg_level) const;
void set_level(level::level_enum log_level);
level::level_enum level() const;
private:
level_t _level;
}; };
inline bool sink::should_log(level::level_enum msg_level) const
{
return msg_level >= _level.load(std::memory_order_relaxed);
}
inline void sink::set_level(level::level_enum log_level)
{
_level.store(log_level);
}
inline level::level_enum sink::level() const
{
return static_cast<spdlog::level::level_enum>(_level.load(std::memory_order_relaxed));
}
} }
} }

View File

@@ -22,7 +22,8 @@ class stdout_sink : public base_sink<Mutex>
{ {
using MyType = stdout_sink<Mutex>; using MyType = stdout_sink<Mutex>;
public: public:
stdout_sink() {} stdout_sink()
{}
static std::shared_ptr<MyType> instance() static std::shared_ptr<MyType> instance()
{ {
static std::shared_ptr<MyType> instance = std::make_shared<MyType>(); static std::shared_ptr<MyType> instance = std::make_shared<MyType>();
@@ -50,7 +51,8 @@ class stderr_sink : public base_sink<Mutex>
{ {
using MyType = stderr_sink<Mutex>; using MyType = stderr_sink<Mutex>;
public: public:
stderr_sink() {} stderr_sink()
{}
static std::shared_ptr<MyType> instance() static std::shared_ptr<MyType> instance()
{ {
static std::shared_ptr<MyType> instance = std::make_shared<MyType>(); static std::shared_ptr<MyType> instance = std::make_shared<MyType>();

View File

@@ -0,0 +1,116 @@
//
// Copyright(c) 2016 spdlog
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
#include <spdlog/sinks/base_sink.h>
#include <spdlog/details/null_mutex.h>
#include <spdlog/common.h>
#include <mutex>
#include <string>
#include <map>
#include <wincon.h>
namespace spdlog
{
namespace sinks
{
/*
* Windows color console sink. Uses WriteConsoleA to write to the console with colors
*/
template<class Mutex>
class wincolor_sink: public base_sink<Mutex>
{
public:
const WORD BOLD = FOREGROUND_INTENSITY;
const WORD RED = FOREGROUND_RED;
const WORD CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE;
const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
wincolor_sink(HANDLE std_handle): out_handle_(std_handle)
{
colors_[level::trace] = CYAN;
colors_[level::debug] = CYAN;
colors_[level::info] = WHITE | BOLD;
colors_[level::warn] = YELLOW | BOLD;
colors_[level::err] = RED | BOLD; // red bold
colors_[level::critical] = BACKGROUND_RED | WHITE | BOLD; // white bold on red background
colors_[level::off] = 0;
}
virtual ~wincolor_sink()
{
flush();
}
wincolor_sink(const wincolor_sink& other) = delete;
wincolor_sink& operator=(const wincolor_sink& other) = delete;
virtual void _sink_it(const details::log_msg& msg) override
{
auto color = colors_[msg.level];
auto orig_attribs = set_console_attribs(color);
WriteConsoleA(out_handle_, msg.formatted.data(), static_cast<DWORD>(msg.formatted.size()), nullptr, nullptr);
SetConsoleTextAttribute(out_handle_, orig_attribs); //reset to orig colors
}
virtual void flush() override
{
// windows console always flushed?
}
// change the color for the given level
void set_color(level::level_enum level, WORD color)
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
colors_[level] = color;
}
private:
HANDLE out_handle_;
std::map<level::level_enum, WORD> colors_;
// set color and return the orig console attributes (for resetting later)
WORD set_console_attribs(WORD attribs)
{
CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info;
GetConsoleScreenBufferInfo(out_handle_, &orig_buffer_info);
SetConsoleTextAttribute(out_handle_, attribs);
return orig_buffer_info.wAttributes; //return orig attribs
}
};
//
// windows color console to stdout
//
template<class Mutex>
class wincolor_stdout_sink: public wincolor_sink<Mutex>
{
public:
wincolor_stdout_sink() : wincolor_sink<Mutex>(GetStdHandle(STD_OUTPUT_HANDLE))
{}
};
typedef wincolor_stdout_sink<std::mutex> wincolor_stdout_sink_mt;
typedef wincolor_stdout_sink<details::null_mutex> wincolor_stdout_sink_st;
//
// windows color console to stderr
//
template<class Mutex>
class wincolor_stderr_sink: public wincolor_sink<Mutex>
{
public:
wincolor_stderr_sink() : wincolor_sink<Mutex>(GetStdHandle(STD_ERROR_HANDLE))
{}
};
typedef wincolor_stderr_sink<std::mutex> wincolor_stderr_sink_mt;
typedef wincolor_stderr_sink<details::null_mutex> wincolor_stderr_sink_st;
}
}

View File

@@ -2,12 +2,13 @@
// Copyright(c) 2015 Gabi Melman. // Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT) // Distributed under the MIT License (http://opensource.org/licenses/MIT)
// //
// spdlog main header file. // spdlog main header file.
// see example.cpp for usage example // see example.cpp for usage example
#pragma once #pragma once
#define SPDLOG_VERSION "0.12.0"
#include <spdlog/tweakme.h> #include <spdlog/tweakme.h>
#include <spdlog/common.h> #include <spdlog/common.h>
#include <spdlog/logger.h> #include <spdlog/logger.h>
@@ -20,15 +21,13 @@
namespace spdlog namespace spdlog
{ {
// Return an existing logger or nullptr if a logger with such name doesn't exist.
// Examples:
// //
// spdlog::get("mylog")->info("Hello"); // Return an existing logger or nullptr if a logger with such name doesn't exist.
// auto logger = spdlog::get("mylog"); // example: spdlog::get("my_logger")->info("hello {}", "world");
// logger.info("This is another message" , x, y, z); //
// logger.info() << "This is another message" << x << y << z;
std::shared_ptr<logger> get(const std::string& name); std::shared_ptr<logger> get(const std::string& name);
// //
// Set global formatting // Set global formatting
// example: spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v"); // example: spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v");
@@ -69,30 +68,38 @@ void set_sync_mode();
// //
// Create and register multi/single basic file logger // Create and register multi/single threaded basic file logger.
// Basic logger simply writes to given file without any limitatons or rotations.
// //
std::shared_ptr<logger> basic_logger_mt(const std::string& logger_name, const filename_t& filename,bool force_flush = false, bool truncate = false); std::shared_ptr<logger> basic_logger_mt(const std::string& logger_name, const filename_t& filename, bool truncate = false);
std::shared_ptr<logger> basic_logger_st(const std::string& logger_name, const filename_t& filename, bool force_flush = false, bool truncate = false); std::shared_ptr<logger> basic_logger_st(const std::string& logger_name, const filename_t& filename, bool truncate = false);
// //
// Create and register multi/single threaded rotating file logger // Create and register multi/single threaded rotating file logger
// //
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, bool force_flush = false); 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);
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, bool force_flush = false); 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);
// //
// 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):
// //
std::shared_ptr<logger> daily_logger_mt(const std::string& logger_name, const filename_t& filename, int hour=0, int minute=0, bool force_flush = false); std::shared_ptr<logger> daily_logger_mt(const std::string& logger_name, const filename_t& filename, int hour=0, int minute=0);
std::shared_ptr<logger> daily_logger_st(const std::string& logger_name, const filename_t& filename, int hour=0, int minute=0, bool force_flush = false); std::shared_ptr<logger> daily_logger_st(const std::string& logger_name, const filename_t& filename, int hour=0, int minute=0);
// //
// Create and register stdout/stderr loggers // Create and register stdout/stderr loggers
// //
std::shared_ptr<logger> stdout_logger_mt(const std::string& logger_name, bool color = false); std::shared_ptr<logger> stdout_logger_mt(const std::string& logger_name);
std::shared_ptr<logger> stdout_logger_st(const std::string& logger_name, bool color = false); std::shared_ptr<logger> stdout_logger_st(const std::string& logger_name);
std::shared_ptr<logger> stderr_logger_mt(const std::string& logger_name, bool color = false); std::shared_ptr<logger> stderr_logger_mt(const std::string& logger_name);
std::shared_ptr<logger> stderr_logger_st(const std::string& logger_name, bool color = false); std::shared_ptr<logger> stderr_logger_st(const std::string& logger_name);
//
// Create and register colored stdout/stderr loggers
//
std::shared_ptr<logger> stdout_color_mt(const std::string& logger_name);
std::shared_ptr<logger> stdout_color_st(const std::string& logger_name);
std::shared_ptr<logger> stderr_color_mt(const std::string& logger_name);
std::shared_ptr<logger> stderr_color_st(const std::string& logger_name);
// //
@@ -102,6 +109,9 @@ std::shared_ptr<logger> stderr_logger_st(const std::string& logger_name, bool co
std::shared_ptr<logger> syslog_logger(const std::string& logger_name, const std::string& ident = "", int syslog_option = 0); std::shared_ptr<logger> syslog_logger(const std::string& logger_name, const std::string& ident = "", int syslog_option = 0);
#endif #endif
#if defined(__ANDROID__)
std::shared_ptr<logger> android_logger(const std::string& logger_name, const std::string& tag = "spdlog");
#endif
// Create and register a logger a single sink // Create and register a logger a single sink
std::shared_ptr<logger> create(const std::string& logger_name, const sink_ptr& sink); std::shared_ptr<logger> create(const std::string& logger_name, const sink_ptr& sink);

View File

@@ -101,3 +101,8 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Uncomment to prevent child processes from inheriting log file descriptors
//
// #define SPDLOG_PREVENT_CHILD_FD
///////////////////////////////////////////////////////////////////////////////

View File

@@ -28,6 +28,7 @@ TEST_CASE("custom_error_handler", "[errors]]")
prepare_logdir(); prepare_logdir();
std::string filename = "logs/simple_log.txt"; std::string filename = "logs/simple_log.txt";
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true); auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
logger->flush_on(spdlog::level::info);
logger->set_error_handler([=](const std::string& msg) logger->set_error_handler([=](const std::string& msg)
{ {
throw custom_ex(); throw custom_ex();

View File

@@ -12,6 +12,7 @@ static void write_with_helper(file_helper &helper, size_t howmany)
log_msg msg; log_msg msg;
msg.formatted << std::string(howmany, '1'); msg.formatted << std::string(howmany, '1');
helper.write(msg); helper.write(msg);
helper.flush();
} }
@@ -19,7 +20,7 @@ TEST_CASE("file_helper_filename", "[file_helper::filename()]]")
{ {
prepare_logdir(); prepare_logdir();
file_helper helper(false); file_helper helper;
helper.open(target_filename); helper.open(target_filename);
REQUIRE(helper.filename() == target_filename); REQUIRE(helper.filename() == target_filename);
} }
@@ -31,7 +32,7 @@ TEST_CASE("file_helper_size", "[file_helper::size()]]")
prepare_logdir(); prepare_logdir();
size_t expected_size = 123; size_t expected_size = 123;
{ {
file_helper helper(true); file_helper helper;
helper.open(target_filename); helper.open(target_filename);
write_with_helper(helper, expected_size); write_with_helper(helper, expected_size);
REQUIRE(static_cast<size_t>(helper.size()) == expected_size); REQUIRE(static_cast<size_t>(helper.size()) == expected_size);
@@ -44,7 +45,7 @@ TEST_CASE("file_helper_exists", "[file_helper::file_exists()]]")
{ {
prepare_logdir(); prepare_logdir();
REQUIRE(!file_helper::file_exists(target_filename)); REQUIRE(!file_helper::file_exists(target_filename));
file_helper helper(false); file_helper helper;
helper.open(target_filename); helper.open(target_filename);
REQUIRE(file_helper::file_exists(target_filename)); REQUIRE(file_helper::file_exists(target_filename));
} }
@@ -52,7 +53,7 @@ TEST_CASE("file_helper_exists", "[file_helper::file_exists()]]")
TEST_CASE("file_helper_reopen", "[file_helper::reopen()]]") TEST_CASE("file_helper_reopen", "[file_helper::reopen()]]")
{ {
prepare_logdir(); prepare_logdir();
file_helper helper(true); file_helper helper;
helper.open(target_filename); helper.open(target_filename);
write_with_helper(helper, 12); write_with_helper(helper, 12);
REQUIRE(helper.size() == 12); REQUIRE(helper.size() == 12);
@@ -64,7 +65,7 @@ TEST_CASE("file_helper_reopen2", "[file_helper::reopen(false)]]")
{ {
prepare_logdir(); prepare_logdir();
size_t expected_size = 14; size_t expected_size = 14;
file_helper helper(true); file_helper helper;
helper.open(target_filename); helper.open(target_filename);
write_with_helper(helper, expected_size); write_with_helper(helper, expected_size);
REQUIRE(helper.size() == expected_size); REQUIRE(helper.size() == expected_size);

View File

@@ -18,22 +18,40 @@ TEST_CASE("simple_file_logger", "[simple_logger]]")
logger->flush(); logger->flush();
REQUIRE(file_contents(filename) == std::string("Test message 1\nTest message 2\n")); REQUIRE(file_contents(filename) == std::string("Test message 1\nTest message 2\n"));
REQUIRE(count_lines(filename) == 2); REQUIRE(count_lines(filename) == 2);
}
TEST_CASE("flush_on", "[flush_on]]")
{
prepare_logdir();
std::string filename = "logs/simple_log.txt";
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename);
logger->set_pattern("%v");
logger->set_level(spdlog::level::trace);
logger->flush_on(spdlog::level::info);
logger->trace("Should not be flushed");
REQUIRE(count_lines(filename) == 0);
logger->info("Test message {}", 1);
logger->info("Test message {}", 2);
logger->flush();
REQUIRE(file_contents(filename) == std::string("Should not be flushed\nTest message 1\nTest message 2\n"));
REQUIRE(count_lines(filename) == 3);
} }
TEST_CASE("rotating_file_logger1", "[rotating_logger]]") TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
{ {
prepare_logdir(); prepare_logdir();
std::string basename = "logs/rotating_log"; std::string basename = "logs/rotating_log";
auto logger = spdlog::rotating_logger_mt("logger", basename, 1024, 0, true); auto logger = spdlog::rotating_logger_mt("logger", basename, 1024, 0);
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
logger->info("Test message {}", i); logger->info("Test message {}", i);
logger->flush();
auto filename = basename + ".txt"; auto filename = basename + ".txt";
REQUIRE(count_lines(filename) == 10); REQUIRE(count_lines(filename) == 10);
for (int i = 0; i < 1000; i++)
logger->info("Test message {}", i);
} }
@@ -41,7 +59,7 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
{ {
prepare_logdir(); prepare_logdir();
std::string basename = "logs/rotating_log"; std::string basename = "logs/rotating_log";
auto logger = spdlog::rotating_logger_mt("logger", basename, 1024, 1, false); auto logger = spdlog::rotating_logger_mt("logger", basename, 1024, 1);
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
logger->info("Test message {}", i); logger->info("Test message {}", i);
@@ -60,7 +78,6 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
TEST_CASE("daily_logger", "[daily_logger]]") TEST_CASE("daily_logger", "[daily_logger]]")
{ {
prepare_logdir(); prepare_logdir();
//calculate filename (time based) //calculate filename (time based)
std::string basename = "logs/daily_log"; std::string basename = "logs/daily_log";
@@ -68,7 +85,8 @@ TEST_CASE("daily_logger", "[daily_logger]]")
fmt::MemoryWriter w; fmt::MemoryWriter w;
w.write("{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}.txt", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min); w.write("{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}.txt", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min);
auto logger = spdlog::daily_logger_mt("logger", basename, 0, 0, true); auto logger = spdlog::daily_logger_mt("logger", basename, 0, 0);
logger->flush_on(spdlog::level::info);
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
logger->info("Test message {}", i); logger->info("Test message {}", i);
@@ -90,10 +108,10 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger_dateonly]]")
fmt::MemoryWriter w; fmt::MemoryWriter w;
w.write("{}_{:04d}-{:02d}-{:02d}.txt", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); w.write("{}_{:04d}-{:02d}-{:02d}.txt", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
auto logger = spdlog::create<sink_type>("logger", basename, "txt", 0, 0, true); auto logger = spdlog::create<sink_type>("logger", basename, "txt", 0, 0);
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
logger->info("Test message {}", i); logger->info("Test message {}", i);
logger->flush();
auto filename = w.str(); auto filename = w.str();
REQUIRE(count_lines(filename) == 10); REQUIRE(count_lines(filename) == 10);
} }
@@ -122,10 +140,11 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger_custom]]")
fmt::MemoryWriter w; fmt::MemoryWriter w;
w.write("{}{:04d}{:02d}{:02d}.txt", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); w.write("{}{:04d}{:02d}{:02d}.txt", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
auto logger = spdlog::create<sink_type>("logger", basename, "txt", 0, 0, true); auto logger = spdlog::create<sink_type>("logger", basename, "txt", 0, 0);
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
logger->info("Test message {}", i); logger->info("Test message {}", i);
logger->flush();
auto filename = w.str(); auto filename = w.str();
REQUIRE(count_lines(filename) == 10); REQUIRE(count_lines(filename) == 10);
} }