Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
spdlog
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
spdlog
Commits
c2a49080
Commit
c2a49080
authored
Jul 14, 2018
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored sink interface and base_sink
parent
2bc05b6b
Changes
25
Show whitespace changes
Inline
Side-by-side
Showing
25 changed files
with
200 additions
and
120 deletions
+200
-120
bench/bench.cpp
bench/bench.cpp
+1
-0
example/Makefile
example/Makefile
+1
-1
example/example.cpp
example/example.cpp
+5
-4
include/spdlog/async_logger.h
include/spdlog/async_logger.h
+1
-1
include/spdlog/details/async_logger_impl.h
include/spdlog/details/async_logger_impl.h
+1
-1
include/spdlog/details/console_globals.h
include/spdlog/details/console_globals.h
+14
-10
include/spdlog/details/logger_impl.h
include/spdlog/details/logger_impl.h
+2
-2
include/spdlog/details/registry.h
include/spdlog/details/registry.h
+2
-3
include/spdlog/logger.h
include/spdlog/logger.h
+2
-2
include/spdlog/sinks/android_sink.h
include/spdlog/sinks/android_sink.h
+22
-11
include/spdlog/sinks/ansicolor_sink.h
include/spdlog/sinks/ansicolor_sink.h
+18
-6
include/spdlog/sinks/base_sink.h
include/spdlog/sinks/base_sink.h
+15
-5
include/spdlog/sinks/basic_file_sink.h
include/spdlog/sinks/basic_file_sink.h
+4
-2
include/spdlog/sinks/daily_file_sink.h
include/spdlog/sinks/daily_file_sink.h
+5
-2
include/spdlog/sinks/dist_sink.h
include/spdlog/sinks/dist_sink.h
+18
-20
include/spdlog/sinks/msvc_sink.h
include/spdlog/sinks/msvc_sink.h
+1
-1
include/spdlog/sinks/null_sink.h
include/spdlog/sinks/null_sink.h
+2
-3
include/spdlog/sinks/ostream_sink.h
include/spdlog/sinks/ostream_sink.h
+5
-3
include/spdlog/sinks/rotating_file_sink.h
include/spdlog/sinks/rotating_file_sink.h
+4
-2
include/spdlog/sinks/sink.h
include/spdlog/sinks/sink.h
+12
-14
include/spdlog/sinks/stdout_sinks.h
include/spdlog/sinks/stdout_sinks.h
+19
-7
include/spdlog/sinks/syslog_sink.h
include/spdlog/sinks/syslog_sink.h
+19
-9
include/spdlog/sinks/wincolor_sink.h
include/spdlog/sinks/wincolor_sink.h
+18
-7
tests/errors.cpp
tests/errors.cpp
+8
-3
tests/test_sink.h
tests/test_sink.h
+1
-1
No files found.
bench/bench.cpp
View file @
c2a49080
...
...
@@ -110,6 +110,7 @@ void bench(int howmany, std::shared_ptr<spdlog::logger> log)
auto
delta
=
high_resolution_clock
::
now
()
-
start
;
auto
delta_d
=
duration_cast
<
duration
<
double
>>
(
delta
).
count
();
cout
<<
"Elapsed: "
<<
delta_d
<<
"
\t
"
<<
format
(
int
(
howmany
/
delta_d
))
<<
"/sec"
<<
endl
;
spdlog
::
drop
(
log
->
name
());
}
...
...
example/Makefile
View file @
c2a49080
CXX
=
g++
CXX
?
=
g++
CXX_FLAGS
=
-Wall
-Wextra
-pedantic
-std
=
c++11
-pthread
-I
../include
-fmax-errors
=
1
CXX_RELEASE_FLAGS
=
-O3
-march
=
native
CXX_DEBUG_FLAGS
=
-g
...
...
example/example.cpp
View file @
c2a49080
...
...
@@ -59,7 +59,8 @@ int main(int, char *[])
}
}
#include "spdlog/sinks/stdout_color_sinks.h" // or "/sinks/stdout_sinks.h" if no colors needed
#include "spdlog/sinks/stdout_color_sinks.h"
// or #include "spdlog/sinks/stdout_sinks.h" if no colors needed
void
stdout_example
()
{
// create color multi threaded logger
...
...
@@ -181,18 +182,18 @@ void err_handler_example()
void
syslog_example
()
{
std
::
string
ident
=
"spdlog-example"
;
auto
syslog_logger
=
spdlog
::
syslog_logger
(
"syslog"
,
ident
,
LOG_PID
);
auto
syslog_logger
=
spdlog
::
syslog_logger
_mt
(
"syslog"
,
ident
,
LOG_PID
);
syslog_logger
->
warn
(
"This is warning that will end up in syslog."
);
}
#endif
// Android example
#if defined(__ANDROID__)
#incude "spdlog/sinks/android_sink.h"
#inc
l
ude "spdlog/sinks/android_sink.h"
void
android_example
()
{
std
::
string
tag
=
"spdlog-android"
;
auto
android_logger
=
spdlog
::
android_logger
(
"android"
,
tag
);
auto
android_logger
=
spdlog
::
android_logger
_mt
(
"android"
,
tag
);
android_logger
->
critical
(
"Use
\"
adb shell logcat
\"
to view this message."
);
}
...
...
include/spdlog/async_logger.h
View file @
c2a49080
...
...
@@ -32,7 +32,7 @@ class async_logger SPDLOG_FINAL : public std::enable_shared_from_this<async_logg
friend
class
details
::
thread_pool
;
public:
template
<
class
It
>
template
<
typename
It
>
async_logger
(
const
std
::
string
&
logger_name
,
const
It
&
begin
,
const
It
&
end
,
std
::
weak_ptr
<
details
::
thread_pool
>
tp
,
async_overflow_policy
overflow_policy
=
async_overflow_policy
::
block
);
...
...
include/spdlog/details/async_logger_impl.h
View file @
c2a49080
...
...
@@ -14,7 +14,7 @@
#include <memory>
#include <string>
template
<
class
It
>
template
<
typename
It
>
inline
spdlog
::
async_logger
::
async_logger
(
const
std
::
string
&
logger_name
,
const
It
&
begin
,
const
It
&
end
,
std
::
weak_ptr
<
details
::
thread_pool
>
tp
,
async_overflow_policy
overflow_policy
)
:
logger
(
logger_name
,
begin
,
end
)
...
...
include/spdlog/details/console_globals.h
View file @
c2a49080
...
...
@@ -4,10 +4,14 @@
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#include "spdlog/details/null_mutex.h"
#include "stdio.h"
#include <mutex>
namespace
spdlog
{
namespace
details
{
struct
console_stdout
_stream
struct
console_stdout
{
static
FILE
*
stream
()
{
...
...
@@ -21,7 +25,7 @@ struct console_stdout_stream
#endif
};
struct
console_stderr
_stream
struct
console_stderr
{
static
FILE
*
stream
()
{
...
...
@@ -35,23 +39,23 @@ struct console_stderr_stream
#endif
};
struct
console_
global_
mutex
struct
console_mutex
{
using
mutex_t
=
std
::
mutex
;
static
mutex_t
&
console_
mutex
()
static
mutex_t
&
mutex
()
{
static
mutex_t
mutex
;
return
mutex
;
static
mutex_t
s_
mutex
;
return
s_
mutex
;
}
};
struct
console_
global_
nullmutex
struct
console_nullmutex
{
using
mutex_t
=
null_mutex
;
static
mutex_t
&
console_
mutex
()
static
mutex_t
&
mutex
()
{
static
mutex_t
mutex
;
return
mutex
;
static
mutex_t
s_
mutex
;
return
s_
mutex
;
}
};
}
// namespace details
...
...
include/spdlog/details/logger_impl.h
View file @
c2a49080
...
...
@@ -10,7 +10,7 @@
// create logger with given name, sinks and the default pattern formatter
// all other ctors will call this one
template
<
class
It
>
template
<
typename
It
>
inline
spdlog
::
logger
::
logger
(
std
::
string
logger_name
,
const
It
&
begin
,
const
It
&
end
)
:
name_
(
std
::
move
(
logger_name
))
,
sinks_
(
begin
,
end
)
...
...
@@ -36,7 +36,7 @@ inline spdlog::logger::logger(const std::string &logger_name, spdlog::sink_ptr s
inline
spdlog
::
logger
::~
logger
()
=
default
;
template
<
class
FormatterT
,
typename
...
Args
>
template
<
typename
FormatterT
,
typename
...
Args
>
inline
void
spdlog
::
logger
::
set_formatter
(
const
Args
&
...
args
)
{
for
(
auto
&
sink
:
sinks_
)
...
...
include/spdlog/details/registry.h
View file @
c2a49080
...
...
@@ -23,7 +23,7 @@ namespace spdlog {
namespace
details
{
class
thread_pool
;
template
<
class
Mutex
>
template
<
typename
Mutex
>
class
registry_t
{
public:
...
...
@@ -46,8 +46,7 @@ public:
auto
logger_name
=
new_logger
->
name
();
throw_if_exists_
(
logger_name
);
// create default formatter if not exists
// set the global formatter pattern
new_logger
->
set_formatter
<
pattern_formatter
>
(
formatter_pattern_
,
pattern_time_type_
);
if
(
err_handler_
)
...
...
include/spdlog/logger.h
View file @
c2a49080
...
...
@@ -31,7 +31,7 @@ public:
logger
(
const
std
::
string
&
name
,
sink_ptr
single_sink
);
logger
(
const
std
::
string
&
name
,
sinks_init_list
sinks
);
template
<
class
It
>
template
<
typename
It
>
logger
(
std
::
string
name
,
const
It
&
begin
,
const
It
&
end
);
virtual
~
logger
();
...
...
@@ -118,7 +118,7 @@ public:
// create a FormatterT formatter for each sink in this logger.
// each sink gets its own private copy of a formatter object.
template
<
class
FormatterT
,
typename
...
Args
>
template
<
typename
FormatterT
,
typename
...
Args
>
void
set_formatter
(
const
Args
&
...
args
);
void
flush
();
...
...
include/spdlog/sinks/android_sink.h
View file @
c2a49080
...
...
@@ -5,10 +5,10 @@
#pragma once
#if defined(__ANDROID__)
#include "spdlog/details/os.h"
#include "spdlog/sinks/sink.h"
#include "spdlog/sinks/base_sink.h"
#include "spdlog/details/null_mutex.h"
#include "spdlog/details/fmt_helper.h"
#include <android/log.h>
#include <chrono>
...
...
@@ -25,9 +25,9 @@ namespace sinks {
/*
* Android sink (logging using __android_log_write)
* __android_log_write is thread-safe. No lock is needed.
*/
class
android_sink
:
public
sink
template
<
typename
Mutex
>
class
android_sink
SPDLOG_FINAL
:
public
base_sink
<
Mutex
>
{
public:
explicit
android_sink
(
const
std
::
string
&
tag
=
"spdlog"
,
bool
use_raw_msg
=
false
)
...
...
@@ -36,13 +36,14 @@ public:
{
}
void
log
(
const
details
::
log_msg
&
msg
)
override
protected:
void
sink_it_
(
const
details
::
log_msg
&
msg
)
override
{
const
android_LogPriority
priority
=
convert_to_android_
(
msg
.
level
);
fmt
::
memory_buffer
formatted
;
if
(
use_raw_msg_
)
{
f
ormatted
.
append
(
msg
.
raw
.
data
(),
msg
.
raw
.
data
()
+
msg
.
raw
.
size
()
);
f
mt_helper
::
append_buf
(
msg
.
raw
,
formatted
);
}
else
{
...
...
@@ -67,7 +68,9 @@ public:
}
}
void
flush
()
override
{}
void
flush_
()
override
{
}
private:
static
android_LogPriority
convert_to_android_
(
spdlog
::
level
::
level_enum
level
)
...
...
@@ -95,16 +98,24 @@ private:
bool
use_raw_msg_
;
};
using
android_sink_mt
=
android_sink
<
std
::
mutex
>
;
using
android_sink_st
=
android_sink
<
details
::
null_mutex
>
;
}
// namespace sinks
// Create and register android syslog logger
template
<
typename
Factory
=
default_factory
>
inline
std
::
shared_ptr
<
logger
>
android_logger
(
const
std
::
string
&
logger_name
,
const
std
::
string
&
tag
=
"spdlog"
)
inline
std
::
shared_ptr
<
logger
>
android_logger
_mt
(
const
std
::
string
&
logger_name
,
const
std
::
string
&
tag
=
"spdlog"
)
{
return
Factory
::
template
create
<
sinks
::
android_sink
>(
logger_name
,
tag
);
return
Factory
::
template
create
<
sinks
::
android_sink_mt
>(
logger_name
,
tag
);
}
template
<
typename
Factory
=
default_factory
>
inline
std
::
shared_ptr
<
logger
>
android_logger_st
(
const
std
::
string
&
logger_name
,
const
std
::
string
&
tag
=
"spdlog"
)
{
return
Factory
::
template
create
<
sinks
::
android_sink_st
>(
logger_name
,
tag
);
}
}
// namespace spdlog
#endif
include/spdlog/sinks/ansicolor_sink.h
View file @
c2a49080
...
...
@@ -22,14 +22,14 @@ namespace sinks {
* of the message.
* If no color terminal detected, omit the escape codes.
*/
template
<
class
TargetStream
,
class
ConsoleMutex
>
template
<
typename
TargetStream
,
class
ConsoleMutex
>
class
ansicolor_sink
:
public
sink
{
public:
using
mutex_t
=
typename
ConsoleMutex
::
mutex_t
;
ansicolor_sink
()
:
target_file_
(
TargetStream
::
stream
())
,
mutex_
(
ConsoleMutex
::
console_
mutex
())
,
mutex_
(
ConsoleMutex
::
mutex
())
{
should_do_colors_
=
details
::
os
::
in_terminal
(
target_file_
)
&&
details
::
os
::
is_color_terminal
();
...
...
@@ -115,6 +115,18 @@ public:
fflush
(
target_file_
);
}
void
set_pattern
(
const
std
::
string
&
pattern
)
override
SPDLOG_FINAL
{
std
::
lock_guard
<
mutex_t
>
lock
(
mutex_
);
formatter_
=
std
::
unique_ptr
<
spdlog
::
formatter
>
(
new
pattern_formatter
(
pattern
));
}
void
set_formatter
(
std
::
unique_ptr
<
spdlog
::
formatter
>
sink_formatter
)
override
SPDLOG_FINAL
{
std
::
lock_guard
<
mutex_t
>
lock
(
mutex_
);
formatter_
=
std
::
move
(
sink_formatter
);
}
private:
void
print_ccode_
(
const
std
::
string
&
color_code
)
{
...
...
@@ -132,11 +144,11 @@ private:
std
::
unordered_map
<
level
::
level_enum
,
std
::
string
,
level
::
level_hasher
>
colors_
;
};
using
ansicolor_stdout_sink_mt
=
ansicolor_sink
<
details
::
console_stdout
_stream
,
details
::
console_global
_mutex
>
;
using
ansicolor_stdout_sink_st
=
ansicolor_sink
<
details
::
console_stdout
_stream
,
details
::
console_global
_nullmutex
>
;
using
ansicolor_stdout_sink_mt
=
ansicolor_sink
<
details
::
console_stdout
,
details
::
console
_mutex
>
;
using
ansicolor_stdout_sink_st
=
ansicolor_sink
<
details
::
console_stdout
,
details
::
console
_nullmutex
>
;
using
ansicolor_stderr_sink_mt
=
ansicolor_sink
<
details
::
console_stderr
_stream
,
details
::
console_global
_mutex
>
;
using
ansicolor_stderr_sink_st
=
ansicolor_sink
<
details
::
console_stderr
_stream
,
details
::
console_global
_nullmutex
>
;
using
ansicolor_stderr_sink_mt
=
ansicolor_sink
<
details
::
console_stderr
,
details
::
console
_mutex
>
;
using
ansicolor_stderr_sink_st
=
ansicolor_sink
<
details
::
console_stderr
,
details
::
console
_nullmutex
>
;
}
// namespace sinks
...
...
include/spdlog/sinks/base_sink.h
View file @
c2a49080
...
...
@@ -17,7 +17,7 @@
namespace
spdlog
{
namespace
sinks
{
template
<
class
Mutex
>
template
<
typename
Mutex
>
class
base_sink
:
public
sink
{
public:
...
...
@@ -32,9 +32,7 @@ public:
void
log
(
const
details
::
log_msg
&
msg
)
SPDLOG_FINAL
override
{
std
::
lock_guard
<
Mutex
>
lock
(
mutex_
);
fmt
::
memory_buffer
formatted
;
formatter_
->
format
(
msg
,
formatted
);
sink_it_
(
msg
,
formatted
);
sink_it_
(
msg
);
}
void
flush
()
SPDLOG_FINAL
override
...
...
@@ -43,8 +41,20 @@ public:
flush_
();
}
void
set_pattern
(
const
std
::
string
&
pattern
)
SPDLOG_FINAL
override
{
std
::
lock_guard
<
Mutex
>
lock
(
mutex_
);
formatter_
=
std
::
unique_ptr
<
spdlog
::
formatter
>
(
new
pattern_formatter
(
pattern
));
}
void
set_formatter
(
std
::
unique_ptr
<
spdlog
::
formatter
>
sink_formatter
)
SPDLOG_FINAL
override
{
std
::
lock_guard
<
Mutex
>
lock
(
mutex_
);
formatter_
=
std
::
move
(
sink_formatter
);
}
protected:
virtual
void
sink_it_
(
const
details
::
log_msg
&
msg
,
const
fmt
::
memory_buffer
&
formatted
)
=
0
;
virtual
void
sink_it_
(
const
details
::
log_msg
&
msg
)
=
0
;
virtual
void
flush_
()
=
0
;
Mutex
mutex_
;
};
...
...
include/spdlog/sinks/basic_file_sink.h
View file @
c2a49080
...
...
@@ -17,7 +17,7 @@ namespace sinks {
/*
* Trivial file sink with single file as target
*/
template
<
class
Mutex
>
template
<
typename
Mutex
>
class
basic_file_sink
SPDLOG_FINAL
:
public
base_sink
<
Mutex
>
{
public:
...
...
@@ -27,8 +27,10 @@ public:
}
protected:
void
sink_it_
(
const
details
::
log_msg
&
,
const
fmt
::
memory_buffer
&
formatted
)
override
void
sink_it_
(
const
details
::
log_msg
&
msg
)
override
{
fmt
::
memory_buffer
formatted
;
sink
::
formatter_
->
format
(
msg
,
formatted
);
file_helper_
.
write
(
formatted
);
}
...
...
include/spdlog/sinks/daily_file_sink.h
View file @
c2a49080
...
...
@@ -39,7 +39,7 @@ struct daily_filename_calculator
/*
* Rotating file sink based on date. rotates at midnight
*/
template
<
class
Mutex
,
class
FileNameCalc
=
daily_filename_calculator
>
template
<
typename
Mutex
,
typename
FileNameCalc
=
daily_filename_calculator
>
class
daily_file_sink
SPDLOG_FINAL
:
public
base_sink
<
Mutex
>
{
public:
...
...
@@ -59,13 +59,16 @@ public:
}
protected:
void
sink_it_
(
const
details
::
log_msg
&
msg
,
const
fmt
::
memory_buffer
&
formatted
)
override
void
sink_it_
(
const
details
::
log_msg
&
msg
)
override
{
if
(
msg
.
time
>=
rotation_tp_
)
{
file_helper_
.
open
(
FileNameCalc
::
calc_filename
(
base_filename_
,
now_tm
(
msg
.
time
)));
rotation_tp_
=
next_rotation_tp_
();
}
fmt
::
memory_buffer
formatted
;
sink
::
formatter_
->
format
(
msg
,
formatted
);
file_helper_
.
write
(
formatted
);
}
...
...
include/spdlog/sinks/dist_sink.h
View file @
c2a49080
...
...
@@ -5,6 +5,7 @@
#pragma once
#include "base_sink.h"
#include "spdlog/details/log_msg.h"
#include "spdlog/details/null_mutex.h"
...
...
@@ -19,16 +20,29 @@ namespace spdlog {
namespace
sinks
{
template
<
typename
Mutex
>
class
dist_sink
:
public
sink
class
dist_sink
:
public
base_sink
<
Mutex
>
{
public:
dist_sink
()
=
default
;
dist_sink
(
const
dist_sink
&
)
=
delete
;
dist_sink
&
operator
=
(
const
dist_sink
&
)
=
delete
;
void
log
(
const
details
::
log_msg
&
msg
)
SPDLOG_FINAL
override
void
add_sink
(
std
::
shared_ptr
<
sink
>
sink
)
{
std
::
lock_guard
<
Mutex
>
lock
(
base_sink
<
Mutex
>::
mutex_
);
sinks_
.
push_back
(
sink
);
}
void
remove_sink
(
std
::
shared_ptr
<
sink
>
sink
)
{
std
::
lock_guard
<
Mutex
>
lock
(
mutex_
);
std
::
lock_guard
<
Mutex
>
lock
(
base_sink
<
Mutex
>::
mutex_
);
sinks_
.
erase
(
std
::
remove
(
sinks_
.
begin
(),
sinks_
.
end
(),
sink
),
sinks_
.
end
());
}
protected:
void
sink_it_
(
const
details
::
log_msg
&
msg
)
override
{
for
(
auto
&
sink
:
sinks_
)
{
if
(
sink
->
should_log
(
msg
.
level
))
...
...
@@ -38,27 +52,11 @@ public:
}
}
void
flush
()
SPDLOG_FINAL
override
void
flush
_
()
override
{
std
::
lock_guard
<
Mutex
>
lock
(
mutex_
);
for
(
auto
&
sink
:
sinks_
)
sink
->
flush
();
}
void
add_sink
(
std
::
shared_ptr
<
sink
>
sink
)
{
std
::
lock_guard
<
Mutex
>
lock
(
mutex_
);
sinks_
.
push_back
(
sink
);
}
void
remove_sink
(
std
::
shared_ptr
<
sink
>
sink
)
{
std
::
lock_guard
<
Mutex
>
lock
(
mutex_
);
sinks_
.
erase
(
std
::
remove
(
sinks_
.
begin
(),
sinks_
.
end
(),
sink
),
sinks_
.
end
());
}
private:
Mutex
mutex_
;
std
::
vector
<
std
::
shared_ptr
<
sink
>>
sinks_
;
};
...
...
include/spdlog/sinks/msvc_sink.h
View file @
c2a49080
...
...
@@ -20,7 +20,7 @@ namespace sinks {
/*
* MSVC sink (logging using OutputDebugStringA)
*/
template
<
class
Mutex
>
template
<
typename
Mutex
>
class
msvc_sink
:
public
base_sink
<
Mutex
>
{
public:
...
...
include/spdlog/sinks/null_sink.h
View file @
c2a49080
...
...
@@ -13,12 +13,11 @@
namespace
spdlog
{
namespace
sinks
{
template
<
class
Mutex
>
template
<
typename
Mutex
>
class
null_sink
:
public
base_sink
<
Mutex
>
{
protected:
void
sink_it_
(
const
details
::
log_msg
&
,
const
fmt
::
memory_buffer
&
)
override
{}
void
sink_it_
(
const
details
::
log_msg
&
)
override
{}
void
flush_
()
override
{}
};
...
...
include/spdlog/sinks/ostream_sink.h
View file @
c2a49080
...
...
@@ -13,8 +13,8 @@
namespace
spdlog
{
namespace
sinks
{
template
<
class
Mutex
>
class
ostream_sink
:
public
base_sink
<
Mutex
>
template
<
typename
Mutex
>
class
ostream_sink
SPDLOG_FINAL
:
public
base_sink
<
Mutex
>
{
public:
explicit
ostream_sink
(
std
::
ostream
&
os
,
bool
force_flush
=
false
)
...
...
@@ -26,8 +26,10 @@ public:
ostream_sink
&
operator
=
(
const
ostream_sink
&
)
=
delete
;
protected:
void
sink_it_
(
const
details
::
log_msg
&
msg
,
const
fmt
::
memory_buffer
&
formatted
)
override
void
sink_it_
(
const
details
::
log_msg
&
msg
)
override
{
fmt
::
memory_buffer
formatted
;
sink
::
formatter_
->
format
(
msg
,
formatted
);
ostream_
.
write
(
formatted
.
data
(),
formatted
.
size
());
if
(
force_flush_
)
ostream_
.
flush
();
...
...
include/spdlog/sinks/rotating_file_sink.h
View file @
c2a49080
...
...
@@ -23,7 +23,7 @@ namespace sinks {
//
// Rotating file sink based on size
//
template
<
class
Mutex
>
template
<
typename
Mutex
>
class
rotating_file_sink
SPDLOG_FINAL
:
public
base_sink
<
Mutex
>
{
public:
...
...
@@ -55,8 +55,10 @@ public:
}
protected:
void
sink_it_
(
const
details
::
log_msg
&
,
const
fmt
::
memory_buffer
&
formatted
)
override
void
sink_it_
(
const
details
::
log_msg
&
msg
)
override
{
fmt
::
memory_buffer
formatted
;
sink
::
formatter_
->
format
(
msg
,
formatted
);
current_size_
+=
formatted
.
size
();
if
(
current_size_
>
max_size_
)
{
...
...
include/spdlog/sinks/sink.h
View file @
c2a49080
...
...
@@ -14,16 +14,21 @@ namespace sinks {
class
sink
{
public:
// default sink ctor with default pattern formatter
sink
()
:
formatter_
(
std
::
unique_ptr
<
spdlog
::
formatter
>
(
new
pattern_formatter
(
"%+"
)))
:
level_
(
level
::
trace
)
,
formatter_
(
new
pattern_formatter
(
"%+"
))
{
}
virtual
~
sink
()
=
default
;
sink
(
std
::
unique_ptr
<
spdlog
::
pattern_formatter
>
formatter
)
:
level_
(
level
::
trace
)
,
formatter_
(
std
::
move
(
formatter
)){};
virtual
~
sink
()
=
default
;
virtual
void
log
(
const
details
::
log_msg
&
msg
)
=
0
;
virtual
void
flush
()
=
0
;
virtual
void
set_pattern
(
const
std
::
string
&
pattern
)
=
0
;
virtual
void
set_formatter
(
std
::
unique_ptr
<
spdlog
::
formatter
>
sink_formatter
)
=
0
;
bool
should_log
(
level
::
level_enum
msg_level
)
const
{
...
...
@@ -40,18 +45,11 @@ public:
return
static_cast
<
spdlog
::
level
::
level_enum
>
(
level_
.
load
(
std
::
memory_order_relaxed
));
}
void
set_pattern
(
const
std
::
string
&
pattern
)
{
formatter_
=
std
::
unique_ptr
<
spdlog
::
formatter
>
(
new
pattern_formatter
(
pattern
));
}
void
set_formatter
(
std
::
unique_ptr
<
spdlog
::
formatter
>
sink_formatter
)
{
formatter_
=
std
::
move
(
sink_formatter
);
}
protected:
level_t
level_
{
level
::
trace
};
// sink log level - default is all
level_t
level_
;
// sink formatter - default is full format
std
::
unique_ptr
<
spdlog
::
formatter
>
formatter_
;
};
...
...
include/spdlog/sinks/stdout_sinks.h
View file @
c2a49080
...
...
@@ -18,13 +18,13 @@ namespace spdlog {
namespace
sinks
{
template
<
class
TargetStream
,
class
ConsoleMutex
>
template
<
typename
TargetStream
,
typename
ConsoleMutex
>
class
stdout_sink
:
public
sink
{
public:
using
mutex_t
=
typename
ConsoleMutex
::
mutex_t
;
stdout_sink
()
:
mutex_
(
ConsoleMutex
::
console_
mutex
())
:
mutex_
(
ConsoleMutex
::
mutex
())
,
file_
(
TargetStream
::
stream
())
{
}
...
...
@@ -45,7 +45,19 @@ public:
void
flush
()
override
{
std
::
lock_guard
<
mutex_t
>
lock
(
mutex_
);
fflush
(
TargetStream
::
stream
());
fflush
(
file_
);
}
void
set_pattern
(
const
std
::
string
&
pattern
)
override
SPDLOG_FINAL
{
std
::
lock_guard
<
mutex_t
>
lock
(
mutex_
);
formatter_
=
std
::
unique_ptr
<
spdlog
::
formatter
>
(
new
pattern_formatter
(
pattern
));
}
void
set_formatter
(
std
::
unique_ptr
<
spdlog
::
formatter
>
sink_formatter
)
override
SPDLOG_FINAL
{
std
::
lock_guard
<
mutex_t
>
lock
(
mutex_
);
formatter_
=
std
::
move
(
sink_formatter
);
}
private:
...
...
@@ -53,11 +65,11 @@ private:
FILE
*
file_
;
};
using
stdout_sink_mt
=
stdout_sink
<
details
::
console_stdout
_stream
,
details
::
console_global
_mutex
>
;
using
stdout_sink_st
=
stdout_sink
<
details
::
console_stdout
_stream
,
details
::
console_global
_nullmutex
>
;
using
stdout_sink_mt
=
stdout_sink
<
details
::
console_stdout
,
details
::
console
_mutex
>
;
using
stdout_sink_st
=
stdout_sink
<
details
::
console_stdout
,
details
::
console
_nullmutex
>
;
using
stderr_sink_mt
=
stdout_sink
<
details
::
console_stderr
_stream
,
details
::
console_global
_mutex
>
;
using
stderr_sink_st
=
stdout_sink
<
details
::
console_stderr
_stream
,
details
::
console_global
_nullmutex
>
;
using
stderr_sink_mt
=
stdout_sink
<
details
::
console_stderr
,
details
::
console
_mutex
>
;
using
stderr_sink_st
=
stdout_sink
<
details
::
console_stderr
,
details
::
console
_nullmutex
>
;
}
// namespace sinks
...
...
include/spdlog/sinks/syslog_sink.h
View file @
c2a49080
...
...
@@ -5,10 +5,8 @@
#pragma once
#include "spdlog/common.h"
#include "spdlog/details/log_msg.h"
#include "spdlog/sinks/sink.h"
#include "spdlog/sinks/base_sink.h"
#include "spdlog/spdlog.h"
#include <array>
#include <string>
...
...
@@ -21,7 +19,8 @@ namespace sinks {
*
* Locking is not needed, as `syslog()` itself is thread-safe.
*/
class
syslog_sink
:
public
sink
template
<
typename
Mutex
>
class
syslog_sink
:
public
base_sink
<
Mutex
>
{
public:
//
...
...
@@ -48,12 +47,13 @@ public:
syslog_sink
(
const
syslog_sink
&
)
=
delete
;
syslog_sink
&
operator
=
(
const
syslog_sink
&
)
=
delete
;
void
log
(
const
details
::
log_msg
&
msg
)
override
protected:
void
sink_it_
(
const
details
::
log_msg
&
msg
)
override
{
::
syslog
(
syslog_prio_from_level
(
msg
),
"%s"
,
fmt
::
to_string
(
msg
.
raw
).
c_str
());
}
void
flush
()
override
{}
void
flush
_
()
override
{}
private:
std
::
array
<
int
,
7
>
priorities_
;
...
...
@@ -68,13 +68,23 @@ private:
return
priorities_
[
static_cast
<
size_t
>
(
msg
.
level
)];
}
};
using
syslog_sink_mt
=
syslog_sink
<
std
::
mutex
>
;
using
syslog_sink_st
=
syslog_sink
<
details
::
null_mutex
>
;
}
// namespace sinks
// Create and register a syslog logger
template
<
typename
Factory
=
default_factory
>
inline
std
::
shared_ptr
<
logger
>
syslog_logger
(
inline
std
::
shared_ptr
<
logger
>
syslog_logger_mt
(
const
std
::
string
&
logger_name
,
const
std
::
string
&
syslog_ident
=
""
,
int
syslog_option
=
0
,
int
syslog_facility
=
(
1
<<
3
))
{
return
Factory
::
template
create
<
sinks
::
syslog_sink_mt
>(
logger_name
,
syslog_ident
,
syslog_option
,
syslog_facility
);
}
template
<
typename
Factory
=
default_factory
>
inline
std
::
shared_ptr
<
logger
>
syslog_logger_st
(
const
std
::
string
&
logger_name
,
const
std
::
string
&
syslog_ident
=
""
,
int
syslog_option
=
0
,
int
syslog_facility
=
(
1
<<
3
))
{
return
Factory
::
template
create
<
sinks
::
syslog_sink
>(
logger_name
,
syslog_ident
,
syslog_option
,
syslog_facility
);
return
Factory
::
template
create
<
sinks
::
syslog_sink
_st
>(
logger_name
,
syslog_ident
,
syslog_option
,
syslog_facility
);
}
}
// namespace spdlog
include/spdlog/sinks/wincolor_sink.h
View file @
c2a49080
...
...
@@ -5,7 +5,6 @@
#pragma once
#include "../fmt/fmt.h"
#include "spdlog/common.h"
#include "spdlog/details/console_globals.h"
#include "spdlog/details/null_mutex.h"
...
...
@@ -22,7 +21,7 @@ namespace sinks {
/*
* Windows color console sink. Uses WriteConsoleA to write to the console with colors
*/
template
<
class
OutHandle
,
class
ConsoleMutex
>
template
<
typename
OutHandle
,
typename
ConsoleMutex
>
class
wincolor_sink
:
public
sink
{
public:
...
...
@@ -35,7 +34,7 @@ public:
wincolor_sink
()
:
out_handle_
(
OutHandle
::
handle
())
,
mutex_
(
ConsoleMutex
::
console_
mutex
())
,
mutex_
(
ConsoleMutex
::
mutex
())
{
colors_
[
level
::
trace
]
=
WHITE
;
colors_
[
level
::
debug
]
=
CYAN
;
...
...
@@ -89,6 +88,18 @@ public:
// windows console always flushed?
}
void
set_pattern
(
const
std
::
string
&
pattern
)
override
SPDLOG_FINAL
{
std
::
lock_guard
<
mutex_t
>
lock
(
mutex_
);
formatter_
=
std
::
unique_ptr
<
spdlog
::
formatter
>
(
new
pattern_formatter
(
pattern
));
}
void
set_formatter
(
std
::
unique_ptr
<
spdlog
::
formatter
>
sink_formatter
)
override
SPDLOG_FINAL
{
std
::
lock_guard
<
mutex_t
>
lock
(
mutex_
);
formatter_
=
std
::
move
(
sink_formatter
);
}
private:
using
mutex_t
=
typename
ConsoleMutex
::
mutex_t
;
// set color and return the orig console attributes (for resetting later)
...
...
@@ -116,11 +127,11 @@ private:
std
::
unordered_map
<
level
::
level_enum
,
WORD
,
level
::
level_hasher
>
colors_
;
};
using
wincolor_stdout_sink_mt
=
wincolor_sink
<
details
::
console_stdout
_stream
,
details
::
console_global
_mutex
>
;
using
wincolor_stdout_sink_st
=
wincolor_sink
<
details
::
console_stdout
_stream
,
details
::
console_global
_nullmutex
>
;
using
wincolor_stdout_sink_mt
=
wincolor_sink
<
details
::
console_stdout
,
details
::
console
_mutex
>
;
using
wincolor_stdout_sink_st
=
wincolor_sink
<
details
::
console_stdout
,
details
::
console
_nullmutex
>
;
using
wincolor_stderr_sink_mt
=
wincolor_sink
<
details
::
console_stderr
_stream
,
details
::
console_global
_mutex
>
;
using
wincolor_stderr_sink_st
=
wincolor_sink
<
details
::
console_stderr
_stream
,
details
::
console_global
_nullmutex
>
;
using
wincolor_stderr_sink_mt
=
wincolor_sink
<
details
::
console_stderr
,
details
::
console
_mutex
>
;
using
wincolor_stderr_sink_st
=
wincolor_sink
<
details
::
console_stderr
,
details
::
console
_nullmutex
>
;
}
// namespace sinks
}
// namespace spdlog
tests/errors.cpp
View file @
c2a49080
...
...
@@ -5,14 +5,19 @@
#include <iostream>
class
failing_sink
:
public
spdlog
::
sinks
::
sink
class
failing_sink
:
public
spdlog
::
sinks
::
base_sink
<
std
::
mutex
>
{
void
log
(
const
spdlog
::
details
::
log_msg
&
)
override
public:
failing_sink
()
=
default
;
~
failing_sink
()
=
default
;
protected:
void
sink_it_
(
const
spdlog
::
details
::
log_msg
&
)
override
{
throw
std
::
runtime_error
(
"some error happened during log"
);
}
void
flush
()
override
void
flush
_
()
override
{
throw
std
::
runtime_error
(
"some error happened during flush"
);
}
...
...
tests/test_sink.h
View file @
c2a49080
...
...
@@ -35,7 +35,7 @@ public:
}
protected:
void
sink_it_
(
const
details
::
log_msg
&
,
const
fmt
::
memory_buffer
&
)
override
void
sink_it_
(
const
details
::
log_msg
&
)
override
{
msg_counter_
++
;
std
::
this_thread
::
sleep_for
(
delay_
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment