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
f909caf3
Commit
f909caf3
authored
Mar 28, 2014
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moved to log_msg
parent
0b2bd0fc
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
118 additions
and
87 deletions
+118
-87
example.cpp
example/example.cpp
+6
-6
common_types.h
include/c11log/common_types.h
+9
-0
line_logger.h
include/c11log/details/line_logger.h
+17
-12
stack_buf.h
include/c11log/details/stack_buf.h
+23
-15
stack_oss.h
include/c11log/details/stack_oss.h
+23
-13
formatter.h
include/c11log/formatter.h
+11
-12
logger.h
include/c11log/logger.h
+4
-7
async_sink.h
include/c11log/sinks/async_sink.h
+1
-1
base_sink.h
include/c11log/sinks/base_sink.h
+13
-11
console_sinks.h
include/c11log/sinks/console_sinks.h
+2
-2
file_sinks.h
include/c11log/sinks/file_sinks.h
+9
-8
No files found.
example/example.cpp
View file @
f909caf3
...
...
@@ -2,7 +2,7 @@
//
#include "c11log/logger.h"
#include "c11log/sinks/async_sink.h"
//
#include "c11log/sinks/async_sink.h"
#include "c11log/sinks/file_sinks.h"
#include "c11log/sinks/console_sinks.h"
...
...
@@ -19,7 +19,7 @@ int main(int argc, char* argv[])
{
if
(
argc
||
argv
)
{};
const
unsigned
int
howmany
=
1
000000
;
const
unsigned
int
howmany
=
5
000000
;
auto
fsink
=
std
::
make_shared
<
sinks
::
rotating_file_sink
>
(
"log"
,
"txt"
,
1024
*
1024
*
50
,
5
,
0
);
//auto fsink = std::make_shared<sinks::simple_file_sink>("simplelog", "txt");
...
...
@@ -40,10 +40,10 @@ int main(int argc, char* argv[])
//async->shutdown(seconds(3));
auto
delta
=
system_clock
::
now
()
-
start
;
auto
delta_d
=
duration_cast
<
duration
<
double
>>
(
delta
);
cout
<<
"Total "
<<
format
(
howmany
)
<<
endl
;
cout
<<
"Delta "
<<
format
(
delta_d
.
count
())
<<
endl
;
cout
<<
"Rate: "
<<
format
(
howmany
/
delta_d
.
count
())
<<
"/sec"
<<
endl
;
auto
delta_d
=
duration_cast
<
duration
<
double
>>
(
delta
)
.
count
()
;
cout
_logger
.
info
()
<<
"Total "
<<
format
(
howmany
)
;
cout
_logger
.
info
()
<<
"Delta "
<<
format
(
delta_d
)
;
cout
_logger
.
info
()
<<
"Rate: "
<<
format
(
howmany
/
delta_d
)
<<
"/sec"
;
return
0
;
...
...
include/c11log/common_types.h
View file @
f909caf3
...
...
@@ -26,4 +26,13 @@ inline const char* to_str(c11log::level::level_enum l)
return
level_names
[
l
];
}
}
struct
log_msg
{
log_clock
::
time_point
when
;
bufpair_t
msg_buf
;
std
::
size_t
header_size
;
level
::
level_enum
msg_level
;
};
}
include/c11log/details/line_logger.h
View file @
f909caf3
...
...
@@ -2,7 +2,7 @@
#include "../common_types.h"
#include "../logger.h"
#include "
fast
_oss.h"
#include "
stack
_oss.h"
// line logger class. should be used by the logger as an rvalue only.
...
...
@@ -20,16 +20,19 @@ class line_logger
public
:
line_logger
(
logger
*
callback_logger
,
level
::
level_enum
msg_level
,
bool
enabled
)
:
_callback_logger
(
callback_logger
),
_log_msg
(),
_oss
(),
_level
(
msg_level
),
_enabled
(
enabled
)
{
if
(
enabled
)
{
_log_msg
.
when
=
log_clock
::
now
();
_log_msg
.
msg_level
=
msg_level
;
callback_logger
->
_formatter
->
format_header
(
callback_logger
->
_logger_name
,
msg_level
,
log_clock
::
now
()
,
_log_msg
.
msg_level
,
_log_msg
.
when
,
_oss
);
_log_msg
.
header_size
=
_oss
.
size
();
}
}
...
...
@@ -38,12 +41,12 @@ public:
line_logger
&
operator
=
(
const
line_logger
&
)
=
delete
;
line_logger
&
operator
=
(
line_logger
&&
)
=
delete
;
// The move ctor should only be called on start of logging line,
// where no logging happened yet for this line so no need to copy the string from the other
line_logger
(
line_logger
&&
other
)
:
_callback_logger
(
other
.
_callback_logger
),
_log_msg
(
other
.
_log_msg
),
// The move ctor should only be called on start of logging line,
// where no logging happened yet for this line so no need to copy the oss from the other
_oss
(),
_level
(
other
.
_level
),
_enabled
(
other
.
_enabled
)
{}
...
...
@@ -52,26 +55,28 @@ public:
if
(
_enabled
)
{
_oss
<<
os
::
eol
();
_callback_logger
->
_log_it
(
_oss
.
buf
(),
_level
);
_log_msg
.
msg_buf
=
_oss
.
buf
();
_callback_logger
->
_log_it
(
_log_msg
);
}
}
template
<
typename
T
>
line_logger
&&
operator
<<
(
const
T
&
msg
)
line_logger
&&
operator
<<
(
const
T
&
what
)
{
if
(
_enabled
)
_oss
<<
msg
;
_oss
<<
what
;
return
std
::
move
(
*
this
);
}
private
:
logger
*
_callback_logger
;
details
::
fast_oss
_oss
;
level
::
level_enum
_level
;
c11log
::
log_msg
_log_msg
;
details
::
stack_oss
_oss
;
bool
_enabled
;
};
}
//Namespace details
}
// Namespace c11log
include/c11log/details/
fast
_buf.h
→
include/c11log/details/
stack
_buf.h
View file @
f909caf3
...
...
@@ -15,18 +15,18 @@ namespace details
{
template
<
std
::
size_t
STACK_SIZE
=
128
>
class
fast
_buf
class
stack
_buf
{
public
:
fast
_buf
()
:
_stack_size
(
0
)
{}
~
fast
_buf
()
{};
stack
_buf
()
:
_stack_size
(
0
)
{}
~
stack
_buf
()
{};
fast_buf
(
const
bufpair_t
&
buf_to_copy
)
:
fast
_buf
()
stack_buf
(
const
bufpair_t
&
buf_to_copy
)
:
stack
_buf
()
{
append
(
buf_to_copy
);
}
fast_buf
(
const
fast
_buf
&
other
)
stack_buf
(
const
stack
_buf
&
other
)
{
_stack_size
=
other
.
_stack_size
;
if
(
!
other
.
_v
.
empty
())
...
...
@@ -35,7 +35,7 @@ public:
std
::
copy
(
other
.
_stack_buf
.
begin
(),
other
.
_stack_buf
.
begin
()
+
_stack_size
,
_stack_buf
.
begin
());
}
fast_buf
(
fast
_buf
&&
other
)
stack_buf
(
stack
_buf
&&
other
)
{
_stack_size
=
other
.
_stack_size
;
if
(
!
other
.
_v
.
empty
())
...
...
@@ -45,31 +45,31 @@ public:
other
.
clear
();
}
fast_buf
&
operator
=
(
const
fast
_buf
&
other
)
=
delete
;
fast_buf
&
operator
=
(
fast
_buf
&&
other
)
=
delete
;
stack_buf
&
operator
=
(
const
stack
_buf
&
other
)
=
delete
;
stack_buf
&
operator
=
(
stack
_buf
&&
other
)
=
delete
;
void
append
(
const
char
*
buf
,
std
::
size_t
size
)
void
append
(
const
char
*
buf
,
std
::
size_t
buf_
size
)
{
//If we are aleady using _v, forget about the stack
if
(
!
_v
.
empty
())
{
_v
.
insert
(
_v
.
end
(),
buf
,
buf
+
size
);
_v
.
insert
(
_v
.
end
(),
buf
,
buf
+
buf_
size
);
}
//Try use the stack
else
{
if
(
_stack_size
+
size
<=
STACK_SIZE
)
if
(
_stack_size
+
buf_
size
<=
STACK_SIZE
)
{
std
::
memcpy
(
&
_stack_buf
[
_stack_size
],
buf
,
size
);
_stack_size
+=
size
;
std
::
memcpy
(
&
_stack_buf
[
_stack_size
],
buf
,
buf_
size
);
_stack_size
+=
buf_
size
;
}
//Not enough stack space. Copy all to _v
else
{
_v
.
reserve
(
_stack_size
+
size
);
_v
.
reserve
(
_stack_size
+
buf_
size
);
if
(
_stack_size
)
_v
.
insert
(
_v
.
end
(),
_stack_buf
.
begin
(),
_stack_buf
.
begin
()
+
_stack_size
);
_v
.
insert
(
_v
.
end
(),
buf
,
buf
+
size
);
_v
.
insert
(
_v
.
end
(),
buf
,
buf
+
buf_
size
);
}
}
}
...
...
@@ -93,6 +93,14 @@ public:
return
bufpair_t
(
_stack_buf
.
data
(),
_stack_size
);
}
std
::
size_t
size
()
{
if
(
!
_v
.
empty
())
return
_v
.
size
();
else
return
_stack_size
;
}
private
:
std
::
vector
<
char
>
_v
;
std
::
array
<
char
,
STACK_SIZE
>
_stack_buf
;
...
...
include/c11log/details/
fast
_oss.h
→
include/c11log/details/
stack
_oss.h
View file @
f909caf3
...
...
@@ -2,7 +2,7 @@
// Faster than ostringstream--returns its string by ref
#include <ostream>
#include "c11log/details/
fast
_buf.h"
#include "c11log/details/
stack
_buf.h"
namespace
c11log
{
...
...
@@ -12,7 +12,7 @@ namespace details
class
str_devicebuf
:
public
std
::
streambuf
{
public
:
using
Base
=
std
::
streambuf
;
using
Base
=
std
::
streambuf
;
str_devicebuf
()
=
default
;
~
str_devicebuf
()
=
default
;
...
...
@@ -23,24 +23,29 @@ public:
bufpair_t
buf
()
{
return
_fastbuf
.
get
();
return
_stackbuf
.
get
();
}
std
::
size_t
size
()
{
return
_stackbuf
.
size
();
}
void
clear
()
{
_
fast
buf
.
clear
();
_
stack
buf
.
clear
();
}
protected
:
// copy the give buffer into the accumulated fast buffer
std
::
streamsize
xsputn
(
const
char_type
*
s
,
std
::
streamsize
count
)
override
{
_
fast
buf
.
append
(
s
,
static_cast
<
unsigned
int
>
(
count
));
_
stack
buf
.
append
(
s
,
static_cast
<
unsigned
int
>
(
count
));
return
count
;
}
int_type
overflow
(
int_type
ch
)
override
{
{
if
(
traits_type
::
not_eof
(
ch
))
{
char
c
=
traits_type
::
to_char_type
(
ch
);
...
...
@@ -49,24 +54,29 @@ protected:
return
ch
;
}
private
:
fast_buf
<
192
>
_fast
buf
;
stack_buf
<
192
>
_stack
buf
;
};
class
fast
_oss
:
public
std
::
ostream
class
stack
_oss
:
public
std
::
ostream
{
public
:
fast
_oss
()
:
std
::
ostream
(
&
_dev
)
{}
~
fast
_oss
()
=
default
;
stack
_oss
()
:
std
::
ostream
(
&
_dev
)
{}
~
stack
_oss
()
=
default
;
fast_oss
(
const
fast
_oss
&
other
)
=
delete
;
fast_oss
(
fast
_oss
&&
other
)
=
delete
;
fast_oss
&
operator
=
(
const
fast
_oss
&
other
)
=
delete
;
stack_oss
(
const
stack
_oss
&
other
)
=
delete
;
stack_oss
(
stack
_oss
&&
other
)
=
delete
;
stack_oss
&
operator
=
(
const
stack
_oss
&
other
)
=
delete
;
bufpair_t
buf
()
{
return
_dev
.
buf
();
}
std
::
size_t
size
()
{
return
_dev
.
size
();
}
void
clear
()
{
_dev
.
clear
();
...
...
include/c11log/formatter.h
View file @
f909caf3
...
...
@@ -10,18 +10,17 @@
#include "common_types.h"
#include "details/os.h"
#include "details/
fast
_oss.h"
#include "details/
stack
_oss.h"
namespace
c11log
{
namespace
formatters
{
class
formatter
{
public
:
virtual
void
format_header
(
const
std
::
string
&
logger_name
,
level
::
level_enum
level
,
const
log_clock
::
time_point
&
tp
,
std
::
ostream
&
des
t
)
=
0
;
public
:
virtual
void
format_header
(
const
std
::
string
&
logger_name
,
level
::
level_enum
level
,
const
log_clock
::
time_point
&
tp
,
std
::
ostream
&
outpu
t
)
=
0
;
};
...
...
@@ -29,17 +28,17 @@ class default_formatter: public formatter
{
public
:
// 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
&
des
t
)
override
void
format_header
(
const
std
::
string
&
logger_name
,
level
::
level_enum
level
,
const
log_clock
::
time_point
&
tp
,
std
::
ostream
&
outpu
t
)
override
{
_format_time
(
tp
,
des
t
);
_format_time
(
tp
,
outpu
t
);
if
(
!
logger_name
.
empty
())
des
t
<<
" ["
<<
logger_name
<<
':'
<<
c11log
::
level
::
to_str
(
level
)
<<
"] "
;
outpu
t
<<
" ["
<<
logger_name
<<
':'
<<
c11log
::
level
::
to_str
(
level
)
<<
"] "
;
else
des
t
<<
" ["
<<
c11log
::
level
::
to_str
(
level
)
<<
"] "
;
outpu
t
<<
" ["
<<
c11log
::
level
::
to_str
(
level
)
<<
"] "
;
}
private
:
void
_format_time
(
const
log_clock
::
time_point
&
tp
,
std
::
ostream
&
des
t
);
void
_format_time
(
const
log_clock
::
time_point
&
tp
,
std
::
ostream
&
outpu
t
);
};
...
...
@@ -48,7 +47,7 @@ private:
}
//namespace c11log
// Format datetime like this: [2014-03-14 17:15:22]
inline
void
c11log
::
formatters
::
default_formatter
::
_format_time
(
const
log_clock
::
time_point
&
tp
,
std
::
ostream
&
des
t
)
inline
void
c11log
::
formatters
::
default_formatter
::
_format_time
(
const
log_clock
::
time_point
&
tp
,
std
::
ostream
&
outpu
t
)
{
using
namespace
c11log
::
details
::
os
;
using
namespace
std
::
chrono
;
...
...
@@ -68,7 +67,7 @@ inline void c11log::formatters::default_formatter::_format_time(const log_clock:
if
(
tp_time_t
!=
s_cache_time_t
)
{
auto
tm_now
=
details
::
os
::
localtime
(
tp_time_t
);
details
::
fast
_oss
time_oss
;
details
::
stack
_oss
time_oss
;
time_oss
.
fill
(
'0'
);
time_oss
<<
'['
<<
tm_now
.
tm_year
+
1900
<<
'-'
;
time_oss
.
width
(
2
);
...
...
@@ -88,5 +87,5 @@ inline void c11log::formatters::default_formatter::_format_time(const log_clock:
std
::
memcpy
(
s_cache_str
,
buf
.
first
,
buf
.
second
);
s_cache_size
=
buf
.
second
;
}
des
t
.
write
(
s_cache_str
,
s_cache_size
);
outpu
t
.
write
(
s_cache_str
,
s_cache_size
);
}
include/c11log/logger.h
View file @
f909caf3
...
...
@@ -11,7 +11,6 @@
#include "common_types.h"
#include "sinks/base_sink.h"
#include "details/factory.h"
#include "c11log/details/log_msg.h"
//Thread safe, fast logger.
...
...
@@ -69,7 +68,7 @@ private:
sinks_vector_t
_sinks
;
std
::
atomic_int
_logger_level
;
void
_log_it
(
const
bufpair_t
&
buf
,
const
level
::
level_enum
level
);
void
_log_it
(
const
log_msg
&
msg
);
};
...
...
@@ -77,12 +76,10 @@ logger& get_logger(const std::string& name);
}
//
// Logger inline implementation
//
#include "details/line_logger.h"
#include "details/fast_buf.h"
inline
c11log
::
logger
::
logger
(
const
std
::
string
&
name
,
formatter_ptr
f
,
sinks_init_list
sinks_list
)
:
...
...
@@ -90,7 +87,7 @@ inline c11log::logger::logger(const std::string& name, formatter_ptr f, sinks_in
_formatter
(
f
),
_sinks
(
sinks_list
)
{
//Seems that vs2013 doesnt support
atomic member initialization in ctor
, so its done here
//Seems that vs2013 doesnt support
std::atomic member initialization
, so its done here
_logger_level
=
level
::
INFO
;
}
...
...
@@ -149,10 +146,10 @@ inline bool c11log::logger::should_log(c11log::level::level_enum level) const
return
level
>=
_logger_level
.
load
();
}
inline
void
c11log
::
logger
::
_log_it
(
const
bufpair_t
&
buf
,
const
level
::
level_enum
level
)
inline
void
c11log
::
logger
::
_log_it
(
const
log_msg
&
msg
)
{
for
(
auto
&
sink
:
_sinks
)
sink
->
log
(
buf
,
level
);
sink
->
log
(
msg
);
}
// Static factory function
...
...
include/c11log/sinks/async_sink.h
View file @
f909caf3
...
...
@@ -75,7 +75,7 @@ inline void c11log::sinks::async_sink::_thread_loop()
bufpair_t
buf
(
msg
.
data
(),
msg
.
size
());
for
(
auto
&
sink
:
_sinks
)
{
sink
->
log
(
buf
,
static_cast
<
level
::
level_enum
>
(
_level
.
load
())
);
sink
->
log
(
buf
,
level
::
INFO
);
if
(
!
_active
)
return
;
}
...
...
include/c11log/sinks/base_sink.h
View file @
f909caf3
...
...
@@ -13,37 +13,39 @@ namespace sinks
class
base_sink
{
public
:
base_sink
()
=
default
;
base_sink
(
level
::
level_enum
l
)
:
_level
(
l
)
{
};
base_sink
()
:
_enabled
(
true
)
{}
virtual
~
base_sink
()
=
default
;
base_sink
(
const
base_sink
&
)
=
delete
;
base_sink
&
operator
=
(
const
base_sink
&
)
=
delete
;
void
log
(
const
bufpair_t
&
msg
,
level
::
level_enum
level
)
void
log
(
const
log_msg
&
msg
)
{
if
(
level
>=
_level
)
if
(
_enabled
)
{
_sink_it
(
msg
);
}
};
void
set_level
(
level
::
level_enum
level
)
void
enable
(
bool
enabled
)
{
_enabled
=
enabled
;
}
bool
is_enabled
()
{
_level
=
level
;
return
_enabled
.
load
()
;
}
protected
:
virtual
void
_sink_it
(
const
bufpair_t
&
msg
)
=
0
;
std
::
atomic
<
int
>
_level
{
level
::
INFO
}
;
virtual
void
_sink_it
(
const
log_msg
&
msg
)
=
0
;
std
::
atomic
<
bool
>
_enabled
;
};
class
null_sink
:
public
base_sink
{
protected
:
void
_sink_it
(
const
bufpair_t
&
)
override
void
_sink_it
(
const
log_msg
&
)
override
{
}
};
...
...
include/c11log/sinks/console_sinks.h
View file @
f909caf3
...
...
@@ -19,10 +19,10 @@ public:
virtual
~
console_sink
()
=
default
;
protected
:
virtual
void
_sink_it
(
const
bufpair_t
&
msg
)
override
virtual
void
_sink_it
(
const
log_msg
&
msg
)
override
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
_mutex
);
_ostream
.
write
(
msg
.
first
,
msg
.
second
);
_ostream
.
write
(
msg
.
msg_buf
.
first
,
msg
.
msg_buf
.
second
);
}
std
::
ostream
&
_ostream
;
...
...
include/c11log/sinks/file_sinks.h
View file @
f909caf3
...
...
@@ -27,10 +27,10 @@ public:
{
}
protected
:
void
_sink_it
(
const
bufpair_t
&
msg
)
override
void
_sink_it
(
const
log_msg
&
msg
)
override
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
_mutex
);
_flush_helper
.
write
(
_ofstream
,
msg
);
_flush_helper
.
write
(
_ofstream
,
msg
.
msg_buf
);
}
private
:
std
::
mutex
_mutex
;
...
...
@@ -60,16 +60,17 @@ public:
}
protected
:
void
_sink_it
(
const
bufpair_t
&
msg
)
override
void
_sink_it
(
const
log_msg
&
msg
)
override
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
_mutex
);
_current_size
+=
msg
.
second
;
_current_size
+=
msg
.
msg_buf
.
second
;
if
(
_current_size
>
_max_size
)
{
_rotate
();
_current_size
=
msg
.
second
;
_current_size
=
msg
.
msg_buf
.
second
;
}
_flush_helper
.
write
(
_ofstream
,
msg
);
_flush_helper
.
write
(
_ofstream
,
msg
.
msg_buf
);
}
...
...
@@ -133,7 +134,7 @@ public:
}
protected
:
void
_sink_it
(
const
bufpair_t
&
msg
)
override
void
_sink_it
(
const
log_msg
&
msg
)
override
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
_mutex
);
if
(
std
::
chrono
::
system_clock
::
now
()
>=
_midnight_tp
)
...
...
@@ -142,7 +143,7 @@ protected:
_ofstream
.
open
(
_calc_filename
(
_base_filename
,
_extension
));
_midnight_tp
=
_calc_midnight_tp
();
}
_flush_helper
.
write
(
_ofstream
,
msg
);
_flush_helper
.
write
(
_ofstream
,
msg
.
msg_buf
);
}
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