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
796ec3db
Commit
796ec3db
authored
Oct 24, 2014
by
gabi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added registry
parent
319db7bc
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
161 additions
and
161 deletions
+161
-161
c11logtest.vcxproj
c11logtest/c11logtest/c11logtest.vcxproj
+4
-1
c11logtest.vcxproj.filters
c11logtest/c11logtest/c11logtest.vcxproj.filters
+7
-3
bench.cpp
example/bench.cpp
+10
-33
example.cpp
example/example.cpp
+0
-1
common.h
include/c11log/common.h
+2
-2
pattern_formatter.h
include/c11log/details/pattern_formatter.h
+3
-1
registry.h
include/c11log/details/registry.h
+59
-0
factory.h
include/c11log/factory.h
+0
-106
logger.h
include/c11log/logger.h
+76
-14
No files found.
c11logtest/c11logtest/c11logtest.vcxproj
View file @
796ec3db
...
...
@@ -95,8 +95,8 @@
<ClInclude
Include=
"..\..\include\c11log\details\null_mutex.h"
/>
<ClInclude
Include=
"..\..\include\c11log\details\os.h"
/>
<ClInclude
Include=
"..\..\include\c11log\details\pattern_formatter.h"
/>
<ClInclude
Include=
"..\..\include\c11log\details\registry.h"
/>
<ClInclude
Include=
"..\..\include\c11log\details\stack_buf.h"
/>
<ClInclude
Include=
"..\..\include\c11log\factory.h"
/>
<ClInclude
Include=
"..\..\include\c11log\formatter.h"
/>
<ClInclude
Include=
"..\..\include\c11log\logger.h"
/>
<ClInclude
Include=
"..\..\include\c11log\sinks\async_sink.h"
/>
...
...
@@ -114,6 +114,9 @@
<ClCompile
Include=
"..\..\example\example.cpp"
/>
<ClCompile
Include=
"stdafx.cpp"
/>
</ItemGroup>
<ItemGroup>
<None
Include=
"ClassDiagram.cd"
/>
</ItemGroup>
<Import
Project=
"$(VCTargetsPath)\Microsoft.Cpp.targets"
/>
<ImportGroup
Label=
"ExtensionTargets"
>
</ImportGroup>
...
...
c11logtest/c11logtest/c11logtest.vcxproj.filters
View file @
796ec3db
...
...
@@ -84,9 +84,6 @@
<ClInclude
Include=
"..\..\include\c11log\common.h"
>
<Filter>
Header Files\c11log
</Filter>
</ClInclude>
<ClInclude
Include=
"..\..\include\c11log\factory.h"
>
<Filter>
Header Files\c11log
</Filter>
</ClInclude>
<ClInclude
Include=
"..\..\include\c11log\details\fast_istostr.h"
>
<Filter>
Header Files\c11log\details
</Filter>
</ClInclude>
...
...
@@ -96,6 +93,9 @@
<ClInclude
Include=
"..\..\include\c11log\details\pattern_formatter.h"
>
<Filter>
Header Files\c11log\details
</Filter>
</ClInclude>
<ClInclude
Include=
"..\..\include\c11log\details\registry.h"
>
<Filter>
Header Files\c11log
</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile
Include=
"stdafx.cpp"
>
...
...
@@ -108,4 +108,7 @@
<Filter>
Source Files
</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None
Include=
"ClassDiagram.cd"
/>
</ItemGroup>
</Project>
\ No newline at end of file
example/bench.cpp
View file @
796ec3db
...
...
@@ -2,63 +2,40 @@
//
#include <mutex>
#include "c11log/logger.h"
#include "c11log/factory.h"
#include "c11log/sinks/async_sink.h"
#include "c11log/sinks/file_sinks.h"
#include "c11log/sinks/stdout_sinks.h"
#include "c11log/sinks/null_sink.h"
#include "utils.h"
#include "c11log/details/registry.h"
using
namespace
std
::
chrono
;
using
namespace
c11log
;
using
namespace
utils
;
std
::
ostringstream
f1
(
int
i
)
{
std
::
ostringstream
oss
;
oss
<<
"Hello oss "
<<
i
;
return
oss
;
}
details
::
fast_oss
f2
(
int
i
)
{
details
::
fast_oss
oss
;
oss
<<
"Hello oss "
<<
i
;
return
oss
;
}
int
main
(
int
argc
,
char
*
argv
[])
{
const
unsigned
int
howmany
=
argc
<=
1
?
600000
:
atoi
(
argv
[
1
]);
const
std
::
string
pattern
=
"%+"
;
auto
formatter
=
std
::
make_shared
<
details
::
pattern_formatter
>
(
pattern
);
//logger cout_logger("bench", { std::make_shared<sinks::stderr_sink_mt>() }, pattern);
//logger::default_formatter(formatter);
logger
cout_logger
(
"bench"
,
{
std
::
make_shared
<
sinks
::
stderr_sink_mt
>
()
});
logger
::
default_formatter
(
formatter
);
cout_logger
.
info
()
<<
"Hello logger "
<<
12.4
<<
5
<<
','
<<
6
<<
7
<<
8
<<
9
<<
10
;
auto
nullsink
=
std
::
make_shared
<
sinks
::
null_sink_st
>
();
auto
rotating
=
std
::
make_shared
<
sinks
::
rotating_file_sink_st
>
(
"myrotating"
,
"txt"
,
1024
*
1024
*
5
,
20
,
100
);
logger
my_logger
(
"my_logger"
,
{
nullsink
});
auto
console
=
c11log
::
create
<
sinks
::
stderr_sink_st
>
(
"reporter"
);
console
->
set_format
(
"[%n %l] %t"
);
console
->
set_level
(
c11log
::
level
::
INFO
);
console
->
info
(
"Starting bench with"
,
howmany
,
"iterations.."
);
auto
bench
=
c11log
::
create
<
sinks
::
rotating_file_sink_st
>
(
"bench"
,
"myrotating"
,
"txt"
,
1024
*
1024
*
5
,
3
,
100
);
auto
start
=
system_clock
::
now
();
for
(
unsigned
int
i
=
1
;
i
<=
howmany
;
++
i
)
{
my_logger
.
info
()
<<
"Hello logger: msg #
"
<<
i
;
bench
->
info
()
<<
"Hello logger: msg number
"
<<
i
;
}
auto
delta
=
system_clock
::
now
()
-
start
;
auto
delta_d
=
duration_cast
<
duration
<
double
>>
(
delta
).
count
();
co
ut_logger
.
info
(
"Total:"
)
<<
format
(
howmany
);
co
ut_logger
.
info
(
"Delta:"
)
<<
format
(
delta_d
);
co
ut_logger
.
info
(
"Rate:"
)
<<
format
(
howmany
/
delta_d
)
<<
"/sec"
;
co
nsole
->
info
(
"Total:"
)
<<
format
(
howmany
);
co
nsole
->
info
(
"Delta:"
)
<<
format
(
delta_d
);
co
nsole
->
info
(
"Rate:"
)
<<
format
(
howmany
/
delta_d
)
<<
"/sec"
;
return
0
;
}
...
...
example/example.cpp
View file @
796ec3db
...
...
@@ -4,7 +4,6 @@
#include <iostream>
#include "c11log/logger.h"
#include "c11log/factory.h"
#include "c11log/sinks/stdout_sinks.h"
#include "c11log/sinks/file_sinks.h"
using
namespace
std
;
...
...
include/c11log/common.h
View file @
796ec3db
...
...
@@ -25,8 +25,8 @@ typedef enum
TRACE
,
DEBUG
,
INFO
,
WARN
ING
,
ERR
OR
,
WARN
,
ERR
,
CRITICAL
,
NONE
=
99
}
level_enum
;
...
...
include/c11log/details/pattern_formatter.h
View file @
796ec3db
...
...
@@ -19,7 +19,9 @@ public:
virtual
void
format
(
details
::
log_msg
&
msg
)
=
0
;
};
// log name appender
///////////////////////////////////////////////////////////////////////
// name & level pattern appenders
///////////////////////////////////////////////////////////////////////
class
name_formatter
:
public
flag_formatter
{
void
format
(
details
::
log_msg
&
msg
)
override
...
...
include/c11log/details/registry.h
0 → 100644
View file @
796ec3db
#pragma once
// Loggers registy of unique name->logger pointer
// If 2 loggers with same name are added, the last will be used
// If user requests a non existing logger, nullptr will be returned
// This class is thread safe
#include <string>
#include <mutex>
#include <unordered_map>
#include "../logger.h"
#include "../common.h"
namespace
c11log
{
namespace
details
{
class
registry
{
public
:
std
::
shared_ptr
<
logger
>
get
(
const
std
::
string
&
name
)
{
std
::
lock_guard
<
std
::
mutex
>
l
(
_mutex
);
auto
found
=
_loggers
.
find
(
name
);
return
found
==
_loggers
.
end
()
?
nullptr
:
found
->
second
;
}
std
::
shared_ptr
<
logger
>
create
(
const
std
::
string
&
logger_name
,
sinks_init_list
sinks
)
{
std
::
lock_guard
<
std
::
mutex
>
l
(
_mutex
);
return
_loggers
[
logger_name
]
=
std
::
make_shared
<
logger
>
(
logger_name
,
sinks
);
}
std
::
shared_ptr
<
logger
>
create
(
const
std
::
string
&
logger_name
,
sink_ptr
sink
)
{
create
(
logger_name
,
{
sink
});
}
template
<
class
It
>
std
::
shared_ptr
<
logger
>
create
(
const
std
::
string
&
logger_name
,
const
It
&
sinks_begin
,
const
It
&
sinks_end
)
{
std
::
lock_guard
<
std
::
mutex
>
l
(
_mutex
);
return
_loggers
[
logger_name
]
=
std
::
make_shared
<
logger
>
(
logger_name
,
sinks_begin
,
sinks_end
);
}
static
registry
&
instance
()
{
static
registry
s_instance
;
return
s_instance
;
}
private
:
registry
()
=
default
;
registry
(
const
registry
&
)
=
delete
;
std
::
mutex
_mutex
;
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
logger
>>
_loggers
;
};
}
}
include/c11log/factory.h
deleted
100644 → 0
View file @
319db7bc
#pragma once
#include <memory>
#include "logger.h"
#include "sinks/file_sinks.h"
#include "sinks/stdout_sinks.h"
//
// logger creation shotcuts
//
namespace
c11log
{
namespace
factory
{
//
//
//std::shared_ptr<logger> get(const std::string& name);
//std::shared_ptr<logger> create(const std::string& name, sinks_init_list , formatter_ptr);
//std::shared_ptr<logger> create(const std::string& name, sinks_init_list, string format);
//template<class It>
//std::shared_ptr<logger> create (const std::string& name, const It& begin, const It& end);
//
//
////
//// console loggers single/multi threaded
////
//std::unique_ptr<logger> stdout_logger(const std::string& name = "")
//{
// auto sink = std::make_shared<sinks::stderr_sink_st>();
// return std::unique_ptr<logger>(new logger(name, { sink }));
//}
//
//std::unique_ptr<logger> stdout_logger_mt(const std::string& name = "")
//{
// auto sink = std::make_shared<sinks::stderr_sink_mt>();
// return std::unique_ptr<logger>(new logger(name, { sink }));
//}
//
////
//// simple file logger single/multi threaded
////
//std::unique_ptr<logger> simple_file_logger(const std::string& filename, const std::string& logger_name = "" )
//{
// auto fsink = std::make_shared<sinks::simple_file_sink_st>(filename);
// return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
//
//}
//std::unique_ptr<logger> simple_file_logger_mt(const std::string& filename, const std::string& logger_name = "")
//{
// auto fsink = std::make_shared<sinks::simple_file_sink_mt>(filename);
// return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
//}
//
////
//// daily file logger single/multi threaded
////
//std::unique_ptr<logger> daily_file_logger(
// const std::string &filename,
// const std::string &extension,
// const std::size_t flush_every,
// const std::string& logger_name = "")
//{
// auto fsink = std::make_shared<sinks::daily_file_sink_st>(filename, extension, flush_every);
// return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
//}
//
//std::unique_ptr<logger> daily_file_logger_mt(
// const std::string &filename,
// const std::string &extension,
// const std::size_t flush_every,
// const std::string& logger_name = "")
//{
// auto fsink = std::make_shared<sinks::daily_file_sink_mt>(filename, extension, flush_every);
// return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
//}
//
////
//// rotating file logger single/multi threaded
////
//std::unique_ptr<logger> rotating_file_logger(
// const std::string &filename,
// const std::string &extension,
// const std::size_t max_size,
// const std::size_t max_files,
// const std::size_t flush_every,
// const std::string& logger_name = "")
//{
// auto fsink = std::make_shared<sinks::rotating_file_sink_st>(filename, extension, max_size, max_files, flush_every);
// return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
//}
//
//std::unique_ptr<logger> rotating_file_logger_mt(
// const std::string &filename,
// const std::string &extension,
// const std::size_t max_size,
// const std::size_t max_files,
// const std::size_t flush_every,
// const std::string& logger_name = "")
//{
// auto fsink = std::make_shared<sinks::rotating_file_sink_mt>(filename, extension, max_size, max_files, flush_every);
// return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
//}
}
// ns factory
}
// ns c11log
include/c11log/logger.h
View file @
796ec3db
...
...
@@ -16,6 +16,7 @@
#include "common.h"
#include "details/pattern_formatter.h"
namespace
c11log
{
...
...
@@ -32,20 +33,15 @@ public:
template
<
class
It
>
logger
(
const
std
::
string
&
name
,
const
It
&
begin
,
const
It
&
end
);
//get/set default formatter
static
formatter_ptr
&
default_formatter
(
formatter_ptr
formatter
=
nullptr
);
void
c11log
::
logger
::
set_format
(
const
std
::
string
&
format
);
void
set_formatter
(
formatter_ptr
);
formatter_ptr
get_formatter
()
const
;
logger
(
const
logger
&
)
=
delete
;
logger
&
operator
=
(
const
logger
&
)
=
delete
;
void
level
(
level
::
level_enum
);
void
set_
level
(
level
::
level_enum
);
level
::
level_enum
level
()
const
;
const
std
::
string
&
name
()
const
;
...
...
@@ -59,6 +55,11 @@ public:
template
<
typename
...
Args
>
details
::
line_logger
error
(
const
Args
&
...
args
);
template
<
typename
...
Args
>
details
::
line_logger
critical
(
const
Args
&
...
args
);
//static functions
//get/set default formatter
static
formatter_ptr
default_formatter
(
formatter_ptr
formatter
=
nullptr
);
static
formatter_ptr
default_format
(
const
std
::
string
&
format
);
private
:
friend
details
::
line_logger
;
std
::
string
_name
;
...
...
@@ -70,10 +71,27 @@ private:
void
_variadic_log
(
details
::
line_logger
&
l
,
const
First
&
first
,
const
Rest
&
...
rest
);
void
_log_msg
(
details
::
log_msg
&
msg
);
};
//
// Registry functions for easy loggers creation and retrieval
// example
// auto console_logger = c11log::create("my_logger", c11log::sinks<stdout_sink_mt>);
// auto same_logger = c11log::get("my_logger");
// auto file_logger = c11
//
std
::
shared_ptr
<
logger
>
get
(
const
std
::
string
&
name
);
std
::
shared_ptr
<
logger
>
create
(
const
std
::
string
&
logger_name
,
sinks_init_list
sinks
);
template
<
typename
Sink
,
typename
...
Args
>
std
::
shared_ptr
<
c11log
::
logger
>
create
(
const
std
::
string
&
logger_name
,
const
Args
&
...
args
);
template
<
class
It
>
std
::
shared_ptr
<
logger
>
create
(
const
std
::
string
&
logger_name
,
const
It
&
sinks_begin
,
const
It
&
sinks_end
);
}
//
//
t
race & debug macros
//
T
race & debug macros
//
#ifdef FFLOG_ENABLE_TRACE
#define FFLOG_TRACE(logger, ...) logger->log(c11log::level::TRACE, __FILE__, " #", __LINE__,": " __VA_ARGS__)
...
...
@@ -114,12 +132,17 @@ inline void c11log::logger::set_formatter(c11log::formatter_ptr msg_formatter)
_formatter
=
msg_formatter
;
}
inline
void
c11log
::
logger
::
set_format
(
const
std
::
string
&
format
)
{
_formatter
=
std
::
make_shared
<
details
::
pattern_formatter
>
(
format
);
}
inline
c11log
::
formatter_ptr
c11log
::
logger
::
get_formatter
()
const
{
return
_formatter
;
}
inline
c11log
::
formatter_ptr
&
c11log
::
logger
::
default_formatter
(
formatter_ptr
formatter
)
inline
c11log
::
formatter_ptr
c11log
::
logger
::
default_formatter
(
formatter_ptr
formatter
)
{
static
formatter_ptr
g_default_formatter
=
std
::
make_shared
<
details
::
pattern_formatter
>
(
"[%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %t"
);
if
(
formatter
)
...
...
@@ -127,6 +150,11 @@ inline c11log::formatter_ptr& c11log::logger::default_formatter(formatter_ptr fo
return
g_default_formatter
;
}
inline
c11log
::
formatter_ptr
c11log
::
logger
::
default_format
(
const
std
::
string
&
format
)
{
return
default_formatter
(
std
::
make_shared
<
details
::
pattern_formatter
>
(
format
));
}
template
<
typename
...
Args
>
inline
c11log
::
details
::
line_logger
c11log
::
logger
::
log
(
level
::
level_enum
lvl
,
const
Args
&
...
args
)
{
bool
msg_enabled
=
should_log
(
lvl
);
...
...
@@ -157,13 +185,13 @@ inline c11log::details::line_logger c11log::logger::info(const Args&... args)
template
<
typename
...
Args
>
inline
c11log
::
details
::
line_logger
c11log
::
logger
::
warn
(
const
Args
&
...
args
)
{
return
log
(
level
::
WARN
ING
,
args
...);
return
log
(
level
::
WARN
,
args
...);
}
template
<
typename
...
Args
>
inline
c11log
::
details
::
line_logger
c11log
::
logger
::
error
(
const
Args
&
...
args
)
{
return
log
(
level
::
ERR
OR
,
args
...);
return
log
(
level
::
ERR
,
args
...);
}
template
<
typename
...
Args
>
...
...
@@ -177,7 +205,7 @@ inline const std::string& c11log::logger::name() const
return
_name
;
}
inline
void
c11log
::
logger
::
level
(
c11log
::
level
::
level_enum
log_level
)
inline
void
c11log
::
logger
::
set_
level
(
c11log
::
level
::
level_enum
log_level
)
{
_level
.
store
(
log_level
);
}
...
...
@@ -201,15 +229,49 @@ template <typename First, typename... Rest>
void
c11log
::
logger
::
_variadic_log
(
c11log
::
details
::
line_logger
&
l
,
const
First
&
first
,
const
Rest
&
...
rest
)
{
l
.
write
(
first
);
l
.
write
(
' '
);
_variadic_log
(
l
,
rest
...);
}
inline
void
c11log
::
logger
::
_log_msg
(
details
::
log_msg
&
msg
)
{
auto
&
formatter
=
_formatter
?
_formatter
:
logger
::
default_formatter
();
formatter
->
format
(
msg
);
if
(
!
_formatter
)
_formatter
=
logger
::
default_formatter
();
_formatter
->
format
(
msg
);
for
(
auto
&
sink
:
_sinks
)
sink
->
log
(
msg
);
}
//
// Global registry functions
//
#include "details/registry.h"
inline
std
::
shared_ptr
<
c11log
::
logger
>
c11log
::
get
(
const
std
::
string
&
name
)
{
return
details
::
registry
::
instance
().
get
(
name
);
}
inline
std
::
shared_ptr
<
c11log
::
logger
>
c11log
::
create
(
const
std
::
string
&
logger_name
,
c11log
::
sinks_init_list
sinks
)
{
return
details
::
registry
::
instance
().
create
(
logger_name
,
sinks
);
}
template
<
typename
Sink
,
typename
...
Args
>
inline
std
::
shared_ptr
<
c11log
::
logger
>
c11log
::
create
(
const
std
::
string
&
logger_name
,
const
Args
&
...
args
)
{
sink_ptr
sink
=
std
::
make_shared
<
Sink
>
(
args
...);
return
details
::
registry
::
instance
().
create
(
logger_name
,
{
sink
});
}
template
<
class
It
>
inline
std
::
shared_ptr
<
c11log
::
logger
>
c11log
::
create
(
const
std
::
string
&
logger_name
,
const
It
&
sinks_begin
,
const
It
&
sinks_end
)
{
std
::
lock_guard
<
std
::
mutex
>
l
(
_mutex
);
return
details
::
registry
::
instance
().
create
(
logger_name
,
std
::
forward
(
sinks_begin
),
std
::
forward
(
sinks_end
));
}
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