-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
85 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,19 @@ | ||
set(CMAKE_POSITION_INDEPENDENT_CODE ON) | ||
|
||
set(tgt acquire-zarr-logger) | ||
|
||
add_library(${tgt} | ||
add_library(acquire-logger | ||
logger.hh | ||
logger.cpp | ||
) | ||
|
||
set(PUBLIC_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include/) | ||
|
||
target_include_directories(${tgt} | ||
PUBLIC | ||
$<BUILD_INTERFACE:${PUBLIC_INCLUDE_DIR}> | ||
target_include_directories(acquire-logger | ||
PRIVATE | ||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> | ||
) | ||
|
||
set_target_properties(${tgt} PROPERTIES | ||
set_target_properties(acquire-logger PROPERTIES | ||
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>" | ||
) | ||
|
||
install(TARGETS ${tgt} | ||
install(TARGETS acquire-logger | ||
LIBRARY DESTINATION lib | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,79 @@ | ||
#include "zarr.types.h" | ||
#include "logger.types.h" | ||
|
||
#include <filesystem> | ||
#include <iostream> | ||
#include <mutex> | ||
|
||
class Logger | ||
{ | ||
public: | ||
static void set_log_level(ZarrLogLevel level); | ||
static ZarrLogLevel get_log_level(); | ||
static void set_log_level(LogLevel level); | ||
static LogLevel get_log_level(); | ||
|
||
static std::string log(ZarrLogLevel level, | ||
template<typename... Args> | ||
static std::string log(LogLevel level, | ||
const char* file, | ||
int line, | ||
const char* func, | ||
const char* format, | ||
...); | ||
Args&&... args) | ||
{ | ||
namespace fs = std::filesystem; | ||
|
||
std::scoped_lock lock(log_mutex_); | ||
|
||
std::string prefix; | ||
auto stream = &std::cout; | ||
|
||
switch (level) { | ||
case LogLevel_Debug: | ||
prefix = "[DEBUG] "; | ||
break; | ||
case LogLevel_Info: | ||
prefix = "[INFO] "; | ||
break; | ||
case LogLevel_Warning: | ||
prefix = "[WARNING] "; | ||
break; | ||
default: | ||
prefix = "[ERROR] "; | ||
stream = &std::cerr; | ||
break; | ||
} | ||
|
||
fs::path filepath(file); | ||
std::string filename = filepath.filename().string(); | ||
|
||
std::ostringstream ss; | ||
ss << get_timestamp_() << " " << prefix << filename << ":" << line | ||
<< " " << func << ": "; | ||
|
||
format_arg_(ss, std::forward<Args>(args)...); | ||
|
||
std::string message = ss.str(); | ||
*stream << message << std::endl; | ||
|
||
return message; | ||
} | ||
|
||
private: | ||
static ZarrLogLevel current_level_; | ||
static LogLevel current_level_; | ||
static std::mutex log_mutex_; | ||
|
||
static void format_arg_(std::ostream& ss) {}; // base case | ||
template<typename T, typename... Args> | ||
static void format_arg_(std::ostream& ss, T&& arg, Args&&... args) { | ||
ss << std::forward<T>(arg); | ||
format_arg_(ss, std::forward<Args>(args)...); | ||
} | ||
|
||
static std::string get_timestamp_(); | ||
}; | ||
|
||
#define LOG_DEBUG(...) \ | ||
Logger::log(ZarrLogLevel_Debug, __FILE__, __LINE__, __func__, __VA_ARGS__) | ||
Logger::log(LogLevel_Debug, __FILE__, __LINE__, __func__, __VA_ARGS__) | ||
#define LOG_INFO(...) \ | ||
Logger::log(LogLevel_Info, __FILE__, __LINE__, __func__, __VA_ARGS__) | ||
#define LOG_WARNING(...) \ | ||
Logger::log(ZarrLogLevel_Warning, __FILE__, __LINE__, __func__, __VA_ARGS__) | ||
Logger::log(LogLevel_Warning, __FILE__, __LINE__, __func__, __VA_ARGS__) | ||
#define LOG_ERROR(...) \ | ||
Logger::log(ZarrLogLevel_Error, __FILE__, __LINE__, __func__, __VA_ARGS__) | ||
Logger::log(LogLevel_Error, __FILE__, __LINE__, __func__, __VA_ARGS__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#pragma once | ||
|
||
typedef enum { | ||
LogLevel_Debug, | ||
LogLevel_Info, | ||
LogLevel_Warning, | ||
LogLevel_Error | ||
} LogLevel; |