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
6c7793d4
Commit
6c7793d4
authored
Oct 09, 2014
by
gabi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Modified sinks to be templates with Mutex param to support single threaded sinks
parent
38468e64
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
63 additions
and
44 deletions
+63
-44
async_sink.h
include/c11log/sinks/async_sink.h
+23
-15
base_sink.h
include/c11log/sinks/base_sink.h
+9
-3
file_sinks.h
include/c11log/sinks/file_sinks.h
+28
-23
null_sink.h
include/c11log/sinks/null_sink.h
+3
-3
No files found.
include/c11log/sinks/async_sink.h
View file @
6c7793d4
...
...
@@ -17,8 +17,8 @@ namespace c11log
namespace
sinks
{
class
async_sink
:
public
base_sink
template
<
class
Mutex
>
class
async_sink
:
public
base_sink
<
Mutex
>
{
public
:
using
q_type
=
details
::
blocking_queue
<
details
::
log_msg
>
;
...
...
@@ -54,28 +54,30 @@ private:
///////////////////////////////////////////////////////////////////////////////
// async_sink class implementation
///////////////////////////////////////////////////////////////////////////////
inline
c11log
::
sinks
::
async_sink
::
async_sink
(
const
q_type
::
size_type
max_queue_size
)
template
<
class
Mutex
>
inline
c11log
::
sinks
::
async_sink
<
Mutex
>
::
async_sink
(
const
q_type
::
size_type
max_queue_size
)
:
_sinks
(),
_active
(
true
),
_q
(
max_queue_size
),
_back_thread
(
&
async_sink
::
_thread_loop
,
this
)
{}
inline
c11log
::
sinks
::
async_sink
::~
async_sink
()
template
<
class
Mutex
>
inline
c11log
::
sinks
::
async_sink
<
Mutex
>::~
async_sink
()
{
_shutdown
();
}
inline
void
c11log
::
sinks
::
async_sink
::
_sink_it
(
const
details
::
log_msg
&
msg
)
template
<
class
Mutex
>
inline
void
c11log
::
sinks
::
async_sink
<
Mutex
>
::
_sink_it
(
const
details
::
log_msg
&
msg
)
{
if
(
!
_active
||
msg
.
formatted
.
empty
())
return
;
_q
.
push
(
msg
);
}
inline
void
c11log
::
sinks
::
async_sink
::
_thread_loop
()
template
<
class
Mutex
>
inline
void
c11log
::
sinks
::
async_sink
<
Mutex
>::
_thread_loop
()
{
static
std
::
chrono
::
seconds
pop_timeout
{
1
};
while
(
_active
)
...
...
@@ -93,23 +95,27 @@ inline void c11log::sinks::async_sink::_thread_loop()
}
}
inline
void
c11log
::
sinks
::
async_sink
::
add_sink
(
logger
::
sink_ptr
sink
)
template
<
class
Mutex
>
inline
void
c11log
::
sinks
::
async_sink
<
Mutex
>::
add_sink
(
logger
::
sink_ptr
sink
)
{
_sinks
.
push_back
(
sink
);
}
inline
void
c11log
::
sinks
::
async_sink
::
remove_sink
(
logger
::
sink_ptr
sink
)
template
<
class
Mutex
>
inline
void
c11log
::
sinks
::
async_sink
<
Mutex
>::
remove_sink
(
logger
::
sink_ptr
sink
)
{
_sinks
.
erase
(
std
::
remove
(
_sinks
.
begin
(),
_sinks
.
end
(),
sink
),
_sinks
.
end
());
}
inline
c11log
::
sinks
::
async_sink
::
q_type
&
c11log
::
sinks
::
async_sink
::
q
()
/*
template<class Mutex>
inline c11log::sinks::async_sink::q_type& c11log::sinks::async_sink<Mutex>::q()
{
return _q;
}
}
*/
inline
void
c11log
::
sinks
::
async_sink
::
shutdown
(
const
std
::
chrono
::
milliseconds
&
timeout
)
template
<
class
Mutex
>
inline
void
c11log
::
sinks
::
async_sink
<
Mutex
>::
shutdown
(
const
std
::
chrono
::
milliseconds
&
timeout
)
{
if
(
timeout
>
std
::
chrono
::
milliseconds
::
zero
())
{
...
...
@@ -122,7 +128,9 @@ inline void c11log::sinks::async_sink::shutdown(const std::chrono::milliseconds&
_shutdown
();
}
inline
void
c11log
::
sinks
::
async_sink
::
_shutdown
()
template
<
class
Mutex
>
inline
void
c11log
::
sinks
::
async_sink
<
Mutex
>::
_shutdown
()
{
std
::
lock_guard
<
std
::
mutex
>
guard
(
_shutdown_mutex
);
if
(
_active
)
...
...
include/c11log/sinks/base_sink.h
View file @
6c7793d4
#pragma once
#include<string>
#include<mutex>
#include<atomic>
#include "isink.h"
#include "../formatter.h"
#include "../common_types.h"
#include "../details/log_msg.h"
namespace
c11log
{
namespace
sinks
{
class
base_sink
template
<
class
Mutex
>
class
base_sink
:
public
isink
{
public
:
base_sink
()
:
_enabled
(
true
)
{}
base_sink
()
:
_mutex
(),
_enabled
(
true
)
{}
virtual
~
base_sink
()
=
default
;
base_sink
(
const
base_sink
&
)
=
delete
;
...
...
@@ -24,6 +27,7 @@ public:
{
if
(
_enabled
)
{
std
::
lock_guard
<
Mutex
>
lock
(
_mutex
);
_sink_it
(
msg
);
}
};
...
...
@@ -40,7 +44,9 @@ public:
protected
:
virtual
void
_sink_it
(
const
details
::
log_msg
&
msg
)
=
0
;
Mutex
_mutex
;
std
::
atomic
<
bool
>
_enabled
;
};
...
...
include/c11log/sinks/file_sinks.h
View file @
6c7793d4
...
...
@@ -3,48 +3,50 @@
#include <fstream>
#include <sstream>
#include <iomanip>
#include <mutex>
#include "base_sink.h"
#include <mutex>
#include "../details/null_mutex.h"
#include "../details/flush_helper.h"
#include "../details/blocking_queue.h"
namespace
c11log
{
namespace
sinks
{
/*
* T
hread safe, t
rivial file sink with single file as target
* Trivial file sink with single file as target
*/
class
simple_file_sink
:
public
base_sink
template
<
class
Mutex
>
class
simple_file_sink
:
public
base_sink
<
Mutex
>
{
public
:
explicit
simple_file_sink
(
const
std
::
string
&
filename
,
const
std
::
string
&
extension
,
const
std
::
size_t
flush_every
=
0
)
:
_mutex
(),
_ofstream
(
filename
+
"."
+
extension
,
std
::
ofstream
::
binary
|
std
::
ofstream
::
app
),
const
std
::
size_t
flush_every
=
0
)
:
_ofstream
(
filename
,
std
::
ofstream
::
binary
|
std
::
ofstream
::
app
),
_flush_helper
(
flush_every
)
{
}
protected
:
void
_sink_it
(
const
details
::
log_msg
&
msg
)
override
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
_mutex
);
_flush_helper
.
write
(
msg
.
formatted
,
_ofstream
);
}
private
:
std
::
mutex
_mutex
;
std
::
ofstream
_ofstream
;
details
::
file_flush_helper
_flush_helper
;
};
typedef
simple_file_sink
<
std
::
mutex
>
simple_file_sink_mt
;
typedef
simple_file_sink
<
details
::
null_mutex
>
simple_file_sink_st
;
/*
*
Thread safe, r
otating file sink based on size
*
R
otating file sink based on size
*/
class
rotating_file_sink
:
public
base_sink
template
<
class
Mutex
>
class
rotating_file_sink
:
public
base_sink
<
Mutex
>
{
public
:
rotating_file_sink
(
const
std
::
string
&
base_filename
,
const
std
::
string
&
extension
,
...
...
@@ -55,7 +57,6 @@ public:
_max_size
(
max_size
),
_max_files
(
max_files
),
_current_size
(
0
),
_mutex
(),
_ofstream
(
_calc_filename
(
_base_filename
,
0
,
_extension
),
std
::
ofstream
::
binary
),
_flush_helper
(
flush_every
)
{
...
...
@@ -64,8 +65,6 @@ public:
protected
:
void
_sink_it
(
const
details
::
log_msg
&
msg
)
override
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
_mutex
);
_current_size
+=
msg
.
formatted
.
size
();
if
(
_current_size
>
_max_size
)
{
...
...
@@ -89,10 +88,13 @@ private:
// Rotate old files:
// log.n-1.txt -> log.n.txt
// log n-2.txt -> log.n-1.txt
// ...
// log.txt -> log.1.txt
// log.n-1.txt -> log.n.txt
// log.n-2.txt -> log.n-1.txt
// log.n-3.txt ->..
// log.n.txt -> log.txt
void
_rotate
()
{
_ofstream
.
close
();
...
...
@@ -112,15 +114,18 @@ private:
std
::
size_t
_max_size
;
std
::
size_t
_max_files
;
std
::
size_t
_current_size
;
std
::
mutex
_mutex
;
std
::
ofstream
_ofstream
;
details
::
file_flush_helper
_flush_helper
;
};
typedef
rotating_file_sink
<
std
::
mutex
>
rotating_file_sink_mt
;
typedef
rotating_file_sink
<
details
::
null_mutex
>
rotating_file_sink_st
;
/*
*
Thread safe, r
otating file sink based on date. rotates at midnight
*
R
otating file sink based on date. rotates at midnight
*/
class
daily_file_sink
:
public
base_sink
template
<
class
Mutex
>
class
daily_file_sink
:
public
base_sink
<
Mutex
>
{
public
:
explicit
daily_file_sink
(
const
std
::
string
&
base_filename
,
...
...
@@ -129,7 +134,6 @@ public:
_base_filename
(
base_filename
),
_extension
(
extension
),
_midnight_tp
(
_calc_midnight_tp
()
),
_mutex
(),
_ofstream
(
_calc_filename
(
_base_filename
,
_extension
),
std
::
ofstream
::
binary
|
std
::
ofstream
::
app
),
_flush_helper
(
flush_every
)
{
...
...
@@ -138,7 +142,6 @@ public:
protected
:
void
_sink_it
(
const
details
::
log_msg
&
msg
)
override
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
_mutex
);
if
(
std
::
chrono
::
system_clock
::
now
()
>=
_midnight_tp
)
{
_ofstream
.
close
();
...
...
@@ -175,10 +178,12 @@ private:
std
::
string
_base_filename
;
std
::
string
_extension
;
std
::
chrono
::
system_clock
::
time_point
_midnight_tp
;
std
::
mutex
_mutex
;
std
::
ofstream
_ofstream
;
details
::
file_flush_helper
_flush_helper
;
};
typedef
daily_file_sink
<
std
::
mutex
>
daily_file_sink_mt
;
typedef
daily_file_sink
<
details
::
null_mutex
>
daily_file_sink_st
;
}
}
include/c11log/sinks/null_sink.h
View file @
6c7793d4
#pragma once
#include <mutex>
#include <memory>
#include "base_sink.h"
namespace
c11log
{
namespace
sinks
{
class
null_sink
:
public
base_sink
template
<
class
Mutex
>
class
null_sink
:
public
base_sink
<
Mutex
>
{
protected
:
void
_sink_it
(
const
details
::
log_msg
&
)
override
...
...
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