Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standalone 3/N: Define the Zarr logger #295

Merged
merged 20 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c7832d7
Move driver source files and tests to a separate directory.
aliddell Sep 17, 2024
a811e22
Define the Zarr streaming API.
aliddell Sep 17, 2024
cf89e13
Define the Zarr logger.
aliddell Sep 17, 2024
fff86c6
Rename zarr.h to acquire.zarr.h.
aliddell Sep 17, 2024
61a53bc
Merge branch 'standalone-sequence-3' into standalone-sequence-4
aliddell Sep 17, 2024
bdd0c51
Instantiate the logger's mutex.
aliddell Sep 17, 2024
d099795
Document the StreamSettings getters.
aliddell Sep 17, 2024
cfd412e
Merge remote-tracking branch 'upstream/standalone-sequence-3' into st…
aliddell Sep 17, 2024
da98e57
call it 'type'
aliddell Sep 17, 2024
aa3489e
Merge branch 'standalone-sequence-3' into standalone-sequence-4
aliddell Sep 17, 2024
996d22e
Document that ZarrStream_append will block for compression and flushing
aliddell Sep 18, 2024
d6ea43e
Merge branch 'standalone-sequence-3' into standalone-sequence-4
aliddell Sep 18, 2024
e546f7d
Respond to PR comments.
aliddell Sep 18, 2024
2d6aae1
Merge branch 'standalone-sequence-2' into standalone-sequence-3
aliddell Sep 18, 2024
a1b0034
Merge remote-tracking branch 'upstream/main' into standalone-sequence-3
aliddell Sep 18, 2024
cb5414f
Merge remote-tracking branch 'upstream/main' into standalone-sequence-4
aliddell Sep 18, 2024
42a4940
Respond to PR comments.
aliddell Sep 20, 2024
9a6212f
Merge branch 'standalone-sequence-3' into standalone-sequence-4
aliddell Sep 20, 2024
ca93466
Merge remote-tracking branch 'upstream/main' into standalone-sequence-4
aliddell Sep 24, 2024
bda14d7
Respond to PR comments
aliddell Sep 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_subdirectory(logger)
if (BUILD_ACQUIRE_DRIVER_ZARR)
add_subdirectory(driver)
endif ()
19 changes: 19 additions & 0 deletions src/logger/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

add_library(acquire-logger
logger.hh
logger.cpp
)

target_include_directories(acquire-logger
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
)

set_target_properties(acquire-logger PROPERTIES
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
)

install(TARGETS acquire-logger
LIBRARY DESTINATION lib
)
44 changes: 44 additions & 0 deletions src/logger/logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "logger.hh"

#include <iomanip>
#include <string>
#include <thread>

LogLevel Logger::current_level_ = LogLevel_Info;
std::mutex Logger::log_mutex_{};

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

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

std::string
Logger::get_timestamp_()
{

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;
Comment on lines +26 to +30
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I mentioned this in the large PR review. I would extract this code and std::put_time to a separate function. This snippet is copied from a logger that I wrote a while back so it may need to be adjusted a bit if you decide to use it.

auto GetTimestamp() -> std::string {
    using std::chrono::system_clock;
    auto now = system_clock::now();
    auto in_time_t = system_clock::to_time_t(now);

    auto time_info = std::tm{};
    #ifdef _WIN32
        localtime_s(&time_info, &in_time_t);
    #else
        localtime_r(&in_time_t, &time_info);
    #endif

    return fmt::format("{:04}-{:02}-{:02} {:02}:{:02}:{:02}",
                       time_info.tm_year + 1900,
                       time_info.tm_mon + 1,
                       time_info.tm_mday,
                       time_info.tm_hour,
                       time_info.tm_min,
                       time_info.tm_sec);
}


std::tm tm{};
#if defined(_WIN32)
localtime_s(&tm, &time);
#else
localtime_r(&time, &tm);
#endif

std::ostringstream ss;
ss << std::put_time(&tm, "%Y-%m-%d %H:%M:%S") << '.' << std::setfill('0')
<< std::setw(3) << ms.count();

return ss.str();
}
80 changes: 80 additions & 0 deletions src/logger/logger.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "logger.types.h"

#include <filesystem>
#include <iostream>
#include <mutex>
#include <sstream>

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

template<typename... Args>
static std::string log(LogLevel level,
const char* file,
int line,
const char* func,
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 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(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__)
8 changes: 8 additions & 0 deletions src/logger/logger.types.h
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;
Loading