From 7b9bedc613aeff95d046787d34e00786857f2cd1 Mon Sep 17 00:00:00 2001 From: ackingliu Date: Thu, 30 Nov 2023 11:52:29 +0800 Subject: [PATCH] fix: self contain --- .gitignore | 5 ++++- include/elog/async_logging.h | 1 + include/elog/logger.h | 20 +++++++++++--------- include/elog/source_location.h | 9 ++++----- src/processinfo.cc | 2 +- tests/CMakeLists.txt | 5 ++--- tests/test_config_micros.cc | 27 +++++++-------------------- 7 files changed, 30 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 508a3aa..ed8891b 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,7 @@ log.txt .vscode .vs gh-md-toc.exe -gh-md-toc \ No newline at end of file +gh-md-toc +.DS_Store +*.log +auto_clean.sh \ No newline at end of file diff --git a/include/elog/async_logging.h b/include/elog/async_logging.h index d90b892..ef3b2f2 100644 --- a/include/elog/async_logging.h +++ b/include/elog/async_logging.h @@ -4,6 +4,7 @@ #pragma once #include +#include #include #include #include diff --git a/include/elog/logger.h b/include/elog/logger.h index 00f0355..d23ce9e 100644 --- a/include/elog/logger.h +++ b/include/elog/logger.h @@ -175,7 +175,8 @@ class Log { context ctx; buffer_t buffer; - fmt::format_to(std::back_inserter(buffer), "{}, ", std::forward(first)); + fmt::format_to(std::back_inserter(buffer), "{}, ", + std::forward(first)); println_(ctx, buffer, std::forward(args)...); } @@ -554,10 +555,11 @@ LBLOG_NAMESPACE_END #ifdef ENABLE_ELG_CHECK // check micro #define ELG_CHECK(condition) \ - elog::Check(condition,elog::source_location::current()) + elog::Check(condition, elog::source_location::current()) #define ELG_ASSERT_IF(cond) \ - elog::CheckIfFatal((cond), elog::source_location::current(), "assertion failed:\"" #cond "\"") + elog::CheckIfFatal((cond), elog::source_location::current(), \ + "assertion failed:\"" #cond "\"") #define ELG_CHECK_NOTNULL(ptr) (ELG_ASSERT_IF(ptr != nullptr), ptr) @@ -578,20 +580,20 @@ LBLOG_NAMESPACE_END #ifdef ENABLE_ELG_LOG // log with position micro #define ELG_TRACE(fmt, ...) \ - elog::Log::trace(elog::loc::current(), fmt, ##__VA_ARGS__) + elog::Log::trace(elog::loc::current(__FILE__, __LINE__), fmt, ##__VA_ARGS__) #define ELG_DEBUG(fmt, ...) \ - elog::Log::debug(elog::loc::current(), fmt, ##__VA_ARGS__) + elog::Log::debug(elog::loc::current(__FILE__, __LINE__), fmt, ##__VA_ARGS__) #define ELG_INFO(fmt, ...) \ - elog::Log::info(elog::loc::current(), fmt, ##__VA_ARGS__) + elog::Log::info(elog::loc::current(__FILE__, __LINE__), fmt, ##__VA_ARGS__) #define ELG_WARN(fmt, ...) \ - elog::Log::warn(elog::loc::current(), fmt, ##__VA_ARGS__) + elog::Log::warn(elog::loc::current(__FILE__, __LINE__), fmt, ##__VA_ARGS__) #define ELG_ERROR(fmt, ...) \ - elog::Log::error(elog::loc::current(), fmt, ##__VA_ARGS__) + elog::Log::error(elog::loc::current(__FILE__, __LINE__), fmt, ##__VA_ARGS__) #define ELG_FATAL(fmt, ...) \ - elog::Log::fatal(elog::loc::current(), fmt, ##__VA_ARGS__) + elog::Log::fatal(elog::loc::current(__FILE__, __LINE__), fmt, ##__VA_ARGS__) #endif \ No newline at end of file diff --git a/include/elog/source_location.h b/include/elog/source_location.h index 1b1ffe3..fdc1f93 100644 --- a/include/elog/source_location.h +++ b/include/elog/source_location.h @@ -18,29 +18,28 @@ struct source_location static constexpr source_location current( const char* fileName = __builtin_FILE(), - const char* functionName = __builtin_FUNCTION(), const uint_least32_t lineNumber = __builtin_LINE(), + const char* functionName = __builtin_FUNCTION(), const uint_least32_t columnOffset = __builtin_COLUMN()) noexcept #elif defined(__apple_build_version__) and defined(__clang__) and \ (__clang_major__ >= 9) static constexpr source_location current( const char* fileName = __builtin_FILE(), - const char* functionName = __builtin_FUNCTION(), const uint_least32_t lineNumber = __builtin_LINE(), + const char* functionName = __builtin_FUNCTION(), const uint_least32_t columnOffset = __builtin_COLUMN()) noexcept #elif defined(__GNUC__) and \ (__GNUC__ > 4 or (__GNUC__ == 4 and __GNUC_MINOR__ >= 8)) static constexpr source_location current( const char* fileName = __builtin_FILE(), - const char* functionName = __builtin_FUNCTION(), const uint_least32_t lineNumber = __builtin_LINE(), + const char* functionName = __builtin_FUNCTION(), const uint_least32_t columnOffset = 0) noexcept #else #warning "unsupported source_location" static constexpr source_location current( - const char* fileName = "unsupported", + const char* fileName = "unsupported", const uint_least32_t lineNumber = 0, const char* functionName = "unsupported", - const uint_least32_t lineNumber = 0, const uint_least32_t columnOffset = 0) noexcept #endif { diff --git a/src/processinfo.cc b/src/processinfo.cc index 90be77d..0226e1c 100644 --- a/src/processinfo.cc +++ b/src/processinfo.cc @@ -34,7 +34,7 @@ elog::tid_t ProcessInfo::GetTid() #if defined(_WIN32) thread_local auto tid = GetCurrentThreadId(); #elif defined(__linux__) - auto tid = syscall(SYS_gettid); + auto tid = syscall(SYS_gettid); #else elog::tid_t tid; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7ebc794..9eaa7f3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -14,10 +14,10 @@ add_definitions(-DPROJECT_ROOT="${PROJECT_SOURCE_DIR}/") file(GLOB TEST_FILES "${PROJECT_SOURCE_DIR}/tests/test_*.cc" - ) +) file(GLOB BENCH_FILES "${PROJECT_SOURCE_DIR}/tests/bench_*.cc" - ) +) message(STATUS "TEST_FILES:${TEST_FILES}\n BENCH_FILES:${BENCH_FILES}") @@ -28,7 +28,6 @@ add_test(NAME ${PROJECT_NAME}-unittest COMMAND unittest) add_executable(benchtest ${BENCH_FILES}) target_link_libraries(benchtest PRIVATE doctest_with_main elog nanobench) -add_test(NAME ${PROJECT_NAME}-benchtest COMMAND benchtest) add_executable(perf perf.cc) target_link_libraries(perf elog) \ No newline at end of file diff --git a/tests/test_config_micros.cc b/tests/test_config_micros.cc index 9a88a55..e77e63d 100644 --- a/tests/test_config_micros.cc +++ b/tests/test_config_micros.cc @@ -3,8 +3,8 @@ #define ENABLE_ELG_CHECK #define ENABLE_ELG_LOG -#include "elog/logger.h" #include "doctest/doctest.h" +#include "elog/logger.h" #include "nanobench.h" using namespace elog; @@ -12,7 +12,7 @@ using namespace elog; struct Timer { std::chrono::time_point startPoint; - void start() { startPoint = std::chrono::high_resolution_clock::now(); } + void start() { startPoint = std::chrono::high_resolution_clock::now(); } [[nodiscard]] int64_t end() const { auto endPoint = std::chrono::high_resolution_clock::now(); @@ -29,7 +29,7 @@ void set_console_json_config() { GlobalConfig::Get() .enableConsole(false) - .setFilepath(PROJECT_ROOT "log/") + .setFilepath(PROJECT_ROOT "tests/test_log/") .setLevel(elog::kInfo) .setFormatter(formatter::jsonFormatter) .setFlag(kStdFlags + kThreadId); @@ -39,7 +39,7 @@ void set_console_colorful_config() { GlobalConfig::Get() .enableConsole(false) - .setFilepath(PROJECT_ROOT "log/") + .setFilepath(PROJECT_ROOT "tests/test_log/") .setFormatter(formatter::colorfulFormatter) .setFlag(kStdFlags + kThreadId); } @@ -48,7 +48,7 @@ void set_custom_config() { GlobalConfig::Get() .enableConsole(false) - .setFilepath(PROJECT_ROOT "log/") + .setFilepath(PROJECT_ROOT "tests/test_log/") .setFormatter( formatter::customFromString("[%n][%T][tid:%t][%L][%F][%f]: %v")) .setFlag(kStdFlags + kThreadId); @@ -58,6 +58,7 @@ void set_timer_callback_and_load_config() { GlobalConfig::Get() .loadFromJSON(PROJECT_ROOT "config.json") + .setFilepath(PROJECT_ROOT "tests/test_log/") .setBefore([](output_buf_t& bf) { bf.setContext(Timer{}); auto& tm = any_cast(bf.getMutableContext()); @@ -119,27 +120,13 @@ void test_console_colorful_config() elog::Log::info("hello ejson4cpp"); } -void test_and_bench_CHECK() -{ - const char* data = "n32432432dsfdsafrerm,m,sdfsfsadfjihaodajfladsjfdskaffds"; - GlobalConfig::Get().enableConsole(false).setFilepath(PROJECT_ROOT "log/"); - ankerl::nanobench::Bench() - .minEpochIterations(1000) - .run("bench CHECK", [&]() { ELG_CHECK(1 == 1).info("{}", data); }) - .doNotOptimizeAway(data); -} - TEST_SUITE_BEGIN("test configµs"); -TEST_CASE("bench CHECK_XXX"){ - test_and_bench_CHECK(); -} - TEST_CASE("test config") { // test config with callback test_timer_callback_and_load_config(); - //test local config + // test local config test_localConfig(); // test json formatter test_console_json_config();