This commit is contained in:
gabime
2019-03-23 13:31:57 +02:00
parent 043c4acc7e
commit 57a312cb1a
8 changed files with 157 additions and 0 deletions

6
lite/CMakeLists.txt Normal file
View File

@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.1)
project(spdlog_lite)
add_library(spdlog_lite logger.cpp)
target_link_libraries(spdlog_lite spdlog::spdlog)

21
lite/logger.cpp Normal file
View File

@@ -0,0 +1,21 @@
#include "logger.h"
#include "spdlog/spdlog.h"
#include "spdlog/logger.h"
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
{
auto spd_level = static_cast<spdlog::level::level_enum >(lvl);
return impl_->should_log(spd_level);//TODO level
}
void spdlog::lite::logger::log_formatted_(spdlog::lite::level lvl, 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
}

64
lite/logger.h Normal file
View File

@@ -0,0 +1,64 @@
//
// Created by gabi on 3/16/19.
//
#ifndef SPDLOG_LIB_LOGGER_H
#define SPDLOG_LIB_LOGGER_H
#include <memory>
#include "spd_types.h"
#include "spdlog/fmt/fmt.h"
namespace spdlog {
class logger;
namespace lite {
class logger {
public:
logger() = default;
logger(std::shared_ptr<spdlog::logger> impl);
logger(const logger&) = default;
logger(logger&&) = default;
logger& operator=(const logger&) = default;
~logger() = default;
bool should_log(spdlog::lite::level lvl) const noexcept;
template<typename... Args>
void log(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_(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) {
log(spdlog::lite::level::trace, fmt, args...);
}
protected:
std::shared_ptr<spdlog::logger> impl_;
void log_formatted_(spdlog::lite::level lvl, const fmt::memory_buffer &formatted);
};
} // 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

24
lite/spd_types.h Normal file
View File

@@ -0,0 +1,24 @@
//
// 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
};
}}