This commit is contained in:
gabime 2019-03-30 14:53:02 +03:00
parent e32c856a04
commit c2b0e223fa
8 changed files with 202 additions and 137 deletions

View File

@ -1,8 +1,10 @@
// Copyright(c) 2015-present Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#include "spdlite.h"
#include "spdlog/spdlog.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/stdout_sinks.h"
#define UNUSED(x) (void)(x)
@ -10,5 +12,5 @@
spdlite::logger create_logger(void *ctx)
{
UNUSED(ctx);
return spdlite::default_logger();
return spdlite::logger(spdlog::basic_logger_mt("logger-name", "log.txt", true));
}

View File

@ -1,12 +1,12 @@
#include "spdlite.h"
// Copyright(c) 2015-present Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#include "spdlite.h"
#include "spdlite_global.h"
#define SPDLITE_ACTIVE_LEVEL SPDLITE_LEVEL_TRACE
#include "spdlite_macros.h"
int main()
{
spdlite::default_logger().set_level(spdlite::level::trace);
spdlite::trace_printf("Hello %d", 123);
spdlite::debug_printf("Hello %d", 123);
spdlite::info_printf("Hello %d", 123);
spdlite::warn_printf("Hello %d", 123);
spdlite::error_printf("Hello %d", 123);
spdlite::critical_printf("Hello %d", 123);
{
SPDLITE_TRACE("SOME INFO {}", 123);
}

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.1)
project(spdlog_lite)
add_library(spdlog_lite spdlite.cpp spdlite.h)
add_library(spdlog_lite spdlite.cpp spdlite.h spdlite_global.cpp spdlite_global.h spdlite_macros.h)
target_link_libraries(spdlog_lite spdlog::spdlog)

View File

@ -1,3 +1,8 @@
//
// Copyright(c) 2019-present spdlog
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#include "spdlite.h"
#include "spdlog/spdlog.h"
@ -147,62 +152,4 @@ spdlite::logger &spdlite::logger::default_logger()
return default_inst_;
}
spdlite::logger &spdlite::default_logger()
{
return spdlite::logger::default_logger();
}
// printf
void spdlite::log_printf(spdlite::level lvl, const char *format, va_list args)
{
default_logger().log_printf(lvl, format, args);
}
void spdlite::trace_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
log_printf(level::trace, format, args);
va_end(args);
}
void spdlite::debug_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
log_printf(level::debug, format, args);
va_end(args);
}
void spdlite::info_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
log_printf(level::info, format, args);
va_end(args);
}
void spdlite::warn_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
log_printf(level::warn, format, args);
va_end(args);
}
void spdlite::error_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
log_printf(level::err, format, args);
va_end(args);
}
void spdlite::critical_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
log_printf(level::critical, format, args);
va_end(args);
}

View File

@ -1,4 +1,3 @@
//
// Copyright(c) 2015-present Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
@ -211,55 +210,6 @@ protected:
void log_formatted_(spdlite::level lvl, const fmt::memory_buffer &formatted);
};
//
// spdlite namespace functions - forward the calls to the default_logger.
//
spdlite::logger &default_logger();
template<typename... Args>
inline void trace(const char *fmt, const Args &... args)
{
default_logger().trace(fmt, args...);
}
template<typename... Args>
inline void debug(const char *fmt, const Args &... args)
{
default_logger().debug(fmt, args...);
}
template<typename... Args>
inline void info(const char *fmt, const Args &... args)
{
default_logger().info(fmt, args...);
}
template<typename... Args>
inline void warn(const char *fmt, const Args &... args)
{
default_logger().warn(fmt, args...);
}
template<typename... Args>
inline void error(const char *fmt, const Args &... args)
{
default_logger().error(fmt, args...);
}
template<typename... Args>
inline void critical(const char *fmt, const Args &... args)
{
default_logger().critical(fmt, args...);
}
void log_printf(spdlite::level lvl, const char *format, va_list args);
void trace_printf(const char *format, ...);
void debug_printf(const char *format, ...);
void info_printf(const char *format, ...);
void warn_printf(const char *format, ...);
void error_printf(const char *format, ...);
void critical_printf(const char *format, ...);
} // namespace spdlite

63
lite/spdlite_global.cpp Normal file
View File

@ -0,0 +1,63 @@
// Copyright(c) 2015-present Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#include "spdlite_global.h"
spdlite::logger &spdlite::default_logger()
{
return spdlite::logger::default_logger();
}
// printf
void spdlite::log_printf(spdlite::level lvl, const char *format, va_list args)
{
default_logger().log_printf(lvl, format, args);
}
void spdlite::trace_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
log_printf(level::trace, format, args);
va_end(args);
}
void spdlite::debug_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
log_printf(level::debug, format, args);
va_end(args);
}
void spdlite::info_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
log_printf(level::info, format, args);
va_end(args);
}
void spdlite::warn_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
log_printf(level::warn, format, args);
va_end(args);
}
void spdlite::error_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
log_printf(level::err, format, args);
va_end(args);
}
void spdlite::critical_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
log_printf(level::critical, format, args);
va_end(args);
}

95
lite/spdlite_global.h Normal file
View File

@ -0,0 +1,95 @@
// Copyright(c) 2015-present Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#include "spdlite.h"
namespace spdlite
{
//
// spdlite namespace functions - forward the calls to the default_logger.
//
spdlite::logger &default_logger();
template<typename... Args>
inline void trace(const char *fmt, const Args &... args)
{
default_logger().trace(fmt, args...);
}
template<typename... Args>
inline void debug(const char *fmt, const Args &... args)
{
default_logger().debug(fmt, args...);
}
template<typename... Args>
inline void info(const char *fmt, const Args &... args)
{
default_logger().info(fmt, args...);
}
template<typename... Args>
inline void warn(const char *fmt, const Args &... args)
{
default_logger().warn(fmt, args...);
}
template<typename... Args>
inline void error(const char *fmt, const Args &... args)
{
default_logger().error(fmt, args...);
}
template<typename... Args>
inline void critical(const char *fmt, const Args &... args)
{
default_logger().critical(fmt, args...);
}
// string view convertable
template<typename T>
inline void trace(const T &msg)
{
default_logger().trace(msg);
}
template<typename T>
inline void debug(const T &msg)
{
default_logger().debug(msg);
}
template<typename T>
inline void info(const T &msg)
{
default_logger().info(msg);
}
template<typename T>
inline void warn(const T &msg)
{
default_logger().warn(msg);
}
template<typename T>
inline void error(const T &msg)
{
default_logger().error(msg);
}
template<typename T>
inline void critical(const T &msg)
{
default_logger().critical(msg);
}
void log_printf(spdlite::level lvl, const char *format, va_list args);
void trace_printf(const char *format, ...);
void debug_printf(const char *format, ...);
void info_printf(const char *format, ...);
void warn_printf(const char *format, ...);
void error_printf(const char *format, ...);
void critical_printf(const char *format, ...);
}

View File

@ -1,6 +1,5 @@
//
// Created by gabi on 3/24/19.
//
// Copyright(c) 2015-present Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
//
@ -8,6 +7,7 @@
//
// define SPDLITE_ACTIVE_LEVEL to one of those (before including lite.h):
#define SPDLITE_LEVEL_TRACE 0
#define SPDLITE_LEVEL_DEBUG 1
#define SPDLITE_LEVEL_INFO 2
@ -18,49 +18,57 @@
#define SPDLITE_LOGGER_CALL(logger, level, ...) logger.log(level, __VA_ARGS__)
#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_TRACE
#define SPDLITE_LOGGER_TRACE(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::trace, __VA_ARGS__)
#define SPDLITE_TRACE(...) SPDLITE_LOGGER_TRACE(spdlog::lite::default_logger(), __VA_ARGS__)
// default level is info
#ifndef SPDLITE_ACTIVE_LEVEL
#define SPDLITE_ACTIVE_LEVEL SPDLITE_LEVEL_INFO
#endif
static_assert(SPDLITE_ACTIVE_LEVEL >= SPDLITE_LEVEL_TRACE && SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_OFF, "SPDLITE_ACTIVE_LEVEL");
#if SPDLITE_ACTIVE_LEVEL == SPDLITE_LEVEL_TRACE
#define SPDLITE_LOGGER_TRACE(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlite::level::trace, __VA_ARGS__)
#define SPDLITE_TRACE(...) SPDLITE_LOGGER_TRACE(spdlite::default_logger(), __VA_ARGS__)
#else
#define SPDLITE_LOGGER_TRACE(logger, ...) (void)0
#define SPDLITE_TRACE(...) (void)0
#endif
#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_DEBUG
#define SPDLITE_LOGGER_DEBUG(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::debug, __VA_ARGS__)
#define SPDLITE_DEBUG(...) SPDLITE_LOGGER_DEBUG(spdlog::lite::default_logger(), __VA_ARGS__)
#define SPDLITE_LOGGER_DEBUG(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlite::level::debug, __VA_ARGS__)
#define SPDLITE_DEBUG(...) SPDLITE_LOGGER_DEBUG(spdlite::default_logger(), __VA_ARGS__)
#else
#define SPDLITE_LOGGER_DEBUG(logger, ...) (void)0
#define SPDLITE_DEBUG(...) (void)0
#endif
#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_INFO
#define SPDLITE_LOGGER_INFO(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::info, __VA_ARGS__)
#define SPDLITE_INFO(...) SPDLITE_LOGGER_INFO(spdlog::lite::default_logger(), __VA_ARGS__)
#define SPDLITE_LOGGER_INFO(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlite::level::info, __VA_ARGS__)
#define SPDLITE_INFO(...) SPDLITE_LOGGER_INFO(spdlite::default_logger(), __VA_ARGS__)
#else
#define SPDLITE_LOGGER_INFO(logger, ...) (void)0
#define SPDLITE_INFO(...) (void)0
#endif
#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_WARN
#define SPDLITE_LOGGER_WARN(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::warn, __VA_ARGS__)
#define SPDLITE_WARN(...) SPDLITE_LOGGER_WARN(spdlog::lite::default_logger(), __VA_ARGS__)
#define SPDLITE_LOGGER_WARN(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlite::level::warn, __VA_ARGS__)
#define SPDLITE_WARN(...) SPDLITE_LOGGER_WARN(spdlite::default_logger(), __VA_ARGS__)
#else
#define SPDLITE_LOGGER_WARN(logger, ...) (void)0
#define SPDLITE_WARN(...) (void)0
#endif
#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_ERROR
#define SPDLITE_LOGGER_ERROR(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::err, __VA_ARGS__)
#define SPDLITE_ERROR(...) SPDLITE_LOGGER_ERROR(spdlog::lite::default_logger(), __VA_ARGS__)
#define SPDLITE_LOGGER_ERROR(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlite::level::err, __VA_ARGS__)
#define SPDLITE_ERROR(...) SPDLITE_LOGGER_ERROR(spdlite::default_logger(), __VA_ARGS__)
#else
#define SPDLITE_LOGGER_ERROR(logger, ...) (void)0
#define SPDLITE_ERROR(...) (void)0
#endif
#if SPDLITE_ACTIVE_LEVEL <= SPDLITE_LEVEL_CRITICAL
#define SPDLITE_LOGGER_CRITICAL(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlog::lite::level::critical, __VA_ARGS__)
#define SPDLITE_CRITICAL(...) SPDLITE_LOGGER_CRITICAL(spdlog::lite::default_logger(), __VA_ARGS__)
#define SPDLITE_LOGGER_CRITICAL(logger, ...) SPDLITE_LOGGER_CALL(logger, spdlite::level::critical, __VA_ARGS__)
#define SPDLITE_CRITICAL(...) SPDLITE_LOGGER_CRITICAL(spdlite::default_logger(), __VA_ARGS__)
#else
#define SPDLITE_LOGGER_CRITICAL(logger, ...) (void)0
#define SPDLITE_CRITICAL(...) (void)0