Commit b368d18b authored by gabime's avatar gabime

Added default logger API

parent 8e4996ba
......@@ -88,7 +88,7 @@ PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SortIncludes: false
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: false
......
......@@ -4,7 +4,7 @@
#
cmake_minimum_required(VERSION 3.1)
project(spdlog VERSION 1.2.0 LANGUAGES CXX)
project(spdlog VERSION 1.3.0 LANGUAGES CXX)
include(CTest)
include(CMakeDependentOption)
include(GNUInstallDirs)
......
......@@ -6,10 +6,11 @@
//
// bench.cpp : spdlog benchmarks
//
#include "spdlog/spdlog.h"
#include "spdlog/async.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"
#include "utils.h"
#include <atomic>
#include <iostream>
......
......@@ -6,12 +6,13 @@
//
// bench.cpp : spdlog benchmarks
//
#include "spdlog/spdlog.h"
#include "spdlog/async.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/sinks/null_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/spdlog.h"
#include "utils.h"
#include <atomic>
#include <cstdlib> // EXIT_FAILURE
......
......@@ -6,12 +6,14 @@
//
// latency.cpp : spdlog latency benchmarks
//
#include "spdlog/spdlog.h"
#include "spdlog/async.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/sinks/null_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/spdlog.h"
#include "utils.h"
#include <atomic>
#include <cstdlib>
......
This diff is collapsed.
#include "spdlog/spdlog.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/stdout_sinks.h"
#include "spdlog/spdlog.h"
#include <iostream>
#include <memory>
......
......@@ -508,10 +508,13 @@ class full_formatter final : public flag_formatter
#endif
#ifndef SPDLOG_NO_NAME
if (!msg.logger_name->empty())
{
dest.push_back('[');
fmt_helper::append_str(*msg.logger_name, dest);
dest.push_back(']');
dest.push_back(' ');
}
#endif
dest.push_back('[');
......
......@@ -14,6 +14,14 @@
#include "spdlog/details/periodic_worker.h"
#include "spdlog/logger.h"
// support for the default stdout color logger
#ifdef _WIN32
#include "spdlog/sinks/wincolor_sink.h"
#else
#include "spdlog/sinks/ansicolor_sink.h"
#endif
#include <chrono>
#include <functional>
#include <memory>
......@@ -66,6 +74,25 @@ public:
return found == loggers_.end() ? nullptr : found->second;
}
std::shared_ptr<logger> get_default_logger() const
{
return default_logger_;
}
// set default logger.
// default logger is stored in default_logger_ (for faster retrieval) and in the map of existing loggers.
void set_default_logger(std::shared_ptr<logger> new_default_logger)
{
std::lock_guard<std::mutex> lock(logger_map_mutex_);
// remove previous default logger from the map
if (default_logger_ != nullptr)
{
loggers_.erase(default_logger_->name());
}
loggers_[new_default_logger->name()] = new_default_logger;
default_logger_ = std::move(new_default_logger);
}
void set_tp(std::shared_ptr<thread_pool> tp)
{
std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
......@@ -154,6 +181,7 @@ public:
{
std::lock_guard<std::mutex> lock(logger_map_mutex_);
loggers_.clear();
default_logger_.reset();
}
// clean all reasources and threads started by the registry
......@@ -187,6 +215,15 @@ private:
registry()
: formatter_(new pattern_formatter("%+"))
{
// create default logger (stdout_color_mt).
#ifdef _WIN32
auto color_sink = std::make_shared<sinks::wincolor_stdout_sink_mt>();
#else
auto color_sink = std::make_shared<sinks::ansicolor_stdout_sink_mt>();
#endif
SPDLOG_CONSTEXPR const char *default_logger_name = "";
default_logger_ = std::make_shared<spdlog::logger>(default_logger_name, std::move(color_sink));
loggers_[default_logger_name] = default_logger_;
}
~registry() = default;
......@@ -208,6 +245,7 @@ private:
log_err_handler err_handler_;
std::shared_ptr<thread_pool> tp_;
std::unique_ptr<periodic_worker> periodic_flusher_;
std::shared_ptr<logger> default_logger_;
};
} // namespace details
......
......@@ -5,6 +5,10 @@
#pragma once
#ifndef SPDLOG_H
#error "spdlog.h must be included before this file."
#endif
#include "spdlog/details/fmt_helper.h"
#include "spdlog/details/null_mutex.h"
#include "spdlog/details/os.h"
......
......@@ -5,6 +5,10 @@
#pragma once
#ifndef SPDLOG_H
#error "spdlog.h must be included before this file."
#endif
#include "spdlog/details/console_globals.h"
#include "spdlog/details/null_mutex.h"
#include "spdlog/details/os.h"
......
......@@ -4,10 +4,14 @@
//
#pragma once
#ifndef SPDLOG_H
#error "spdlog.h must be included before this file."
#endif
#include "spdlog/details/file_helper.h"
#include "spdlog/details/null_mutex.h"
#include "spdlog/sinks/base_sink.h"
#include "spdlog/spdlog.h"
#include <mutex>
#include <string>
......
......@@ -4,11 +4,15 @@
//
#pragma once
#ifndef SPDLOG_H
#error "spdlog.h must be included before this file."
#endif
#include "spdlog/details/file_helper.h"
#include "spdlog/details/null_mutex.h"
#include "spdlog/fmt/fmt.h"
#include "spdlog/sinks/base_sink.h"
#include "spdlog/spdlog.h"
#include <chrono>
#include <cstdio>
......
......@@ -5,6 +5,10 @@
#pragma once
#ifndef SPDLOG_H
#error "spdlog.h must be included before this file."
#endif
#include "base_sink.h"
#include "spdlog/details/log_msg.h"
#include "spdlog/details/null_mutex.h"
......
......@@ -5,6 +5,10 @@
#pragma once
#ifndef SPDLOG_H
#error "spdlog.h must be included before this file."
#endif
#if defined(_WIN32)
#include "spdlog/details/null_mutex.h"
......
......@@ -5,6 +5,10 @@
#pragma once
#ifndef SPDLOG_H
#error "spdlog.h must be included before this file."
#endif
#include "spdlog/details/null_mutex.h"
#include "spdlog/sinks/base_sink.h"
......@@ -25,4 +29,17 @@ using null_sink_mt = null_sink<std::mutex>;
using null_sink_st = null_sink<details::null_mutex>;
} // namespace sinks
template<typename Factory = default_factory>
inline std::shared_ptr<logger> null_logger_mt(const std::string &logger_name)
{
return Factory::template create<sinks::null_sink_mt>(logger_name);
}
template<typename Factory = default_factory>
inline std::shared_ptr<logger> null_logger_st(const std::string &logger_name)
{
return Factory::template create<sinks::null_sink_st>(logger_name);
}
} // namespace spdlog
......@@ -5,6 +5,10 @@
#pragma once
#ifndef SPDLOG_H
#error "spdlog.h must be included before this file."
#endif
#include "spdlog/details/null_mutex.h"
#include "spdlog/sinks/base_sink.h"
......
......@@ -4,11 +4,15 @@
//
#pragma once
#ifndef SPDLOG_H
#error "spdlog.h must be included before this file."
#endif
#include "spdlog/details/file_helper.h"
#include "spdlog/details/null_mutex.h"
#include "spdlog/fmt/fmt.h"
#include "spdlog/sinks/base_sink.h"
#include "spdlog/spdlog.h"
#include <cerrno>
#include <chrono>
......
......@@ -5,7 +5,10 @@
#pragma once
#include "spdlog/spdlog.h"
#ifndef SPDLOG_H
#error "spdlog.h must be included before this file."
#endif
#ifdef _WIN32
#include "spdlog/sinks/wincolor_sink.h"
#else
......
......@@ -5,9 +5,12 @@
#pragma once
#ifndef SPDLOG_H
#error "spdlog.h must be included before this file."
#endif
#include "spdlog/details/console_globals.h"
#include "spdlog/details/null_mutex.h"
#include "spdlog/spdlog.h"
#include <cstdio>
#include <memory>
......
......@@ -5,8 +5,11 @@
#pragma once
#ifndef SPDLOG_H
#error "spdlog.h must be included before this file."
#endif
#include "spdlog/sinks/base_sink.h"
#include "spdlog/spdlog.h"
#include <array>
#include <string>
......
......@@ -5,6 +5,10 @@
#pragma once
#ifndef SPDLOG_H
#error "spdlog.h must be included before this file."
#endif
#include "spdlog/common.h"
#include "spdlog/details/console_globals.h"
#include "spdlog/details/null_mutex.h"
......
......@@ -14,8 +14,6 @@
#include "spdlog/logger.h"
#include "spdlog/version.h"
#include "spdlog/default_logger.h"
#include <chrono>
#include <functional>
#include <memory>
......@@ -128,19 +126,173 @@ inline void shutdown()
details::registry::instance().shutdown();
}
///////////////////////////////////////////////////////////////////////////////
//
// Trace & Debug can be switched on/off at compile time for zero cost debug
// statements.
// API for using default logger (stdout_color_mt),
// e.g: spdlog::info("Message {}", 1);
//
// The default logger object can be accessed using the spdlog::get():
// For example, to add another sink to it:
// spdlog::get()->sinks()->push_back(some_sink);
//
// The default logger can replaced using spdlog::set_default_logger(new_logger).
// For example, to replace it with a file logger:
// spdlog::set_default_logger(std::move(spdlog::basic_logger_st("mylog.txt"));
//
// Return the default logger
// inline std::shared_ptr<spdlog::logger> get()
//{
// return details::registry::instance().get_default_logger();
//}
inline std::shared_ptr<spdlog::logger> get()
{
return details::registry::instance().get_default_logger();
}
inline void set_default_logger(std::shared_ptr<spdlog::logger> default_logger)
{
details::registry::instance().set_default_logger(std::move(default_logger));
}
template<typename... Args>
inline void log(level::level_enum lvl, const char *fmt, const Args &... args)
{
get()->log(lvl, fmt, args...);
}
template<typename... Args>
inline void trace(const char *fmt, const Args &... args)
{
get()->trace(fmt, args...);
}
template<typename... Args>
inline void debug(const char *fmt, const Args &... args)
{
get()->debug(fmt, args...);
}
template<typename... Args>
inline void info(const char *fmt, const Args &... args)
{
get()->info(fmt, args...);
}
template<typename... Args>
inline void warn(const char *fmt, const Args &... args)
{
get()->warn(fmt, args...);
}
template<typename... Args>
inline void error(const char *fmt, const Args &... args)
{
get()->error(fmt, args...);
}
template<typename... Args>
inline void critical(const char *fmt, const Args &... args)
{
get()->critical(fmt, args...);
}
template<typename T>
inline void log(level::level_enum lvl, const T &msg)
{
get()->log(lvl, msg);
}
template<typename T>
inline void trace(const T &msg)
{
get()->trace(msg);
}
template<typename T>
inline void debug(const T &msg)
{
get()->debug(msg);
}
template<typename T>
inline void info(const T &msg)
{
get()->info(msg);
}
template<typename T>
inline void warn(const T &msg)
{
get()->warn(msg);
}
template<typename T>
inline void error(const T &msg)
{
get()->error(msg);
}
template<typename T>
inline void critical(const T &msg)
{
get()->critical(msg);
}
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
template<typename... Args>
inline void log(level::level_enum lvl, const wchar_t *fmt, const Args &... args)
{
get()->log(lvl, fmt, args...);
}
template<typename... Args>
inline void trace(const wchar_t *fmt, const Args &... args)
{
get()->trace(fmt, args...);
}
template<typename... Args>
inline void debug(const wchar_t *fmt, const Args &... args)
{
get()->debug(fmt, args...);
}
template<typename... Args>
inline void info(const wchar_t *fmt, const Args &... args)
{
get()->info(fmt, args...);
}
template<typename... Args>
inline void warn(const wchar_t *fmt, const Args &... args)
{
get()->warn(fmt, args...);
}
template<typename... Args>
inline void error(const wchar_t *fmt, const Args &... args)
{
get()->error(fmt, args...);
}
template<typename... Args>
inline void critical(const wchar_t *fmt, const Args &... args)
{
get()->critical(fmt, args...);
}
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
//
// Trace & Debug can be switched on/off at compile time with zero cost.
// Uncomment SPDLOG_DEBUG_ON/SPDLOG_TRACE_ON in tweakme.h to enable.
// SPDLOG_TRACE(..) will also print current file and line.
//
// Example:
// spdlog::set_level(spdlog::level::trace);
// SPDLOG_TRACE(my_logger, "some trace message");
// SPDLOG_TRACE(my_logger, "another trace message {} {}", 1, 2);
// SPDLOG_DEBUG(my_logger, "some debug message {} {}", 3, 4);
///////////////////////////////////////////////////////////////////////////////
//
#ifdef SPDLOG_TRACE_ON
#define SPDLOG_STR_H(x) #x
......@@ -165,5 +317,4 @@ inline void shutdown()
#endif
} // namespace spdlog
#endif // #define SPDLOG_H
#endif // SPDLOG_H
......@@ -6,7 +6,7 @@
#pragma once
#define SPDLOG_VER_MAJOR 1
#define SPDLOG_VER_MINOR 2
#define SPDLOG_VER_MINOR 3
#define SPDLOG_VER_PATCH 0
#define SPDLOG_VERSION (SPDLOG_VER_MAJOR * 10000 + SPDLOG_VER_MINOR * 100 + SPDLOG_VER_PATCH)
......@@ -14,6 +14,7 @@
#define SPDLOG_DEBUG_ON
#define SPDLOG_ENABLE_MESSAGE_COUNTER
#include "spdlog/spdlog.h"
#include "spdlog/async.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/daily_file_sink.h"
......@@ -21,4 +22,3 @@
#include "spdlog/sinks/ostream_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"
......@@ -12,8 +12,7 @@ TEST_CASE("register_drop", "[registry]")
REQUIRE_THROWS_AS(spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name), const spdlog::spdlog_ex &);
}
TEST_CASE("explicit register"
"[registry]")
TEST_CASE("explicit register", "[registry]")
{
spdlog::drop_all();
auto logger = std::make_shared<spdlog::logger>(tested_logger_name, std::make_shared<spdlog::sinks::null_sink_st>());
......@@ -23,8 +22,7 @@ TEST_CASE("explicit register"
REQUIRE_THROWS_AS(spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name), const spdlog::spdlog_ex &);
}
TEST_CASE("apply_all"
"[registry]")
TEST_CASE("apply_all", "[registry]")
{
spdlog::drop_all();
auto logger = std::make_shared<spdlog::logger>(tested_logger_name, std::make_shared<spdlog::sinks::null_sink_st>());
......@@ -45,8 +43,7 @@ TEST_CASE("apply_all"
REQUIRE(counter == 1);
}
TEST_CASE("drop"
"[registry]")
TEST_CASE("drop", "[registry]")
{
spdlog::drop_all();
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
......@@ -54,8 +51,7 @@ TEST_CASE("drop"
REQUIRE_FALSE(spdlog::get(tested_logger_name));
}
TEST_CASE("drop_all"
"[registry]")
TEST_CASE("drop_all", "[registry]")
{
spdlog::drop_all();
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
......@@ -65,8 +61,7 @@ TEST_CASE("drop_all"
REQUIRE_FALSE(spdlog::get(tested_logger_name2));
}
TEST_CASE("drop non existing"
"[registry]")
TEST_CASE("drop non existing", "[registry]")
{
spdlog::drop_all();
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
......@@ -75,3 +70,11 @@ TEST_CASE("drop non existing"
REQUIRE(spdlog::get(tested_logger_name));
spdlog::drop_all();
}
TEST_CASE("default logger", "[registry]")
{
spdlog::drop_all();
spdlog::set_default_logger(std::move(spdlog::null_logger_st(tested_logger_name)));
REQUIRE(spdlog::get(tested_logger_name) == spdlog::get());
spdlog::drop_all();
}
......@@ -191,3 +191,42 @@ TEST_CASE("message_counter", "[message_counter]")
REQUIRE(oss.str() == "000002 Hello again" + std::string(spdlog::details::os::default_eol));
}
TEST_CASE("default logger API", "[default logger]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::set_default_logger(std::make_shared<spdlog::logger>("oss", oss_sink));
spdlog::get()->set_pattern("%v");
spdlog::get()->set_level(spdlog::level::trace);
spdlog::trace("hello trace");
REQUIRE(oss.str() == "hello trace" + std::string(spdlog::details::os::default_eol));
oss.str("");
spdlog::debug("hello debug");
REQUIRE(oss.str() == "hello debug" + std::string(spdlog::details::os::default_eol));
oss.str("");
spdlog::info("Hello");
REQUIRE(oss.str() == "Hello" + std::string(spdlog::details::os::default_eol));
oss.str("");
spdlog::warn("Hello again {}", 2);
REQUIRE(oss.str() == "Hello again 2" + std::string(spdlog::details::os::default_eol));
oss.str("");
spdlog::error(123);
REQUIRE(oss.str() == "123" + std::string(spdlog::details::os::default_eol));
oss.str("");
spdlog::critical(std::string("some string"));
REQUIRE(oss.str() == "some string" + std::string(spdlog::details::os::default_eol));
oss.str("");
spdlog::set_level(spdlog::level::info);
spdlog::debug("should not be logged");
REQUIRE(oss.str().empty());
spdlog::drop_all();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment