1
0
mirror of https://github.com/gabime/spdlog.git synced 2025-01-15 17:27:57 +00:00

Compare commits

...

12 Commits

Author SHA1 Message Date
gabime
5bf99dfd61 Renamed loaders.cpp to cfg.cpp 2019-12-22 22:51:52 +02:00
gabime
bc42415ceb Updated fmt.cpp to 6.1.2 2019-12-22 22:51:12 +02:00
gabime
284e6a80ac Fixed cfg tests 2019-12-22 22:33:19 +02:00
gabime
0243882238 Updated example 2019-12-22 20:58:16 +02:00
gabime
877eee408e renamed loaders with cfg 2019-12-22 20:40:19 +02:00
gabime
8dd54de326 Merge remote-tracking branch 'origin/v1.x' into conf-env3 2019-12-22 20:29:31 +02:00
Gabi Melman
09d729bfba
Update README.md 2019-12-22 19:46:56 +02:00
Gabi Melman
9caaca742e
Update README.md 2019-12-22 19:42:08 +02:00
Gabi Melman
ac95c3ffbf
Update README.md 2019-12-22 19:40:50 +02:00
Gabi Melman
9715d80030
Update README.md 2019-12-22 19:39:52 +02:00
Gabi Melman
a0a1e5c078
Update README.md 2019-12-22 19:37:42 +02:00
Gabi Melman
d7ba1fdd3d
Update README.md 2019-12-22 19:36:58 +02:00
16 changed files with 429 additions and 82 deletions

View File

@ -103,7 +103,7 @@ set(SPDLOG_SRCS
src/color_sinks.cpp
src/file_sinks.cpp
src/async.cpp
src/loaders.cpp)
src/cfg.cpp)
if(NOT SPDLOG_FMT_EXTERNAL AND NOT SPDLOG_FMT_EXTERNAL_HO)

View File

@ -37,7 +37,7 @@ $ cmake .. && make -j
* Very fast (see [benchmarks](#benchmarks) below).
* Headers only, just copy and use. Or use as a compiled library.
* Feature rich formatting, using the excellent [fmt](https://github.com/fmtlib/fmt) library.
* **New!** [Backtrace](#backtrace-support) support - store debug or other messages in a ring buffer and display later on demand.
* **New!** [Backtrace](#backtrace-support) support - store debug messages in a ring buffer and display later on demand.
* Fast asynchronous mode (optional)
* [Custom](https://github.com/gabime/spdlog/wiki/3.-Custom-formatting) formatting.
* Multi/Single threaded loggers.
@ -158,7 +158,7 @@ spdlog::dump_backtrace(); // log them now! show the last 32 messages
#### Periodic flush
```c++
// periodically flush all *registered* loggers every 3 seconds:
// warning: only use if all your loggers are thread safe!
// warning: only use if all your loggers are thread safe ("_mt" loggers)
spdlog::flush_every(std::chrono::seconds(3));
```
@ -321,7 +321,7 @@ Below are some [benchmarks](https://github.com/gabime/spdlog/blob/v1.x/bench/ben
[info] daily_st Elapsed: 0.42 secs 2,393,298/sec
[info] null_st Elapsed: 0.04 secs 27,446,957/sec
[info] **************************************************************
[info] 10 threads sharing same logger, 1,000,000 iterations
[info] 10 threads, competing over the same logger object, 1,000,000 iterations
[info] **************************************************************
[info] basic_mt Elapsed: 0.60 secs 1,659,613/sec
[info] rotating_mt Elapsed: 0.62 secs 1,612,493/sec
@ -335,7 +335,6 @@ Below are some [benchmarks](https://github.com/gabime/spdlog/blob/v1.x/bench/ben
[info] Threads : 10
[info] Queue : 8,192 slots
[info] Queue memory : 8,192 x 272 = 2,176 KB
[info] Total iters : 3
[info] -------------------------------------------------
[info]
[info] *********************************

View File

@ -10,6 +10,8 @@ void stdout_logger_example();
void basic_example();
void rotating_example();
void daily_example();
void env_cfg_example();
void argv_cfg_example(int, char*[]);
void async_example();
void binary_example();
void trace_example();
@ -18,17 +20,237 @@ void user_defined_example();
void err_handler_example();
void syslog_example();
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/loaders/env.h>
#include <spdlog/loaders/argv.h>
#include "spdlog/spdlog.h"
int main(int args, char *argv[])
{
spdlog::loaders::load_env();
spdlog::loaders::load_argv(args, argv);
spdlog::info("HELLO INFO");
spdlog::info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH);
spdlog::warn("Easy padding in numbers like {:08d}", 12);
spdlog::critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);
spdlog::info("Support for floats {:03.2f}", 1.23456);
spdlog::info("Positional args are {1} {0}..", "too", "supported");
spdlog::info("{:>8} aligned, {:<8} aligned", "right", "left");
auto l1 = spdlog::stderr_color_st("l1");
l1->trace("L1 TRACE");
}
// Runtime log levels
spdlog::set_level(spdlog::level::info); // Set global log level to info
spdlog::debug("This message should not be displayed!");
spdlog::set_level(spdlog::level::trace); // Set specific logger's log level
spdlog::debug("This message should be displayed..");
// Customize msg format for all loggers
spdlog::set_pattern("[%H:%M:%S %z] [%^%L%$] [thread %t] %v");
spdlog::info("This an info message with custom format");
spdlog::set_pattern("%+"); // back to default format
spdlog::set_level(spdlog::level::info);
// Backtrace support
// Loggers can store in a ring buffer all messages (including debug/trace) for later inspection.
// When needed, call dump_backtrace() to see what happened:
spdlog::enable_backtrace(10); // create ring buffer with capacity of 10 messages
for (int i = 0; i < 100; i++)
{
spdlog::debug("Backtrace message {}", i); // not logged..
}
// e.g. if some error happened:
spdlog::dump_backtrace(); // log them now!
try
{
stdout_logger_example();
basic_example();
rotating_example();
daily_example();
async_example();
binary_example();
multi_sink_example();
user_defined_example();
err_handler_example();
trace_example();
// Init levels from SPDLOG_LEVEL env variable
env_cfg_example();
// Init levels from args (example.exe "SPDLOG_LEVEL=trace,l2=info,l3=off")
argv_cfg_example(args, argv);
// Flush all *registered* loggers using a worker thread every 3 seconds.
// note: registered loggers *must* be thread safe for this to work correctly!
spdlog::flush_every(std::chrono::seconds(3));
// Apply some function on all registered loggers
spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) { l->info("End of example."); });
// Release all spdlog resources, and drop all loggers in the registry.
// This is optional (only mandatory if using windows + async log).
spdlog::shutdown();
}
// Exceptions will only be thrown upon failed logger or sink construction (not during logging).
catch (const spdlog::spdlog_ex &ex)
{
std::printf("Log initialization failed: %s\n", ex.what());
return 1;
}
}
#include "spdlog/sinks/stdout_color_sinks.h"
// or #include "spdlog/sinks/stdout_sinks.h" if no colors needed.
void stdout_logger_example()
{
// Create color multi threaded logger.
auto console = spdlog::stdout_color_mt("console");
// or for stderr:
// auto console = spdlog::stderr_color_mt("error-logger");
}
#include "spdlog/sinks/basic_file_sink.h"
void basic_example()
{
// Create basic file logger (not rotated).
auto my_logger = spdlog::basic_logger_mt("file_logger", "logs/basic-log.txt");
}
#include "spdlog/sinks/rotating_file_sink.h"
void rotating_example()
{
// Create a file rotating logger with 5mb size max and 3 rotated files.
auto rotating_logger = spdlog::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3);
}
#include "spdlog/sinks/daily_file_sink.h"
void daily_example()
{
// Create a daily logger - a new file is created every day on 2:30am.
auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
}
#include "spdlog/cfg/env.h"
void env_cfg_example()
{
// set levels from SPDLOG_LEVEL env variable
spdlog::cfg::load_env();
}
#include "spdlog/cfg/argv.h"
void argv_cfg_example(int args, char *argv[])
{
spdlog::cfg::load_argv(args, argv);
}
#include "spdlog/async.h"
void async_example()
{
// Default thread pool settings can be modified *before* creating the async logger:
// spdlog::init_thread_pool(32768, 1); // queue with max 32k items 1 backing thread.
auto async_file = spdlog::basic_logger_mt<spdlog::async_factory>("async_file_logger", "logs/async_log.txt");
// alternatively:
// auto async_file = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("async_file_logger", "logs/async_log.txt");
for (int i = 1; i < 101; ++i)
{
async_file->info("Async message #{}", i);
}
}
// Log binary data as hex.
// Many types of std::container<char> types can be used.
// Iterator ranges are supported too.
// Format flags:
// {:X} - print in uppercase.
// {:s} - don't separate each byte with space.
// {:p} - don't print the position on each line start.
// {:n} - don't split the output to lines.
#include "spdlog/fmt/bin_to_hex.h"
void binary_example()
{
std::vector<char> buf(80);
for (int i = 0; i < 80; i++)
{
buf.push_back(static_cast<char>(i & 0xff));
}
spdlog::info("Binary example: {}", spdlog::to_hex(buf));
spdlog::info("Another binary example:{:n}", spdlog::to_hex(std::begin(buf), std::begin(buf) + 10));
// more examples:
// logger->info("uppercase: {:X}", spdlog::to_hex(buf));
// logger->info("uppercase, no delimiters: {:Xs}", spdlog::to_hex(buf));
// logger->info("uppercase, no delimiters, no position info: {:Xsp}", spdlog::to_hex(buf));
}
// Compile time log levels.
// define SPDLOG_ACTIVE_LEVEL to required level (e.g. SPDLOG_LEVEL_TRACE)
void trace_example()
{
// trace from default logger
SPDLOG_TRACE("Some trace message.. {} ,{}", 1, 3.23);
// debug from default logger
SPDLOG_DEBUG("Some debug message.. {} ,{}", 1, 3.23);
// trace from logger object
auto logger = spdlog::get("file_logger");
SPDLOG_LOGGER_TRACE(logger, "another trace message");
}
// A logger with multiple sinks (stdout and file) - each with a different format and log level.
void multi_sink_example()
{
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
console_sink->set_level(spdlog::level::warn);
console_sink->set_pattern("[multi_sink_example] [%^%l%$] %v");
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/multisink.txt", true);
file_sink->set_level(spdlog::level::trace);
spdlog::logger logger("multi_sink", {console_sink, file_sink});
logger.set_level(spdlog::level::debug);
logger.warn("this should appear in both console and file");
logger.info("this message should not appear in the console, only in the file");
}
// User defined types logging by implementing operator<<
#include "spdlog/fmt/ostr.h" // must be included
struct my_type
{
int i;
template<typename OStream>
friend OStream &operator<<(OStream &os, const my_type &c)
{
return os << "[my_type i=" << c.i << "]";
}
};
void user_defined_example()
{
spdlog::info("user defined type: {}", my_type{14});
}
// Custom error handler. Will be triggered on log failure.
void err_handler_example()
{
// can be set globally or per logger(logger->set_error_handler(..))
spdlog::set_error_handler([](const std::string &msg) { printf("*** Custom log error handler: %s ***\n", msg.c_str()); });
}
// syslog example (linux/osx/freebsd)
#ifndef _WIN32
#include "spdlog/sinks/syslog_sink.h"
void syslog_example()
{
std::string ident = "spdlog-example";
auto syslog_logger = spdlog::syslog_logger_mt("syslog", ident, LOG_PID);
syslog_logger->warn("This is warning that will end up in syslog.");
}
#endif
// Android example.
#if defined(__ANDROID__)
#include "spdlog/sinks/android_sink.h"
void android_example()
{
std::string tag = "spdlog-android";
auto android_logger = spdlog::android_logger_mt("android", tag);
android_logger->critical("Use \"adb shell logcat\" to view this message.");
}
#endif

View File

@ -2,7 +2,7 @@
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#include <spdlog/loaders/helpers.h>
#include <spdlog/cfg/helpers.h>
#include <spdlog/details/os.h>
//
@ -18,7 +18,7 @@
// example.exe "SPDLOG_LEVEL=off,logger1=debug,logger2=info"
namespace spdlog {
namespace loaders {
namespace cfg {
// search for SPDLOG_LEVEL= in the args and use it to init the levels
void load_argv(int args, char **argv)
@ -36,5 +36,5 @@ void load_argv(int args, char **argv)
}
}
} // namespace loaders
} // namespace cfg
} // namespace spdlog

View File

@ -2,7 +2,7 @@
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#include <spdlog/loaders/helpers.h>
#include <spdlog/cfg/helpers.h>
#include <spdlog/details/registry.h>
#include <spdlog/details/os.h>
@ -24,7 +24,7 @@
// export SPDLOG_LEVEL="off,logger1=debug,logger2=info"
namespace spdlog {
namespace loaders {
namespace cfg {
void load_env()
{
auto env_val = details::os::getenv("SPDLOG_LEVEL");
@ -32,5 +32,5 @@ void load_env()
details::registry::instance().update_levels(std::move(levels));
}
} // namespace loaders
} // namespace cfg
} // namespace spdlog

View File

@ -4,7 +4,7 @@
#pragma once
#ifndef SPDLOG_HEADER_ONLY
#include <spdlog/loaders/helpers.h>
#include <spdlog/cfg/helpers.h>
#endif
#include <spdlog/spdlog.h>
@ -16,7 +16,7 @@
#include <sstream>
namespace spdlog {
namespace loaders {
namespace cfg {
namespace helpers {
// inplace convert to lowercase
@ -99,5 +99,5 @@ SPDLOG_INLINE log_levels extract_levels(const std::string &input)
}
} // namespace helpers
} // namespace loaders
} // namespace cfg
} // namespace spdlog

View File

@ -3,10 +3,10 @@
#pragma once
#include <spdlog/loaders/log_levels.h>
#include <spdlog/cfg/log_levels.h>
namespace spdlog {
namespace loaders {
namespace cfg {
namespace helpers {
//
// Init levels from given string
@ -20,7 +20,7 @@ namespace helpers {
log_levels extract_levels(const std::string &txt);
} // namespace helpers
} // namespace loaders
} // namespace cfg
} // namespace spdlog
#ifdef SPDLOG_HEADER_ONLY

View File

@ -8,7 +8,7 @@
#include <unordered_map>
namespace spdlog {
namespace loaders {
namespace cfg {
class log_levels
{
std::unordered_map<std::string, spdlog::level::level_enum> levels_;
@ -43,5 +43,5 @@ public:
return default_level_;
}
};
} // namespace loaders
} // namespace cfg
} // namespace spdlog

View File

@ -260,7 +260,7 @@ SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_registrat
automatic_registration_ = automatic_registration;
}
SPDLOG_INLINE void registry::update_levels(loaders::log_levels levels)
SPDLOG_INLINE void registry::update_levels(cfg::log_levels levels)
{
std::lock_guard<std::mutex> lock(logger_map_mutex_);
levels_ = std::move(levels);

View File

@ -9,7 +9,7 @@
// This class is thread safe
#include <spdlog/common.h>
#include <spdlog/loaders/log_levels.h>
#include <spdlog/cfg/log_levels.h>
#include <chrono>
#include <functional>
@ -80,7 +80,7 @@ public:
void set_automatic_registration(bool automatic_registration);
void update_levels(loaders::log_levels levels);
void update_levels(cfg::log_levels levels);
static registry &instance();
@ -93,7 +93,7 @@ private:
std::mutex logger_map_mutex_, flusher_mutex_;
std::recursive_mutex tp_mutex_;
std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;
loaders::log_levels levels_;
cfg::log_levels levels_;
std::unique_ptr<formatter> formatter_;
level::level_enum flush_level_ = level::off;
void (*err_handler_)(const std::string &msg);

View File

@ -83,7 +83,7 @@ spdlog_srcs = files([
'src/file_sinks.cpp',
'src/spdlog.cpp',
'src/stdout_sinks.cpp',
'src/loaders.cpp'
'src/cfg.cpp'
])
if not get_option('external_fmt')

View File

@ -9,45 +9,174 @@
#if !defined(SPDLOG_FMT_EXTERNAL)
#include "spdlog/fmt/bundled/format-inl.h"
FMT_BEGIN_NAMESPACE
template struct FMT_API internal::basic_data<void>;
namespace internal {
template <typename T>
int format_float(char* buf, std::size_t size, const char* format, int precision,
T value) {
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
if (precision > 100000)
throw std::runtime_error(
"fuzz mode - avoid large allocation inside snprintf");
#endif
// Suppress the warning about nonliteral format string.
auto snprintf_ptr = FMT_SNPRINTF;
return precision < 0 ? snprintf_ptr(buf, size, format, value)
: snprintf_ptr(buf, size, format, precision, value);
}
struct sprintf_specs {
int precision;
char type;
bool alt : 1;
template <typename Char>
constexpr sprintf_specs(basic_format_specs<Char> specs)
: precision(specs.precision), type(specs.type), alt(specs.alt) {}
constexpr bool has_precision() const { return precision >= 0; }
};
// This is deprecated and is kept only to preserve ABI compatibility.
template <typename Double>
char* sprintf_format(Double value, internal::buffer<char>& buf,
sprintf_specs specs) {
// Buffer capacity must be non-zero, otherwise MSVC's vsnprintf_s will fail.
FMT_ASSERT(buf.capacity() != 0, "empty buffer");
// Build format string.
enum { max_format_size = 10 }; // longest format: %#-*.*Lg
char format[max_format_size];
char* format_ptr = format;
*format_ptr++ = '%';
if (specs.alt || !specs.type) *format_ptr++ = '#';
if (specs.precision >= 0) {
*format_ptr++ = '.';
*format_ptr++ = '*';
}
if (std::is_same<Double, long double>::value) *format_ptr++ = 'L';
char type = specs.type;
if (type == '%')
type = 'f';
else if (type == 0 || type == 'n')
type = 'g';
#if FMT_MSC_VER
if (type == 'F') {
// MSVC's printf doesn't support 'F'.
type = 'f';
}
#endif
*format_ptr++ = type;
*format_ptr = '\0';
// Format using snprintf.
char* start = nullptr;
char* decimal_point_pos = nullptr;
for (;;) {
std::size_t buffer_size = buf.capacity();
start = &buf[0];
int result =
format_float(start, buffer_size, format, specs.precision, value);
if (result >= 0) {
unsigned n = internal::to_unsigned(result);
if (n < buf.capacity()) {
// Find the decimal point.
auto p = buf.data(), end = p + n;
if (*p == '+' || *p == '-') ++p;
if (specs.type != 'a' && specs.type != 'A') {
while (p < end && *p >= '0' && *p <= '9') ++p;
if (p < end && *p != 'e' && *p != 'E') {
decimal_point_pos = p;
if (!specs.type) {
// Keep only one trailing zero after the decimal point.
++p;
if (*p == '0') ++p;
while (p != end && *p >= '1' && *p <= '9') ++p;
char* where = p;
while (p != end && *p == '0') ++p;
if (p == end || *p < '0' || *p > '9') {
if (p != end) std::memmove(where, p, to_unsigned(end - p));
n -= static_cast<unsigned>(p - where);
}
}
}
}
buf.resize(n);
break; // The buffer is large enough - continue with formatting.
}
buf.reserve(n + 1);
} else {
// If result is negative we ask to increase the capacity by at least 1,
// but as std::vector, the buffer grows exponentially.
buf.reserve(buf.capacity() + 1);
}
}
return decimal_point_pos;
}
} // namespace internal
template FMT_API char* internal::sprintf_format(double, internal::buffer<char>&,
sprintf_specs);
template FMT_API char* internal::sprintf_format(long double,
internal::buffer<char>&,
sprintf_specs);
template struct FMT_API internal::basic_data<void>;
// Workaround a bug in MSVC2013 that prevents instantiation of format_float.
int (*instantiate_format_float)(double, int, internal::float_specs, internal::buffer<char> &) = internal::format_float;
int (*instantiate_format_float)(double, int, internal::float_specs,
internal::buffer<char>&) =
internal::format_float;
#ifndef FMT_STATIC_THOUSANDS_SEPARATOR
template FMT_API internal::locale_ref::locale_ref(const std::locale &loc);
template FMT_API std::locale internal::locale_ref::get<std::locale>() const;
template FMT_API internal::locale_ref::locale_ref(const std::locale& loc);
template FMT_API std::locale internal::locale_ref::get<std::locale>() const;
#endif
// Explicit instantiations for char.
template FMT_API std::string internal::grouping_impl<char>(locale_ref);
template FMT_API char internal::thousands_sep_impl(locale_ref);
template FMT_API char internal::decimal_point_impl(locale_ref);
template FMT_API std::string internal::grouping_impl<char>(locale_ref);
template FMT_API char internal::thousands_sep_impl(locale_ref);
template FMT_API char internal::decimal_point_impl(locale_ref);
template FMT_API void internal::buffer<char>::append(const char *, const char *);
template FMT_API void internal::buffer<char>::append(const char*, const char*);
template FMT_API void internal::arg_map<format_context>::init(const basic_format_args<format_context> &args);
template FMT_API void internal::arg_map<format_context>::init(
const basic_format_args<format_context>& args);
template FMT_API std::string internal::vformat<char>(string_view, basic_format_args<format_context>);
template FMT_API std::string internal::vformat<char>(
string_view, basic_format_args<format_context>);
template FMT_API format_context::iterator internal::vformat_to(internal::buffer<char> &, string_view, basic_format_args<format_context>);
template FMT_API format_context::iterator internal::vformat_to(
internal::buffer<char>&, string_view, basic_format_args<format_context>);
template FMT_API int internal::snprintf_float(double, int, internal::float_specs, internal::buffer<char> &);
template FMT_API int internal::snprintf_float(long double, int, internal::float_specs, internal::buffer<char> &);
template FMT_API int internal::format_float(double, int, internal::float_specs, internal::buffer<char> &);
template FMT_API int internal::format_float(long double, int, internal::float_specs, internal::buffer<char> &);
template FMT_API int internal::snprintf_float(double, int,
internal::float_specs,
internal::buffer<char>&);
template FMT_API int internal::snprintf_float(long double, int,
internal::float_specs,
internal::buffer<char>&);
template FMT_API int internal::format_float(double, int, internal::float_specs,
internal::buffer<char>&);
template FMT_API int internal::format_float(long double, int,
internal::float_specs,
internal::buffer<char>&);
// Explicit instantiations for wchar_t.
template FMT_API std::string internal::grouping_impl<wchar_t>(locale_ref);
template FMT_API wchar_t internal::thousands_sep_impl(locale_ref);
template FMT_API wchar_t internal::decimal_point_impl(locale_ref);
template FMT_API std::string internal::grouping_impl<wchar_t>(locale_ref);
template FMT_API wchar_t internal::thousands_sep_impl(locale_ref);
template FMT_API wchar_t internal::decimal_point_impl(locale_ref);
template FMT_API void internal::buffer<wchar_t>::append(const wchar_t *, const wchar_t *);
template FMT_API void internal::buffer<wchar_t>::append(const wchar_t*,
const wchar_t*);
template FMT_API std::wstring internal::vformat<wchar_t>(wstring_view, basic_format_args<wformat_context>);
template FMT_API std::wstring internal::vformat<wchar_t>(
wstring_view, basic_format_args<wformat_context>);
FMT_END_NAMESPACE
#endif

View File

@ -1,8 +0,0 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#ifndef SPDLOG_COMPILED_LIB
#error Please define SPDLOG_COMPILED_LIB to compile this file.
#endif
#include "spdlog/loaders/helpers-inl.h"

View File

@ -24,7 +24,7 @@ set(SPDLOG_UTESTS_SOURCES
test_stdout_api.cpp
test_backtrace.cpp
test_create_dir.cpp
test_loaders.cpp)
test_cfg.cpp)
if(NOT SPDLOG_NO_EXCEPTIONS)
list(APPEND SPDLOG_UTESTS_SOURCES test_errors.cpp)

View File

@ -15,7 +15,7 @@ test_sources = files([
'test_stdout_api.cpp',
'test_backtrace.cpp',
'test_create_dir.cpp',
'test_loaders.cpp',
'test_cfg.cpp',
])
if not get_option('no_exceptions')

View File

@ -1,16 +1,17 @@
#include "includes.h"
#include "test_sink.h"
#include <spdlog/loaders/env.h>
#include <spdlog/loaders/argv.h>
#include <spdlog/cfg/env.h>
#include <spdlog/cfg/argv.h>
using spdlog::loaders::load_argv;
using spdlog::loaders::load_env;
using spdlog::cfg::load_argv;
using spdlog::cfg::load_env;
using spdlog::sinks::test_sink_st;
TEST_CASE("env", "[loaders]")
TEST_CASE("env", "[cfg]")
{
spdlog::drop("l1");
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
auto l1 = spdlog::create<test_sink_st>("l1");
#ifdef _MSC_VER
_putenv_s("SPDLOG_LEVEL", "l1=warn");
#else
@ -18,10 +19,11 @@ TEST_CASE("env", "[loaders]")
#endif
load_env();
REQUIRE(l1->level() == spdlog::level::warn);
spdlog::set_default_logger(spdlog::create<test_sink_st>("cfg-default"));
REQUIRE(spdlog::default_logger()->level() == spdlog::level::info);
}
TEST_CASE("argv1", "[loaders]")
TEST_CASE("argv1", "[cfg]")
{
spdlog::drop("l1");
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=warn"};
@ -31,58 +33,61 @@ TEST_CASE("argv1", "[loaders]")
REQUIRE(spdlog::default_logger()->level() == spdlog::level::info);
}
TEST_CASE("argv2", "[loaders]")
TEST_CASE("argv2", "[cfg]")
{
spdlog::drop("l1");
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=warn,trace"};
load_argv(2, const_cast<char**>(argv));
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
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", "[loaders]")
TEST_CASE("argv3", "[cfg]")
{
spdlog::drop("l1");
const char *argv[] = {"ignore", "SPDLOG_LEVEL="};
load_argv(2, const_cast<char**>(argv));
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
auto l1 = spdlog::create<test_sink_st>("l1");
REQUIRE(l1->level() == spdlog::level::info);
REQUIRE(spdlog::default_logger()->level() == spdlog::level::info);
}
TEST_CASE("argv4", "[loaders]")
TEST_CASE("argv4", "[cfg]")
{
spdlog::drop("l1");
const char *argv[] = {"ignore", "SPDLOG_LEVEL=junk"};
load_argv(2, const_cast<char**>(argv));
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
auto l1 = spdlog::create<test_sink_st>("l1");
REQUIRE(l1->level() == spdlog::level::info);
}
TEST_CASE("argv5", "[loaders]")
TEST_CASE("argv5", "[cfg]")
{
spdlog::drop("l1");
const char *argv[] = {"ignore", "ignore", "SPDLOG_LEVEL=l1=warn,trace"};
load_argv(3, const_cast<char**>(argv));
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
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("argv6", "[loaders]")
TEST_CASE("argv6", "[cfg]")
{
spdlog::set_level(spdlog::level::err);
const char *argv[] = {""};
load_argv(1, const_cast<char**>(argv));
REQUIRE(spdlog::default_logger()->level() == spdlog::level::err);
spdlog::set_level(spdlog::level::info);
}
TEST_CASE("argv7", "[loaders]")
TEST_CASE("argv7", "[cfg]")
{
spdlog::set_level(spdlog::level::err);
const char *argv[] = {""};
load_argv(0, const_cast<char**>(argv));
REQUIRE(spdlog::default_logger()->level() == spdlog::level::err);
spdlog::set_level(spdlog::level::info);
}