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
ee525f9b
Commit
ee525f9b
authored
Oct 05, 2017
by
fogo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
allow printf style logging
* tests updated for printf style * tests makefile support printf style
parent
552d6214
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
103 additions
and
3 deletions
+103
-3
.travis.yml
.travis.yml
+1
-0
logger_impl.h
include/spdlog/details/logger_impl.h
+13
-1
tweakme.h
include/spdlog/tweakme.h
+8
-0
Makefile
tests/Makefile
+7
-1
cond_logging.cpp
tests/cond_logging.cpp
+21
-0
errors.cpp
tests/errors.cpp
+13
-0
file_log.cpp
tests/file_log.cpp
+40
-1
No files found.
.travis.yml
View file @
ee525f9b
...
...
@@ -85,6 +85,7 @@ script:
-
./"${BIN}"
-
valgrind --trace-children=yes --leak-check=full ./"${BIN}"
-
cd $CHECKOUT_PATH/tests; make rebuild; ./tests
-
cd $CHECKOUT_PATH/tests; STYLE=printf make rebuild; ./tests
notifications
:
email
:
false
include/spdlog/details/logger_impl.h
View file @
ee525f9b
...
...
@@ -8,6 +8,14 @@
#include "spdlog/logger.h"
#include "spdlog/sinks/stdout_sinks.h"
#if defined(SPDLOG_FMT_PRINTF)
#if !defined(SPDLOG_FMT_EXTERNAL)
#include "spdlog/fmt/bundled/printf.h"
#else //external fmtlib
#include <fmt/printf.h>
#endif
#endif
#include <memory>
#include <string>
...
...
@@ -58,7 +66,6 @@ inline void spdlog::logger::set_pattern(const std::string& pattern, pattern_time
_set_pattern
(
pattern
,
pattern_time
);
}
template
<
typename
...
Args
>
inline
void
spdlog
::
logger
::
log
(
level
::
level_enum
lvl
,
const
char
*
fmt
,
const
Args
&
...
args
)
{
...
...
@@ -67,7 +74,12 @@ inline void spdlog::logger::log(level::level_enum lvl, const char* fmt, const Ar
try
{
details
::
log_msg
log_msg
(
&
_name
,
lvl
);
#if defined(SPDLOG_FMT_PRINTF)
fmt
::
printf
(
log_msg
.
raw
,
fmt
,
args
...);
#else
log_msg
.
raw
.
write
(
fmt
,
args
...);
#endif
_sink_it
(
log_msg
);
}
catch
(
const
std
::
exception
&
ex
)
...
...
include/spdlog/tweakme.h
View file @
ee525f9b
...
...
@@ -96,6 +96,14 @@
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Uncomment to use printf-style messages in your logs instead of the usual
// format-style used by default.
//
// #define SPDLOG_FMT_PRINTF
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Uncomment to enable syslog (disabled by default)
//
...
...
tests/Makefile
View file @
ee525f9b
CXX
?=
g++
CXXFLAGS
=
-Wall
-pedantic
-std
=
c++11
-pthread
-O2
-I
../include
ifeq
($(STYLE),printf)
$(info
***
PRINTF
STYLE
***)
CXXFLAGS
=
-DSPDLOG_FMT_PRINTF
-Wall
-pedantic
-std
=
c++11
-pthread
-O2
-I
../include
else
$(info
***
FORMAT
STYLE
***)
CXXFLAGS
=
-Wall
-pedantic
-std
=
c++11
-pthread
-O2
-I
../include
endif
LDPFALGS
=
-pthread
CPP_FILES
:=
$
(
wildcard
*
.cpp
)
...
...
tests/cond_logging.cpp
View file @
ee525f9b
...
...
@@ -135,6 +135,7 @@ TEST_CASE("conditional_trace_varargs", "[conditional_trace_varargs]")
//const char
for
(
auto
i
=
0
;
i
<
2
;
i
++
)
{
#if !defined(SPDLOG_FMT_PRINTF)
REQUIRE
(
conditional_log_varags
(
spdlog
::
level
::
trace
,
(
i
%
2
==
0
),
"Hello {}"
,
i
)
==
(
i
%
2
==
0
?
"Hello "
+
std
::
to_string
(
i
)
:
""
));
REQUIRE
(
conditional_log_varags
(
spdlog
::
level
::
debug
,
(
i
%
2
==
0
),
"Hello {}"
,
i
)
==
(
i
%
2
==
0
?
"Hello "
+
std
::
to_string
(
i
)
:
""
));
REQUIRE
(
conditional_log_varags
(
spdlog
::
level
::
info
,
(
i
%
2
==
0
),
"Hello {}"
,
i
)
==
(
i
%
2
==
0
?
"Hello "
+
std
::
to_string
(
i
)
:
""
));
...
...
@@ -150,5 +151,24 @@ TEST_CASE("conditional_trace_varargs", "[conditional_trace_varargs]")
REQUIRE
(
conditional_log_varags
(
spdlog
::
level
::
err
,
(
i
%
2
==
0
),
L"Hello {}"
,
i
)
==
(
i
%
2
==
0
?
L"Hello "
+
std
::
to_wstring
(
i
)
:
L""
));
REQUIRE
(
conditional_log_varags
(
spdlog
::
level
::
critical
,
(
i
%
2
==
0
),
L"Hello {}"
,
i
)
==
(
i
%
2
==
0
?
L"Hello "
+
std
::
to_wstring
(
i
)
:
L""
));
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
#else
REQUIRE
(
conditional_log_varags
(
spdlog
::
level
::
trace
,
(
i
%
2
==
0
),
"Hello %d"
,
i
)
==
(
i
%
2
==
0
?
"Hello "
+
std
::
to_string
(
i
)
:
""
));
REQUIRE
(
conditional_log_varags
(
spdlog
::
level
::
debug
,
(
i
%
2
==
0
),
"Hello %d"
,
i
)
==
(
i
%
2
==
0
?
"Hello "
+
std
::
to_string
(
i
)
:
""
));
REQUIRE
(
conditional_log_varags
(
spdlog
::
level
::
info
,
(
i
%
2
==
0
),
"Hello %d"
,
i
)
==
(
i
%
2
==
0
?
"Hello "
+
std
::
to_string
(
i
)
:
""
));
REQUIRE
(
conditional_log_varags
(
spdlog
::
level
::
warn
,
(
i
%
2
==
0
),
"Hello %d"
,
i
)
==
(
i
%
2
==
0
?
"Hello "
+
std
::
to_string
(
i
)
:
""
));
REQUIRE
(
conditional_log_varags
(
spdlog
::
level
::
err
,
(
i
%
2
==
0
),
"Hello %d"
,
i
)
==
(
i
%
2
==
0
?
"Hello "
+
std
::
to_string
(
i
)
:
""
));
REQUIRE
(
conditional_log_varags
(
spdlog
::
level
::
critical
,
(
i
%
2
==
0
),
"Hello %d"
,
i
)
==
(
i
%
2
==
0
?
"Hello "
+
std
::
to_string
(
i
)
:
""
));
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
REQUIRE
(
conditional_log_varags
(
spdlog
::
level
::
trace
,
(
i
%
2
==
0
),
L"Hello %d"
,
i
)
==
(
i
%
2
==
0
?
L"Hello "
+
std
::
to_wstring
(
i
)
:
L""
));
REQUIRE
(
conditional_log_varags
(
spdlog
::
level
::
debug
,
(
i
%
2
==
0
),
L"Hello %d"
,
i
)
==
(
i
%
2
==
0
?
L"Hello "
+
std
::
to_wstring
(
i
)
:
L""
));
REQUIRE
(
conditional_log_varags
(
spdlog
::
level
::
info
,
(
i
%
2
==
0
),
L"Hello %d"
,
i
)
==
(
i
%
2
==
0
?
L"Hello "
+
std
::
to_wstring
(
i
)
:
L""
));
REQUIRE
(
conditional_log_varags
(
spdlog
::
level
::
warn
,
(
i
%
2
==
0
),
L"Hello %d"
,
i
)
==
(
i
%
2
==
0
?
L"Hello "
+
std
::
to_wstring
(
i
)
:
L""
));
REQUIRE
(
conditional_log_varags
(
spdlog
::
level
::
err
,
(
i
%
2
==
0
),
L"Hello %d"
,
i
)
==
(
i
%
2
==
0
?
L"Hello "
+
std
::
to_wstring
(
i
)
:
L""
));
REQUIRE
(
conditional_log_varags
(
spdlog
::
level
::
critical
,
(
i
%
2
==
0
),
L"Hello %d"
,
i
)
==
(
i
%
2
==
0
?
L"Hello "
+
std
::
to_wstring
(
i
)
:
L""
));
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
#endif // !defined(SPDLOG_FMT_PRINTF)
}
}
\ No newline at end of file
tests/errors.cpp
View file @
ee525f9b
...
...
@@ -26,8 +26,13 @@ TEST_CASE("default_error_handler", "[errors]]")
auto
logger
=
spdlog
::
create
<
spdlog
::
sinks
::
simple_file_sink_mt
>
(
"logger"
,
filename
,
true
);
logger
->
set_pattern
(
"%v"
);
#if !defined(SPDLOG_FMT_PRINTF)
logger
->
info
(
"Test message {} {}"
,
1
);
logger
->
info
(
"Test message {}"
,
2
);
#else
logger
->
info
(
"Test message %d %d"
,
1
);
logger
->
info
(
"Test message %d"
,
2
);
#endif
logger
->
flush
();
REQUIRE
(
file_contents
(
filename
)
==
std
::
string
(
"Test message 2
\n
"
));
...
...
@@ -50,7 +55,11 @@ TEST_CASE("custom_error_handler", "[errors]]")
throw
custom_ex
();
});
logger
->
info
(
"Good message #1"
);
#if !defined(SPDLOG_FMT_PRINTF)
REQUIRE_THROWS_AS
(
logger
->
info
(
"Bad format msg {} {}"
,
"xxx"
),
custom_ex
);
#else
REQUIRE_THROWS_AS
(
logger
->
info
(
"Bad format msg %s %s"
,
"xxx"
),
custom_ex
);
#endif
logger
->
info
(
"Good message #2"
);
REQUIRE
(
count_lines
(
filename
)
==
2
);
}
...
...
@@ -81,7 +90,11 @@ TEST_CASE("async_error_handler", "[errors]]")
ofs
<<
err_msg
;
});
logger
->
info
(
"Good message #1"
);
#if !defined(SPDLOG_FMT_PRINTF)
logger
->
info
(
"Bad format msg {} {}"
,
"xxx"
);
#else
logger
->
info
(
"Bad format msg %s %s"
,
"xxx"
);
#endif
logger
->
info
(
"Good message #2"
);
spdlog
::
drop
(
"logger"
);
//force logger to drain the queue and shutdown
spdlog
::
set_sync_mode
();
...
...
tests/file_log.cpp
View file @
ee525f9b
...
...
@@ -12,9 +12,13 @@ TEST_CASE("simple_file_logger", "[simple_logger]]")
auto
logger
=
spdlog
::
create
<
spdlog
::
sinks
::
simple_file_sink_mt
>
(
"logger"
,
filename
);
logger
->
set_pattern
(
"%v"
);
#if !defined(SPDLOG_FMT_PRINTF)
logger
->
info
(
"Test message {}"
,
1
);
logger
->
info
(
"Test message {}"
,
2
);
#else
logger
->
info
(
"Test message %d"
,
1
);
logger
->
info
(
"Test message %d"
,
2
);
#endif
logger
->
flush
();
REQUIRE
(
file_contents
(
filename
)
==
std
::
string
(
"Test message 1
\n
Test message 2
\n
"
));
REQUIRE
(
count_lines
(
filename
)
==
2
);
...
...
@@ -33,8 +37,13 @@ TEST_CASE("flush_on", "[flush_on]]")
logger
->
trace
(
"Should not be flushed"
);
REQUIRE
(
count_lines
(
filename
)
==
0
);
#if !defined(SPDLOG_FMT_PRINTF)
logger
->
info
(
"Test message {}"
,
1
);
logger
->
info
(
"Test message {}"
,
2
);
#else
logger
->
info
(
"Test message %d"
,
1
);
logger
->
info
(
"Test message %d"
,
2
);
#endif
logger
->
flush
();
REQUIRE
(
file_contents
(
filename
)
==
std
::
string
(
"Should not be flushed
\n
Test message 1
\n
Test message 2
\n
"
));
REQUIRE
(
count_lines
(
filename
)
==
3
);
...
...
@@ -47,7 +56,13 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
auto
logger
=
spdlog
::
rotating_logger_mt
(
"logger"
,
basename
,
1024
,
0
);
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
#if !defined(SPDLOG_FMT_PRINTF)
logger
->
info
(
"Test message {}"
,
i
);
#else
logger
->
info
(
"Test message %d"
,
i
);
#endif
}
logger
->
flush
();
auto
filename
=
basename
;
...
...
@@ -67,7 +82,13 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
auto
filename
=
basename
;
REQUIRE
(
count_lines
(
filename
)
==
10
);
for
(
int
i
=
0
;
i
<
1000
;
i
++
)
{
#if !defined(SPDLOG_FMT_PRINTF)
logger
->
info
(
"Test message {}"
,
i
);
#else
logger
->
info
(
"Test message %d"
,
i
);
#endif
}
logger
->
flush
();
REQUIRE
(
get_filesize
(
filename
)
<=
1024
);
...
...
@@ -88,7 +109,13 @@ TEST_CASE("daily_logger", "[daily_logger]]")
auto
logger
=
spdlog
::
daily_logger_mt
(
"logger"
,
basename
,
0
,
0
);
logger
->
flush_on
(
spdlog
::
level
::
info
);
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
#if !defined(SPDLOG_FMT_PRINTF)
logger
->
info
(
"Test message {}"
,
i
);
#else
logger
->
info
(
"Test message %d"
,
i
);
#endif
}
auto
filename
=
w
.
str
();
REQUIRE
(
count_lines
(
filename
)
==
10
);
...
...
@@ -110,7 +137,13 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger_dateonly]]")
auto
logger
=
spdlog
::
create
<
sink_type
>
(
"logger"
,
basename
,
0
,
0
);
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
#if !defined(SPDLOG_FMT_PRINTF)
logger
->
info
(
"Test message {}"
,
i
);
#else
logger
->
info
(
"Test message %d"
,
i
);
#endif
}
logger
->
flush
();
auto
filename
=
w
.
str
();
REQUIRE
(
count_lines
(
filename
)
==
10
);
...
...
@@ -142,7 +175,13 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger_custom]]")
auto
logger
=
spdlog
::
create
<
sink_type
>
(
"logger"
,
basename
,
0
,
0
);
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
#if !defined(SPDLOG_FMT_PRINTF)
logger
->
info
(
"Test message {}"
,
i
);
#else
logger
->
info
(
"Test message %d"
,
i
);
#endif
}
logger
->
flush
();
auto
filename
=
w
.
str
();
...
...
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