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
e97621d6
Commit
e97621d6
authored
Sep 18, 2016
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Removed force_flush arg from everywhere. Use flush_on(level) instead
parent
b0f82307
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
56 additions
and
48 deletions
+56
-48
include/spdlog/details/file_helper.h
include/spdlog/details/file_helper.h
+4
-9
include/spdlog/details/spdlog_impl.h
include/spdlog/details/spdlog_impl.h
+2
-2
include/spdlog/sinks/file_sinks.h
include/spdlog/sinks/file_sinks.h
+17
-23
tests/errors.cpp
tests/errors.cpp
+1
-0
tests/file_helper.cpp
tests/file_helper.cpp
+6
-5
tests/file_log.cpp
tests/file_log.cpp
+26
-9
No files found.
include/spdlog/details/file_helper.h
View file @
e97621d6
...
@@ -31,9 +31,8 @@ public:
...
@@ -31,9 +31,8 @@ public:
const
int
open_tries
=
5
;
const
int
open_tries
=
5
;
const
int
open_interval
=
10
;
const
int
open_interval
=
10
;
explicit
file_helper
(
bool
force_flush
)
:
explicit
file_helper
()
:
_fd
(
nullptr
),
_fd
(
nullptr
)
_force_flush
(
force_flush
)
{}
{}
file_helper
(
const
file_helper
&
)
=
delete
;
file_helper
(
const
file_helper
&
)
=
delete
;
...
@@ -91,9 +90,6 @@ public:
...
@@ -91,9 +90,6 @@ public:
auto
data
=
msg
.
formatted
.
data
();
auto
data
=
msg
.
formatted
.
data
();
if
(
std
::
fwrite
(
data
,
1
,
msg_size
,
_fd
)
!=
msg_size
)
if
(
std
::
fwrite
(
data
,
1
,
msg_size
,
_fd
)
!=
msg_size
)
throw
spdlog_ex
(
"Failed writing to file "
+
os
::
filename_to_str
(
_filename
),
errno
);
throw
spdlog_ex
(
"Failed writing to file "
+
os
::
filename_to_str
(
_filename
),
errno
);
if
(
_force_flush
)
std
::
fflush
(
_fd
);
}
}
size_t
size
()
size_t
size
()
...
@@ -117,7 +113,6 @@ public:
...
@@ -117,7 +113,6 @@ public:
private:
private:
FILE
*
_fd
;
FILE
*
_fd
;
filename_t
_filename
;
filename_t
_filename
;
bool
_force_flush
;
};
};
}
}
}
}
include/spdlog/details/spdlog_impl.h
View file @
e97621d6
...
@@ -39,12 +39,12 @@ inline void spdlog::drop(const std::string &name)
...
@@ -39,12 +39,12 @@ inline void spdlog::drop(const std::string &name)
// Create multi/single threaded simple file logger
// Create multi/single threaded simple file logger
inline
std
::
shared_ptr
<
spdlog
::
logger
>
spdlog
::
basic_logger_mt
(
const
std
::
string
&
logger_name
,
const
filename_t
&
filename
,
bool
truncate
)
inline
std
::
shared_ptr
<
spdlog
::
logger
>
spdlog
::
basic_logger_mt
(
const
std
::
string
&
logger_name
,
const
filename_t
&
filename
,
bool
truncate
)
{
{
return
create
<
spdlog
::
sinks
::
simple_file_sink_mt
>
(
logger_name
,
filename
,
false
,
truncate
);
return
create
<
spdlog
::
sinks
::
simple_file_sink_mt
>
(
logger_name
,
filename
,
truncate
);
}
}
inline
std
::
shared_ptr
<
spdlog
::
logger
>
spdlog
::
basic_logger_st
(
const
std
::
string
&
logger_name
,
const
filename_t
&
filename
,
bool
truncate
)
inline
std
::
shared_ptr
<
spdlog
::
logger
>
spdlog
::
basic_logger_st
(
const
std
::
string
&
logger_name
,
const
filename_t
&
filename
,
bool
truncate
)
{
{
return
create
<
spdlog
::
sinks
::
simple_file_sink_st
>
(
logger_name
,
filename
,
false
,
truncate
);
return
create
<
spdlog
::
sinks
::
simple_file_sink_st
>
(
logger_name
,
filename
,
truncate
);
}
}
// Create multi/single threaded rotating file logger
// Create multi/single threaded rotating file logger
...
...
include/spdlog/sinks/file_sinks.h
View file @
e97621d6
...
@@ -23,16 +23,13 @@ namespace spdlog
...
@@ -23,16 +23,13 @@ namespace spdlog
namespace
sinks
namespace
sinks
{
{
/*
/*
* Trivial file sink with single file as target
* Trivial file sink with single file as target
*/
*/
template
<
class
Mutex
>
template
<
class
Mutex
>
class
simple_file_sink
:
public
base_sink
<
Mutex
>
class
simple_file_sink
:
public
base_sink
<
Mutex
>
{
{
public:
public:
explicit
simple_file_sink
(
const
filename_t
&
filename
,
explicit
simple_file_sink
(
const
filename_t
&
filename
,
bool
truncate
=
false
)
bool
force_flush
=
false
,
bool
truncate
=
false
)
:
_file_helper
(
force_flush
)
{
{
_file_helper
.
open
(
filename
,
truncate
);
_file_helper
.
open
(
filename
,
truncate
);
}
}
...
@@ -54,21 +51,20 @@ typedef simple_file_sink<std::mutex> simple_file_sink_mt;
...
@@ -54,21 +51,20 @@ typedef simple_file_sink<std::mutex> simple_file_sink_mt;
typedef
simple_file_sink
<
details
::
null_mutex
>
simple_file_sink_st
;
typedef
simple_file_sink
<
details
::
null_mutex
>
simple_file_sink_st
;
/*
/*
* Rotating file sink based on size
* Rotating file sink based on size
*/
*/
template
<
class
Mutex
>
template
<
class
Mutex
>
class
rotating_file_sink
:
public
base_sink
<
Mutex
>
class
rotating_file_sink
:
public
base_sink
<
Mutex
>
{
{
public:
public:
rotating_file_sink
(
const
filename_t
&
base_filename
,
const
filename_t
&
extension
,
rotating_file_sink
(
const
filename_t
&
base_filename
,
const
filename_t
&
extension
,
std
::
size_t
max_size
,
std
::
size_t
max_files
,
std
::
size_t
max_size
,
std
::
size_t
max_files
)
:
bool
force_flush
=
false
)
:
_base_filename
(
base_filename
),
_base_filename
(
base_filename
),
_extension
(
extension
),
_extension
(
extension
),
_max_size
(
max_size
),
_max_size
(
max_size
),
_max_files
(
max_files
),
_max_files
(
max_files
),
_current_size
(
0
),
_current_size
(
0
),
_file_helper
(
force_flush
)
_file_helper
()
{
{
_file_helper
.
open
(
calc_filename
(
_base_filename
,
0
,
_extension
));
_file_helper
.
open
(
calc_filename
(
_base_filename
,
0
,
_extension
));
_current_size
=
_file_helper
.
size
();
//expensive. called only once
_current_size
=
_file_helper
.
size
();
//expensive. called only once
...
@@ -143,11 +139,11 @@ typedef rotating_file_sink<std::mutex> rotating_file_sink_mt;
...
@@ -143,11 +139,11 @@ 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.
* Default generator of daily log file names.
*/
*/
struct
default_daily_file_name_calculator
struct
default_daily_file_name_calculator
{
{
//Create filename for the form basename.YYYY-MM-DD_hh-mm.extension
//
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
)
static
filename_t
calc_filename
(
const
filename_t
&
basename
,
const
filename_t
&
extension
)
{
{
std
::
tm
tm
=
spdlog
::
details
::
os
::
localtime
();
std
::
tm
tm
=
spdlog
::
details
::
os
::
localtime
();
...
@@ -158,11 +154,11 @@ struct default_daily_file_name_calculator
...
@@ -158,11 +154,11 @@ struct default_daily_file_name_calculator
};
};
/*
/*
* Generator of daily log file names in format basename.YYYY-MM-DD.extension
* Generator of daily log file names in format basename.YYYY-MM-DD.extension
*/
*/
struct
dateonly_daily_file_name_calculator
struct
dateonly_daily_file_name_calculator
{
{
//Create filename for the form basename.YYYY-MM-DD.extension
//
Create filename for the form basename.YYYY-MM-DD.extension
static
filename_t
calc_filename
(
const
filename_t
&
basename
,
const
filename_t
&
extension
)
static
filename_t
calc_filename
(
const
filename_t
&
basename
,
const
filename_t
&
extension
)
{
{
std
::
tm
tm
=
spdlog
::
details
::
os
::
localtime
();
std
::
tm
tm
=
spdlog
::
details
::
os
::
localtime
();
...
@@ -173,8 +169,8 @@ struct dateonly_daily_file_name_calculator
...
@@ -173,8 +169,8 @@ struct dateonly_daily_file_name_calculator
};
};
/*
/*
* Rotating file sink based on date. rotates at midnight
* Rotating file sink based on date. rotates at midnight
*/
*/
template
<
class
Mutex
,
class
FileNameCalc
=
default_daily_file_name_calculator
>
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
>
{
{
...
@@ -184,12 +180,10 @@ public:
...
@@ -184,12 +180,10 @@ public:
const
filename_t
&
base_filename
,
const
filename_t
&
base_filename
,
const
filename_t
&
extension
,
const
filename_t
&
extension
,
int
rotation_hour
,
int
rotation_hour
,
int
rotation_minute
,
int
rotation_minute
)
:
_base_filename
(
base_filename
),
bool
force_flush
=
false
)
:
_base_filename
(
base_filename
),
_extension
(
extension
),
_extension
(
extension
),
_rotation_h
(
rotation_hour
),
_rotation_h
(
rotation_hour
),
_rotation_m
(
rotation_minute
),
_rotation_m
(
rotation_minute
)
_file_helper
(
force_flush
)
{
{
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"
);
...
...
tests/errors.cpp
View file @
e97621d6
...
@@ -28,6 +28,7 @@ TEST_CASE("custom_error_handler", "[errors]]")
...
@@ -28,6 +28,7 @@ TEST_CASE("custom_error_handler", "[errors]]")
prepare_logdir
();
prepare_logdir
();
std
::
string
filename
=
"logs/simple_log.txt"
;
std
::
string
filename
=
"logs/simple_log.txt"
;
auto
logger
=
spdlog
::
create
<
spdlog
::
sinks
::
simple_file_sink_mt
>
(
"logger"
,
filename
,
true
);
auto
logger
=
spdlog
::
create
<
spdlog
::
sinks
::
simple_file_sink_mt
>
(
"logger"
,
filename
,
true
);
logger
->
flush_on
(
spdlog
::
level
::
info
);
logger
->
set_error_handler
([
=
](
const
std
::
string
&
msg
)
logger
->
set_error_handler
([
=
](
const
std
::
string
&
msg
)
{
{
throw
custom_ex
();
throw
custom_ex
();
...
...
tests/file_helper.cpp
View file @
e97621d6
...
@@ -12,6 +12,7 @@ static void write_with_helper(file_helper &helper, size_t howmany)
...
@@ -12,6 +12,7 @@ static void write_with_helper(file_helper &helper, size_t howmany)
log_msg
msg
;
log_msg
msg
;
msg
.
formatted
<<
std
::
string
(
howmany
,
'1'
);
msg
.
formatted
<<
std
::
string
(
howmany
,
'1'
);
helper
.
write
(
msg
);
helper
.
write
(
msg
);
helper
.
flush
();
}
}
...
@@ -19,7 +20,7 @@ TEST_CASE("file_helper_filename", "[file_helper::filename()]]")
...
@@ -19,7 +20,7 @@ TEST_CASE("file_helper_filename", "[file_helper::filename()]]")
{
{
prepare_logdir
();
prepare_logdir
();
file_helper
helper
(
false
)
;
file_helper
helper
;
helper
.
open
(
target_filename
);
helper
.
open
(
target_filename
);
REQUIRE
(
helper
.
filename
()
==
target_filename
);
REQUIRE
(
helper
.
filename
()
==
target_filename
);
}
}
...
@@ -31,7 +32,7 @@ TEST_CASE("file_helper_size", "[file_helper::size()]]")
...
@@ -31,7 +32,7 @@ TEST_CASE("file_helper_size", "[file_helper::size()]]")
prepare_logdir
();
prepare_logdir
();
size_t
expected_size
=
123
;
size_t
expected_size
=
123
;
{
{
file_helper
helper
(
true
)
;
file_helper
helper
;
helper
.
open
(
target_filename
);
helper
.
open
(
target_filename
);
write_with_helper
(
helper
,
expected_size
);
write_with_helper
(
helper
,
expected_size
);
REQUIRE
(
static_cast
<
size_t
>
(
helper
.
size
())
==
expected_size
);
REQUIRE
(
static_cast
<
size_t
>
(
helper
.
size
())
==
expected_size
);
...
@@ -44,7 +45,7 @@ TEST_CASE("file_helper_exists", "[file_helper::file_exists()]]")
...
@@ -44,7 +45,7 @@ TEST_CASE("file_helper_exists", "[file_helper::file_exists()]]")
{
{
prepare_logdir
();
prepare_logdir
();
REQUIRE
(
!
file_helper
::
file_exists
(
target_filename
));
REQUIRE
(
!
file_helper
::
file_exists
(
target_filename
));
file_helper
helper
(
false
)
;
file_helper
helper
;
helper
.
open
(
target_filename
);
helper
.
open
(
target_filename
);
REQUIRE
(
file_helper
::
file_exists
(
target_filename
));
REQUIRE
(
file_helper
::
file_exists
(
target_filename
));
}
}
...
@@ -52,7 +53,7 @@ TEST_CASE("file_helper_exists", "[file_helper::file_exists()]]")
...
@@ -52,7 +53,7 @@ TEST_CASE("file_helper_exists", "[file_helper::file_exists()]]")
TEST_CASE
(
"file_helper_reopen"
,
"[file_helper::reopen()]]"
)
TEST_CASE
(
"file_helper_reopen"
,
"[file_helper::reopen()]]"
)
{
{
prepare_logdir
();
prepare_logdir
();
file_helper
helper
(
true
)
;
file_helper
helper
;
helper
.
open
(
target_filename
);
helper
.
open
(
target_filename
);
write_with_helper
(
helper
,
12
);
write_with_helper
(
helper
,
12
);
REQUIRE
(
helper
.
size
()
==
12
);
REQUIRE
(
helper
.
size
()
==
12
);
...
@@ -64,7 +65,7 @@ TEST_CASE("file_helper_reopen2", "[file_helper::reopen(false)]]")
...
@@ -64,7 +65,7 @@ TEST_CASE("file_helper_reopen2", "[file_helper::reopen(false)]]")
{
{
prepare_logdir
();
prepare_logdir
();
size_t
expected_size
=
14
;
size_t
expected_size
=
14
;
file_helper
helper
(
true
)
;
file_helper
helper
;
helper
.
open
(
target_filename
);
helper
.
open
(
target_filename
);
write_with_helper
(
helper
,
expected_size
);
write_with_helper
(
helper
,
expected_size
);
REQUIRE
(
helper
.
size
()
==
expected_size
);
REQUIRE
(
helper
.
size
()
==
expected_size
);
...
...
tests/file_log.cpp
View file @
e97621d6
...
@@ -18,7 +18,26 @@ TEST_CASE("simple_file_logger", "[simple_logger]]")
...
@@ -18,7 +18,26 @@ TEST_CASE("simple_file_logger", "[simple_logger]]")
logger
->
flush
();
logger
->
flush
();
REQUIRE
(
file_contents
(
filename
)
==
std
::
string
(
"Test message 1
\n
Test message 2
\n
"
));
REQUIRE
(
file_contents
(
filename
)
==
std
::
string
(
"Test message 1
\n
Test message 2
\n
"
));
REQUIRE
(
count_lines
(
filename
)
==
2
);
REQUIRE
(
count_lines
(
filename
)
==
2
);
}
TEST_CASE
(
"flush_on"
,
"[flush_on]]"
)
{
prepare_logdir
();
std
::
string
filename
=
"logs/simple_log.txt"
;
auto
logger
=
spdlog
::
create
<
spdlog
::
sinks
::
simple_file_sink_mt
>
(
"logger"
,
filename
);
logger
->
set_pattern
(
"%v"
);
logger
->
set_level
(
spdlog
::
level
::
trace
);
logger
->
flush_on
(
spdlog
::
level
::
info
);
logger
->
trace
(
"Should not be flushed"
);
REQUIRE
(
count_lines
(
filename
)
==
0
);
logger
->
info
(
"Test message {}"
,
1
);
logger
->
info
(
"Test message {}"
,
2
);
logger
->
flush
();
REQUIRE
(
file_contents
(
filename
)
==
std
::
string
(
"Should not be flushed
\n
Test message 1
\n
Test message 2
\n
"
));
REQUIRE
(
count_lines
(
filename
)
==
3
);
}
}
TEST_CASE
(
"rotating_file_logger1"
,
"[rotating_logger]]"
)
TEST_CASE
(
"rotating_file_logger1"
,
"[rotating_logger]]"
)
...
@@ -26,15 +45,13 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
...
@@ -26,15 +45,13 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
prepare_logdir
();
prepare_logdir
();
std
::
string
basename
=
"logs/rotating_log"
;
std
::
string
basename
=
"logs/rotating_log"
;
auto
logger
=
spdlog
::
rotating_logger_mt
(
"logger"
,
basename
,
1024
,
0
);
auto
logger
=
spdlog
::
rotating_logger_mt
(
"logger"
,
basename
,
1024
,
0
);
logger
->
flush_on
(
spdlog
::
level
::
info
);
for
(
int
i
=
0
;
i
<
10
;
++
i
)
for
(
int
i
=
0
;
i
<
10
;
++
i
)
logger
->
info
(
"Test message {}"
,
i
);
logger
->
info
(
"Test message {}"
,
i
);
logger
->
flush
();
auto
filename
=
basename
+
".txt"
;
auto
filename
=
basename
+
".txt"
;
REQUIRE
(
count_lines
(
filename
)
==
10
);
REQUIRE
(
count_lines
(
filename
)
==
10
);
for
(
int
i
=
0
;
i
<
1000
;
i
++
)
logger
->
info
(
"Test message {}"
,
i
);
}
}
...
@@ -61,7 +78,6 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
...
@@ -61,7 +78,6 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
TEST_CASE
(
"daily_logger"
,
"[daily_logger]]"
)
TEST_CASE
(
"daily_logger"
,
"[daily_logger]]"
)
{
{
prepare_logdir
();
prepare_logdir
();
//calculate filename (time based)
//calculate filename (time based)
std
::
string
basename
=
"logs/daily_log"
;
std
::
string
basename
=
"logs/daily_log"
;
...
@@ -92,10 +108,10 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger_dateonly]]")
...
@@ -92,10 +108,10 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger_dateonly]]")
fmt
::
MemoryWriter
w
;
fmt
::
MemoryWriter
w
;
w
.
write
(
"{}_{:04d}-{:02d}-{:02d}.txt"
,
basename
,
tm
.
tm_year
+
1900
,
tm
.
tm_mon
+
1
,
tm
.
tm_mday
);
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
);
auto
logger
=
spdlog
::
create
<
sink_type
>
(
"logger"
,
basename
,
"txt"
,
0
,
0
);
for
(
int
i
=
0
;
i
<
10
;
++
i
)
for
(
int
i
=
0
;
i
<
10
;
++
i
)
logger
->
info
(
"Test message {}"
,
i
);
logger
->
info
(
"Test message {}"
,
i
);
logger
->
flush
();
auto
filename
=
w
.
str
();
auto
filename
=
w
.
str
();
REQUIRE
(
count_lines
(
filename
)
==
10
);
REQUIRE
(
count_lines
(
filename
)
==
10
);
}
}
...
@@ -124,10 +140,11 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger_custom]]")
...
@@ -124,10 +140,11 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger_custom]]")
fmt
::
MemoryWriter
w
;
fmt
::
MemoryWriter
w
;
w
.
write
(
"{}{:04d}{:02d}{:02d}.txt"
,
basename
,
tm
.
tm_year
+
1900
,
tm
.
tm_mon
+
1
,
tm
.
tm_mday
);
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
);
auto
logger
=
spdlog
::
create
<
sink_type
>
(
"logger"
,
basename
,
"txt"
,
0
,
0
);
for
(
int
i
=
0
;
i
<
10
;
++
i
)
for
(
int
i
=
0
;
i
<
10
;
++
i
)
logger
->
info
(
"Test message {}"
,
i
);
logger
->
info
(
"Test message {}"
,
i
);
logger
->
flush
();
auto
filename
=
w
.
str
();
auto
filename
=
w
.
str
();
REQUIRE
(
count_lines
(
filename
)
==
10
);
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