Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
S
spdlog
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
spdlog
Commits
0c19bdd7
Commit
0c19bdd7
authored
Jun 23, 2018
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pattern per sink and pattern caching
parent
9d7a5c25
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
24 changed files
with
185 additions
and
129 deletions
+185
-129
common.h
include/spdlog/common.h
+1
-1
async_logger_impl.h
include/spdlog/details/async_logger_impl.h
+0
-1
file_helper.h
include/spdlog/details/file_helper.h
+3
-3
fmt_helper.h
include/spdlog/details/fmt_helper.h
+7
-1
log_msg.h
include/spdlog/details/log_msg.h
+2
-3
logger_impl.h
include/spdlog/details/logger_impl.h
+8
-5
pattern_formatter_impl.h
include/spdlog/details/pattern_formatter_impl.h
+0
-0
registry.h
include/spdlog/details/registry.h
+6
-17
formatter.h
include/spdlog/formatter.h
+10
-8
logger.h
include/spdlog/logger.h
+14
-5
android_sink.h
include/spdlog/sinks/android_sink.h
+11
-1
ansicolor_sink.h
include/spdlog/sinks/ansicolor_sink.h
+9
-6
base_sink.h
include/spdlog/sinks/base_sink.h
+18
-3
daily_file_sink.h
include/spdlog/sinks/daily_file_sink.h
+2
-2
null_sink.h
include/spdlog/sinks/null_sink.h
+2
-2
ostream_sink.h
include/spdlog/sinks/ostream_sink.h
+2
-2
rotating_file_sink.h
include/spdlog/sinks/rotating_file_sink.h
+4
-4
simple_file_sink.h
include/spdlog/sinks/simple_file_sink.h
+2
-2
sink.h
include/spdlog/sinks/sink.h
+42
-19
wincolor_sink.h
include/spdlog/sinks/wincolor_sink.h
+9
-7
spdlog.h
include/spdlog/spdlog.h
+0
-5
file_helper.cpp
tests/file_helper.cpp
+3
-3
test_pattern_formatter.cpp
tests/test_pattern_formatter.cpp
+29
-28
test_sink.h
tests/test_sink.h
+1
-1
No files found.
include/spdlog/common.h
View file @
0c19bdd7
...
...
@@ -49,7 +49,7 @@
#define SPDLOG_DEPRECATED
#endif
#include "fmt/fmt.h"
#include "
spdlog/
fmt/fmt.h"
namespace
spdlog
{
...
...
include/spdlog/details/async_logger_impl.h
View file @
0c19bdd7
...
...
@@ -71,7 +71,6 @@ inline void spdlog::async_logger::backend_log_(details::log_msg &incoming_log_ms
{
try
{
formatter_
->
format
(
incoming_log_msg
);
for
(
auto
&
s
:
sinks_
)
{
if
(
s
->
should_log
(
incoming_log_msg
.
level
))
...
...
include/spdlog/details/file_helper.h
View file @
0c19bdd7
...
...
@@ -80,10 +80,10 @@ public:
}
}
void
write
(
const
log_msg
&
msg
)
void
write
(
const
fmt
::
memory_buffer
&
buf
)
{
size_t
msg_size
=
msg
.
formatted
.
size
();
auto
data
=
msg
.
formatted
.
data
();
size_t
msg_size
=
buf
.
size
();
auto
data
=
buf
.
data
();
if
(
std
::
fwrite
(
data
,
1
,
msg_size
,
fd_
)
!=
msg_size
)
{
throw
spdlog_ex
(
"Failed writing to file "
+
os
::
filename_to_str
(
_filename
),
errno
);
...
...
include/spdlog/details/fmt_helper.h
View file @
0c19bdd7
...
...
@@ -14,6 +14,12 @@ inline void append_str(const std::string &str, fmt::memory_buffer &dest)
dest
.
append
(
str_ptr
,
str_ptr
+
str
.
size
());
}
inline
void
append_c_str
(
const
char
*
c_str
,
fmt
::
memory_buffer
&
dest
)
{
auto
str_size
=
strlen
(
c_str
);
dest
.
append
(
c_str
,
c_str
+
str_size
);
}
inline
void
append_buf
(
const
fmt
::
memory_buffer
&
buf
,
fmt
::
memory_buffer
&
dest
)
{
const
char
*
buf_ptr
=
buf
.
data
();
...
...
@@ -74,7 +80,7 @@ inline void append_and_pad3(int n, fmt::memory_buffer &dest)
append_int
(
n
,
dest
);
}
void
append_and_pad6
(
int
n
,
fmt
::
memory_buffer
&
dest
)
inline
void
append_and_pad6
(
int
n
,
fmt
::
memory_buffer
&
dest
)
{
if
(
n
>
99999
)
{
...
...
include/spdlog/details/log_msg.h
View file @
0c19bdd7
...
...
@@ -38,11 +38,10 @@ struct log_msg
log_clock
::
time_point
time
;
size_t
thread_id
;
fmt
::
memory_buffer
raw
;
fmt
::
memory_buffer
formatted
;
size_t
msg_id
{
0
};
// info about wrapping the formatted text with color
size_t
color_range_start
{
0
};
size_t
color_range_end
{
0
};
mutable
size_t
color_range_start
{
0
};
mutable
size_t
color_range_end
{
0
};
};
}
// namespace details
}
// namespace spdlog
include/spdlog/details/logger_impl.h
View file @
0c19bdd7
...
...
@@ -14,7 +14,6 @@ template<class It>
inline
spdlog
::
logger
::
logger
(
std
::
string
logger_name
,
const
It
&
begin
,
const
It
&
end
)
:
name_
(
std
::
move
(
logger_name
))
,
sinks_
(
begin
,
end
)
,
formatter_
(
std
::
make_shared
<
pattern_formatter
>
(
"%+"
))
,
level_
(
level
::
info
)
,
flush_level_
(
level
::
off
)
,
last_err_time_
(
0
)
...
...
@@ -37,14 +36,19 @@ inline spdlog::logger::logger(const std::string &logger_name, spdlog::sink_ptr s
inline
spdlog
::
logger
::~
logger
()
=
default
;
inline
void
spdlog
::
logger
::
set_formatter
(
spdlog
::
formatter_ptr
msg_formatter
)
template
<
class
FormatterT
,
typename
...
Args
>
inline
void
spdlog
::
logger
::
set_formatter
(
const
Args
&
...
args
)
{
formatter_
=
std
::
move
(
msg_formatter
);
for
(
auto
&
sink
:
sinks_
)
{
std
::
unique_ptr
<
FormatterT
>
formatter
(
new
FormatterT
(
args
...));
sink
->
set_formatter
(
std
::
move
(
formatter
));
}
}
inline
void
spdlog
::
logger
::
set_pattern
(
const
std
::
string
&
pattern
,
pattern_time_type
pattern_time
)
{
formatter_
=
std
::
make_shared
<
pattern_formatter
>
(
pattern
,
pattern_time
);
set_formatter
<
spdlog
::
pattern_formatter
>
(
pattern
,
pattern_time
);
}
template
<
typename
...
Args
>
...
...
@@ -288,7 +292,6 @@ inline void spdlog::logger::sink_it_(details::log_msg &msg)
#if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)
incr_msg_counter_
(
msg
);
#endif
formatter_
->
format
(
msg
);
for
(
auto
&
sink
:
sinks_
)
{
if
(
sink
->
should_log
(
msg
.
level
))
...
...
include/spdlog/details/pattern_formatter_impl.h
View file @
0c19bdd7
This diff is collapsed.
Click to expand it.
include/spdlog/details/registry.h
View file @
0c19bdd7
...
...
@@ -46,10 +46,9 @@ public:
auto
logger_name
=
new_logger
->
name
();
throw_if_exists
(
logger_name
);
if
(
formatter_
)
{
new_logger
->
set_formatter
(
formatter_
);
}
// create default formatter if not exists
new_logger
->
set_pattern
(
formatter_pattern_
);
if
(
err_handler_
)
{
...
...
@@ -82,23 +81,13 @@ public:
return
tp_
;
}
void
set_formatter
(
formatter_ptr
f
)
{
std
::
lock_guard
<
Mutex
>
lock
(
mutex_
);
formatter_
=
f
;
for
(
auto
&
l
:
loggers_
)
{
l
.
second
->
set_formatter
(
formatter_
);
}
}
void
set_pattern
(
const
std
::
string
&
pattern
)
{
std
::
lock_guard
<
Mutex
>
lock
(
mutex_
);
formatter_
=
std
::
make_shared
<
pattern_formatter
>
(
pattern
)
;
formatter_
pattern_
=
pattern
;
for
(
auto
&
l
:
loggers_
)
{
l
.
second
->
set_
formatter
(
formatter_
);
l
.
second
->
set_
pattern
(
pattern
);
}
}
...
...
@@ -184,7 +173,7 @@ private:
Mutex
mutex_
;
Mutex
tp_mutex_
;
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
logger
>>
loggers_
;
formatter_ptr
formatter_
;
std
::
string
formatter_pattern_
=
"%+"
;
level
::
level_enum
level_
=
level
::
info
;
level
::
level_enum
flush_level_
=
level
::
off
;
log_err_handler
err_handler_
;
...
...
include/spdlog/formatter.h
View file @
0c19bdd7
...
...
@@ -5,6 +5,7 @@
#pragma once
#include "fmt/fmt.h"
#include "spdlog/details/log_msg.h"
#include <memory>
...
...
@@ -12,31 +13,32 @@
#include <vector>
namespace
spdlog
{
namespace
details
{
class
flag_formatter
;
}
class
formatter
{
public
:
virtual
~
formatter
()
=
default
;
virtual
void
format
(
details
::
log_msg
&
msg
)
=
0
;
virtual
void
format
(
const
details
::
log_msg
&
msg
,
fmt
::
memory_buffer
&
dest
)
=
0
;
};
namespace
details
{
class
flag_formatter
;
}
class
pattern_formatter
SPDLOG_FINAL
:
public
formatter
{
public
:
explicit
pattern_formatter
(
const
std
::
string
&
pattern
,
pattern_time_type
pattern_time
=
pattern_time_type
::
local
,
std
::
string
eol
=
spdlog
::
details
::
os
::
default_eol
);
pattern_formatter
(
const
pattern_formatter
&
)
=
de
lete
;
pattern_formatter
&
operator
=
(
const
pattern_formatter
&
)
=
de
lete
;
void
format
(
details
::
log_msg
&
msg
)
override
;
pattern_formatter
(
const
pattern_formatter
&
)
=
de
fault
;
pattern_formatter
&
operator
=
(
const
pattern_formatter
&
)
=
de
fault
;
void
format
(
const
details
::
log_msg
&
msg
,
fmt
::
memory_buffer
&
dest
)
override
;
private
:
const
std
::
string
eol_
;
const
pattern_time_type
pattern_time_
;
std
::
vector
<
std
::
unique_ptr
<
details
::
flag_formatter
>>
formatters_
;
std
::
tm
get_time
(
details
::
log_msg
&
msg
);
std
::
tm
get_time
(
const
details
::
log_msg
&
msg
);
void
handle_flag
(
char
flag
);
void
compile_pattern
(
const
std
::
string
&
pattern
);
};
...
...
include/spdlog/logger.h
View file @
0c19bdd7
...
...
@@ -8,9 +8,12 @@
// 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
// Upon each log write the logger:
// 1. Checks if its log level is enough to log the message
// 2. Format the message using the formatter function
// 3. Pass the formatted message to its sinks to performa the actual logging
// 1. Checks if its log level is enough to log the message and if yes:
// 2. Call the underlying sinks to do the job.
// 3. Each sink use its own private copy of a formatter to format the message and send to its destination.
//
// The use of private formatter per sink provides the opportunity to cache some formatted data,
// and support customize format per each sink.
#include "spdlog/common.h"
#include "spdlog/formatter.h"
...
...
@@ -111,8 +114,15 @@ public:
void
set_level
(
level
::
level_enum
log_level
);
level
::
level_enum
level
()
const
;
const
std
::
string
&
name
()
const
;
// create a pattern formatter all the sinks in this logger.
// each sink gets itw own private copy of a formatter object.
void
set_pattern
(
const
std
::
string
&
pattern
,
pattern_time_type
pattern_time
=
pattern_time_type
::
local
);
void
set_formatter
(
formatter_ptr
msg_formatter
);
// create a FormatterT formatter all the sinks in this logger.
// each sink gets itw own private copy of a formatter object.
template
<
class
FormatterT
,
typename
...
Args
>
void
set_formatter
(
const
Args
&
...
args
);
void
flush
();
void
flush_on
(
level
::
level_enum
log_level
);
...
...
@@ -137,7 +147,6 @@ protected:
const
std
::
string
name_
;
std
::
vector
<
sink_ptr
>
sinks_
;
formatter_ptr
formatter_
;
spdlog
::
level_t
level_
;
spdlog
::
level_t
flush_level_
;
log_err_handler
err_handler_
;
...
...
include/spdlog/sinks/android_sink.h
View file @
0c19bdd7
...
...
@@ -39,7 +39,17 @@ public:
void
log
(
const
details
::
log_msg
&
msg
)
override
{
const
android_LogPriority
priority
=
convert_to_android
(
msg
.
level
);
const
char
*
msg_output
=
(
use_raw_msg_
?
msg
.
raw
.
c_str
()
:
msg
.
formatted
.
c_str
());
fmt
::
memory_buffer
formatted
;
if
(
use_raw_msg_
)
{
formatted
.
append
(
msg
.
raw
.
data
(),
msg
.
raw
.
data
()
+
msg
.
raw
.
size
());
}
else
{
formatter_
->
format
(
msg
,
formatted
);
}
formatted
.
push_back
(
'\0'
);
const
char
*
msg_output
=
formatted
.
data
();
// See system/core/liblog/logger_write.c for explanation of return value
int
ret
=
__android_log_write
(
priority
,
tag_
.
c_str
(),
msg_output
);
...
...
include/spdlog/sinks/ansicolor_sink.h
View file @
0c19bdd7
...
...
@@ -88,20 +88,23 @@ public:
// Wrap the originally formatted message in color codes.
// If color is not supported in the terminal, log as is instead.
std
::
lock_guard
<
mutex_t
>
lock
(
mutex_
);
fmt
::
memory_buffer
formatted
;
formatter_
->
format
(
msg
,
formatted
);
if
(
should_do_colors_
&&
msg
.
color_range_end
>
msg
.
color_range_start
)
{
// before color range
print_range_
(
msg
,
0
,
msg
.
color_range_start
);
print_range_
(
formatted
,
0
,
msg
.
color_range_start
);
// in color range
print_ccode_
(
colors_
[
msg
.
level
]);
print_range_
(
msg
,
msg
.
color_range_start
,
msg
.
color_range_end
);
print_range_
(
formatted
,
msg
.
color_range_start
,
msg
.
color_range_end
);
print_ccode_
(
reset
);
// after color range
print_range_
(
msg
,
msg
.
color_range_end
,
msg
.
formatted
.
size
());
print_range_
(
formatted
,
msg
.
color_range_end
,
formatted
.
size
());
}
else
// no color
{
print_range_
(
msg
,
0
,
msg
.
formatted
.
size
());
print_range_
(
formatted
,
0
,
formatted
.
size
());
}
fflush
(
target_file_
);
}
...
...
@@ -117,9 +120,9 @@ private:
{
fwrite
(
color_code
.
data
(),
sizeof
(
char
),
color_code
.
size
(),
target_file_
);
}
void
print_range_
(
const
details
::
log_msg
&
msg
,
size_t
start
,
size_t
end
)
void
print_range_
(
const
fmt
::
memory_buffer
&
formatted
,
size_t
start
,
size_t
end
)
{
fwrite
(
msg
.
formatted
.
data
()
+
start
,
sizeof
(
char
),
end
-
start
,
target_file_
);
fwrite
(
formatted
.
data
()
+
start
,
sizeof
(
char
),
end
-
start
,
target_file_
);
}
FILE
*
target_file_
;
...
...
include/spdlog/sinks/base_sink.h
View file @
0c19bdd7
...
...
@@ -21,7 +21,20 @@ template<class Mutex>
class
base_sink
:
public
sink
{
public
:
base_sink
()
=
default
;
base_sink
()
:
sink
()
{
}
base_sink
(
const
std
::
string
&
formatter_pattern
)
:
sink
(
formatter_pattern
)
{
}
base_sink
(
std
::
unique_ptr
<
spdlog
::
formatter
>
sink_formatter
)
:
sink
(
std
::
move
(
sink_formatter
))
{
}
base_sink
(
const
base_sink
&
)
=
delete
;
base_sink
&
operator
=
(
const
base_sink
&
)
=
delete
;
...
...
@@ -29,7 +42,9 @@ public:
void
log
(
const
details
::
log_msg
&
msg
)
SPDLOG_FINAL
override
{
std
::
lock_guard
<
Mutex
>
lock
(
mutex_
);
sink_it_
(
msg
);
fmt
::
memory_buffer
formatted
;
formatter_
->
format
(
msg
,
formatted
);
sink_it_
(
msg
,
formatted
);
}
void
flush
()
SPDLOG_FINAL
override
...
...
@@ -39,7 +54,7 @@ public:
}
protected
:
virtual
void
sink_it_
(
const
details
::
log_msg
&
msg
)
=
0
;
virtual
void
sink_it_
(
const
details
::
log_msg
&
msg
,
const
fmt
::
memory_buffer
&
formatted
)
=
0
;
virtual
void
flush_
()
=
0
;
Mutex
mutex_
;
};
...
...
include/spdlog/sinks/daily_file_sink.h
View file @
0c19bdd7
...
...
@@ -76,14 +76,14 @@ public:
}
protected
:
void
sink_it_
(
const
details
::
log_msg
&
msg
)
override
void
sink_it_
(
const
details
::
log_msg
&
,
const
fmt
::
memory_buffer
&
formatted
)
override
{
if
(
std
::
chrono
::
system_clock
::
now
()
>=
rotation_tp_
)
{
file_helper_
.
open
(
FileNameCalc
::
calc_filename
(
base_filename_
));
rotation_tp_
=
next_rotation_tp_
();
}
file_helper_
.
write
(
msg
);
file_helper_
.
write
(
formatted
);
}
void
flush_
()
override
...
...
include/spdlog/sinks/null_sink.h
View file @
0c19bdd7
...
...
@@ -17,12 +17,12 @@ template<class Mutex>
class
null_sink
:
public
base_sink
<
Mutex
>
{
protected
:
void
sink_it_
(
const
details
::
log_msg
&
)
override
{}
void
sink_it_
(
const
details
::
log_msg
&
,
const
fmt
::
memory_buffer
&
)
override
{}
void
flush_
()
override
{}
};
using
null_sink_mt
=
null_sink
<
details
::
null_
mutex
>
;
using
null_sink_mt
=
null_sink
<
std
::
mutex
>
;
using
null_sink_st
=
null_sink
<
details
::
null_mutex
>
;
}
// namespace sinks
...
...
include/spdlog/sinks/ostream_sink.h
View file @
0c19bdd7
...
...
@@ -26,9 +26,9 @@ public:
ostream_sink
&
operator
=
(
const
ostream_sink
&
)
=
delete
;
protected
:
void
sink_it_
(
const
details
::
log_msg
&
msg
)
override
void
sink_it_
(
const
details
::
log_msg
&
msg
,
const
fmt
::
memory_buffer
&
formatted
)
override
{
ostream_
.
write
(
msg
.
formatted
.
data
(),
msg
.
formatted
.
size
());
ostream_
.
write
(
formatted
.
data
(),
formatted
.
size
());
if
(
force_flush_
)
ostream_
.
flush
();
}
...
...
include/spdlog/sinks/rotating_file_sink.h
View file @
0c19bdd7
...
...
@@ -55,15 +55,15 @@ public:
}
protected
:
void
sink_it_
(
const
details
::
log_msg
&
msg
)
override
void
sink_it_
(
const
details
::
log_msg
&
,
const
fmt
::
memory_buffer
&
formatted
)
override
{
current_size_
+=
msg
.
formatted
.
size
();
current_size_
+=
formatted
.
size
();
if
(
current_size_
>
max_size_
)
{
rotate_
();
current_size_
=
msg
.
formatted
.
size
();
current_size_
=
formatted
.
size
();
}
file_helper_
.
write
(
msg
);
file_helper_
.
write
(
formatted
);
}
void
flush_
()
override
...
...
include/spdlog/sinks/simple_file_sink.h
View file @
0c19bdd7
...
...
@@ -33,9 +33,9 @@ public:
}
protected
:
void
sink_it_
(
const
details
::
log_msg
&
msg
)
override
void
sink_it_
(
const
details
::
log_msg
&
,
const
fmt
::
memory_buffer
&
formatted
)
override
{
file_helper_
.
write
(
msg
);
file_helper_
.
write
(
formatted
);
if
(
force_flush_
)
{
file_helper_
.
flush
();
...
...
include/spdlog/sinks/sink.h
View file @
0c19bdd7
...
...
@@ -6,39 +6,62 @@
#pragma once
#include "spdlog/details/log_msg.h"
#include "spdlog/formatter.h"
namespace
spdlog
{
namespace
sinks
{
class
sink
{
public
:
// default sink ctor with default pattern formatter
sink
()
:
formatter_
(
std
::
unique_ptr
<
spdlog
::
formatter
>
(
new
pattern_formatter
(
"%+"
)))
{
}
explicit
sink
(
const
std
::
string
&
formatter_pattern
)
:
formatter_
(
std
::
unique_ptr
<
spdlog
::
formatter
>
(
new
pattern_formatter
(
formatter_pattern
)))
{
}
// sink with custom formatter
explicit
sink
(
std
::
unique_ptr
<
spdlog
::
formatter
>
sink_formatter
)
:
formatter_
(
std
::
move
(
sink_formatter
))
{
}
virtual
~
sink
()
=
default
;
virtual
void
log
(
const
details
::
log_msg
&
msg
)
=
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
;
bool
should_log
(
level
::
level_enum
msg_level
)
const
{
return
msg_level
>=
level_
.
load
(
std
::
memory_order_relaxed
);
}
void
set_level
(
level
::
level_enum
log_level
)
{
level_
.
store
(
log_level
);
}
level
::
level_enum
level
()
const
{
return
static_cast
<
spdlog
::
level
::
level_enum
>
(
level_
.
load
(
std
::
memory_order_relaxed
));
}
private
:
level_t
level_
{
level
::
trace
};
};
void
set_formatter
(
std
::
unique_ptr
<
spdlog
::
formatter
>
sink_formatter
)
{
formatter_
=
std
::
move
(
sink_formatter
);
}
inline
bool
sink
::
should_log
(
level
::
level_enum
msg_level
)
const
{
return
msg_level
>=
level_
.
load
(
std
::
memory_order_relaxed
);
}
spdlog
::
formatter
*
formatter
()
{
return
formatter_
.
get
(
);
}
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
));
}
protected
:
level_t
level_
{
level
::
trace
};
std
::
unique_ptr
<
spdlog
::
formatter
>
formatter_
;
};
}
// namespace sinks
}
// namespace spdlog
include/spdlog/sinks/wincolor_sink.h
View file @
0c19bdd7
...
...
@@ -5,6 +5,7 @@
#pragma once
#include "../fmt/fmt.h"
#include "spdlog/common.h"
#include "spdlog/details/null_mutex.h"
#include "spdlog/details/traits.h"
...
...
@@ -63,22 +64,23 @@ public:
void
log
(
const
details
::
log_msg
&
msg
)
SPDLOG_FINAL
override
{
std
::
lock_guard
<
mutex_t
>
lock
(
mutex_
);
fmt
::
memory_buffer
formatted
;
formatter_
->
format
(
msg
,
formatted
);
if
(
msg
.
color_range_end
>
msg
.
color_range_start
)
{
// before color range
print_range_
(
msg
,
0
,
msg
.
color_range_start
);
print_range_
(
formatted
,
0
,
msg
.
color_range_start
);
// in color range
auto
orig_attribs
=
set_console_attribs
(
colors_
[
msg
.
level
]);
print_range_
(
msg
,
msg
.
color_range_start
,
msg
.
color_range_end
);
print_range_
(
formatted
,
msg
.
color_range_start
,
msg
.
color_range_end
);
::
SetConsoleTextAttribute
(
out_handle_
,
orig_attribs
);
// reset to orig colors
// after color range
print_range_
(
msg
,
msg
.
color_range_end
,
msg
.
formatted
.
size
());
print_range_
(
formatted
,
msg
.
color_range_end
,
formatted
.
size
());
}
else
// print without colors if color range is invalid
{
print_range_
(
msg
,
0
,
msg
.
formatted
.
size
());
print_range_
(
formatted
,
0
,
formatted
.
size
());
}
}
...
...
@@ -103,10 +105,10 @@ private:
}
// print a range of formatted message to console
void
print_range_
(
const
details
::
log_msg
&
msg
,
size_t
start
,
size_t
end
)
void
print_range_
(
const
fmt
::
memory_buffer
formatted
,
size_t
start
,
size_t
end
)
{
auto
size
=
static_cast
<
DWORD
>
(
end
-
start
);
::
WriteConsoleA
(
out_handle_
,
msg
.
formatted
.
data
()
+
start
,
size
,
nullptr
,
nullptr
);
::
WriteConsoleA
(
out_handle_
,
formatted
.
data
()
+
start
,
size
,
nullptr
,
nullptr
);
}
HANDLE
out_handle_
;
...
...
include/spdlog/spdlog.h
View file @
0c19bdd7
...
...
@@ -62,11 +62,6 @@ inline void set_pattern(const std::string &format_string)
details
::
registry
::
instance
().
set_pattern
(
format_string
);
}
inline
void
set_formatter
(
formatter_ptr
f
)
{
details
::
registry
::
instance
().
set_formatter
(
std
::
move
(
f
));
}
//
// Set global logging level
//
...
...
tests/file_helper.cpp
View file @
0c19bdd7
...
...
@@ -11,9 +11,9 @@ static const std::string target_filename = "logs/file_helper_test.txt";
static
void
write_with_helper
(
file_helper
&
helper
,
size_t
howmany
)
{
log_msg
msg
;
fmt
::
format_to
(
msg
.
formatted
,
"{}"
,
std
::
string
(
howmany
,
'1'
));
helper
.
write
(
msg
);
fmt
::
memory_buffer
formatted
;
fmt
::
format_to
(
formatted
,
"{}"
,
std
::
string
(
howmany
,
'1'
));
helper
.
write
(
formatted
);
helper
.
flush
();
}
...
...
tests/test_pattern_formatter.cpp
View file @
0c19bdd7
#include "includes.h"
// log to str and return it
static
std
::
string
log_to_str
(
const
std
::
string
&
msg
,
const
std
::
shared_ptr
<
spdlog
::
formatter
>
&
formatter
=
nullptr
)
template
<
typename
...
Args
>
static
std
::
string
log_to_str
(
const
std
::
string
&
msg
,
const
Args
&
...
args
)
{
std
::
ostringstream
oss
;
auto
oss_sink
=
std
::
make_shared
<
spdlog
::
sinks
::
ostream_sink_mt
>
(
oss
);
spdlog
::
logger
oss_logger
(
"pattern_tester"
,
oss_sink
);
oss_logger
.
set_level
(
spdlog
::
level
::
info
);
if
(
formatter
)
{
oss_logger
.
set_formatter
(
formatter
);
}
oss_logger
.
set_formatter
<
spdlog
::
pattern_formatter
>
(
args
...);
oss_logger
.
info
(
msg
);
return
oss
.
str
();
}
...
...
@@ -19,49 +19,44 @@ TEST_CASE("custom eol", "[pattern_formatter]")
{
std
::
string
msg
=
"Hello custom eol test"
;
std
::
string
eol
=
";)"
;
auto
formatter
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
(
"%v"
,
spdlog
::
pattern_time_type
::
local
,
";)"
);
// auto formatter = std::make_shared<spdlog::pattern_formatter>("%v", spdlog::pattern_time_type::local, ";)");
std
::
unique_ptr
<
spdlog
::
formatter
>
(
new
spdlog
::
pattern_formatter
(
"%v"
,
spdlog
::
pattern_time_type
::
local
,
";)"
));
REQUIRE
(
log_to_str
(
msg
,
formatter
)
==
msg
+
eol
);
REQUIRE
(
log_to_str
(
msg
,
"%v"
,
spdlog
::
pattern_time_type
::
local
,
";)"
)
==
msg
+
eol
);
}
TEST_CASE
(
"empty format"
,
"[pattern_formatter]"
)
{
auto
formatter
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
(
""
,
spdlog
::
pattern_time_type
::
local
,
""
);
REQUIRE
(
log_to_str
(
"Some message"
,
formatter
)
==
""
);
REQUIRE
(
log_to_str
(
"Some message"
,
""
,
spdlog
::
pattern_time_type
::
local
,
""
)
==
""
);
}
TEST_CASE
(
"empty format2"
,
"[pattern_formatter]"
)
{
auto
formatter
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
(
""
,
spdlog
::
pattern_time_type
::
local
,
"
\n
"
);
REQUIRE
(
log_to_str
(
"Some message"
,
formatter
)
==
"
\n
"
);
REQUIRE
(
log_to_str
(
"Some message"
,
""
,
spdlog
::
pattern_time_type
::
local
,
"
\n
"
)
==
"
\n
"
);
}
TEST_CASE
(
"level"
,
"[pattern_formatter]"
)
{
auto
formatter
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
(
"[%l] %v"
,
spdlog
::
pattern_time_type
::
local
,
"
\n
"
);
REQUIRE
(
log_to_str
(
"Some message"
,
formatter
)
==
"[info] Some message
\n
"
);
REQUIRE
(
log_to_str
(
"Some message"
,
"[%l] %v"
,
spdlog
::
pattern_time_type
::
local
,
"
\n
"
)
==
"[info] Some message
\n
"
);
}
TEST_CASE
(
"short level"
,
"[pattern_formatter]"
)
{
auto
formatter
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
(
"[%L] %v"
,
spdlog
::
pattern_time_type
::
local
,
"
\n
"
);
REQUIRE
(
log_to_str
(
"Some message"
,
formatter
)
==
"[I] Some message
\n
"
);
REQUIRE
(
log_to_str
(
"Some message"
,
"[%L] %v"
,
spdlog
::
pattern_time_type
::
local
,
"
\n
"
)
==
"[I] Some message
\n
"
);
}
TEST_CASE
(
"name"
,
"[pattern_formatter]"
)
{
auto
formatter
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
(
"[%n] %v"
,
spdlog
::
pattern_time_type
::
local
,
"
\n
"
);
REQUIRE
(
log_to_str
(
"Some message"
,
formatter
)
==
"[pattern_tester] Some message
\n
"
);
REQUIRE
(
log_to_str
(
"Some message"
,
"[%n] %v"
,
spdlog
::
pattern_time_type
::
local
,
"
\n
"
)
==
"[pattern_tester] Some message
\n
"
);
}
TEST_CASE
(
"date MM/DD/YY "
,
"[pattern_formatter]"
)
{
auto
formatter
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
(
"%D %v"
,
spdlog
::
pattern_time_type
::
local
,
"
\n
"
);
auto
now_tm
=
spdlog
::
details
::
os
::
localtime
();
std
::
stringstream
oss
;
oss
<<
std
::
setfill
(
'0'
)
<<
std
::
setw
(
2
)
<<
now_tm
.
tm_mon
+
1
<<
"/"
<<
std
::
setw
(
2
)
<<
now_tm
.
tm_mday
<<
"/"
<<
std
::
setw
(
2
)
<<
(
now_tm
.
tm_year
+
1900
)
%
1000
<<
" Some message
\n
"
;
REQUIRE
(
log_to_str
(
"Some message"
,
formatter
)
==
oss
.
str
());
REQUIRE
(
log_to_str
(
"Some message"
,
"%D %v"
,
spdlog
::
pattern_time_type
::
local
,
"
\n
"
)
==
oss
.
str
());
}
TEST_CASE
(
"color range test1"
,
"[pattern_formatter]"
)
...
...
@@ -69,27 +64,30 @@ TEST_CASE("color range test1", "[pattern_formatter]")
auto
formatter
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
(
"%^%v%$"
,
spdlog
::
pattern_time_type
::
local
,
"
\n
"
);
spdlog
::
details
::
log_msg
msg
;
fmt
::
format_to
(
msg
.
raw
,
"Hello"
);
formatter
->
format
(
msg
);
fmt
::
memory_buffer
formatted
;
formatter
->
format
(
msg
,
formatted
);
REQUIRE
(
msg
.
color_range_start
==
0
);
REQUIRE
(
msg
.
color_range_end
==
5
);
REQUIRE
(
log_to_str
(
"hello"
,
formatter
)
==
"hello
\n
"
);
REQUIRE
(
log_to_str
(
"hello"
,
"%^%v%$"
,
spdlog
::
pattern_time_type
::
local
,
"
\n
"
)
==
"hello
\n
"
);
}
TEST_CASE
(
"color range test2"
,
"[pattern_formatter]"
)
{
auto
formatter
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
(
"%^%$"
,
spdlog
::
pattern_time_type
::
local
,
"
\n
"
);
spdlog
::
details
::
log_msg
msg
;
formatter
->
format
(
msg
);
fmt
::
memory_buffer
formatted
;
formatter
->
format
(
msg
,
formatted
);
REQUIRE
(
msg
.
color_range_start
==
0
);
REQUIRE
(
msg
.
color_range_end
==
0
);
REQUIRE
(
log_to_str
(
""
,
formatter
)
==
"
\n
"
);
REQUIRE
(
log_to_str
(
""
,
"%^%$"
,
spdlog
::
pattern_time_type
::
local
,
"
\n
"
)
==
"
\n
"
);
}
TEST_CASE
(
"color range test3"
,
"[pattern_formatter]"
)
{
auto
formatter
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
(
"%^***%$"
);
spdlog
::
details
::
log_msg
msg
;
formatter
->
format
(
msg
);
fmt
::
memory_buffer
formatted
;
formatter
->
format
(
msg
,
formatted
);
REQUIRE
(
msg
.
color_range_start
==
0
);
REQUIRE
(
msg
.
color_range_end
==
3
);
}
...
...
@@ -99,17 +97,19 @@ TEST_CASE("color range test4", "[pattern_formatter]")
auto
formatter
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
(
"XX%^YYY%$"
,
spdlog
::
pattern_time_type
::
local
,
"
\n
"
);
spdlog
::
details
::
log_msg
msg
;
fmt
::
format_to
(
msg
.
raw
,
"ignored"
);
formatter
->
format
(
msg
);
fmt
::
memory_buffer
formatted
;
formatter
->
format
(
msg
,
formatted
);
REQUIRE
(
msg
.
color_range_start
==
2
);
REQUIRE
(
msg
.
color_range_end
==
5
);
REQUIRE
(
log_to_str
(
"ignored"
,
formatter
)
==
"XXYYY
\n
"
);
REQUIRE
(
log_to_str
(
"ignored"
,
"XX%^YYY%$"
,
spdlog
::
pattern_time_type
::
local
,
"
\n
"
)
==
"XXYYY
\n
"
);
}
TEST_CASE
(
"color range test5"
,
"[pattern_formatter]"
)
{
auto
formatter
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
(
"**%^"
);
spdlog
::
details
::
log_msg
msg
;
formatter
->
format
(
msg
);
fmt
::
memory_buffer
formatted
;
formatter
->
format
(
msg
,
formatted
);
REQUIRE
(
msg
.
color_range_start
==
2
);
REQUIRE
(
msg
.
color_range_end
==
0
);
}
...
...
@@ -118,7 +118,8 @@ TEST_CASE("color range test6", "[pattern_formatter]")
{
auto
formatter
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
(
"**%$"
);
spdlog
::
details
::
log_msg
msg
;
formatter
->
format
(
msg
);
fmt
::
memory_buffer
formatted
;
formatter
->
format
(
msg
,
formatted
);
REQUIRE
(
msg
.
color_range_start
==
0
);
REQUIRE
(
msg
.
color_range_end
==
2
);
}
tests/test_sink.h
View file @
0c19bdd7
...
...
@@ -35,7 +35,7 @@ public:
}
protected
:
void
sink_it_
(
const
details
::
log_msg
&
)
override
void
sink_it_
(
const
details
::
log_msg
&
,
const
fmt
::
memory_buffer
&
)
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