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

View File

@ -54,6 +54,8 @@ option(SPDLOG_BUILD_BENCH "Build benchmarks (Requires https://github.com/google/
option(SPDLOG_BUILD_TESTS "Build tests" ${SPDLOG_MASTER_PROJECT})
option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of bundled" OFF)
option(SPDLOG_INSTALL "Generate the install target." ${SPDLOG_MASTER_PROJECT})
option(SPDLOG_BUILD_LITE "Build spdlog lite" ${SPDLOG_MASTER_PROJECT})
if(SPDLOG_FMT_EXTERNAL AND NOT TARGET fmt::fmt)
find_package(fmt REQUIRED CONFIG)
@ -86,6 +88,11 @@ if(SPDLOG_BUILD_BENCH)
add_subdirectory(bench)
endif()
if(SPDLOG_BUILD_LITE)
add_subdirectory(lite)
add_subdirectory(lite-example)
endif()
#---------------------------------------------------------------------------------------
# Install/export targets and files
#---------------------------------------------------------------------------------------

View File

@ -0,0 +1,13 @@
project(spdlog-lite-example CXX)
find_package(Threads REQUIRED)
set(LITE_SOURCES example.cpp create_lite.cpp)
add_executable(${PROJECT_NAME} ${LITE_SOURCES})
include_directories(../lite)
target_link_libraries(${PROJECT_NAME} PRIVATE Threads::Threads)
target_link_libraries(${PROJECT_NAME} PRIVATE spdlog_lite)

View File

@ -0,0 +1,15 @@
#include "logger.h"
#include "spdlog/spdlog.h"
spdlog::lite::logger spdlog::create_lite(void* ctx)
{
if(ctx) {
//..
}
auto logger_impl = spdlog::default_logger();
logger_impl->set_level(spdlog::level::trace);
return spdlog::lite::logger(logger_impl);
}

7
lite-example/example.cpp Normal file
View File

@ -0,0 +1,7 @@
#include "logger.h"
int main()
{
auto l = spdlog::create_lite();
l.trace("HELLO {}!!!", "lite");
}

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
};
}}