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
ef9842c3
Commit
ef9842c3
authored
May 03, 2016
by
eao197
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
daily_file_sink with custom file name calculator
parent
0143d9a9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
78 additions
and
16 deletions
+78
-16
include/spdlog/sinks/file_sinks.h
include/spdlog/sinks/file_sinks.h
+33
-12
tests/file_log.cpp
tests/file_log.cpp
+45
-4
No files found.
include/spdlog/sinks/file_sinks.h
View file @
ef9842c3
...
@@ -139,10 +139,40 @@ private:
...
@@ -139,10 +139,40 @@ private:
typedef
rotating_file_sink
<
std
::
mutex
>
rotating_file_sink_mt
;
typedef
rotating_file_sink
<
std
::
mutex
>
rotating_file_sink_mt
;
typedef
rotating_file_sink
<
details
::
null_mutex
>
rotating_file_sink_st
;
typedef
rotating_file_sink
<
details
::
null_mutex
>
rotating_file_sink_st
;
/*
* Default generator of daily log file names.
*/
struct
default_daily_file_name_calculator
{
//Create filename for the form basename.YYYY-MM-DD_hh-mm.extension
static
filename_t
calc_filename
(
const
filename_t
&
basename
,
const
filename_t
&
extension
)
{
std
::
tm
tm
=
spdlog
::
details
::
os
::
localtime
();
std
::
conditional
<
std
::
is_same
<
filename_t
::
value_type
,
char
>::
value
,
fmt
::
MemoryWriter
,
fmt
::
WMemoryWriter
>::
type
w
;
w
.
write
(
SPDLOG_FILENAME_T
(
"{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}.{}"
),
basename
,
tm
.
tm_year
+
1900
,
tm
.
tm_mon
+
1
,
tm
.
tm_mday
,
tm
.
tm_hour
,
tm
.
tm_min
,
extension
);
return
w
.
str
();
}
};
/*
* Generator of daily log file names in format basename.YYYY-MM-DD.extension
*/
struct
dateonly_daily_file_name_calculator
{
//Create filename for the form basename.YYYY-MM-DD.extension
static
filename_t
calc_filename
(
const
filename_t
&
basename
,
const
filename_t
&
extension
)
{
std
::
tm
tm
=
spdlog
::
details
::
os
::
localtime
();
std
::
conditional
<
std
::
is_same
<
filename_t
::
value_type
,
char
>::
value
,
fmt
::
MemoryWriter
,
fmt
::
WMemoryWriter
>::
type
w
;
w
.
write
(
SPDLOG_FILENAME_T
(
"{}_{:04d}-{:02d}-{:02d}.{}"
),
basename
,
tm
.
tm_year
+
1900
,
tm
.
tm_mon
+
1
,
tm
.
tm_mday
,
extension
);
return
w
.
str
();
}
};
/*
/*
* Rotating file sink based on date. rotates at midnight
* Rotating file sink based on date. rotates at midnight
*/
*/
template
<
class
Mutex
>
template
<
class
Mutex
,
class
FileNameCalc
=
default_daily_file_name_calculator
>
class
daily_file_sink
:
public
base_sink
<
Mutex
>
class
daily_file_sink
:
public
base_sink
<
Mutex
>
{
{
public:
public:
...
@@ -161,7 +191,7 @@ public:
...
@@ -161,7 +191,7 @@ public:
if
(
rotation_hour
<
0
||
rotation_hour
>
23
||
rotation_minute
<
0
||
rotation_minute
>
59
)
if
(
rotation_hour
<
0
||
rotation_hour
>
23
||
rotation_minute
<
0
||
rotation_minute
>
59
)
throw
spdlog_ex
(
"daily_file_sink: Invalid rotation time in ctor"
);
throw
spdlog_ex
(
"daily_file_sink: Invalid rotation time in ctor"
);
_rotation_tp
=
_next_rotation_tp
();
_rotation_tp
=
_next_rotation_tp
();
_file_helper
.
open
(
calc_filename
(
_base_filename
,
_extension
));
_file_helper
.
open
(
FileNameCalc
::
calc_filename
(
_base_filename
,
_extension
));
}
}
void
flush
()
override
void
flush
()
override
...
@@ -174,7 +204,7 @@ protected:
...
@@ -174,7 +204,7 @@ protected:
{
{
if
(
std
::
chrono
::
system_clock
::
now
()
>=
_rotation_tp
)
if
(
std
::
chrono
::
system_clock
::
now
()
>=
_rotation_tp
)
{
{
_file_helper
.
open
(
calc_filename
(
_base_filename
,
_extension
));
_file_helper
.
open
(
FileNameCalc
::
calc_filename
(
_base_filename
,
_extension
));
_rotation_tp
=
_next_rotation_tp
();
_rotation_tp
=
_next_rotation_tp
();
}
}
_file_helper
.
write
(
msg
);
_file_helper
.
write
(
msg
);
...
@@ -197,15 +227,6 @@ private:
...
@@ -197,15 +227,6 @@ private:
return
system_clock
::
time_point
(
rotation_time
+
hours
(
24
));
return
system_clock
::
time_point
(
rotation_time
+
hours
(
24
));
}
}
//Create filename for the form basename.YYYY-MM-DD.extension
static
filename_t
calc_filename
(
const
filename_t
&
basename
,
const
filename_t
&
extension
)
{
std
::
tm
tm
=
spdlog
::
details
::
os
::
localtime
();
std
::
conditional
<
std
::
is_same
<
filename_t
::
value_type
,
char
>::
value
,
fmt
::
MemoryWriter
,
fmt
::
WMemoryWriter
>::
type
w
;
w
.
write
(
SPDLOG_FILENAME_T
(
"{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}.{}"
),
basename
,
tm
.
tm_year
+
1900
,
tm
.
tm_mon
+
1
,
tm
.
tm_mday
,
tm
.
tm_hour
,
tm
.
tm_min
,
extension
);
return
w
.
str
();
}
filename_t
_base_filename
;
filename_t
_base_filename
;
filename_t
_extension
;
filename_t
_extension
;
int
_rotation_h
;
int
_rotation_h
;
...
...
tests/file_log.cpp
View file @
ef9842c3
...
@@ -77,15 +77,56 @@ TEST_CASE("daily_logger", "[daily_logger]]")
...
@@ -77,15 +77,56 @@ TEST_CASE("daily_logger", "[daily_logger]]")
}
}
TEST_CASE
(
"daily_logger with dateonly calculator"
,
"[daily_logger_dateonly]]"
)
{
using
sink_type
=
spdlog
::
sinks
::
daily_file_sink
<
std
::
mutex
,
spdlog
::
sinks
::
dateonly_daily_file_name_calculator
>
;
prepare_logdir
();
//calculate filename (time based)
std
::
string
basename
=
"logs/daily_dateonly"
;
std
::
tm
tm
=
spdlog
::
details
::
os
::
localtime
();
fmt
::
MemoryWriter
w
;
w
.
write
(
"{}_{:04d}-{:02d}-{:02d}.txt"
,
basename
,
tm
.
tm_year
+
1900
,
tm
.
tm_mon
+
1
,
tm
.
tm_mday
);
auto
logger
=
spdlog
::
create
<
sink_type
>
(
"logger"
,
basename
,
"txt"
,
0
,
0
,
true
);
for
(
int
i
=
0
;
i
<
10
;
++
i
)
logger
->
info
(
"Test message {}"
,
i
);
auto
filename
=
w
.
str
();
REQUIRE
(
count_lines
(
filename
)
==
10
);
}
struct
custom_daily_file_name_calculator
{
static
spdlog
::
filename_t
calc_filename
(
const
spdlog
::
filename_t
&
basename
,
const
spdlog
::
filename_t
&
extension
)
{
std
::
tm
tm
=
spdlog
::
details
::
os
::
localtime
();
fmt
::
MemoryWriter
w
;
w
.
write
(
"{}{:04d}{:02d}{:02d}.{}"
,
basename
,
tm
.
tm_year
+
1900
,
tm
.
tm_mon
+
1
,
tm
.
tm_mday
,
extension
);
return
w
.
str
();
}
};
TEST_CASE
(
"daily_logger with custom calculator"
,
"[daily_logger_custom]]"
)
{
using
sink_type
=
spdlog
::
sinks
::
daily_file_sink
<
std
::
mutex
,
custom_daily_file_name_calculator
>
;
prepare_logdir
();
//calculate filename (time based)
std
::
string
basename
=
"logs/daily_dateonly"
;
std
::
tm
tm
=
spdlog
::
details
::
os
::
localtime
();
fmt
::
MemoryWriter
w
;
w
.
write
(
"{}{:04d}{:02d}{:02d}.txt"
,
basename
,
tm
.
tm_year
+
1900
,
tm
.
tm_mon
+
1
,
tm
.
tm_mday
);
auto
logger
=
spdlog
::
create
<
sink_type
>
(
"logger"
,
basename
,
"txt"
,
0
,
0
,
true
);
for
(
int
i
=
0
;
i
<
10
;
++
i
)
logger
->
info
(
"Test message {}"
,
i
);
auto
filename
=
w
.
str
();
REQUIRE
(
count_lines
(
filename
)
==
10
);
}
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