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
497d5117
Commit
497d5117
authored
May 09, 2014
by
gabi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
log_msg refactoring and raw message support
parent
bafea6a6
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
113 additions
and
109 deletions
+113
-109
example.cpp
example/example.cpp
+7
-9
line_logger.h
include/c11log/details/line_logger.h
+11
-17
log_msg.h
include/c11log/details/log_msg.h
+44
-45
os.h
include/c11log/details/os.h
+14
-6
formatter.h
include/c11log/formatter.h
+21
-17
logger.h
include/c11log/logger.h
+6
-6
async_sink.h
include/c11log/sinks/async_sink.h
+2
-2
console_sinks.h
include/c11log/sinks/console_sinks.h
+1
-1
file_sinks.h
include/c11log/sinks/file_sinks.h
+7
-6
No files found.
example/example.cpp
View file @
497d5117
...
@@ -17,16 +17,14 @@ using namespace utils;
...
@@ -17,16 +17,14 @@ using namespace utils;
int
main
(
int
argc
,
char
*
argv
[])
int
main
(
int
argc
,
char
*
argv
[])
{
{
const
unsigned
int
howmany
=
argc
<=
1
?
4000000
:
atoi
(
argv
[
1
]);
logger
cout_logger
(
""
,
sinks
::
stdout_sink
());
const
unsigned
int
howmany
=
argc
<=
1
?
1000000
:
atoi
(
argv
[
1
]);
cout_logger
.
set_min_level
(
c11log
::
level
::
TRACE
);
cout_logger
.
info
()
<<
"Hello "
<<
"man"
<<
123
;
logger
cout_logger
(
"example"
,
sinks
::
stdout_sink
());
cout_logger
.
info
()
<<
"Hello logger"
;
auto
fsink
=
std
::
make_shared
<
sinks
::
rotating_file_sink
>
(
"log"
,
"txt"
,
1024
*
1024
*
50
,
5
,
0
);
auto
nullsink
=
sinks
::
null_sink
::
get
();
auto
nullsink
=
sinks
::
null_sink
::
get
();
//auto fsink = std::make_shared<sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5, 0);
//auto as = std::make_shared<sinks::async_sink>(1000);
//auto as = std::make_shared<sinks::async_sink>(1000);
//as->add_sink(fsink);
//as->add_sink(fsink);
...
@@ -34,10 +32,10 @@ int main(int argc, char* argv[])
...
@@ -34,10 +32,10 @@ int main(int argc, char* argv[])
auto
start
=
system_clock
::
now
();
auto
start
=
system_clock
::
now
();
for
(
unsigned
int
i
=
1
;
i
<=
howmany
;
++
i
)
for
(
unsigned
int
i
=
1
;
i
<=
howmany
;
++
i
)
my_logger
.
info
(
"Hello logger:
"
)
<<
4.5
<<
'\t'
<<
i
<<
"
\t
asdasd:"
<<
123
<<
'f'
;
my_logger
.
info
(
"Hello logger:
msg #"
)
<<
i
<<
" ??"
;
//as->shutdown(std::chrono::milliseconds(15000));
//as->shutdown(std::chrono::milliseconds(15000));
auto
delta
=
system_clock
::
now
()
-
start
;
auto
delta
=
system_clock
::
now
()
-
start
;
auto
delta_d
=
duration_cast
<
duration
<
double
>>
(
delta
).
count
();
auto
delta_d
=
duration_cast
<
duration
<
double
>>
(
delta
).
count
();
...
...
include/c11log/details/line_logger.h
View file @
497d5117
...
@@ -26,12 +26,7 @@ public:
...
@@ -26,12 +26,7 @@ public:
{
{
if
(
enabled
)
if
(
enabled
)
{
{
_log_msg
.
msg_time
=
log_clock
::
now
();
_log_msg
.
time
=
log_clock
::
now
();
callback_logger
->
_formatter
->
format_header
(
callback_logger
->
_logger_name
,
_log_msg
.
msg_level
,
_log_msg
.
msg_time
,
_oss
);
_log_msg
.
msg_header_size
=
_oss
.
str
().
size
();
}
}
}
}
...
@@ -46,10 +41,10 @@ public:
...
@@ -46,10 +41,10 @@ public:
_log_msg
(
std
::
move
(
other
.
_log_msg
)),
_log_msg
(
std
::
move
(
other
.
_log_msg
)),
_oss
(
std
::
move
(
other
.
_oss
.
str
())),
_oss
(
std
::
move
(
other
.
_oss
.
str
())),
_enabled
(
other
.
_enabled
),
_enabled
(
other
.
_enabled
),
_empty
(
other
.
_empty
)
_empty
(
other
.
_empty
)
{
{
other
.
disable
();
other
.
disable
();
}
}
...
@@ -58,8 +53,7 @@ public:
...
@@ -58,8 +53,7 @@ public:
//only if enabled and not empty
//only if enabled and not empty
if
(
_enabled
&&
!
_empty
)
if
(
_enabled
&&
!
_empty
)
{
{
_oss
<<
os
::
eol
();
_log_msg
.
raw
=
_oss
.
str
();
_log_msg
.
str
=
_oss
.
str
();
_callback_logger
->
_log_it
(
_log_msg
);
_callback_logger
->
_log_it
(
_log_msg
);
}
}
}
}
...
@@ -81,10 +75,10 @@ public:
...
@@ -81,10 +75,10 @@ public:
return
*
this
;
return
*
this
;
}
}
void
disable
()
void
disable
()
{
{
_enabled
=
false
;
_enabled
=
false
;
}
}
...
@@ -92,7 +86,7 @@ private:
...
@@ -92,7 +86,7 @@ private:
logger
*
_callback_logger
;
logger
*
_callback_logger
;
log_msg
_log_msg
;
log_msg
_log_msg
;
//details::stack_oss _oss;
//details::stack_oss _oss;
std
::
ostringstream
_oss
;
std
::
ostringstream
_oss
;
bool
_enabled
;
bool
_enabled
;
bool
_empty
;
bool
_empty
;
};
};
...
...
include/c11log/details/log_msg.h
View file @
497d5117
...
@@ -8,51 +8,50 @@ struct log_msg
...
@@ -8,51 +8,50 @@ struct log_msg
{
{
log_msg
()
=
default
;
log_msg
()
=
default
;
log_msg
(
level
::
level_enum
l
)
:
log_msg
(
level
::
level_enum
l
)
:
msg_level
(
l
),
level
(
l
),
msg_time
(),
time
(),
msg_header_size
(
0
),
raw
(),
str
()
{}
formatted
()
{}
log_msg
(
const
log_msg
&
other
)
:
log_msg
(
const
log_msg
&
other
)
:
msg_level
(
other
.
msg_level
),
level
(
other
.
level
),
msg_time
(
other
.
msg_time
),
time
(
other
.
time
),
msg_header_size
(
other
.
msg_header_size
),
raw
(
other
.
raw
),
str
(
other
.
str
)
{}
formatted
(
other
.
formatted
)
{}
log_msg
(
log_msg
&&
other
)
:
log_msg
()
log_msg
(
log_msg
&&
other
)
{
{
swap
(
*
this
,
other
);
swap
(
*
this
,
other
);
}
}
friend
void
swap
(
log_msg
&
l
,
log_msg
&
r
)
friend
void
swap
(
log_msg
&
l
,
log_msg
&
r
)
{
{
using
std
::
swap
;
using
std
::
swap
;
swap
(
l
.
msg_level
,
r
.
msg_level
);
swap
(
l
.
level
,
r
.
level
);
swap
(
l
.
msg_time
,
r
.
msg_time
);
swap
(
l
.
time
,
r
.
time
);
swap
(
l
.
msg_header_size
,
r
.
msg_header_size
);
swap
(
l
.
raw
,
r
.
raw
);
swap
(
l
.
str
,
r
.
str
);
swap
(
l
.
formatted
,
r
.
formatted
);
}
}
log_msg
&
operator
=
(
log_msg
other
)
log_msg
&
operator
=
(
log_msg
other
)
{
{
swap
(
*
this
,
other
);
swap
(
*
this
,
other
);
return
*
this
;
return
*
this
;
}
}
void
clear
()
void
clear
()
{
{
raw
.
clear
();
msg_header_size
=
0
;
formatted
.
clear
();
str
.
clear
();
}
}
level
::
level_enum
level
;
level
::
level_enum
msg_level
;
log_clock
::
time_point
time
;
log_clock
::
time_point
msg_time
;
std
::
string
raw
;
std
::
size_t
msg_header_size
;
std
::
string
formatted
;
std
::
string
str
;
};
};
}
}
...
...
include/c11log/details/os.h
View file @
497d5117
...
@@ -44,23 +44,31 @@ inline bool operator!=(const std::tm& tm1, const std::tm& tm2)
...
@@ -44,23 +44,31 @@ inline bool operator!=(const std::tm& tm1, const std::tm& tm2)
return
!
(
tm1
==
tm2
);
return
!
(
tm1
==
tm2
);
}
}
constexpr
inline
const
char
*
eol
()
{
#ifdef _WIN32
#ifdef _WIN32
inline
const
char
*
eol
()
{
return
"
\r\n
"
;
return
"
\r\n
"
;
}
#else
#else
constexpr
inline
const
char
*
eol
()
{
return
"
\n
"
;
return
"
\n
"
;
#endif
}
}
#endif
constexpr
inline
unsigned
short
eol_size
()
{
#ifdef _WIN32
#ifdef _WIN32
inline
unsigned
short
eol_size
()
{
return
2
;
return
2
;
}
#else
#else
constexpr
inline
unsigned
short
eol_size
()
{
return
1
;
return
1
;
#endif
}
}
#endif
}
//os
}
//os
}
//details
}
//details
}
//c11log
}
//c11log
...
...
include/c11log/formatter.h
View file @
497d5117
...
@@ -2,14 +2,15 @@
...
@@ -2,14 +2,15 @@
#include <string>
#include <string>
#include <chrono>
#include <chrono>
#include <functional>
#include <iomanip>
#include <iomanip>
#include <thread>
#include <thread>
#include <cstring>
#include <cstring>
#include <sstream>
#include <sstream>
#include "common_types.h"
#include "common_types.h"
#include "details/os.h"
#include "details/os.h"
#include "details/log_msg.h"
namespace
c11log
namespace
c11log
...
@@ -20,7 +21,7 @@ namespace formatters
...
@@ -20,7 +21,7 @@ namespace formatters
class
formatter
class
formatter
{
{
public
:
public
:
virtual
void
format
_header
(
const
std
::
string
&
logger_name
,
level
::
level_enum
level
,
const
log_clock
::
time_point
&
tp
,
std
::
ostream
&
output
)
=
0
;
virtual
void
format
(
const
std
::
string
&
logger_name
,
details
::
log_msg
&
msg
)
=
0
;
};
};
...
@@ -28,17 +29,20 @@ class default_formatter: public formatter
...
@@ -28,17 +29,20 @@ class default_formatter: public formatter
{
{
public
:
public
:
// Format: [2013-12-29 01:04:42.900] [logger_name:Info] Message body
// Format: [2013-12-29 01:04:42.900] [logger_name:Info] Message body
void
format
_header
(
const
std
::
string
&
logger_name
,
level
::
level_enum
level
,
const
log_clock
::
time_point
&
tp
,
std
::
ostream
&
output
)
override
void
format
(
const
std
::
string
&
logger_name
,
details
::
log_msg
&
msg
)
override
{
{
_format_time
(
tp
,
output
);
std
::
ostringstream
oss
;
//Format datetime like this:[2014 - 03 - 14 17:15 : 22]
_format_time
(
msg
.
time
,
oss
);
if
(
!
logger_name
.
empty
())
if
(
!
logger_name
.
empty
())
o
utput
<<
" ["
<<
logger_name
<<
':'
<<
c11log
::
level
::
to_str
(
level
)
<<
"] "
;
o
ss
<<
" ["
<<
logger_name
<<
':'
<<
c11log
::
level
::
to_str
(
msg
.
level
)
<<
"] "
;
else
else
output
<<
" ["
<<
c11log
::
level
::
to_str
(
level
)
<<
"] "
;
oss
<<
" ["
<<
c11log
::
level
::
to_str
(
msg
.
level
)
<<
"] "
;
oss
<<
msg
.
raw
<<
details
::
os
::
eol
();
msg
.
formatted
=
oss
.
str
();
}
}
private
:
private
:
void
_format_time
(
const
log_clock
::
time_point
&
tp
,
std
::
ostream
&
output
);
void
_format_time
(
const
log_clock
::
time_point
&
tp
,
std
::
ostream
&
output
);
};
};
...
@@ -52,12 +56,13 @@ inline void c11log::formatters::default_formatter::_format_time(const log_clock:
...
@@ -52,12 +56,13 @@ inline void c11log::formatters::default_formatter::_format_time(const log_clock:
using
namespace
c11log
::
details
::
os
;
using
namespace
c11log
::
details
::
os
;
using
namespace
std
::
chrono
;
using
namespace
std
::
chrono
;
#ifdef _WIN32 //VS2013 doesn't support yet thread_local keyword
#ifdef _WIN32 //VS2013 doesn't support yet thread_local keyword
__declspec
(
thread
)
static
char
s_cache_
str
[
64
];
__declspec
(
thread
)
static
char
s_cache_
timestr
[
128
];
__declspec
(
thread
)
static
size_t
s_cache_size
;
__declspec
(
thread
)
static
int
s_cache_timesize
=
0
;
__declspec
(
thread
)
static
std
::
time_t
s_cache_time_t
=
0
;
__declspec
(
thread
)
static
std
::
time_t
s_cache_time_t
=
0
;
#else
#else
thread_local
static
std
::
string
s_cache_timestr
;
thread_local
static
char
s_cache_timestr
[
128
];
thread_local
static
int
s_cache_timesize
=
0
;
thread_local
static
std
::
time_t
s_cache_time_t
=
0
;
thread_local
static
std
::
time_t
s_cache_time_t
=
0
;
#endif
#endif
...
@@ -81,10 +86,9 @@ inline void c11log::formatters::default_formatter::_format_time(const log_clock:
...
@@ -81,10 +86,9 @@ inline void c11log::formatters::default_formatter::_format_time(const log_clock:
time_oss
<<
tm_now
.
tm_sec
<<
']'
;
time_oss
<<
tm_now
.
tm_sec
<<
']'
;
//Cache the resulted string and its size
//Cache the resulted string and its size
s_cache_time_t
=
tp_time_t
;
s_cache_time_t
=
tp_time_t
;
//const std::string &s = time_oss.str_ref();
const
std
::
string
s
=
time_oss
.
str
();
s_cache_timestr
=
time_oss
.
str
();
s_cache_timesize
=
s
.
size
();
std
::
memcpy
(
s_cache_timestr
,
s
.
c_str
(),
s_cache_timesize
);
}
}
output
<<
s_cache_timestr
;
output
.
write
(
s_cache_timestr
,
s_cache_timesize
)
;
}
}
include/c11log/logger.h
View file @
497d5117
...
@@ -27,11 +27,11 @@ class logger
...
@@ -27,11 +27,11 @@ class logger
{
{
public
:
public
:
using
sink_ptr
=
std
::
shared_ptr
<
sinks
::
base_sink
>
;
using
sink_ptr
=
std
::
shared_ptr
<
sinks
::
base_sink
>
;
using
sinks_vector_t
=
std
::
vector
<
sink_ptr
>
;
using
sinks_vector_t
=
std
::
vector
<
sink_ptr
>
;
using
sinks_init_list
=
std
::
initializer_list
<
sink_ptr
>
;
using
sinks_init_list
=
std
::
initializer_list
<
sink_ptr
>
;
using
formatter_ptr
=
std
::
shared_ptr
<
c11log
::
formatters
::
formatter
>
;
using
formatter_ptr
=
std
::
shared_ptr
<
c11log
::
formatters
::
formatter
>
;
logger
(
const
std
::
string
&
name
,
sinks_init_list
,
formatter_ptr
=
nullptr
);
logger
(
const
std
::
string
&
name
,
sinks_init_list
,
formatter_ptr
=
nullptr
);
logger
(
const
std
::
string
&
name
,
sink_ptr
,
formatter_ptr
=
nullptr
);
logger
(
const
std
::
string
&
name
,
sink_ptr
,
formatter_ptr
=
nullptr
);
...
@@ -75,8 +75,7 @@ private:
...
@@ -75,8 +75,7 @@ private:
sinks_vector_t
_sinks
;
sinks_vector_t
_sinks
;
std
::
atomic_int
_min_level
;
std
::
atomic_int
_min_level
;
void
_log_it
(
const
details
::
log_msg
&
msg
);
void
_log_it
(
details
::
log_msg
&
msg
);
};
};
...
@@ -208,8 +207,9 @@ inline bool c11log::logger::should_log(c11log::level::level_enum level) const
...
@@ -208,8 +207,9 @@ inline bool c11log::logger::should_log(c11log::level::level_enum level) const
return
level
>=
_min_level
.
load
();
return
level
>=
_min_level
.
load
();
}
}
inline
void
c11log
::
logger
::
_log_it
(
const
details
::
log_msg
&
msg
)
inline
void
c11log
::
logger
::
_log_it
(
details
::
log_msg
&
msg
)
{
{
_formatter
->
format
(
_logger_name
,
msg
);
for
(
auto
&
sink
:
_sinks
)
for
(
auto
&
sink
:
_sinks
)
sink
->
log
(
msg
);
sink
->
log
(
msg
);
}
}
...
...
include/c11log/sinks/async_sink.h
View file @
497d5117
...
@@ -69,8 +69,8 @@ inline c11log::sinks::async_sink::~async_sink()
...
@@ -69,8 +69,8 @@ inline c11log::sinks::async_sink::~async_sink()
inline
void
c11log
::
sinks
::
async_sink
::
_sink_it
(
const
details
::
log_msg
&
msg
)
inline
void
c11log
::
sinks
::
async_sink
::
_sink_it
(
const
details
::
log_msg
&
msg
)
{
{
if
(
!
_active
||
msg
.
str
.
empty
())
if
(
!
_active
||
msg
.
formatted
.
empty
())
return
;
return
;
_q
.
push
(
msg
);
_q
.
push
(
msg
);
}
}
...
...
include/c11log/sinks/console_sinks.h
View file @
497d5117
...
@@ -23,7 +23,7 @@ protected:
...
@@ -23,7 +23,7 @@ protected:
virtual
void
_sink_it
(
const
details
::
log_msg
&
msg
)
override
virtual
void
_sink_it
(
const
details
::
log_msg
&
msg
)
override
{
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
_mutex
);
std
::
lock_guard
<
std
::
mutex
>
lock
(
_mutex
);
_ostream
<<
msg
.
str
;
_ostream
<<
msg
.
formatted
;
}
}
std
::
ostream
&
_ostream
;
std
::
ostream
&
_ostream
;
...
...
include/c11log/sinks/file_sinks.h
View file @
497d5117
...
@@ -8,6 +8,7 @@
...
@@ -8,6 +8,7 @@
#include "../details/flush_helper.h"
#include "../details/flush_helper.h"
#include "../details/blocking_queue.h"
#include "../details/blocking_queue.h"
namespace
c11log
namespace
c11log
{
{
namespace
sinks
namespace
sinks
...
@@ -31,7 +32,7 @@ protected:
...
@@ -31,7 +32,7 @@ protected:
void
_sink_it
(
const
details
::
log_msg
&
msg
)
override
void
_sink_it
(
const
details
::
log_msg
&
msg
)
override
{
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
_mutex
);
std
::
lock_guard
<
std
::
mutex
>
lock
(
_mutex
);
_flush_helper
.
write
(
msg
.
str
,
_ofstream
);
_flush_helper
.
write
(
msg
.
formatted
,
_ofstream
);
}
}
private
:
private
:
std
::
mutex
_mutex
;
std
::
mutex
_mutex
;
...
@@ -65,13 +66,13 @@ protected:
...
@@ -65,13 +66,13 @@ protected:
{
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
_mutex
);
std
::
lock_guard
<
std
::
mutex
>
lock
(
_mutex
);
_current_size
+=
msg
.
str
.
size
();
_current_size
+=
msg
.
formatted
.
size
();
if
(
_current_size
>
_max_size
)
if
(
_current_size
>
_max_size
)
{
{
_rotate
();
_rotate
();
_current_size
=
msg
.
str
.
size
();
_current_size
=
msg
.
formatted
.
size
();
}
}
_flush_helper
.
write
(
msg
.
str
,
_ofstream
);
_flush_helper
.
write
(
msg
.
formatted
,
_ofstream
);
}
}
...
@@ -104,7 +105,7 @@ private:
...
@@ -104,7 +105,7 @@ private:
std
::
remove
(
target
.
c_str
());
std
::
remove
(
target
.
c_str
());
std
::
rename
(
src
.
c_str
(),
target
.
c_str
());
std
::
rename
(
src
.
c_str
(),
target
.
c_str
());
}
}
_ofstream
.
open
(
_calc_filename
(
_base_filename
,
0
,
_extension
));
_ofstream
.
open
(
_calc_filename
(
_base_filename
,
0
,
_extension
)
,
std
::
ofstream
::
binary
);
}
}
std
::
string
_base_filename
;
std
::
string
_base_filename
;
std
::
string
_extension
;
std
::
string
_extension
;
...
@@ -144,7 +145,7 @@ protected:
...
@@ -144,7 +145,7 @@ protected:
_ofstream
.
open
(
_calc_filename
(
_base_filename
,
_extension
));
_ofstream
.
open
(
_calc_filename
(
_base_filename
,
_extension
));
_midnight_tp
=
_calc_midnight_tp
();
_midnight_tp
=
_calc_midnight_tp
();
}
}
_flush_helper
.
write
(
msg
.
str
,
_ofstream
);
_flush_helper
.
write
(
msg
.
formatted
,
_ofstream
);
}
}
private
:
private
:
...
...
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