diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f0951fc..c4093403 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 #--------------------------------------------------------------------------------------- diff --git a/lite-example/CMakeLists.txt b/lite-example/CMakeLists.txt new file mode 100644 index 00000000..d152ecd1 --- /dev/null +++ b/lite-example/CMakeLists.txt @@ -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) + + diff --git a/lite-example/create_lite.cpp b/lite-example/create_lite.cpp new file mode 100644 index 00000000..aa375d4e --- /dev/null +++ b/lite-example/create_lite.cpp @@ -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); +} + + diff --git a/lite-example/example.cpp b/lite-example/example.cpp new file mode 100644 index 00000000..a1a78eb6 --- /dev/null +++ b/lite-example/example.cpp @@ -0,0 +1,7 @@ +#include "logger.h" + +int main() +{ + auto l = spdlog::create_lite(); + l.trace("HELLO {}!!!", "lite"); +} \ No newline at end of file diff --git a/lite/CMakeLists.txt b/lite/CMakeLists.txt new file mode 100644 index 00000000..3ed30be7 --- /dev/null +++ b/lite/CMakeLists.txt @@ -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) diff --git a/lite/logger.cpp b/lite/logger.cpp new file mode 100644 index 00000000..6098df84 --- /dev/null +++ b/lite/logger.cpp @@ -0,0 +1,21 @@ +#include "logger.h" +#include "spdlog/spdlog.h" +#include "spdlog/logger.h" + +spdlog::lite::logger::logger(std::shared_ptr impl) +{ + impl_ = std::move(impl); +} + + +bool spdlog::lite::logger::should_log(spdlog::lite::level lvl) const SPDLOG_NOEXCEPT +{ + auto spd_level = static_cast(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(lvl); + impl_->log(spd_level, spdlog::details::fmt_helper::to_string_view(formatted)); //TODO and source_loc +} \ No newline at end of file diff --git a/lite/logger.h b/lite/logger.h new file mode 100644 index 00000000..6cc60b09 --- /dev/null +++ b/lite/logger.h @@ -0,0 +1,64 @@ +// +// Created by gabi on 3/16/19. +// + +#ifndef SPDLOG_LIB_LOGGER_H +#define SPDLOG_LIB_LOGGER_H + +#include +#include "spd_types.h" +#include "spdlog/fmt/fmt.h" + + +namespace spdlog { + class logger; + + namespace lite { + class logger { + public: + logger() = default; + + logger(std::shared_ptr 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 + 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 +// void log(spdlog::lite::level lvl, const char *fmt, const Args &... args) +// { +// log(lvl, fmt, args...); +// } + + template + void trace(const char *fmt, const Args &... args) { + log(spdlog::lite::level::trace, fmt, args...); + } + + protected: + std::shared_ptr 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 diff --git a/lite/spd_types.h b/lite/spd_types.h new file mode 100644 index 00000000..94be9b34 --- /dev/null +++ b/lite/spd_types.h @@ -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 + + }; +}} \ No newline at end of file