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
f5939f9e
Commit
f5939f9e
authored
Dec 01, 2017
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
astyle
parent
84e30752
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
112 additions
and
112 deletions
+112
-112
example/example.cpp
example/example.cpp
+2
-2
include/spdlog/details/file_helper.h
include/spdlog/details/file_helper.h
+20
-20
include/spdlog/details/pattern_formatter_impl.h
include/spdlog/details/pattern_formatter_impl.h
+1
-1
include/spdlog/fmt/bundled/format.h
include/spdlog/fmt/bundled/format.h
+6
-6
include/spdlog/logger.h
include/spdlog/logger.h
+1
-1
include/spdlog/sinks/file_sinks.h
include/spdlog/sinks/file_sinks.h
+23
-23
tests/file_helper.cpp
tests/file_helper.cpp
+37
-37
tests/file_log.cpp
tests/file_log.cpp
+22
-22
No files found.
example/example.cpp
View file @
f5939f9e
//
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
...
...
@@ -50,7 +50,7 @@ int main(int, char*[])
auto
rotating_logger
=
spd
::
rotating_logger_mt
(
"some_logger_name"
,
"logs/rotating.txt"
,
1048576
*
5
,
3
);
for
(
int
i
=
0
;
i
<
10
;
++
i
)
rotating_logger
->
info
(
"{} * {} equals {:>10}"
,
i
,
i
,
i
*
i
);
// Create a daily logger - a new file is created every day on 2:30am
auto
daily_logger
=
spd
::
daily_logger_mt
(
"daily_logger"
,
"logs/daily.txt"
,
2
,
30
);
// trigger flush if the log severity is error or higher
...
...
include/spdlog/details/file_helper.h
View file @
f5939f9e
...
...
@@ -108,26 +108,26 @@ public:
return
os
::
file_exists
(
fname
);
}
//
// return basename and extension:
//
// "mylog.txt" => ("mylog", ".txt")
// "mylog" => ("mylog", "")
//
// the starting dot in filenames is ignored (hidden files):
//
// "my_folder/.mylog" => ("my_folder/.mylog")
// "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt")
static
std
::
tuple
<
filename_t
,
filename_t
>
split_by_extenstion
(
const
filename_t
&
fname
)
{
auto
index
=
fname
.
rfind
(
'.'
);
bool
found_ext
=
index
!=
filename_t
::
npos
&&
index
!=
0
&&
fname
[
index
-
1
]
!=
details
::
os
::
folder_sep
;
if
(
found_ext
)
return
std
::
make_tuple
(
fname
.
substr
(
0
,
index
),
fname
.
substr
(
index
));
else
return
std
::
make_tuple
(
fname
,
filename_t
());
}
//
// return basename and extension:
//
// "mylog.txt" => ("mylog", ".txt")
// "mylog" => ("mylog", "")
//
// the starting dot in filenames is ignored (hidden files):
//
// "my_folder/.mylog" => ("my_folder/.mylog")
// "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt")
static
std
::
tuple
<
filename_t
,
filename_t
>
split_by_extenstion
(
const
filename_t
&
fname
)
{
auto
index
=
fname
.
rfind
(
'.'
);
bool
found_ext
=
index
!=
filename_t
::
npos
&&
index
!=
0
&&
fname
[
index
-
1
]
!=
details
::
os
::
folder_sep
;
if
(
found_ext
)
return
std
::
make_tuple
(
fname
.
substr
(
0
,
index
),
fname
.
substr
(
index
));
else
return
std
::
make_tuple
(
fname
,
filename_t
());
}
private:
FILE
*
_fd
;
...
...
include/spdlog/details/pattern_formatter_impl.h
View file @
f5939f9e
...
...
@@ -526,7 +526,7 @@ inline void spdlog::pattern_formatter::handle_flag(char flag)
{
switch
(
flag
)
{
// logger name
// logger name
case
'n'
:
_formatters
.
push_back
(
std
::
unique_ptr
<
details
::
flag_formatter
>
(
new
details
::
name_formatter
()));
break
;
...
...
include/spdlog/fmt/bundled/format.h
View file @
f5939f9e
...
...
@@ -488,7 +488,7 @@ template <typename Impl, typename Char, typename Spec = fmt::FormatSpec>
class
BasicPrintfArgFormatter
;
template
<
typename
CharType
,
typename
ArgFormatter
=
fmt
::
ArgFormatter
<
CharType
>
>
typename
ArgFormatter
=
fmt
::
ArgFormatter
<
CharType
>
>
class
BasicFormatter
;
/**
...
...
@@ -2618,15 +2618,15 @@ inline uint64_t make_type(const T &arg)
}
template
<
std
::
size_t
N
,
bool
/*IsPacked*/
=
(
N
<
ArgList
::
MAX_PACKED_ARGS
)>
struct
ArgArray
;
struct
ArgArray
;
template
<
std
::
size_t
N
>
struct
ArgArray
<
N
,
true
/*IsPacked*/
>
{
typedef
Value
Type
[
N
>
0
?
N
:
1
];
template
<
typename
Formatter
,
typename
T
>
static
Value
make
(
const
T
&
value
)
template
<
typename
Formatter
,
typename
T
>
static
Value
make
(
const
T
&
value
)
{
#ifdef __clang__
Value
result
=
MakeValue
<
Formatter
>
(
value
);
...
...
@@ -2638,7 +2638,7 @@ struct ArgArray<N, true/*IsPacked*/>
return
MakeValue
<
Formatter
>
(
value
);
#endif
}
};
};
template
<
std
::
size_t
N
>
struct
ArgArray
<
N
,
false
/*IsPacked*/
>
...
...
@@ -3473,7 +3473,7 @@ void BasicWriter<Char>::write_double(T value, const Spec &spec)
// MSVC's printf doesn't support 'F'.
type
=
'f'
;
#endif
// Fall through.
// Fall through.
case
'E'
:
case
'G'
:
case
'A'
:
...
...
include/spdlog/logger.h
View file @
f5939f9e
...
...
@@ -63,7 +63,7 @@ public:
template
<
typename
T
>
void
warn
(
const
T
&
);
template
<
typename
T
>
void
error
(
const
T
&
);
template
<
typename
T
>
void
critical
(
const
T
&
);
bool
should_log
(
level
::
level_enum
)
const
;
void
set_level
(
level
::
level_enum
);
level
::
level_enum
level
()
const
;
...
...
include/spdlog/sinks/file_sinks.h
View file @
f5939f9e
...
...
@@ -77,23 +77,23 @@ public:
_current_size
=
_file_helper
.
size
();
//expensive. called only once
}
// calc filename according to index and file extension if exists.
// e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt".
static
filename_t
calc_filename
(
const
filename_t
&
filename
,
std
::
size_t
index
)
{
std
::
conditional
<
std
::
is_same
<
filename_t
::
value_type
,
char
>::
value
,
fmt
::
MemoryWriter
,
fmt
::
WMemoryWriter
>::
type
w
;
if
(
index
)
{
filename_t
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
details
::
file_helper
::
split_by_extenstion
(
filename
);
w
.
write
(
SPDLOG_FILENAME_T
(
"{}.{}{}"
),
basename
,
index
,
ext
);
}
else
{
w
.
write
(
SPDLOG_FILENAME_T
(
"{}"
),
filename
);
}
return
w
.
str
();
}
// calc filename according to index and file extension if exists.
// e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt".
static
filename_t
calc_filename
(
const
filename_t
&
filename
,
std
::
size_t
index
)
{
std
::
conditional
<
std
::
is_same
<
filename_t
::
value_type
,
char
>::
value
,
fmt
::
MemoryWriter
,
fmt
::
WMemoryWriter
>::
type
w
;
if
(
index
)
{
filename_t
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
details
::
file_helper
::
split_by_extenstion
(
filename
);
w
.
write
(
SPDLOG_FILENAME_T
(
"{}.{}{}"
),
basename
,
index
,
ext
);
}
else
{
w
.
write
(
SPDLOG_FILENAME_T
(
"{}"
),
filename
);
}
return
w
.
str
();
}
protected:
void
_sink_it
(
const
details
::
log_msg
&
msg
)
override
...
...
@@ -111,9 +111,9 @@ protected:
{
_file_helper
.
flush
();
}
private:
private:
// Rotate files:
// log.txt -> log.1.txt
// log.1.txt -> log.2.txt
...
...
@@ -161,8 +161,8 @@ struct default_daily_file_name_calculator
static
filename_t
calc_filename
(
const
filename_t
&
filename
)
{
std
::
tm
tm
=
spdlog
::
details
::
os
::
localtime
();
filename_t
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
details
::
file_helper
::
split_by_extenstion
(
filename
);
filename_t
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
details
::
file_helper
::
split_by_extenstion
(
filename
);
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
,
ext
);
return
w
.
str
();
...
...
@@ -178,8 +178,8 @@ struct dateonly_daily_file_name_calculator
static
filename_t
calc_filename
(
const
filename_t
&
filename
)
{
std
::
tm
tm
=
spdlog
::
details
::
os
::
localtime
();
filename_t
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
details
::
file_helper
::
split_by_extenstion
(
filename
);
filename_t
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
details
::
file_helper
::
split_by_extenstion
(
filename
);
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
,
ext
);
return
w
.
str
();
...
...
tests/file_helper.cpp
View file @
f5939f9e
...
...
@@ -75,73 +75,73 @@ TEST_CASE("file_helper_reopen2", "[file_helper::reopen(false)]]")
TEST_CASE
(
"file_helper_split_by_extenstion"
,
"[file_helper::split_by_extenstion()]]"
)
{
std
::
string
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
file_helper
::
split_by_extenstion
(
"mylog.txt"
);
REQUIRE
(
basename
==
"mylog"
);
REQUIRE
(
ext
==
".txt"
);
std
::
string
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
file_helper
::
split_by_extenstion
(
"mylog.txt"
);
REQUIRE
(
basename
==
"mylog"
);
REQUIRE
(
ext
==
".txt"
);
}
TEST_CASE
(
"file_helper_split_by_extenstion2"
,
"[file_helper::split_by_extenstion()]]"
)
{
std
::
string
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
file_helper
::
split_by_extenstion
(
"mylog"
);
REQUIRE
(
basename
==
"mylog"
);
REQUIRE
(
ext
==
""
);
std
::
string
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
file_helper
::
split_by_extenstion
(
"mylog"
);
REQUIRE
(
basename
==
"mylog"
);
REQUIRE
(
ext
==
""
);
}
TEST_CASE
(
"file_helper_split_by_extenstion3"
,
"[file_helper::split_by_extenstion()]]"
)
{
std
::
string
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
file_helper
::
split_by_extenstion
(
"mylog.xyz.txt"
);
REQUIRE
(
basename
==
"mylog.xyz"
);
REQUIRE
(
ext
==
".txt"
);
std
::
string
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
file_helper
::
split_by_extenstion
(
"mylog.xyz.txt"
);
REQUIRE
(
basename
==
"mylog.xyz"
);
REQUIRE
(
ext
==
".txt"
);
}
TEST_CASE
(
"file_helper_split_by_extenstion4"
,
"[file_helper::split_by_extenstion()]]"
)
{
std
::
string
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
file_helper
::
split_by_extenstion
(
"mylog.xyz....txt"
);
REQUIRE
(
basename
==
"mylog.xyz..."
);
REQUIRE
(
ext
==
".txt"
);
std
::
string
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
file_helper
::
split_by_extenstion
(
"mylog.xyz....txt"
);
REQUIRE
(
basename
==
"mylog.xyz..."
);
REQUIRE
(
ext
==
".txt"
);
}
TEST_CASE
(
"file_helper_split_by_extenstion5"
,
"[file_helper::split_by_extenstion(hidden_file)]]"
)
{
std
::
string
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
file_helper
::
split_by_extenstion
(
".mylog"
);
REQUIRE
(
basename
==
".mylog"
);
REQUIRE
(
ext
==
""
);
std
::
string
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
file_helper
::
split_by_extenstion
(
".mylog"
);
REQUIRE
(
basename
==
".mylog"
);
REQUIRE
(
ext
==
""
);
}
TEST_CASE
(
"file_helper_split_by_extenstion6"
,
"[file_helper::split_by_extenstion(hidden_file)]]"
)
{
{
#ifdef _WIN32
auto
filename
=
"folder
\\
.mylog"
;
auto
expected_basename
=
"folder
\\
.mylog"
;
auto
filename
=
"folder
\\
.mylog"
;
auto
expected_basename
=
"folder
\\
.mylog"
;
#else
auto
filename
=
"folder/.mylog"
;
auto
expected_basename
=
"folder/.mylog"
;
auto
filename
=
"folder/.mylog"
;
auto
expected_basename
=
"folder/.mylog"
;
#endif
std
::
string
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
file_helper
::
split_by_extenstion
(
filename
);
REQUIRE
(
basename
==
expected_basename
);
REQUIRE
(
ext
==
""
);
std
::
string
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
file_helper
::
split_by_extenstion
(
filename
);
REQUIRE
(
basename
==
expected_basename
);
REQUIRE
(
ext
==
""
);
}
TEST_CASE
(
"file_helper_split_by_extenstion7"
,
"[file_helper::split_by_extenstion(hidden_file)]]"
)
{
#ifdef _WIN32
auto
filename
=
"folder
\\
.mylog.txt"
;
auto
expected_basename
=
"folder
\\
.mylog"
;
auto
filename
=
"folder
\\
.mylog.txt"
;
auto
expected_basename
=
"folder
\\
.mylog"
;
#else
auto
filename
=
"folder/.mylog.txt"
;
auto
expected_basename
=
"folder/.mylog"
;
auto
filename
=
"folder/.mylog.txt"
;
auto
expected_basename
=
"folder/.mylog"
;
#endif
std
::
string
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
file_helper
::
split_by_extenstion
(
filename
);
REQUIRE
(
basename
==
expected_basename
);
REQUIRE
(
ext
==
".txt"
);
std
::
string
basename
,
ext
;
std
::
tie
(
basename
,
ext
)
=
file_helper
::
split_by_extenstion
(
filename
);
REQUIRE
(
basename
==
expected_basename
);
REQUIRE
(
ext
==
".txt"
);
}
...
...
tests/file_log.cpp
View file @
f5939f9e
...
...
@@ -195,48 +195,48 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger_custom]]")
TEST_CASE
(
"rotating_file_sink::calc_filename1"
,
"[rotating_file_sink]]"
)
{
auto
filename
=
spdlog
::
sinks
::
rotating_file_sink_st
::
calc_filename
(
"rotated.txt"
,
3
);
REQUIRE
(
filename
==
"rotated.3.txt"
);
auto
filename
=
spdlog
::
sinks
::
rotating_file_sink_st
::
calc_filename
(
"rotated.txt"
,
3
);
REQUIRE
(
filename
==
"rotated.3.txt"
);
}
TEST_CASE
(
"rotating_file_sink::calc_filename2"
,
"[rotating_file_sink]]"
)
{
auto
filename
=
spdlog
::
sinks
::
rotating_file_sink_st
::
calc_filename
(
"rotated"
,
3
);
REQUIRE
(
filename
==
"rotated.3"
);
auto
filename
=
spdlog
::
sinks
::
rotating_file_sink_st
::
calc_filename
(
"rotated"
,
3
);
REQUIRE
(
filename
==
"rotated.3"
);
}
TEST_CASE
(
"rotating_file_sink::calc_filename3"
,
"[rotating_file_sink]]"
)
{
auto
filename
=
spdlog
::
sinks
::
rotating_file_sink_st
::
calc_filename
(
"rotated.txt"
,
0
);
REQUIRE
(
filename
==
"rotated.txt"
);
auto
filename
=
spdlog
::
sinks
::
rotating_file_sink_st
::
calc_filename
(
"rotated.txt"
,
0
);
REQUIRE
(
filename
==
"rotated.txt"
);
}
TEST_CASE
(
"daily_file_sink::default_daily_file_name_calculator1"
,
"[daily_file_sink]]"
)
{
// daily.YYYY-MM-DD_hh-mm.txt
auto
filename
=
spdlog
::
sinks
::
default_daily_file_name_calculator
::
calc_filename
(
"daily.txt"
);
std
::
regex
re
(
R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])_\d\d-[0-5][0-9].txt$)"
);
std
::
smatch
match
;
REQUIRE
(
std
::
regex_match
(
filename
,
match
,
re
));
// daily.YYYY-MM-DD_hh-mm.txt
auto
filename
=
spdlog
::
sinks
::
default_daily_file_name_calculator
::
calc_filename
(
"daily.txt"
);
std
::
regex
re
(
R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])_\d\d-[0-5][0-9].txt$)"
);
std
::
smatch
match
;
REQUIRE
(
std
::
regex_match
(
filename
,
match
,
re
));
}
TEST_CASE
(
"daily_file_sink::default_daily_file_name_calculator2"
,
"[daily_file_sink]]"
)
{
// daily.YYYY-MM-DD_hh-mm.txt
auto
filename
=
spdlog
::
sinks
::
default_daily_file_name_calculator
::
calc_filename
(
"daily"
);
std
::
regex
re
(
R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])_\d\d-[0-5][0-9]$)"
);
std
::
smatch
match
;
REQUIRE
(
std
::
regex_match
(
filename
,
match
,
re
));
// daily.YYYY-MM-DD_hh-mm.txt
auto
filename
=
spdlog
::
sinks
::
default_daily_file_name_calculator
::
calc_filename
(
"daily"
);
std
::
regex
re
(
R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])_\d\d-[0-5][0-9]$)"
);
std
::
smatch
match
;
REQUIRE
(
std
::
regex_match
(
filename
,
match
,
re
));
}
TEST_CASE
(
"daily_file_sink::dateonly_daily_file_name_calculator"
,
"[daily_file_sink]]"
)
{
// daily.YYYY-MM-DD_hh-mm.txt
auto
filename
=
spdlog
::
sinks
::
dateonly_daily_file_name_calculator
::
calc_filename
(
"daily.txt"
);
// date regex based on https://www.regular-expressions.info/dates.html
std
::
regex
re
(
R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\.txt$)"
);
std
::
smatch
match
;
REQUIRE
(
std
::
regex_match
(
filename
,
match
,
re
));
// daily.YYYY-MM-DD_hh-mm.txt
auto
filename
=
spdlog
::
sinks
::
dateonly_daily_file_name_calculator
::
calc_filename
(
"daily.txt"
);
// date regex based on https://www.regular-expressions.info/dates.html
std
::
regex
re
(
R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\.txt$)"
);
std
::
smatch
match
;
REQUIRE
(
std
::
regex_match
(
filename
,
match
,
re
));
}
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