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
567e85e6
Commit
567e85e6
authored
Nov 01, 2014
by
gabi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bench
parent
bf303fad
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
168 additions
and
88 deletions
+168
-88
bench.cpp
example/bench.cpp
+95
-70
example.cpp
example/example.cpp
+4
-3
blocking_queue.h
include/spdlog/details/blocking_queue.h
+4
-4
logger_impl.h
include/spdlog/details/logger_impl.h
+7
-1
pattern_formatter_impl.h
include/spdlog/details/pattern_formatter_impl.h
+16
-2
registry.h
include/spdlog/details/registry.h
+2
-2
spdlog_impl.h
include/spdlog/details/spdlog_impl.h
+2
-2
logger.h
include/spdlog/logger.h
+2
-1
async_sink.h
include/spdlog/sinks/async_sink.h
+11
-0
file_sinks.h
include/spdlog/sinks/file_sinks.h
+14
-0
null_sink.h
include/spdlog/sinks/null_sink.h
+3
-0
ostream_sink.h
include/spdlog/sinks/ostream_sink.h
+3
-0
sink.h
include/spdlog/sinks/sink.h
+1
-0
stdout_sinks.h
include/spdlog/sinks/stdout_sinks.h
+1
-0
spdlog.h
include/spdlog/spdlog.h
+3
-3
No files found.
example/bench.cpp
View file @
567e85e6
...
...
@@ -8,7 +8,7 @@
#include <atomic>
#include "spdlog/spdlog.h"
#include "spdlog/sinks/file_sinks.h"
#include "spdlog/sinks/
stdout_sinks
.h"
#include "spdlog/sinks/
async_sink
.h"
#include "spdlog/sinks/null_sink.h"
#include "utils.h"
...
...
@@ -20,82 +20,57 @@ using namespace spdlog::sinks;
using
namespace
utils
;
void
bench
(
int
howmany
,
std
::
shared_ptr
<
spdlog
::
logger
>
log
)
void
bench
(
int
howmany
,
std
::
shared_ptr
<
spdlog
::
logger
>
log
);
void
bench_mt
(
int
howmany
,
std
::
shared_ptr
<
spdlog
::
logger
>
log
,
int
thread_count
);
int
main
(
int
argc
,
char
*
argv
[])
{
cout
<<
log
->
name
()
<<
"...
\t\t
"
<<
flush
;
auto
start
=
system_clock
::
now
();
for
(
auto
i
=
0
;
i
<
howmany
;
++
i
)
{
log
->
info
(
"Hello logger: msg number "
)
<<
i
;
}
auto
delta
=
system_clock
::
now
()
-
start
;
auto
delta_d
=
duration_cast
<
duration
<
double
>>
(
delta
).
count
();
cout
<<
format
(
int
(
howmany
/
delta_d
))
<<
"/sec"
<<
endl
;
}
int
howmany
=
123456
;
int
threads
=
4
;
int
flush_interval
=
1000
;
int
file_size
=
30
*
1024
*
1024
;
int
rotating_files
=
5
;
void
bench_mt
(
int
howmany
,
std
::
shared_ptr
<
spdlog
::
logger
>
log
,
int
thread_count
)
{
try
{
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
([
&
]()
{
while
(
msg_counter
++
<
howmany
)
log
->
info
(
"Hello logger: msg number "
)
<<
msg_counter
;
}));
}
for
(
auto
&
t
:
threads
)
{
t
.
join
();
};
auto
delta
=
system_clock
::
now
()
-
start
;
auto
delta_d
=
duration_cast
<
duration
<
double
>>
(
delta
).
count
();
cout
<<
format
(
int
(
howmany
/
delta_d
))
<<
"/sec"
<<
endl
;
}
if
(
argc
>
1
)
howmany
=
atoi
(
argv
[
1
]);
if
(
argc
>
2
)
threads
=
atoi
(
argv
[
2
]);
cout
<<
"*******************************************************************************
\n
"
;
cout
<<
"Single thread, "
<<
format
(
howmany
)
<<
" iterations, flush every "
<<
flush_interval
<<
" lines"
<<
endl
;
cout
<<
"*******************************************************************************
\n
"
;
int
main
(
int
argc
,
char
*
argv
[])
{
auto
rotating_st
=
spdlog
::
rotating_logger_st
(
"rotating_st"
,
"logs/rotating_st"
,
file_size
,
rotating_files
,
flush_interval
);
bench
(
howmany
,
rotating_st
);
try
{
int
howmany
=
argc
<=
1
?
250000
:
atoi
(
argv
[
1
]);
int
threads
=
argc
<=
2
?
4
:
atoi
(
argv
[
2
]);
int
flush_interval
=
100
;
cout
<<
"*******************************************************************************
\n
"
;
cout
<<
"Single thread, "
<<
format
(
howmany
)
<<
" iterations, flush every "
<<
flush_interval
<<
" lines"
<<
endl
;
cout
<<
"*******************************************************************************
\n
"
;
auto
rotating_st
=
spdlog
::
rotating_logger_st
(
"rotating_st"
,
"logs/rotating_st"
,
1024
*
1024
*
5
,
5
,
flush_interval
);
bench
(
howmany
,
rotating_st
);
auto
daily_st
=
spdlog
::
daily_logger_st
(
"daily_st"
,
"logs/daily_st"
,
flush_interval
);
bench
(
howmany
,
daily_st
);
bench
(
howmany
,
spdlog
::
create
<
null_sink_st
>
(
"null_st"
));
cout
<<
"
\n
*******************************************************************************
\n
"
;
cout
<<
threads
<<
" threads, "
<<
format
(
howmany
)
<<
" iterations, flush every "
<<
flush_interval
<<
" lines"
<<
endl
;
cout
<<
"*******************************************************************************
\n
"
;
auto
rotating_mt
=
spdlog
::
rotating_logger_mt
(
"rotating_mt"
,
"logs/rotating_mt"
,
1024
*
1024
*
5
,
5
,
flush_interval
);
bench_mt
(
howmany
,
rotating_mt
,
threads
);
auto
daily_mt
=
spdlog
::
daily_logger_mt
(
"daily_mt"
,
"logs/daily_mt"
,
flush_interval
);
bench_mt
(
howmany
,
daily_mt
,
threads
);
bench_mt
(
howmany
,
spdlog
::
create
<
null_sink_mt
>
(
"null_mt"
),
threads
);
auto
daily_st
=
spdlog
::
daily_logger_st
(
"daily_st"
,
"logs/daily_st"
,
flush_interval
);
bench
(
howmany
,
daily_st
);
bench
(
howmany
,
spdlog
::
create
<
null_sink_st
>
(
"null_st"
));
cout
<<
"
\n
*******************************************************************************
\n
"
;
cout
<<
threads
<<
" threads sharing same logger, "
<<
format
(
howmany
)
<<
" iterations, flush every "
<<
flush_interval
<<
" lines"
<<
endl
;
cout
<<
"*******************************************************************************
\n
"
;
auto
rotating_mt
=
spdlog
::
rotating_logger_mt
(
"rotating_mt"
,
"logs/rotating_mt"
,
file_size
,
rotating_files
,
flush_interval
);
bench_mt
(
howmany
,
rotating_mt
,
threads
);
auto
daily_mt
=
spdlog
::
daily_logger_mt
(
"daily_mt"
,
"logs/daily_mt"
,
flush_interval
);
bench_mt
(
howmany
,
daily_mt
,
threads
);
auto
async_sink
=
std
::
make_shared
<
sinks
::
async_sink
>
(
1000
);
auto
file_sink
=
std
::
make_shared
<
sinks
::
rotating_file_sink_mt
>
(
"logs/async_logger"
,
"txt"
,
file_size
,
rotating_files
,
flush_interval
);
async_sink
->
add_sink
(
file_sink
);
auto
async_logger
=
std
::
make_shared
<
spdlog
::
logger
>
(
"async_logger"
,
async_sink
);
bench_mt
(
howmany
,
async_logger
,
threads
);
bench_mt
(
howmany
,
spdlog
::
create
<
null_sink_mt
>
(
"null_mt"
),
threads
);
}
catch
(
std
::
exception
&
ex
)
{
...
...
@@ -105,3 +80,53 @@ int main(int argc, char* argv[])
return
0
;
}
void
bench
(
int
howmany
,
std
::
shared_ptr
<
spdlog
::
logger
>
log
)
{
cout
<<
log
->
name
()
<<
"...
\t\t
"
<<
flush
;
auto
start
=
system_clock
::
now
();
for
(
auto
i
=
0
;
i
<
howmany
;
++
i
)
{
log
->
info
(
"Hello logger: msg number "
)
<<
i
<<
"**************************"
<<
i
+
1
<<
","
<<
1
+
2
;;
}
auto
delta
=
system_clock
::
now
()
-
start
;
auto
delta_d
=
duration_cast
<
duration
<
double
>>
(
delta
).
count
();
cout
<<
format
(
int
(
howmany
/
delta_d
))
<<
"/sec"
<<
endl
;
log
->
close
();
}
void
bench_mt
(
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
([
&
]()
{
while
(
true
)
{
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
();
cout
<<
format
(
int
(
howmany
/
delta_d
))
<<
"/sec"
<<
endl
;
log
->
close
();
}
example/example.cpp
View file @
567e85e6
...
...
@@ -6,11 +6,11 @@
#include "spdlog/spdlog.h"
int
main
(
int
,
char
*
[])
int
main
_
(
int
,
char
*
[])
{
namespace
spd
=
spdlog
;
try
{
std
::
string
filename
=
"spdlog_example"
;
...
...
@@ -33,12 +33,13 @@ int main(int, char* [])
console
->
warn
(
"This should!"
);
// Change format pattern to all loggers
spd
::
set_pattern
(
" **** %Y-%m-%d %H:%M:%S.%e %l **** %
t
"
);
spd
::
set_pattern
(
" **** %Y-%m-%d %H:%M:%S.%e %l **** %
v
"
);
spd
::
get
(
"console"
)
->
warn
(
"This is another message with different format"
);
}
catch
(
const
spd
::
spdlog_ex
&
ex
)
{
std
::
cout
<<
"Log failed: "
<<
ex
.
what
()
<<
std
::
endl
;
}
return
0
;
}
include/spdlog/details/blocking_queue.h
View file @
567e85e6
...
...
@@ -49,7 +49,7 @@ public:
std
::
unique_lock
<
std
::
mutex
>
ul
(
_mutex
);
if
(
_q
.
size
()
>=
_max_size
)
{
if
(
!
_item_popped_cond
.
wait_until
(
ul
,
clock
::
now
()
+
timeout
,
[
this
]()
if
(
!
_item_popped_cond
.
wait_until
(
ul
,
clock
::
now
()
+
timeout
,[
this
]()
{
return
this
->
_q
.
size
()
<
this
->
_max_size
;
}))
...
...
@@ -59,7 +59,7 @@ public:
if
(
_q
.
size
()
<=
1
)
{
ul
.
unlock
();
//So the notified thread will have better chance to accuire the lock immediatly..
_item_pushed_cond
.
notify_
one
();
_item_pushed_cond
.
notify_
all
();
}
return
true
;
}
...
...
@@ -69,7 +69,7 @@ public:
template
<
typename
TT
>
void
push
(
TT
&&
item
)
{
while
(
!
push
(
std
::
forward
<
TT
>
(
item
),
std
::
chrono
::
hours
(
1
)));
while
(
!
push
(
std
::
forward
<
TT
>
(
item
),
std
::
chrono
::
seconds
(
10
)));
}
// Pop a copy of the front item in the queue into the given item ref.
...
...
@@ -92,7 +92,7 @@ public:
if
(
_q
.
size
()
>=
_max_size
-
1
)
{
ul
.
unlock
();
//So the notified thread will have better chance to accuire the lock immediatly..
_item_popped_cond
.
notify_
one
();
_item_popped_cond
.
notify_
all
();
}
return
true
;
}
...
...
include/spdlog/details/logger_impl.h
View file @
567e85e6
...
...
@@ -22,6 +22,10 @@ inline spdlog::logger::logger(const std::string& logger_name, const It& begin, c
{}
inline
spdlog
::
logger
::
logger
(
const
std
::
string
&
logger_name
,
spdlog
::
sink_ptr
single_sink
)
:
logger
(
logger_name
,
{
single_sink
})
{}
inline
void
spdlog
::
logger
::
set_formatter
(
spdlog
::
formatter_ptr
msg_formatter
)
{
_formatter
=
msg_formatter
;
...
...
@@ -108,9 +112,11 @@ inline bool spdlog::logger::should_log(spdlog::level::level_enum msg_level) cons
return
msg_level
>=
_level
.
load
();
}
inline
void
spdlog
::
logger
::
stop_logging
()
inline
void
spdlog
::
logger
::
close
()
{
set_level
(
level
::
OFF
);
for
(
auto
&
sink
:
_sinks
)
sink
->
close
();
}
...
...
include/spdlog/details/pattern_formatter_impl.h
View file @
567e85e6
...
...
@@ -4,6 +4,7 @@
#include <chrono>
#include <memory>
#include <vector>
#include <thread>
#include "../formatter.h"
#include "./log_msg.h"
...
...
@@ -301,8 +302,17 @@ private:
};
//Thread id
class
t_formatter
:
public
flag_formatter
{
void
format
(
details
::
log_msg
&
msg
)
override
{
msg
.
formatted
<<
std
::
this_thread
::
get_id
();
}
};
class
v_formatter
:
public
flag_formatter
{
void
format
(
details
::
log_msg
&
msg
)
override
{
...
...
@@ -343,7 +353,7 @@ private:
};
// Full info formatter
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %
t
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %
v
class
full_formatter
:
public
flag_formatter
{
void
format
(
details
::
log_msg
&
msg
)
override
...
...
@@ -428,6 +438,10 @@ inline void spdlog::pattern_formatter::handle_flag(char flag)
_formatters
.
push_back
(
std
::
unique_ptr
<
details
::
flag_formatter
>
(
new
details
::
t_formatter
()));
break
;
case
(
'v'
)
:
_formatters
.
push_back
(
std
::
unique_ptr
<
details
::
flag_formatter
>
(
new
details
::
v_formatter
()));
break
;
case
(
'a'
)
:
_formatters
.
push_back
(
std
::
unique_ptr
<
details
::
flag_formatter
>
(
new
details
::
a_formatter
()));
break
;
...
...
include/spdlog/details/registry.h
View file @
567e85e6
...
...
@@ -77,12 +77,12 @@ public:
}
void
stop
_all
()
void
close
_all
()
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
_mutex
);
_level
=
level
::
OFF
;
for
(
auto
&
l
:
_loggers
)
l
.
second
->
stop_logging
();
l
.
second
->
close
();
}
...
...
include/spdlog/details/spdlog_impl.h
View file @
567e85e6
...
...
@@ -95,7 +95,7 @@ inline void spdlog::set_level(level::level_enum log_level)
return
details
::
registry
::
instance
().
set_level
(
log_level
);
}
inline
void
spdlog
::
stop
()
inline
void
spdlog
::
close
()
{
return
details
::
registry
::
instance
().
stop
_all
();
return
details
::
registry
::
instance
().
close
_all
();
}
include/spdlog/logger.h
View file @
567e85e6
...
...
@@ -24,6 +24,7 @@ class logger
{
public
:
logger
(
const
std
::
string
&
logger_name
,
sink_ptr
single_sink
);
logger
(
const
std
::
string
&
name
,
sinks_init_list
);
template
<
class
It
>
logger
(
const
std
::
string
&
name
,
const
It
&
begin
,
const
It
&
end
);
...
...
@@ -42,7 +43,7 @@ public:
const
std
::
string
&
name
()
const
;
bool
should_log
(
level
::
level_enum
)
const
;
void
stop_logging
();
void
close
();
template
<
typename
...
Args
>
details
::
line_logger
log
(
level
::
level_enum
lvl
,
const
Args
&
...
args
);
template
<
typename
...
Args
>
details
::
line_logger
log
(
const
Args
&
...
args
);
...
...
include/spdlog/sinks/async_sink.h
View file @
567e85e6
...
...
@@ -33,6 +33,9 @@ public:
//Wait to remaining items (if any) in the queue to be written and shutdown
void
shutdown
(
const
std
::
chrono
::
milliseconds
&
timeout
);
void
close
()
override
;
protected
:
void
_sink_it
(
const
details
::
log_msg
&
msg
)
override
;
...
...
@@ -123,6 +126,11 @@ inline void spdlog::sinks::async_sink::shutdown(const std::chrono::milliseconds&
_shutdown
();
}
inline
void
spdlog
::
sinks
::
async_sink
::
close
()
{
_shutdown
();
}
inline
void
spdlog
::
sinks
::
async_sink
::
_shutdown
()
{
...
...
@@ -133,5 +141,8 @@ inline void spdlog::sinks::async_sink::_shutdown()
if
(
_back_thread
.
joinable
())
_back_thread
.
join
();
}
for
(
auto
&
s
:
_sinks
)
s
->
close
();
}
include/spdlog/sinks/file_sinks.h
View file @
567e85e6
...
...
@@ -28,6 +28,10 @@ public:
{
_file_helper
.
open
(
filename
);
}
void
close
()
override
{
_file_helper
.
close
();
}
protected
:
void
_sink_it
(
const
details
::
log_msg
&
msg
)
override
{
...
...
@@ -60,6 +64,11 @@ public:
_file_helper
.
open
(
calc_filename
(
_base_filename
,
0
,
_extension
));
}
void
close
()
override
{
_file_helper
.
close
();
}
protected
:
void
_sink_it
(
const
details
::
log_msg
&
msg
)
override
{
...
...
@@ -140,6 +149,11 @@ public:
_file_helper
.
open
(
calc_filename
(
_base_filename
,
_extension
));
}
void
close
()
override
{
_file_helper
.
close
();
}
protected
:
void
_sink_it
(
const
details
::
log_msg
&
msg
)
override
{
...
...
include/spdlog/sinks/null_sink.h
View file @
567e85e6
...
...
@@ -13,6 +13,9 @@ class null_sink : public base_sink<Mutex>
protected
:
void
_sink_it
(
const
details
::
log_msg
&
)
override
{}
void
close
()
override
{}
};
typedef
null_sink
<
details
::
null_mutex
>
null_sink_st
;
...
...
include/spdlog/sinks/ostream_sink.h
View file @
567e85e6
...
...
@@ -21,6 +21,9 @@ public:
virtual
~
ostream_sink
()
=
default
;
void
close
()
override
{}
protected
:
virtual
void
_sink_it
(
const
details
::
log_msg
&
msg
)
override
{
...
...
include/spdlog/sinks/sink.h
View file @
567e85e6
...
...
@@ -11,6 +11,7 @@ class sink
public
:
virtual
~
sink
()
{}
virtual
void
log
(
const
details
::
log_msg
&
msg
)
=
0
;
virtual
void
close
()
=
0
;
};
}
}
...
...
include/spdlog/sinks/stdout_sinks.h
View file @
567e85e6
...
...
@@ -15,6 +15,7 @@ class stdout_sink : public ostream_sink<Mutex>
{
public
:
stdout_sink
()
:
ostream_sink
<
Mutex
>
(
std
::
cout
)
{}
};
typedef
stdout_sink
<
details
::
null_mutex
>
stdout_sink_st
;
...
...
include/spdlog/spdlog.h
View file @
567e85e6
...
...
@@ -30,7 +30,7 @@ std::shared_ptr<logger> get(const std::string& name);
// Set global formatting
// spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %
t
");
// spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %
v
");
void
set_pattern
(
const
std
::
string
&
format_string
);
...
...
@@ -74,8 +74,8 @@ std::shared_ptr<spdlog::logger> create(const std::string& logger_name, const Arg
// Set global formatter object
void
set_formatter
(
formatter_ptr
f
);
//
Stop all loggers
void
stop
();
//
Close all loggers and stop logging
void
close
();
//
...
...
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