mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-28 17:19:34 +08:00
wip lite
This commit is contained in:
@@ -2,20 +2,82 @@
|
||||
#include "spdlog/spdlog.h"
|
||||
#include "spdlog/logger.h"
|
||||
|
||||
static spdlog::level::level_enum to_spdlog_level(spdlog::lite::level level)
|
||||
{
|
||||
return static_cast<spdlog::level::level_enum >(level);
|
||||
}
|
||||
|
||||
static spdlog::lite::level to_lite_level(spdlog::level::level_enum level)
|
||||
{
|
||||
return static_cast<spdlog::lite::level>(level);
|
||||
}
|
||||
|
||||
spdlog::lite::logger::logger(std::shared_ptr<spdlog::logger> impl)
|
||||
{
|
||||
impl_ = std::move(impl);
|
||||
}
|
||||
|
||||
|
||||
bool spdlog::lite::logger::should_log(spdlog::lite::level lvl) const SPDLOG_NOEXCEPT
|
||||
bool spdlog::lite::logger::should_log(spdlog::lite::level level) const SPDLOG_NOEXCEPT
|
||||
{
|
||||
auto spd_level = static_cast<spdlog::level::level_enum >(lvl);
|
||||
auto spd_level = to_spdlog_level(level);
|
||||
return impl_->should_log(spd_level);//TODO level
|
||||
}
|
||||
|
||||
void spdlog::lite::logger::log_formatted_(spdlog::lite::level lvl, const fmt::memory_buffer &formatted)
|
||||
void spdlog::lite::logger::log_formatted_(spdlog::lite::level level, const fmt::memory_buffer &formatted)
|
||||
{
|
||||
auto spd_level = static_cast<spdlog::level::level_enum >(lvl);
|
||||
impl_->log(spd_level, spdlog::details::fmt_helper::to_string_view(formatted)); //TODO and source_loc
|
||||
}
|
||||
auto spd_level = to_spdlog_level(level);
|
||||
impl_->log(spd_level, spdlog::details::fmt_helper::to_string_view(formatted));
|
||||
}
|
||||
|
||||
|
||||
void spdlog::lite::logger::log_formatted_src(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const fmt::memory_buffer &formatted)
|
||||
{
|
||||
auto spd_level = to_spdlog_level(lvl);
|
||||
spdlog::source_loc source_loc{src.filename, src.line, src.funcname};
|
||||
impl_->log(source_loc, spd_level, spdlog::details::fmt_helper::to_string_view(formatted));
|
||||
}
|
||||
|
||||
void spdlog::lite::logger::set_level(spdlog::lite::level level)
|
||||
{
|
||||
auto spd_level = to_spdlog_level(level);
|
||||
impl_->set_level(spd_level);
|
||||
}
|
||||
|
||||
spdlog::lite::level spdlog::lite::logger::get_level() const
|
||||
{
|
||||
return to_lite_level(impl_->level());
|
||||
}
|
||||
|
||||
std::string spdlog::lite::logger::name() const
|
||||
{
|
||||
return impl_->name();
|
||||
}
|
||||
|
||||
void spdlog::lite::logger::flush()
|
||||
{
|
||||
impl_->flush();
|
||||
}
|
||||
|
||||
void spdlog::lite::logger::flush_on(spdlog::lite::level level)
|
||||
{
|
||||
auto spd_level = to_spdlog_level(level);
|
||||
impl_->flush_on(spd_level);
|
||||
}
|
||||
|
||||
spdlog::lite::level spdlog::lite::logger::flush_level() const
|
||||
{
|
||||
return to_lite_level(impl_->flush_level());
|
||||
}
|
||||
|
||||
// pattern
|
||||
void spdlog::lite::logger::set_pattern(std::string pattern)
|
||||
{
|
||||
impl_->set_pattern(std::move(pattern));
|
||||
}
|
||||
|
||||
|
||||
spdlog::lite::logger &spdlog::lite::default_logger()
|
||||
{
|
||||
static spdlog::lite::logger s_default(spdlog::default_logger());
|
||||
return s_default;
|
||||
}
|
||||
|
139
lite/logger.h
139
lite/logger.h
@@ -1,19 +1,36 @@
|
||||
//
|
||||
// Created by gabi on 3/16/19.
|
||||
//
|
||||
|
||||
#ifndef SPDLOG_LIB_LOGGER_H
|
||||
#define SPDLOG_LIB_LOGGER_H
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "spd_types.h"
|
||||
#include <string>
|
||||
#include "spdlog/fmt/fmt.h"
|
||||
|
||||
|
||||
//#define SPDLITE_LOGGER_INFO(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::info, __VA_ARGS__)
|
||||
//#define SPDLITE_INFO(...) SPDLOG_LOGGER_INFO(spdlog::default_logger_raw(), __VA_ARGS__)
|
||||
|
||||
namespace spdlog {
|
||||
class logger;
|
||||
|
||||
namespace lite {
|
||||
enum class level{
|
||||
trace,
|
||||
debug,
|
||||
info,
|
||||
warn,
|
||||
err,
|
||||
critical,
|
||||
off
|
||||
};
|
||||
|
||||
struct src_loc {
|
||||
const char *filename;
|
||||
int line;
|
||||
const char* funcname;
|
||||
};
|
||||
|
||||
class logger {
|
||||
public:
|
||||
logger() = default;
|
||||
@@ -37,28 +54,124 @@ namespace spdlog {
|
||||
log_formatted_(lvl, formatted_buf);
|
||||
}
|
||||
|
||||
// template<typename... Args>
|
||||
// void log(spdlog::lite::level lvl, const char *fmt, const Args &... args)
|
||||
// {
|
||||
// log(lvl, fmt, args...);
|
||||
// }
|
||||
|
||||
template<typename... Args>
|
||||
void trace(const char *fmt, const Args &... args) {
|
||||
void log(const spdlog::lite::src_loc& src, spdlog::lite::level lvl, const char *fmt, const Args &... args) {
|
||||
if (!should_log(lvl)) {
|
||||
return;
|
||||
}
|
||||
fmt::memory_buffer formatted_buf;
|
||||
fmt::format_to(formatted_buf, fmt, args...);
|
||||
log_formatted_src(src, lvl, formatted_buf);
|
||||
}
|
||||
|
||||
|
||||
template<typename... Args>
|
||||
void trace(const char *fmt, const Args &... args)
|
||||
{
|
||||
log(spdlog::lite::level::trace, fmt, args...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void trace(const char* source_file, int source_line, const char* source_func, const char *fmt, const Args &... args)
|
||||
{
|
||||
spdlog::lite::src_loc src{source_file, source_line, source_func};
|
||||
log(src, spdlog::lite::level::trace, fmt, args...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void debug(const char *fmt, const Args &... args)
|
||||
{
|
||||
log(spdlog::lite::level::debug, fmt, args...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void info(const char *fmt, const Args &... args)
|
||||
{
|
||||
log(spdlog::lite::level::info, fmt, args...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void warn(const char *fmt, const Args &... args)
|
||||
{
|
||||
log(spdlog::lite::level::warn, fmt, args...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void error(const char *fmt, const Args &... args)
|
||||
{
|
||||
log(spdlog::lite::level::err, fmt, args...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void critical(const char *fmt, const Args &... args)
|
||||
{
|
||||
log(spdlog::lite::level::critical, fmt, args...);
|
||||
}
|
||||
|
||||
std::string name() const;
|
||||
|
||||
// level
|
||||
void set_level(lite::level level);
|
||||
lite::level get_level() const;
|
||||
|
||||
// flush
|
||||
void flush();
|
||||
void flush_on(spdlog::lite::level log_level);
|
||||
spdlog::lite::level flush_level() const;
|
||||
|
||||
// pattern
|
||||
void set_pattern(std::string pattern);
|
||||
|
||||
protected:
|
||||
std::shared_ptr<spdlog::logger> impl_;
|
||||
|
||||
void log_formatted_(spdlog::lite::level lvl, const fmt::memory_buffer &formatted);
|
||||
void log_formatted_src(const spdlog::lite::src_loc& src, spdlog::lite::level lvl, const fmt::memory_buffer &formatted);
|
||||
|
||||
};
|
||||
spdlog::lite::logger& default_logger();
|
||||
|
||||
|
||||
template<typename... Args>
|
||||
void trace(const char *fmt, const Args &... args)
|
||||
{
|
||||
default_logger().trace(fmt, args...);
|
||||
}
|
||||
template<typename... Args>
|
||||
void debug(const char *fmt, const Args &... args)
|
||||
{
|
||||
default_logger().debug(fmt, args...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void info(const char *fmt, const Args &... args)
|
||||
{
|
||||
default_logger().info(fmt, args...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void warn(const char *fmt, const Args &... args)
|
||||
{
|
||||
default_logger().warn(fmt, args...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void error(const char *fmt, const Args &... args)
|
||||
{
|
||||
default_logger().error(fmt, args...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void critical(const char *fmt, const Args &... args)
|
||||
{
|
||||
default_logger().critical(fmt, args...);
|
||||
}
|
||||
|
||||
|
||||
} // namespace lite
|
||||
|
||||
// factory to create lite logger
|
||||
// implement it in a dedicated compilation unit for fast compiles
|
||||
spdlog::lite::logger create_lite(void* ctx = nullptr);
|
||||
} // namespace spdlog
|
||||
#endif //SPDLOG_LIB_LOGGER_H
|
||||
|
||||
} // namespace spdlog
|
@@ -1,24 +0,0 @@
|
||||
//
|
||||
// Copyright(c) 2019 Gabi Melman.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
//
|
||||
|
||||
// core types, forward declarations and defines used by spdlog
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
namespace lite
|
||||
{
|
||||
enum class level{
|
||||
trace,
|
||||
debug,
|
||||
info,
|
||||
warning,
|
||||
error,
|
||||
critical,
|
||||
off
|
||||
|
||||
};
|
||||
}}
|
Reference in New Issue
Block a user