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
34f3d29d
Commit
34f3d29d
authored
Aug 29, 2020
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added stopwatch support
parent
cd701761
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
0 deletions
+73
-0
example/example.cpp
example/example.cpp
+12
-0
include/spdlog/stopwatch.h
include/spdlog/stopwatch.h
+61
-0
No files found.
example/example.cpp
View file @
34f3d29d
...
@@ -13,6 +13,7 @@ void rotating_example();
...
@@ -13,6 +13,7 @@ void rotating_example();
void
daily_example
();
void
daily_example
();
void
async_example
();
void
async_example
();
void
binary_example
();
void
binary_example
();
void
stopwatch_example
();
void
trace_example
();
void
trace_example
();
void
multi_sink_example
();
void
multi_sink_example
();
void
user_defined_example
();
void
user_defined_example
();
...
@@ -71,6 +72,7 @@ int main(int, char *[])
...
@@ -71,6 +72,7 @@ int main(int, char *[])
user_defined_example
();
user_defined_example
();
err_handler_example
();
err_handler_example
();
trace_example
();
trace_example
();
stopwatch_example
();
custom_flags_example
();
custom_flags_example
();
// Flush all *registered* loggers using a worker thread every 3 seconds.
// Flush all *registered* loggers using a worker thread every 3 seconds.
...
@@ -192,6 +194,16 @@ void trace_example()
...
@@ -192,6 +194,16 @@ void trace_example()
SPDLOG_LOGGER_TRACE
(
logger
,
"another trace message"
);
SPDLOG_LOGGER_TRACE
(
logger
,
"another trace message"
);
}
}
// stopwatch example
#include "spdlog/stopwatch.h"
#include <thread>
void
stopwatch_example
()
{
spdlog
::
stopwatch
sw
;
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
123
));
spdlog
::
info
(
"Stopwatch: {} seconds"
,
sw
);
}
// A logger with multiple sinks (stdout and file) - each with a different format and log level.
// A logger with multiple sinks (stdout and file) - each with a different format and log level.
void
multi_sink_example
()
void
multi_sink_example
()
{
{
...
...
include/spdlog/stopwatch.h
0 → 100644
View file @
34f3d29d
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#include <spdlog/fmt/fmt.h>
// Stopwatch support for spdlog (using std::chrono::high_resolution_clock).
// Displays elapsed seconds since construction as double.
//
// Usage:
//
// spdlog::stopwatch sw;
// ...
// spdlog::debug("Elapsed: {} seconds", sw); => "Elapsed 0.005116733 seconds"
// spdlog::info("Elapsed: {:.6} seconds", sw); => "Elapsed 0.005163 seconds"
//
//
// If other units are needed (e.g. millis instead of double), include "fmt/chrono.h" and use "duration_cast<..>(sw.elapsed())":
//
// #include <fmt/chrono.h>
//..
// using std::chrono::duration_cast;
// using std::chrono::milliseconds;
// spdlog::info("Elapsed {}", duration_cast<milliseconds>(sw.elapsed())); => "Elapsed 5ms"
namespace
spdlog
{
class
stopwatch
{
using
clock
=
std
::
chrono
::
high_resolution_clock
;
std
::
chrono
::
time_point
<
clock
>
start_tp_
;
public:
stopwatch
()
:
start_tp_
{
clock
::
now
()}
{}
std
::
chrono
::
duration
<
double
>
elapsed
()
const
{
return
std
::
chrono
::
duration
<
double
>
(
clock
::
now
()
-
start_tp_
);
}
void
reset
()
{
start_tp_
=
clock
::
now
();
}
};
}
// namespace spdlog
// Support for fmt formatting (e.g. "{:012.9}" or just "{}")
namespace
fmt
{
template
<
>
struct
formatter
<
spdlog
::
stopwatch
>
:
formatter
<
double
>
{
template
<
typename
FormatContext
>
auto
format
(
const
spdlog
::
stopwatch
&
sw
,
FormatContext
&
ctx
)
->
decltype
(
ctx
.
out
())
{
return
formatter
<
double
>::
format
(
sw
.
elapsed
().
count
(),
ctx
);
}
};
}
// namespace fmt
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