Skip to content

Commit

Permalink
Move driver tests to tests/driver. Add logger and stream settings code.
Browse files Browse the repository at this point in the history
  • Loading branch information
aliddell committed Aug 30, 2024
1 parent e5afc9d commit 4ef1c86
Show file tree
Hide file tree
Showing 39 changed files with 1,017 additions and 87 deletions.
31 changes: 31 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,37 @@ set(tgt acquire-zarr)

add_library(${tgt} STATIC
include/zarr.h
internal/logger.hh
internal/logger.cpp
internal/stream.settings.hh
internal/stream.settings.cpp
)

target_include_directories(${tgt}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/internal>
)

target_link_libraries(${tgt} PRIVATE
blosc_static
miniocpp::miniocpp
)

set_target_properties(${tgt} PROPERTIES
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
)

install(TARGETS ${tgt}
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)

# Install public header files
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/
DESTINATION include
FILES_MATCHING PATTERN "*.h"
)

####### Acquire Zarr Driver #######
Expand Down
81 changes: 81 additions & 0 deletions src/internal/logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "logger.hh"

#include <iostream>
#include <cstdarg>
#include <chrono>
#include <iomanip>
#include <filesystem>

LogLevel Logger::current_level = LogLevel_Info;

void
Logger::set_log_level(LogLevel level)
{
current_level = level;
}

LogLevel
Logger::get_log_level()
{
return current_level;
}

std::string
Logger::log(LogLevel level,
const char* file,
int line,
const char* func,
const char* format,
...)
{
if (current_level == LogLevel_None || level < current_level) {
return {}; // Suppress logs
}

va_list args;
va_start(args, format);

std::string prefix;
std::ostream* stream = &std::cout;

switch (level) {
case LogLevel_Debug:
prefix = "[DEBUG] ";
break;
case LogLevel_Info:
prefix = "[INFO] ";
break;
case LogLevel_Warning:
prefix = "[WARNING] ";
stream = &std::cerr;
break;
case LogLevel_Error:
prefix = "[ERROR] ";
stream = &std::cerr;
break;
}

// Get current time
auto now = std::chrono::system_clock::now();
auto time = std::chrono::system_clock::to_time_t(now);
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch()) %
1000;

// Get filename without path
std::filesystem::path filepath(file);
std::string filename = filepath.filename().string();

// Output timestamp, log level, filename
*stream << std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S") << '.'
<< std::setfill('0') << std::setw(3) << ms.count() << " " << prefix
<< filename << ":" << line << " " << func << ": ";

char buffer[1024];
vsnprintf(buffer, sizeof(buffer), format, args);
*stream << buffer << std::endl;

va_end(args);

return buffer;
}
38 changes: 38 additions & 0 deletions src/internal/logger.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "zarr.h"

#include <string>

class Logger
{
public:
static void set_log_level(LogLevel level);
static LogLevel get_log_level();

static std::string log(LogLevel level,
const char* file,
int line,
const char* func,
const char* format,
...);

private:
static LogLevel current_level;
};

#define LOG_DEBUG(...) \
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(LogLevel_Warning, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define LOG_ERROR(...) \
Logger::log(LogLevel_Error, __FILE__, __LINE__, __func__, __VA_ARGS__)

#define EXPECT(e, ...) \
do { \
if (!(e)) { \
const std::string __err = LOG_ERROR(__VA_ARGS__); \
throw std::runtime_error(__err); \
} \
} while (0)
#define CHECK(e) EXPECT(e, "Expression evaluated as false:\n\t%s", #e)
Loading

0 comments on commit 4ef1c86

Please sign in to comment.