Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
spdlog
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
spdlog
Commits
8923922f
Commit
8923922f
authored
Sep 27, 2020
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleaned level loading from env var
parent
7542e42e
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
51 additions
and
48 deletions
+51
-48
include/spdlog/cfg/argv.h
include/spdlog/cfg/argv.h
+1
-2
include/spdlog/cfg/env.h
include/spdlog/cfg/env.h
+1
-2
include/spdlog/cfg/helpers-inl.h
include/spdlog/cfg/helpers-inl.h
+20
-8
include/spdlog/cfg/helpers.h
include/spdlog/cfg/helpers.h
+1
-1
include/spdlog/details/registry-inl.h
include/spdlog/details/registry-inl.h
+17
-29
include/spdlog/details/registry.h
include/spdlog/details/registry.h
+4
-2
tests/test_cfg.cpp
tests/test_cfg.cpp
+7
-4
No files found.
include/spdlog/cfg/argv.h
View file @
8923922f
...
...
@@ -30,8 +30,7 @@ inline void load_argv_levels(int argc, const char **argv)
if
(
arg
.
find
(
spdlog_level_prefix
)
==
0
)
{
auto
levels_string
=
arg
.
substr
(
spdlog_level_prefix
.
size
());
auto
levels
=
helpers
::
extract_levels
(
levels_string
);
details
::
registry
::
instance
().
set_levels
(
std
::
move
(
levels
));
helpers
::
load_levels
(
levels_string
);
}
}
}
...
...
include/spdlog/cfg/env.h
View file @
8923922f
...
...
@@ -30,8 +30,7 @@ inline void load_env_levels()
auto
env_val
=
details
::
os
::
getenv
(
"SPDLOG_LEVEL"
);
if
(
!
env_val
.
empty
())
{
auto
levels
=
helpers
::
extract_levels
(
env_val
);
details
::
registry
::
instance
().
set_levels
(
std
::
move
(
levels
));
helpers
::
load_levels
(
env_val
);
}
}
...
...
include/spdlog/cfg/helpers-inl.h
View file @
8923922f
...
...
@@ -73,19 +73,22 @@ inline std::unordered_map<std::string, std::string> extract_key_vals_(const std:
continue
;
}
auto
kv
=
extract_kv_
(
'='
,
token
);
if
(
kv
.
first
.
empty
())
{
kv
.
first
=
"*"
;
}
rv
[
kv
.
first
]
=
kv
.
second
;
}
return
rv
;
}
SPDLOG_INLINE
std
::
unordered_map
<
std
::
string
,
spdlog
::
level
::
level_enum
>
extract
_levels
(
const
std
::
string
&
input
)
SPDLOG_INLINE
void
load
_levels
(
const
std
::
string
&
input
)
{
if
(
input
.
empty
()
||
input
.
size
()
>
512
)
{
return
;
}
auto
key_vals
=
extract_key_vals_
(
input
);
std
::
unordered_map
<
std
::
string
,
spdlog
::
level
::
level_enum
>
rv
;
std
::
unordered_map
<
std
::
string
,
level
::
level_enum
>
levels
;
level
::
level_enum
global_level
=
level
::
info
;
bool
global_level_found
=
false
;
for
(
auto
&
name_level
:
key_vals
)
{
...
...
@@ -97,9 +100,18 @@ SPDLOG_INLINE std::unordered_map<std::string, spdlog::level::level_enum> extract
{
continue
;
}
rv
[
logger_name
]
=
level
;
if
(
logger_name
.
empty
())
// no logger name indicate global level
{
global_level_found
=
true
;
global_level
=
level
;
}
else
{
levels
[
logger_name
]
=
level
;
}
}
return
rv
;
details
::
registry
::
instance
().
set_levels
(
std
::
move
(
levels
),
global_level_found
?
&
global_level
:
nullptr
);
}
}
// namespace helpers
...
...
include/spdlog/cfg/helpers.h
View file @
8923922f
...
...
@@ -18,7 +18,7 @@ namespace helpers {
// turn off all logging except for logger1: "off,logger1=debug"
// turn off all logging except for logger1 and logger2: "off,logger1=debug,logger2=info"
//
SPDLOG_API
std
::
unordered_map
<
std
::
string
,
spdlog
::
level
::
level_enum
>
extract
_levels
(
const
std
::
string
&
txt
);
SPDLOG_API
void
load
_levels
(
const
std
::
string
&
txt
);
}
// namespace helpers
}
// namespace cfg
...
...
include/spdlog/details/registry-inl.h
View file @
8923922f
...
...
@@ -57,28 +57,6 @@ SPDLOG_INLINE void registry::register_logger(std::shared_ptr<logger> new_logger)
register_logger_
(
std
::
move
(
new_logger
));
}
// set level if this logger was configured in the cfg_levels_
// return true if found and set
SPDLOG_INLINE
bool
registry
::
set_level_from_cfg_
(
logger
*
logger
)
{
if
(
cfg_levels_
.
empty
())
{
return
false
;
}
auto
cfg_level_it
=
cfg_levels_
.
find
(
logger
->
name
());
if
(
cfg_level_it
==
cfg_levels_
.
end
())
{
// if logger name not found, set it anyway if "*" exists (i.e. all loggers)
cfg_level_it
=
cfg_levels_
.
find
(
"*"
);
}
if
(
cfg_level_it
!=
cfg_levels_
.
end
())
{
logger
->
set_level
(
cfg_level_it
->
second
);
return
true
;
}
return
false
;
}
SPDLOG_INLINE
void
registry
::
initialize_logger
(
std
::
shared_ptr
<
logger
>
new_logger
)
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
logger_map_mutex_
);
...
...
@@ -89,10 +67,10 @@ SPDLOG_INLINE void registry::initialize_logger(std::shared_ptr<logger> new_logge
new_logger
->
set_error_handler
(
err_handler_
);
}
if
(
!
set_level_from_cfg_
(
new_logger
.
get
()))
{
new_logger
->
set_level
(
global_log_level_
)
;
}
// set new level according to previously configured level or default level
auto
it
=
log_levels_
.
find
(
new_logger
->
name
());
auto
new_level
=
it
!=
log_levels_
.
end
()
?
it
->
second
:
global_log_level_
;
new_logger
->
set_level
(
new_level
);
new_logger
->
flush_on
(
flush_level_
);
...
...
@@ -289,14 +267,24 @@ SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_registrat
automatic_registration_
=
automatic_registration
;
}
SPDLOG_INLINE
void
registry
::
set_levels
(
std
::
unordered_map
<
std
::
string
,
spdlog
::
level
::
level_enum
>
levels
)
SPDLOG_INLINE
void
registry
::
set_levels
(
log_levels
levels
,
level
::
level_enum
*
global_level
)
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
logger_map_mutex_
);
cfg_levels_
=
std
::
move
(
levels
);
log_levels_
=
std
::
move
(
levels
);
auto
global_level_requested
=
global_level
!=
nullptr
;
global_log_level_
=
global_level_requested
?
*
global_level
:
global_log_level_
;
for
(
auto
&
logger
:
loggers_
)
{
set_level_from_cfg_
(
logger
.
second
.
get
());
auto
logger_entry
=
log_levels_
.
find
(
logger
.
first
);
if
(
logger_entry
!=
log_levels_
.
end
())
{
logger
.
second
->
set_level
(
logger_entry
->
second
);
}
else
if
(
global_level_requested
)
{
logger
.
second
->
set_level
(
*
global_level
);
}
}
}
...
...
include/spdlog/details/registry.h
View file @
8923922f
...
...
@@ -27,6 +27,7 @@ class periodic_worker;
class
SPDLOG_API
registry
{
public:
using
log_levels
=
std
::
unordered_map
<
std
::
string
,
level
::
level_enum
>
;
registry
(
const
registry
&
)
=
delete
;
registry
&
operator
=
(
const
registry
&
)
=
delete
;
...
...
@@ -79,7 +80,8 @@ public:
void
set_automatic_registration
(
bool
automatic_registration
);
void
set_levels
(
std
::
unordered_map
<
std
::
string
,
spdlog
::
level
::
level_enum
>
levels
);
// set levels for all existing/future loggers. global_level can be null if should not set.
void
set_levels
(
log_levels
levels
,
level
::
level_enum
*
global_level
);
static
registry
&
instance
();
...
...
@@ -93,7 +95,7 @@ private:
std
::
mutex
logger_map_mutex_
,
flusher_mutex_
;
std
::
recursive_mutex
tp_mutex_
;
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
logger
>>
loggers_
;
std
::
unordered_map
<
std
::
string
,
spdlog
::
level
::
level_enum
>
cf
g_levels_
;
log_levels
lo
g_levels_
;
std
::
unique_ptr
<
formatter
>
formatter_
;
spdlog
::
level
::
level_enum
global_log_level_
=
level
::
info
;
level
::
level_enum
flush_level_
=
level
::
off
;
...
...
tests/test_cfg.cpp
View file @
8923922f
...
...
@@ -42,21 +42,23 @@ TEST_CASE("argv2", "[cfg]")
auto
l1
=
spdlog
::
create
<
test_sink_st
>
(
"l1"
);
REQUIRE
(
l1
->
level
()
==
spdlog
::
level
::
warn
);
REQUIRE
(
spdlog
::
default_logger
()
->
level
()
==
spdlog
::
level
::
trace
);
spdlog
::
set_level
(
spdlog
::
level
::
info
);
}
TEST_CASE
(
"argv3"
,
"[cfg]"
)
{
spdlog
::
set_level
(
spdlog
::
level
::
trace
);
spdlog
::
drop
(
"l1"
);
const
char
*
argv
[]
=
{
"ignore"
,
"SPDLOG_LEVEL="
};
const
char
*
argv
[]
=
{
"ignore"
,
"SPDLOG_LEVEL=
junk_name=warn
"
};
load_argv_levels
(
2
,
argv
);
auto
l1
=
spdlog
::
create
<
test_sink_st
>
(
"l1"
);
REQUIRE
(
l1
->
level
()
==
spdlog
::
level
::
info
);
REQUIRE
(
spdlog
::
default_logger
()
->
level
()
==
spdlog
::
level
::
info
);
REQUIRE
(
l1
->
level
()
==
spdlog
::
level
::
trace
);
REQUIRE
(
spdlog
::
default_logger
()
->
level
()
==
spdlog
::
level
::
trace
);
}
TEST_CASE
(
"argv4"
,
"[cfg]"
)
{
spdlog
::
set_level
(
spdlog
::
level
::
info
);
spdlog
::
drop
(
"l1"
);
const
char
*
argv
[]
=
{
"ignore"
,
"SPDLOG_LEVEL=junk"
};
load_argv_levels
(
2
,
argv
);
...
...
@@ -66,6 +68,7 @@ TEST_CASE("argv4", "[cfg]")
TEST_CASE
(
"argv5"
,
"[cfg]"
)
{
spdlog
::
set_level
(
spdlog
::
level
::
info
);
spdlog
::
drop
(
"l1"
);
const
char
*
argv
[]
=
{
"ignore"
,
"ignore"
,
"SPDLOG_LEVEL=l1=warn,trace"
};
load_argv_levels
(
3
,
argv
);
...
...
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