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
a776a774
Commit
a776a774
authored
Jul 09, 2018
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cache millis in full_formatter
parent
374a22b4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
36 deletions
+60
-36
fmt_helper.h
include/spdlog/details/fmt_helper.h
+15
-9
pattern_formatter.h
include/spdlog/details/pattern_formatter.h
+45
-27
No files found.
include/spdlog/details/fmt_helper.h
View file @
a776a774
...
...
@@ -8,13 +8,16 @@
namespace
spdlog
{
namespace
details
{
namespace
fmt_helper
{
inline
void
append_str
(
const
std
::
string
&
str
,
fmt
::
memory_buffer
&
dest
)
template
<
size_t
Buffer_Size
>
inline
void
append_str
(
const
std
::
string
&
str
,
fmt
::
basic_memory_buffer
<
char
,
Buffer_Size
>
&
dest
)
{
auto
*
str_ptr
=
str
.
data
();
dest
.
append
(
str_ptr
,
str_ptr
+
str
.
size
());
}
inline
void
append_c_str
(
const
char
*
c_str
,
fmt
::
memory_buffer
&
dest
)
template
<
size_t
Buffer_Size
>
inline
void
append_c_str
(
const
char
*
c_str
,
fmt
::
basic_memory_buffer
<
char
,
Buffer_Size
>
&
dest
)
{
char
ch
;
while
((
ch
=
*
c_str
)
!=
'\0'
)
...
...
@@ -24,21 +27,22 @@ inline void append_c_str(const char *c_str, fmt::memory_buffer &dest)
}
}
template
<
size_t
N1
,
size_t
N
2
>
inline
void
append_buf
(
const
fmt
::
basic_memory_buffer
<
char
,
N1
>
&
buf
,
fmt
::
basic_memory_buffer
<
char
,
N
2
>
&
dest
)
template
<
size_t
Buffer_Size1
,
size_t
Buffer_Size
2
>
inline
void
append_buf
(
const
fmt
::
basic_memory_buffer
<
char
,
Buffer_Size1
>
&
buf
,
fmt
::
basic_memory_buffer
<
char
,
Buffer_Size
2
>
&
dest
)
{
auto
*
buf_ptr
=
buf
.
data
();
dest
.
append
(
buf_ptr
,
buf_ptr
+
buf
.
size
());
}
template
<
typename
T
>
inline
void
append_int
(
T
n
,
fmt
::
memory_buffer
&
dest
)
template
<
typename
T
,
size_t
Buffer_Size
>
inline
void
append_int
(
T
n
,
fmt
::
basic_memory_buffer
<
char
,
Buffer_Size
>
&
dest
)
{
fmt
::
format_int
i
(
n
);
dest
.
append
(
i
.
data
(),
i
.
data
()
+
i
.
size
());
}
inline
void
pad2
(
int
n
,
fmt
::
memory_buffer
&
dest
)
template
<
size_t
Buffer_Size
>
inline
void
pad2
(
int
n
,
fmt
::
basic_memory_buffer
<
char
,
Buffer_Size
>
&
dest
)
{
if
(
n
>
99
)
{
...
...
@@ -61,7 +65,8 @@ inline void pad2(int n, fmt::memory_buffer &dest)
fmt
::
format_to
(
dest
,
"{:02}"
,
n
);
}
inline
void
pad3
(
int
n
,
fmt
::
memory_buffer
&
dest
)
template
<
size_t
Buffer_Size
>
inline
void
pad3
(
int
n
,
fmt
::
basic_memory_buffer
<
char
,
Buffer_Size
>
&
dest
)
{
if
(
n
>
99
)
{
...
...
@@ -86,7 +91,8 @@ inline void pad3(int n, fmt::memory_buffer &dest)
fmt
::
format_to
(
dest
,
"{:03}"
,
n
);
}
inline
void
pad6
(
size_t
n
,
fmt
::
memory_buffer
&
dest
)
template
<
size_t
Buffer_Size
>
inline
void
pad6
(
size_t
n
,
fmt
::
basic_memory_buffer
<
char
,
Buffer_Size
>
&
dest
)
{
// todo: maybe replace this implementation with
// pad3(n / 1000, dest);
...
...
include/spdlog/details/pattern_formatter.h
View file @
a776a774
...
...
@@ -120,7 +120,16 @@ class c_formatter SPDLOG_FINAL : public flag_formatter
{
void
format
(
const
details
::
log_msg
&
,
const
std
::
tm
&
tm_time
,
fmt
::
memory_buffer
&
dest
)
override
{
fmt
::
format_to
(
dest
,
"{} {} {} "
,
days
[
tm_time
.
tm_wday
],
months
[
tm_time
.
tm_mon
],
tm_time
.
tm_mday
);
//
//fmt::format_to(dest, "{} {} {} ", days[tm_time.tm_wday], months[tm_time.tm_mon], tm_time.tm_mday);
// date
fmt_helper
::
append_str
(
days
[
tm_time
.
tm_wday
],
dest
);
dest
.
push_back
(
' '
);
fmt_helper
::
append_str
(
months
[
tm_time
.
tm_mon
],
dest
);
dest
.
push_back
(
' '
);
fmt_helper
::
append_int
(
tm_time
.
tm_mday
,
dest
);
dest
.
push_back
(
' '
);
// time
fmt_helper
::
pad2
(
tm_time
.
tm_hour
,
dest
);
dest
.
push_back
(
':'
);
fmt_helper
::
pad2
(
tm_time
.
tm_min
,
dest
);
...
...
@@ -366,7 +375,6 @@ class t_formatter SPDLOG_FINAL : public flag_formatter
{
void
format
(
const
details
::
log_msg
&
msg
,
const
std
::
tm
&
,
fmt
::
memory_buffer
&
dest
)
override
{
fmt_helper
::
pad6
(
msg
.
thread_id
,
dest
);
}
};
...
...
@@ -385,7 +393,6 @@ class i_formatter SPDLOG_FINAL : public flag_formatter
{
void
format
(
const
details
::
log_msg
&
msg
,
const
std
::
tm
&
,
fmt
::
memory_buffer
&
dest
)
override
{
fmt_helper
::
pad6
(
msg
.
msg_id
,
dest
);
}
};
...
...
@@ -457,39 +464,48 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
{
#ifndef SPDLOG_NO_DATETIME
//
each second cache the header
//
cache the date/time part for the next second.
auto
duration
=
msg
.
time
.
time_since_epoch
();
auto
seconds
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
seconds
>
(
duration
).
count
();
if
(
cached_header_
.
size
()
==
0
||
cached_seconds_ts_
!=
seconds
)
std
::
chrono
::
seconds
seconds
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
seconds
>
(
duration
);
if
(
cache_timestamp_
!=
seconds
||
cached_datetime_
.
size
()
==
0
)
{
cached_
header
_
.
resize
(
0
);
cached_
header
_
.
push_back
(
'['
);
fmt_helper
::
append_int
(
tm_time
.
tm_year
+
1900
,
cached_
header
_
);
cached_
header
_
.
push_back
(
'-'
);
cached_
datetime
_
.
resize
(
0
);
cached_
datetime
_
.
push_back
(
'['
);
fmt_helper
::
append_int
(
tm_time
.
tm_year
+
1900
,
cached_
datetime
_
);
cached_
datetime
_
.
push_back
(
'-'
);
fmt_helper
::
pad2
(
tm_time
.
tm_mon
+
1
,
cached_
header
_
);
cached_
header
_
.
push_back
(
'-'
);
fmt_helper
::
pad2
(
tm_time
.
tm_mon
+
1
,
cached_
datetime
_
);
cached_
datetime
_
.
push_back
(
'-'
);
fmt_helper
::
pad2
(
tm_time
.
tm_mday
,
cached_
header
_
);
cached_
header
_
.
push_back
(
' '
);
fmt_helper
::
pad2
(
tm_time
.
tm_mday
,
cached_
datetime
_
);
cached_
datetime
_
.
push_back
(
' '
);
fmt_helper
::
pad2
(
tm_time
.
tm_hour
,
cached_
header
_
);
cached_
header
_
.
push_back
(
':'
);
fmt_helper
::
pad2
(
tm_time
.
tm_hour
,
cached_
datetime
_
);
cached_
datetime
_
.
push_back
(
':'
);
fmt_helper
::
pad2
(
tm_time
.
tm_min
,
cached_
header
_
);
cached_
header
_
.
push_back
(
':'
);
fmt_helper
::
pad2
(
tm_time
.
tm_min
,
cached_
datetime
_
);
cached_
datetime
_
.
push_back
(
':'
);
fmt_helper
::
pad2
(
tm_time
.
tm_sec
,
cached_
header
_
);
cached_
header
_
.
push_back
(
'.'
);
fmt_helper
::
pad2
(
tm_time
.
tm_sec
,
cached_
datetime
_
);
cached_
datetime
_
.
push_back
(
'.'
);
cache
d_seconds_ts
_
=
seconds
;
cache
_timestamp
_
=
seconds
;
}
fmt_helper
::
append_buf
(
cached_
header
_
,
dest
);
fmt_helper
::
append_buf
(
cached_
datetime
_
,
dest
);
// cache the millis part for the next milli.
auto
millis
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
milliseconds
>
(
duration
).
count
()
%
1000
;
fmt_helper
::
pad3
(
static_cast
<
int
>
(
millis
),
dest
);
dest
.
push_back
(
']'
);
dest
.
push_back
(
' '
);
if
(
millis
!=
millis_cache_timestamp_
||
cached_millis_
.
size
()
==
0
)
{
cached_millis_
.
resize
(
0
);
fmt_helper
::
pad3
(
millis
,
cached_millis_
);
cached_millis_
.
push_back
(
']'
);
cached_millis_
.
push_back
(
' '
);
millis_cache_timestamp_
=
millis
;
}
fmt_helper
::
append_buf
(
cached_millis_
,
dest
);
#else // no datetime needed
(
void
)
tm_time
;
#endif
...
...
@@ -512,8 +528,10 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
}
private
:
std
::
chrono
::
seconds
::
rep
cached_seconds_ts_
{
0
};
fmt
::
memory_buffer
cached_header_
;
std
::
chrono
::
seconds
cache_timestamp_
{
0
};
std
::
chrono
::
milliseconds
::
rep
millis_cache_timestamp_
{
0
};
fmt
::
basic_memory_buffer
<
char
,
128
>
cached_datetime_
;
fmt
::
basic_memory_buffer
<
char
,
8
>
cached_millis_
;
};
}
// namespace details
...
...
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