Commit e2e3df90 authored by gabime's avatar gabime

static lib wip

parent ef8773a8
......@@ -10,16 +10,15 @@
#include "spdlog/logger.h"
namespace spdlog {
class logger;
}
spdlog::logger *get_logger();
int main(int, char *[])
{
auto *l = get_logger();
l->info("HELLO");
l->info("HELLO {}", "World");
l->warn("SOME WARNINNG");
l->error("Some {}", "error");
}
\ No newline at end of file
......@@ -27,6 +27,13 @@
#include "spdlog/fmt/fmt.h"
#ifdef SPDLOG_HEADER_ONLY
#define SPDLOG_INLINE inline
#else
#define SPDLOG_INLINE
#endif
// visual studio upto 2013 does not support noexcept nor constexpr
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define SPDLOG_NOEXCEPT throw()
......
......@@ -79,7 +79,14 @@ inline void spdlog::async_logger::backend_log_(const details::log_msg &incoming_
}
}
}
SPDLOG_CATCH_AND_HANDLE
catch (const std::exception &ex)
{
err_handler_(ex.what());
}
catch (...)
{
err_handler_("Unknown exception in logger");
}
if (should_flush_(incoming_log_msg))
{
......@@ -96,7 +103,14 @@ inline void spdlog::async_logger::backend_flush_()
sink->flush();
}
}
SPDLOG_CATCH_AND_HANDLE
catch (const std::exception &ex)
{
err_handler_(ex.what());
}
catch (...)
{
err_handler_("Unknown exception in logger");
}
}
inline std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name)
......
......@@ -7,6 +7,7 @@
#include <chrono>
#include <type_traits>
#include "spdlog/fmt/fmt.h"
#include "spdlog/common.h"
// Some fmt helpers to efficiently format and pad ints and strings
namespace spdlog {
......
......@@ -6,36 +6,16 @@
#pragma once
#include "spdlog/common.h"
#include "spdlog/details/os.h"
#include <string>
#include <utility>
namespace spdlog {
namespace details {
struct log_msg
{
log_msg(source_loc loc, const std::string *loggers_name, level::level_enum lvl, string_view_t view)
: logger_name(loggers_name)
, level(lvl)
#ifndef SPDLOG_NO_DATETIME
, time(os::now())
#endif
#ifndef SPDLOG_NO_THREAD_ID
, thread_id(os::thread_id())
#endif
, source(loc)
, payload(view)
{
}
log_msg(const std::string *loggers_name, level::level_enum lvl, string_view_t view)
: log_msg(source_loc{}, loggers_name, lvl, view)
{
}
log_msg(source_loc loc, const std::string *loggers_name, level::level_enum lvl, string_view_t view);
log_msg(const std::string *loggers_name, level::level_enum lvl, string_view_t view);
log_msg(const log_msg &other) = default;
const std::string *logger_name{nullptr};
......
This diff is collapsed.
This diff is collapsed.
......@@ -4,7 +4,6 @@
//
#pragma once
#include "spdlog/details/fmt_helper.h"
#include "spdlog/details/log_msg.h"
#include "spdlog/details/os.h"
......
......@@ -13,6 +13,7 @@
#include "spdlog/common.h"
#include "spdlog/details/periodic_worker.h"
#include "spdlog/logger.h"
#include "spdlog/details/pattern_formatter.h"
#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER
// support for the default stdout color logger
......
This diff is collapsed.
......@@ -6,49 +6,41 @@
#pragma once
#include "spdlog/details/log_msg.h"
#include "spdlog/details/pattern_formatter.h"
//#include "spdlog/details/pattern_formatter.h"
#include "spdlog/formatter.h"
namespace spdlog {
namespace sinks {
class sink
{
public:
sink() = default;
explicit sink(std::unique_ptr<spdlog::formatter> formatter)
: formatter_{std::move(formatter)}
{
}
sink();
explicit sink(std::unique_ptr<spdlog::formatter> formatter);
virtual ~sink() = default;
virtual void log(const details::log_msg &msg) = 0;
virtual void flush() = 0;
virtual void set_pattern(const std::string &pattern) = 0;
virtual void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) = 0;
bool should_log(level::level_enum msg_level) const
{
return msg_level >= level_.load(std::memory_order_relaxed);
}
void set_level(level::level_enum log_level)
{
level_.store(log_level);
}
level::level_enum level() const
{
return static_cast<spdlog::level::level_enum>(level_.load(std::memory_order_relaxed));
}
bool should_log(level::level_enum msg_level) const;
void set_level(level::level_enum log_level);
level::level_enum level() const;
protected:
// sink log level - default is all
level_t level_{level::trace};
// sink formatter - default is full format
std::unique_ptr<spdlog::formatter> formatter_{details::make_unique<spdlog::pattern_formatter>()};
// sink formatter
std::unique_ptr<spdlog::formatter> formatter_;
};
} // namespace sinks
} // namespace spdlog
#ifdef SPDLOG_HEADER_ONLY
#include "../src/sink.cpp"
#endif // SPDLOG_HEADER_ONLY
#include "spdlog/details/os.h"
#include "spdlog/sinks/sink.h"
#ifndef SPDLOG_HEADER_ONLY
#include "spdlog/details/log_msg.h"
#endif
SPDLOG_INLINE spdlog::details::log_msg::log_msg(
spdlog::source_loc loc, const std::string *loggers_name, spdlog::level::level_enum lvl, spdlog::string_view_t view)
: logger_name(loggers_name)
, level(lvl)
#ifndef SPDLOG_NO_DATETIME
, time(os::now())
#endif
#ifndef SPDLOG_NO_THREAD_ID
, thread_id(os::thread_id())
#endif
, source(loc)
, payload(view)
{
}
SPDLOG_INLINE spdlog::details::log_msg::log_msg(const std::string *loggers_name, spdlog::level::level_enum lvl, spdlog::string_view_t view)
: log_msg(source_loc{}, loggers_name, lvl, view)
{
}
\ No newline at end of file
#ifndef SPDLOG_HEADER_ONLY
#include "spdlog/logger.h"
#endif
#include "spdlog/sinks/sink.h"
#include "spdlog/details/pattern_formatter.h"
// public methods
SPDLOG_INLINE void spdlog::logger::log(spdlog::source_loc loc, spdlog::level::level_enum lvl, const char *msg)
{
if (!should_log(lvl))
{
return;
}
try
{
details::log_msg log_msg(loc, &name_, lvl, spdlog::string_view_t(msg));
sink_it_(log_msg);
}
catch (const std::exception &ex)
{
err_handler_(ex.what());
}
catch (...)
{
err_handler_("Unknown exception in logger");
}
}
SPDLOG_INLINE void spdlog::logger::log(level::level_enum lvl, const char *msg)
{
log(source_loc{}, lvl, msg);
}
SPDLOG_INLINE bool spdlog::logger::should_log(spdlog::level::level_enum msg_level) const
{
return msg_level >= level_.load(std::memory_order_relaxed);
}
SPDLOG_INLINE void spdlog::logger::set_level(spdlog::level::level_enum log_level)
{
level_.store(log_level);
}
SPDLOG_INLINE spdlog::level::level_enum spdlog::logger::default_level()
{
return static_cast<spdlog::level::level_enum>(SPDLOG_ACTIVE_LEVEL);
}
SPDLOG_INLINE spdlog::level::level_enum spdlog::logger::level() const
{
return static_cast<spdlog::level::level_enum>(level_.load(std::memory_order_relaxed));
}
SPDLOG_INLINE const std::string &spdlog::logger::name() const
{
return name_;
}
// set formatting for the sinks in this logger.
// each sink will get a seperate instance of the formatter object.
SPDLOG_INLINE void spdlog::logger::set_formatter(std::unique_ptr<spdlog::formatter> f)
{
for (auto &sink : sinks_)
{
sink->set_formatter(f->clone());
}
}
SPDLOG_INLINE void spdlog::logger::set_pattern(std::string pattern, spdlog::pattern_time_type time_type)
{
auto new_formatter = details::make_unique<spdlog::pattern_formatter>(std::move(pattern), time_type);
set_formatter(std::move(new_formatter));
}
// flush functions
SPDLOG_INLINE void spdlog::logger::flush()
{
try
{
flush_();
}
catch (const std::exception &ex)
{
err_handler_(ex.what());
}
catch (...)
{
err_handler_("Unknown exception in logger");
}
}
SPDLOG_INLINE void spdlog::logger::flush_on(level::level_enum log_level)
{
flush_level_.store(log_level);
}
SPDLOG_INLINE spdlog::level::level_enum spdlog::logger::flush_level() const
{
return static_cast<spdlog::level::level_enum>(flush_level_.load(std::memory_order_relaxed));
}
// sinks
SPDLOG_INLINE const std::vector<spdlog::sink_ptr> &spdlog::logger::sinks() const
{
return sinks_;
}
SPDLOG_INLINE std::vector<spdlog::sink_ptr> &spdlog::logger::sinks()
{
return sinks_;
}
// error handler
SPDLOG_INLINE void spdlog::logger::set_error_handler(spdlog::log_err_handler err_handler)
{
err_handler_ = std::move(err_handler);
}
SPDLOG_INLINE spdlog::log_err_handler spdlog::logger::error_handler() const
{
return err_handler_;
}
// create new logger with same sinks and configuration.
SPDLOG_INLINE std::shared_ptr<spdlog::logger> spdlog::logger::clone(std::string logger_name)
{
auto cloned = std::make_shared<spdlog::logger>(std::move(logger_name), sinks_.begin(), sinks_.end());
cloned->set_level(this->level());
cloned->flush_on(this->flush_level());
cloned->set_error_handler(this->error_handler());
return cloned;
}
// protected methods
SPDLOG_INLINE void spdlog::logger::sink_it_(spdlog::details::log_msg &msg)
{
#if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)
incr_msg_counter_(msg);
#endif
for (auto &sink : sinks_)
{
if (sink->should_log(msg.level))
{
sink->log(msg);
}
}
if (should_flush_(msg))
{
flush_();
}
}
SPDLOG_INLINE void spdlog::logger::flush_()
{
for (auto &sink : sinks_)
{
sink->flush();
}
}
SPDLOG_INLINE bool spdlog::logger::should_flush_(const spdlog::details::log_msg &msg)
{
auto flush_level = flush_level_.load(std::memory_order_relaxed);
return (msg.level >= flush_level) && (msg.level != level::off);
}
SPDLOG_INLINE void spdlog::logger::default_err_handler_(const std::string &msg)
{
auto now = time(nullptr);
if (now - last_err_time_ < 60)
{
return;
}
last_err_time_ = now;
auto tm_time = details::os::localtime(now);
char date_buf[100];
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
fmt::print(stderr, "[*** LOG ERROR ***] [{}] [{}] {}\n", date_buf, name(), msg);
}
SPDLOG_INLINE void spdlog::logger::incr_msg_counter_(spdlog::details::log_msg &msg)
{
msg.msg_id = msg_counter_.fetch_add(1, std::memory_order_relaxed);
}
This diff is collapsed.
#include "spdlog/common.h"
#include "spdlog/details/pattern_formatter.h"
#ifndef SPDLOG_HEADER_ONLY
#include "spdlog/sinks/sink.h"
#endif
SPDLOG_INLINE spdlog::sinks::sink::sink()
: formatter_{details::make_unique<spdlog::pattern_formatter>()}
{}
SPDLOG_INLINE spdlog::sinks::sink::sink(std::unique_ptr<spdlog::formatter> formatter)
: formatter_{std::move(formatter)}
{}
SPDLOG_INLINE bool spdlog::sinks::sink::should_log(spdlog::level::level_enum msg_level) const
{
return msg_level >= level_.load(std::memory_order_relaxed);
}
SPDLOG_INLINE void spdlog::sinks::sink::set_level(level::level_enum log_level)
{
level_.store(log_level);
}
SPDLOG_INLINE spdlog::level::level_enum spdlog::sinks::sink::level() const
{
return static_cast<spdlog::level::level_enum>(level_.load(std::memory_order_relaxed));
}
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