Skip to content

Commit

Permalink
fix: self contain
Browse files Browse the repository at this point in the history
  • Loading branch information
ackingliu committed Nov 30, 2023
1 parent c14dab4 commit 7b9bedc
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 39 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ log.txt
.vscode
.vs
gh-md-toc.exe
gh-md-toc
gh-md-toc
.DS_Store
*.log
auto_clean.sh
1 change: 1 addition & 0 deletions include/elog/async_logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#pragma once
#include <atomic>
#include <string>
#include <cstring>
#include <thread>
#include <vector>
Expand Down
20 changes: 11 additions & 9 deletions include/elog/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ class Log
{
context ctx;
buffer_t buffer;
fmt::format_to(std::back_inserter(buffer), "{}, ", std::forward<T>(first));
fmt::format_to(std::back_inserter(buffer), "{}, ",
std::forward<T>(first));
println_(ctx, buffer, std::forward<Args>(args)...);
}

Expand Down Expand Up @@ -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)

Expand All @@ -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
9 changes: 4 additions & 5 deletions include/elog/source_location.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/processinfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 2 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand All @@ -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)
27 changes: 7 additions & 20 deletions tests/test_config_micros.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

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

struct Timer
{
std::chrono::time_point<std::chrono::high_resolution_clock> 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();
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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<Timer&>(bf.getMutableContext());
Expand Down Expand Up @@ -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&micros");

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();
Expand Down

0 comments on commit 7b9bedc

Please sign in to comment.