mirror of
https://github.com/gabime/spdlog.git
synced 2025-10-02 11:29:01 +08:00
Added default logger API
This commit is contained in:
@@ -185,7 +185,7 @@ inline int rename(const filename_t &filename1, const filename_t &filename2) SPDL
|
||||
}
|
||||
|
||||
// Return if file exists
|
||||
inline bool file_exists(const filename_t &filename) SPDLOG_NOEXCEPT
|
||||
inline bool file_exists(const filename_t &filename) SPDLOG_NOEXCEPT
|
||||
{
|
||||
#ifdef _WIN32
|
||||
#ifdef SPDLOG_WCHAR_FILENAMES
|
||||
|
@@ -508,10 +508,13 @@ class full_formatter final : public flag_formatter
|
||||
#endif
|
||||
|
||||
#ifndef SPDLOG_NO_NAME
|
||||
dest.push_back('[');
|
||||
fmt_helper::append_str(*msg.logger_name, dest);
|
||||
dest.push_back(']');
|
||||
dest.push_back(' ');
|
||||
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)
|
||||
|
Reference in New Issue
Block a user