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
6f4c7800
Commit
6f4c7800
authored
Feb 22, 2014
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
flush_helper
parent
005dc7e6
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
16 deletions
+45
-16
example.cpp
example/example.cpp
+2
-2
flush_helper.h
include/c11log/details/flush_helper.h
+25
-0
file_sinks.h
include/c11log/sinks/file_sinks.h
+18
-14
No files found.
example/example.cpp
View file @
6f4c7800
...
...
@@ -60,12 +60,12 @@ int main(int argc, char* argv[])
auto
null_sink
=
std
::
make_shared
<
sinks
::
null_sink
>
();
auto
stdout_sink
=
std
::
make_shared
<
sinks
::
stdout_sink
>
();
auto
async
=
std
::
make_shared
<
sinks
::
async_sink
>
(
qsize
);
auto
fsink
=
std
::
make_shared
<
sinks
::
rotating_file_sink
>
(
"
example_log"
,
"txt"
,
1024
*
1024
*
50
,
5
);
auto
fsink
=
std
::
make_shared
<
sinks
::
rotating_file_sink
>
(
"
log"
,
"txt"
,
1024
*
1024
*
50
,
5
,
1000
);
async
->
add_sink
(
fsink
);
auto
&
logger
=
c11log
::
get_logger
(
"async"
);
logger
.
add_sink
(
async
);
logger
.
add_sink
(
fsink
);
testlog
(
threads
);
}
...
...
include/c11log/details/flush_helper.h
0 → 100644
View file @
6f4c7800
#pragma once
// Flush to file every X writes..
namespace
c11log
{
namespace
details
{
class
file_flush_helper
{
public
:
explicit
file_flush_helper
(
std
::
size_t
flush_every
)
:
_flush_every
(
flush_every
),
_write_counter
(
0
)
{};
void
write
(
std
::
ofstream
&
ofs
,
const
std
::
string
&
msg
)
{
ofs
<<
msg
;
if
(
++
_write_counter
>=
_flush_every
)
{
ofs
.
flush
();
_write_counter
=
0
;
}
}
private
:
std
::
size_t
_flush_every
;
std
::
size_t
_write_counter
;
};
}
}
include/c11log/sinks/file_sinks.h
View file @
6f4c7800
...
...
@@ -3,46 +3,48 @@
#include <fstream>
#include <iomanip>
#include <mutex>
#include "base_sink.h"
#include "../details/flush_helper.h"
namespace
c11log
{
namespace
sinks
{
/*
* Trivial file sink with single file as target
*/
class
simple_file_sink
:
public
base_sink
{
public
:
explicit
simple_file_sink
(
const
std
::
string
&
filename
,
const
std
::
string
&
extension
=
"txt"
)
explicit
simple_file_sink
(
const
std
::
string
&
filename
,
const
std
::
string
&
extension
,
size_t
flush_after
=
1
)
:
_mutex
(),
_ofstream
(
filename
+
"."
+
extension
,
std
::
ofstream
::
app
)
{
_ofstream
(
filename
+
"."
+
extension
,
std
::
ofstream
::
app
),
_flush_helper
(
flush_after
)
{
}
protected
:
void
_sink_it
(
const
std
::
string
&
msg
)
override
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
_mutex
);
_ofstream
<<
msg
;
_ofstream
.
flush
();
_flush_helper
.
write
(
_ofstream
,
msg
);
}
private
:
std
::
mutex
_mutex
;
std
::
ofstream
_ofstream
;
details
::
file_flush_helper
_flush_helper
;
};
/*
* Thread safe, size limited file sink
*/
class
rotating_file_sink
:
public
base_sink
{
public
:
rotating_file_sink
(
const
std
::
string
&
base_filename
,
const
std
::
string
&
extension
,
size_t
max_size
,
size_t
max_files
)
:
rotating_file_sink
(
const
std
::
string
&
base_filename
,
const
std
::
string
&
extension
,
size_t
max_size
,
size_t
max_files
,
size_t
flush_after
=
1
)
:
_base_filename
(
base_filename
),
_extension
(
extension
),
_max_size
(
max_size
),
_max_files
(
max_files
),
_current_size
(
0
),
_mutex
(),
_ofstream
(
_calc_filename
(
_base_filename
,
0
,
_extension
))
{
_ofstream
(
_calc_filename
(
_base_filename
,
0
,
_extension
)),
_flush_helper
(
flush_after
)
{
}
protected
:
...
...
@@ -53,8 +55,7 @@ protected:
_rotate
();
_current_size
=
msg
.
length
();
}
_ofstream
<<
msg
;
_ofstream
.
flush
();
_flush_helper
.
write
(
_ofstream
,
msg
);
}
...
...
@@ -93,6 +94,7 @@ private:
std
::
size_t
_current_size
;
std
::
mutex
_mutex
;
std
::
ofstream
_ofstream
;
details
::
file_flush_helper
_flush_helper
;
};
/*
...
...
@@ -100,12 +102,14 @@ private:
*/
class
daily_file_sink
:
public
base_sink
{
public
:
explicit
daily_file_sink
(
const
std
::
string
&
base_filename
,
const
std
::
string
&
extension
=
"txt"
)
:
explicit
daily_file_sink
(
const
std
::
string
&
base_filename
,
const
std
::
string
&
extension
,
size_t
flush_after
=
1
)
:
_base_filename
(
base_filename
),
_extension
(
extension
),
_midnight_tp
(
_calc_midnight_tp
()
),
_mutex
(),
_ofstream
(
_calc_filename
(
_base_filename
,
_extension
),
std
::
ofstream
::
app
)
{
_ofstream
(
_calc_filename
(
_base_filename
,
_extension
),
std
::
ofstream
::
app
),
_flush_helper
(
flush_after
)
{
}
protected
:
...
...
@@ -116,8 +120,7 @@ protected:
_ofstream
.
open
(
_calc_filename
(
_base_filename
,
_extension
));
_midnight_tp
=
_calc_midnight_tp
();
}
_ofstream
<<
msg
;
_ofstream
.
flush
();
_flush_helper
.
write
(
_ofstream
,
msg
);
}
private
:
...
...
@@ -144,6 +147,7 @@ private:
std
::
chrono
::
system_clock
::
time_point
_midnight_tp
;
std
::
mutex
_mutex
;
std
::
ofstream
_ofstream
;
details
::
file_flush_helper
_flush_helper
;
};
}
...
...
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