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
60a8c5f1
Commit
60a8c5f1
authored
Mar 21, 2020
by
Gabi Melman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added padding support and set_pattern to custom_flags
parent
d1a10244
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
70 additions
and
28 deletions
+70
-28
example/example.cpp
example/example.cpp
+3
-3
include/spdlog/pattern_formatter-inl.h
include/spdlog/pattern_formatter-inl.h
+10
-5
include/spdlog/pattern_formatter.h
include/spdlog/pattern_formatter.h
+8
-3
tests/test_pattern_formatter.cpp
tests/test_pattern_formatter.cpp
+49
-17
No files found.
example/example.cpp
View file @
60a8c5f1
...
...
@@ -276,8 +276,8 @@ public:
void
custom_flags_example
()
{
using
spdlog
::
details
::
make_unique
;
//for pre c++14
auto
formatter
=
make_unique
<
spdlog
::
pattern_formatter
>
(
"[%+] [%*]"
);
formatter
->
add_flag
<
my_formatter_flag
>
(
'*'
).
recompile
(
);
using
spdlog
::
details
::
make_unique
;
//
for pre c++14
auto
formatter
=
make_unique
<
spdlog
::
pattern_formatter
>
();
formatter
->
add_flag
<
my_formatter_flag
>
(
'*'
).
set_pattern
(
"[%n] [%*] [%^%l%$] %v"
);
spdlog
::
set_formatter
(
std
::
move
(
formatter
));
}
include/spdlog/pattern_formatter-inl.h
View file @
60a8c5f1
...
...
@@ -1024,8 +1024,9 @@ SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory
details
::
fmt_helper
::
append_string_view
(
eol_
,
dest
);
}
SPDLOG_INLINE
void
pattern_formatter
::
recompile
(
)
SPDLOG_INLINE
void
pattern_formatter
::
set_pattern
(
std
::
string
pattern
)
{
pattern_
=
std
::
move
(
pattern
);
compile_pattern_
(
pattern_
);
}
...
...
@@ -1041,15 +1042,19 @@ SPDLOG_INLINE std::tm pattern_formatter::get_time_(const details::log_msg &msg)
template
<
typename
Padder
>
SPDLOG_INLINE
void
pattern_formatter
::
handle_flag_
(
char
flag
,
details
::
padding_info
padding
)
{
// process custom flags
auto
it
=
custom_handlers_
.
find
(
flag
);
if
(
it
!=
custom_handlers_
.
end
())
if
(
it
!=
custom_handlers_
.
end
())
{
formatters_
.
push_back
(
it
->
second
->
clone
());
auto
custom_handler
=
it
->
second
->
clone
();
custom_handler
->
set_padding_info
(
padding
);
formatters_
.
push_back
(
std
::
move
(
custom_handler
));
return
;
}
switch
(
flag
)
{
// process built-in flags
switch
(
flag
)
{
case
(
'+'
):
// default formatter
formatters_
.
push_back
(
details
::
make_unique
<
details
::
full_formatter
>
(
padding
));
break
;
...
...
include/spdlog/pattern_formatter.h
View file @
60a8c5f1
...
...
@@ -41,8 +41,8 @@ struct padding_info
{
return
enabled_
;
}
const
size_t
width_
=
0
;
const
pad_side
side_
=
left
;
size_t
width_
=
0
;
pad_side
side_
=
left
;
bool
truncate_
=
false
;
bool
enabled_
=
false
;
};
...
...
@@ -67,6 +67,11 @@ class SPDLOG_API custom_flag_formatter : public details::flag_formatter
{
public:
virtual
std
::
unique_ptr
<
custom_flag_formatter
>
clone
()
const
=
0
;
void
set_padding_info
(
details
::
padding_info
padding
)
{
flag_formatter
::
padinfo_
=
padding
;
}
};
class
SPDLOG_API
pattern_formatter
final
:
public
formatter
...
...
@@ -92,7 +97,7 @@ public:
custom_handlers_
[
flag
]
=
details
::
make_unique
<
T
>
(
args
...);
return
*
this
;
}
void
recompile
(
);
void
set_pattern
(
std
::
string
pattern
);
private:
std
::
string
pattern_
;
...
...
tests/test_pattern_formatter.cpp
View file @
60a8c5f1
...
...
@@ -246,8 +246,6 @@ TEST_CASE("paddinng_truncate_funcname", "[pattern_formatter]")
REQUIRE
(
lines
[
1
]
==
"message [funct]"
);
}
TEST_CASE
(
"clone-default-formatter"
,
"[pattern_formatter]"
)
{
auto
formatter_1
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
();
...
...
@@ -307,17 +305,26 @@ TEST_CASE("clone-formatter-2", "[pattern_formatter]")
REQUIRE
(
fmt
::
to_string
(
formatted_1
)
==
fmt
::
to_string
(
formatted_2
));
}
class
custom_test_flag
:
public
spdlog
::
custom_flag_formatter
{
public:
class
custom_test_flag
(
std
::
string
txt
)
:
some_txt
{
std
::
move
(
txt
)}
class
custom_test_flag
(
std
::
string
txt
)
:
some_txt
{
std
::
move
(
txt
)}
{}
void
format
(
const
spdlog
::
details
::
log_msg
&
,
const
std
::
tm
&
,
spdlog
::
memory_buf_t
&
dest
)
override
{
if
(
some_txt
==
"throw_me"
)
{
throw
spdlog
::
spdlog_ex
(
"custom_flag_exception_test"
);
}
some_txt
=
std
::
string
(
padinfo_
.
width_
,
' '
)
+
some_txt
;
dest
.
append
(
some_txt
.
data
(),
some_txt
.
data
()
+
some_txt
.
size
());
}
spdlog
::
details
::
padding_info
get_padding_info
()
{
return
padinfo_
;
}
std
::
string
some_txt
;
...
...
@@ -329,8 +336,9 @@ public:
// test clone with custom flag formatters
TEST_CASE
(
"clone-custom_formatter"
,
"[pattern_formatter]"
)
{
auto
formatter_1
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
(
"[%n] [%t] %v"
,
spdlog
::
pattern_time_type
::
utc
,
""
);
formatter_1
->
add_flag
<
custom_test_flag
>
(
't'
,
"custom_output"
).
recompile
();
auto
formatter_1
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
();
formatter_1
->
add_flag
<
custom_test_flag
>
(
't'
,
"custom_output"
).
set_pattern
(
"[%n] [%t] %v"
);
auto
formatter_2
=
formatter_1
->
clone
();
std
::
string
logger_name
=
"logger-name"
;
spdlog
::
details
::
log_msg
msg
(
logger_name
,
spdlog
::
level
::
info
,
"some message"
);
...
...
@@ -338,10 +346,11 @@ TEST_CASE("clone-custom_formatter", "[pattern_formatter]")
memory_buf_t
formatted_1
;
memory_buf_t
formatted_2
;
formatter_1
->
format
(
msg
,
formatted_1
);
formatter_2
->
format
(
msg
,
formatted_2
);
REQUIRE
(
fmt
::
to_string
(
formatted_1
)
==
"[logger-name] [custom_output] some message"
);
REQUIRE
(
fmt
::
to_string
(
formatted_2
)
==
"[logger-name] [custom_output] some message"
);
formatter_2
->
format
(
msg
,
formatted_2
);
auto
expected
=
fmt
::
format
(
"[logger-name] [custom_output] some message{}"
,
spdlog
::
details
::
os
::
default_eol
);
REQUIRE
(
fmt
::
to_string
(
formatted_1
)
==
expected
);
REQUIRE
(
fmt
::
to_string
(
formatted_2
)
==
expected
);
}
//
...
...
@@ -398,15 +407,38 @@ TEST_CASE("full filename formatter", "[pattern_formatter]")
REQUIRE
(
fmt
::
to_string
(
formatted
)
==
test_path
);
}
TEST_CASE
(
"custom flags"
,
"[pattern_formatter]"
)
{
auto
formatter
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
(
"[%n] [%t] [%u] %v"
,
spdlog
::
pattern_time_type
::
utc
,
""
);
formatter
->
add_flag
<
custom_test_flag
>
(
't'
,
"custom1"
).
add_flag
<
custom_test_flag
>
(
'u'
,
"custom2"
).
recompile
();
auto
formatter
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
();
formatter
->
add_flag
<
custom_test_flag
>
(
't'
,
"custom1"
).
add_flag
<
custom_test_flag
>
(
'u'
,
"custom2"
).
set_pattern
(
"[%n] [%t] [%u] %v"
);
memory_buf_t
formatted
;
spdlog
::
details
::
log_msg
msg
(
spdlog
::
source_loc
{},
"logger-name"
,
spdlog
::
level
::
info
,
"some message"
);
formatter
->
format
(
msg
,
formatted
);
auto
expected
=
fmt
::
format
(
"[logger-name] [custom1] [custom2] some message{}"
,
spdlog
::
details
::
os
::
default_eol
);
REQUIRE
(
fmt
::
to_string
(
formatted
)
==
expected
);
}
TEST_CASE
(
"custom flags-padding"
,
"[pattern_formatter]"
)
{
auto
formatter
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
();
formatter
->
add_flag
<
custom_test_flag
>
(
't'
,
"custom1"
).
add_flag
<
custom_test_flag
>
(
'u'
,
"custom2"
).
set_pattern
(
"[%n] [%t] [%5u] %v"
);
memory_buf_t
formatted
;
spdlog
::
details
::
log_msg
msg
(
spdlog
::
source_loc
{},
"logger-name"
,
spdlog
::
level
::
info
,
"some message"
);
formatter
->
format
(
msg
,
formatted
);
auto
expected
=
fmt
::
format
(
"[logger-name] [custom1] [ custom2] some message{}"
,
spdlog
::
details
::
os
::
default_eol
);
REQUIRE
(
fmt
::
to_string
(
formatted
)
==
expected
);
}
TEST_CASE
(
"custom flags-exception"
,
"[pattern_formatter]"
)
{
auto
formatter
=
std
::
make_shared
<
spdlog
::
pattern_formatter
>
();
formatter
->
add_flag
<
custom_test_flag
>
(
't'
,
"throw_me"
).
add_flag
<
custom_test_flag
>
(
'u'
,
"custom2"
).
set_pattern
(
"[%n] [%t] [%u] %v"
);
memory_buf_t
formatted
;
spdlog
::
details
::
log_msg
msg
(
spdlog
::
source_loc
{},
"logger-name"
,
spdlog
::
level
::
info
,
"some message"
);
formatter
->
format
(
msg
,
formatted
);
REQUIRE
(
fmt
::
to_string
(
formatted
)
==
"[logger-name] [custom1] [custom2] some message"
);
CHECK_THROWS_AS
(
formatter
->
format
(
msg
,
formatted
),
spdlog
::
spdlog_ex
);
}
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