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
e32c856a
Commit
e32c856a
authored
Mar 30, 2019
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
wip lite
parent
bc7cd2cc
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
159 additions
and
63 deletions
+159
-63
create_logger.cpp
lite-example/create_logger.cpp
+3
-12
example.cpp
lite-example/example.cpp
+9
-15
spdlite.cpp
lite/spdlite.cpp
+68
-3
spdlite.h
lite/spdlite.h
+79
-33
No files found.
lite-example/create_logger.cpp
View file @
e32c856a
...
...
@@ -2,22 +2,13 @@
#include "spdlog/spdlog.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/stdout_sinks.h"
#define UNUSED(x) (void)(x)
// example of creating lite logger with console and file sink
spdlite
::
logger
spdlite
::
create_logger
(
void
*
ctx
)
spdlite
::
logger
create_logger
(
void
*
ctx
)
{
UNUSED
(
ctx
);
std
::
shared_ptr
<
spdlog
::
logger
>
logger_impl
;
auto
console_sink
=
std
::
make_shared
<
spdlog
::
sinks
::
stdout_color_sink_mt
>
();
console_sink
->
set_level
(
spdlog
::
level
::
debug
);
auto
file_sink
=
std
::
make_shared
<
spdlog
::
sinks
::
basic_file_sink_mt
>
(
"log.txt"
,
true
);
file_sink
->
set_level
(
spdlog
::
level
::
info
);
logger_impl
=
std
::
make_unique
<
spdlog
::
logger
>
(
"my-logger"
,
spdlog
::
sinks_init_list
{
console_sink
,
file_sink
});
logger_impl
->
set_level
(
spdlog
::
level
::
debug
);
return
spdlite
::
logger
(
std
::
move
(
logger_impl
));
return
spdlite
::
default_logger
();
}
lite-example/example.cpp
View file @
e32c856a
#include "spdlite.h"
int
main
()
{
auto
l
=
spdlite
::
create_logger
();
l
.
set_level
(
spdlite
::
level
::
trace
);
l
.
trace_printf
(
"Hello %s "
,
"GABI"
);
l
.
info_printf
(
"Hello %d"
,
12346
);
l
.
warn_printf
(
"Hello %f"
,
12346.5656
);
l
.
warn
(
"Hello {}"
,
"LITE :) "
);
auto
l2
=
l
.
clone
(
"logger2"
);
l2
.
debug
(
"HELLO"
);
auto
l3
=
std
::
move
(
l
);
l3
.
warn
(
"HELLO FROM L3"
);
{
spdlite
::
default_logger
().
set_level
(
spdlite
::
level
::
trace
);
spdlite
::
trace_printf
(
"Hello %d"
,
123
);
spdlite
::
debug_printf
(
"Hello %d"
,
123
);
spdlite
::
info_printf
(
"Hello %d"
,
123
);
spdlite
::
warn_printf
(
"Hello %d"
,
123
);
spdlite
::
error_printf
(
"Hello %d"
,
123
);
spdlite
::
critical_printf
(
"Hello %d"
,
123
);
}
\ No newline at end of file
lite/spdlite.cpp
View file @
e32c856a
...
...
@@ -16,6 +16,11 @@ spdlite::logger::logger(std::shared_ptr<spdlog::logger> impl)
impl_
=
std
::
move
(
impl
);
}
void
spdlite
::
logger
::
set_impl
(
std
::
shared_ptr
<
spdlog
::
logger
>
impl
)
{
impl_
=
std
::
move
(
impl
);
}
bool
spdlite
::
logger
::
should_log
(
spdlite
::
level
level
)
const
SPDLOG_NOEXCEPT
{
auto
spd_level
=
to_spdlog_level
(
level
);
...
...
@@ -30,7 +35,7 @@ void spdlite::logger::log(spdlite::level lvl, const string_view_t &sv)
void
spdlite
::
logger
::
log_printf
(
spdlite
::
level
lvl
,
const
char
*
format
,
va_list
args
)
{
char
buffer
[
500
];
char
buffer
[
256
];
auto
size
=
vsnprintf
(
buffer
,
sizeof
(
buffer
),
format
,
args
);
if
(
size
<
0
)
{
...
...
@@ -136,8 +141,68 @@ void spdlite::logger::log_formatted_(spdlite::level lvl, const fmt::memory_buffe
impl_
->
log
(
spd_level
,
spdlog
::
details
::
fmt_helper
::
to_string_view
(
formatted
));
}
spdlite
::
logger
&
spdlite
::
logger
::
default_logger
()
{
static
spdlite
::
logger
default_inst_
=
spdlite
::
logger
(
spdlog
::
default_logger
());
return
default_inst_
;
}
spdlite
::
logger
&
spdlite
::
default_logger
()
{
static
spdlite
::
logger
s_default
(
spdlog
::
default_logger
());
return
s_default
;
return
spdlite
::
logger
::
default_logger
();
}
// printf
void
spdlite
::
log_printf
(
spdlite
::
level
lvl
,
const
char
*
format
,
va_list
args
)
{
default_logger
().
log_printf
(
lvl
,
format
,
args
);
}
void
spdlite
::
trace_printf
(
const
char
*
format
,
...)
{
va_list
args
;
va_start
(
args
,
format
);
log_printf
(
level
::
trace
,
format
,
args
);
va_end
(
args
);
}
void
spdlite
::
debug_printf
(
const
char
*
format
,
...)
{
va_list
args
;
va_start
(
args
,
format
);
log_printf
(
level
::
debug
,
format
,
args
);
va_end
(
args
);
}
void
spdlite
::
info_printf
(
const
char
*
format
,
...)
{
va_list
args
;
va_start
(
args
,
format
);
log_printf
(
level
::
info
,
format
,
args
);
va_end
(
args
);
}
void
spdlite
::
warn_printf
(
const
char
*
format
,
...)
{
va_list
args
;
va_start
(
args
,
format
);
log_printf
(
level
::
warn
,
format
,
args
);
va_end
(
args
);
}
void
spdlite
::
error_printf
(
const
char
*
format
,
...)
{
va_list
args
;
va_start
(
args
,
format
);
log_printf
(
level
::
err
,
format
,
args
);
va_end
(
args
);
}
void
spdlite
::
critical_printf
(
const
char
*
format
,
...)
{
va_list
args
;
va_start
(
args
,
format
);
log_printf
(
level
::
critical
,
format
,
args
);
va_end
(
args
);
}
lite/spdlite.h
View file @
e32c856a
...
...
@@ -42,13 +42,15 @@ enum class level
class
logger
{
public
:
explicit
logger
(
std
::
shared_ptr
<
spdlog
::
logger
>
impl
);
explicit
logger
(
std
::
shared_ptr
<
spdlog
::
logger
>
impl
);
// logger() = default; //logger with nullptr impl
logger
(
const
logger
&
)
=
default
;
logger
(
logger
&&
)
=
default
;
logger
&
operator
=
(
const
logger
&
)
=
default
;
~
logger
()
=
default
;
void
set_impl
(
std
::
shared_ptr
<
spdlog
::
logger
>
impl
);
bool
should_log
(
spdlite
::
level
lvl
)
const
noexcept
;
template
<
typename
...
Args
>
...
...
@@ -65,26 +67,28 @@ public:
// log string view
void
log
(
spdlite
::
level
lvl
,
const
string_view_t
&
sv
);
// log using printf format
void
log_printf
(
spdlite
::
level
lvl
,
const
char
*
format
,
va_list
args
);
//
// trace
//
void
trace
(
const
char
*
msg
)
{
log
(
spdlite
::
level
::
trace
,
string_view_t
(
msg
));
}
template
<
typename
T
>
void
trace
(
const
T
&
msg
)
{
log
(
spdlite
::
level
::
trace
,
string_view_t
(
msg
));
}
template
<
typename
...
Args
>
void
trace
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
log
(
spdlite
::
level
::
trace
,
fmt
,
args
...);
}
void
trace_printf
(
const
char
*
format
,
...);
//
// debug
//
...
...
@@ -93,72 +97,101 @@ public:
log
(
spdlite
::
level
::
debug
,
string_view_t
(
msg
));
}
template
<
typename
T
>
void
debug
(
const
T
&
msg
)
{
log
(
spdlite
::
level
::
debug
,
string_view_t
(
msg
));
}
template
<
typename
...
Args
>
void
debug
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
log
(
spdlite
::
level
::
debug
,
fmt
,
args
...);
}
void
debug_printf
(
const
char
*
format
,
...);
// info
// info
void
info
(
const
char
*
msg
)
{
log
(
spdlite
::
level
::
info
,
string_view_t
(
msg
));
}
template
<
typename
T
>
void
info
(
const
T
&
msg
)
{
log
(
spdlite
::
level
::
info
,
string_view_t
(
msg
));
}
template
<
typename
...
Args
>
void
info
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
log
(
spdlite
::
level
::
info
,
fmt
,
args
...);
}
void
info_printf
(
const
char
*
format
,
...);
// warn
void
warn
(
const
char
*
msg
)
{
log
(
spdlite
::
level
::
warn
,
string_view_t
(
msg
));
}
template
<
typename
T
>
void
warn
(
const
T
&
msg
)
{
log
(
spdlite
::
level
::
warn
,
string_view_t
(
msg
));
}
template
<
typename
...
Args
>
void
warn
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
log
(
spdlite
::
level
::
warn
,
fmt
,
args
...);
}
void
warn_printf
(
const
char
*
format
,
...);
// error
void
error
(
const
char
*
msg
)
{
log
(
spdlite
::
level
::
err
,
string_view_t
(
msg
));
}
template
<
typename
T
>
void
error
(
const
T
&
msg
)
{
log
(
spdlite
::
level
::
err
,
string_view_t
(
msg
));
}
template
<
typename
...
Args
>
void
error
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
log
(
spdlite
::
level
::
err
,
fmt
,
args
...);
}
void
error_printf
(
const
char
*
format
,
...);
// critical
void
critical
(
const
char
*
msg
)
{
log
(
spdlite
::
level
::
critical
,
string_view_t
(
msg
));
}
template
<
typename
T
>
void
critical
(
const
T
&
msg
)
{
log
(
spdlite
::
level
::
critical
,
string_view_t
(
msg
));
}
template
<
typename
...
Args
>
void
critical
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
log
(
spdlite
::
level
::
critical
,
fmt
,
args
...);
}
// printf formatting
void
log_printf
(
spdlite
::
level
lvl
,
const
char
*
format
,
va_list
args
);
void
trace_printf
(
const
char
*
format
,
...);
void
debug_printf
(
const
char
*
format
,
...);
void
info_printf
(
const
char
*
format
,
...);
void
warn_printf
(
const
char
*
format
,
...);
void
error_printf
(
const
char
*
format
,
...);
void
critical_printf
(
const
char
*
format
,
...);
// setters/getters
void
set_level
(
spdlite
::
level
level
)
noexcept
;
// setters/getters
void
set_level
(
spdlite
::
level
level
)
noexcept
;
void
set_pattern
(
std
::
string
pattern
)
noexcept
;
spdlite
::
level
level
()
const
noexcept
;
std
::
string
name
()
const
noexcept
;
...
...
@@ -167,56 +200,69 @@ public:
// flush
void
flush
();
void
flush_on
(
spdlite
::
level
log_level
);
//clone with new name
//
clone with new name
spdlite
::
logger
clone
(
std
::
string
logger_name
);
static
spdlite
::
logger
&
default_logger
();
protected
:
std
::
shared_ptr
<
spdlog
::
logger
>
impl_
;
void
log_formatted_
(
spdlite
::
level
lvl
,
const
fmt
::
memory_buffer
&
formatted
);
};
//
// spdlite namespace functions - forward the calls to the default_logger.
//
spdlite
::
logger
&
default_logger
();
template
<
typename
...
Args
>
void
trace
(
const
char
*
fmt
,
const
Args
&
...
args
)
inline
void
trace
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
default_logger
().
trace
(
fmt
,
args
...);
}
template
<
typename
...
Args
>
void
debug
(
const
char
*
fmt
,
const
Args
&
...
args
)
inline
void
debug
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
default_logger
().
debug
(
fmt
,
args
...);
}
template
<
typename
...
Args
>
void
info
(
const
char
*
fmt
,
const
Args
&
...
args
)
inline
void
info
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
default_logger
().
info
(
fmt
,
args
...);
}
template
<
typename
...
Args
>
void
warn
(
const
char
*
fmt
,
const
Args
&
...
args
)
inline
void
warn
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
default_logger
().
warn
(
fmt
,
args
...);
}
template
<
typename
...
Args
>
void
error
(
const
char
*
fmt
,
const
Args
&
...
args
)
inline
void
error
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
default_logger
().
error
(
fmt
,
args
...);
}
template
<
typename
...
Args
>
void
critical
(
const
char
*
fmt
,
const
Args
&
...
args
)
inline
void
critical
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
default_logger
().
critical
(
fmt
,
args
...);
}
// user implemented factory to create lite logger
// implement it in a seperated and dedicated compilation unit for fast compiles.
logger
create_logger
(
void
*
ctx
=
nullptr
);
void
log_printf
(
spdlite
::
level
lvl
,
const
char
*
format
,
va_list
args
);
void
trace_printf
(
const
char
*
format
,
...);
void
debug_printf
(
const
char
*
format
,
...);
void
info_printf
(
const
char
*
format
,
...);
void
warn_printf
(
const
char
*
format
,
...);
void
error_printf
(
const
char
*
format
,
...);
void
critical_printf
(
const
char
*
format
,
...);
}
// namespace spdlite
// user implemented factory to create lite logger
// implement it in a seperated and dedicated compilation unit for fast compiles.
spdlite
::
logger
create_logger
(
void
*
ctx
=
nullptr
);
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