diff --git a/python/acquire-zarr-py.cpp b/python/acquire-zarr-py.cpp index d8dbaed..f50d7e0 100644 --- a/python/acquire-zarr-py.cpp +++ b/python/acquire-zarr-py.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -89,6 +90,25 @@ dimension_type_to_str(ZarrDimensionType t) return "UNKNOWN"; } } + +const char* +log_level_to_str(ZarrLogLevel level) +{ + switch (level) { + case ZarrLogLevel_Debug: + return "DEBUG"; + case ZarrLogLevel_Info: + return "INFO"; + case ZarrLogLevel_Warning: + return "WARNING"; + case ZarrLogLevel_Error: + return "ERROR"; + case ZarrLogLevel_None: + return "NONE"; + default: + return "UNKNOWN"; + } +} } // namespace class PyZarrS3Settings @@ -444,6 +464,13 @@ PYBIND11_MODULE(acquire_zarr, m) .value(dimension_type_to_str(ZarrDimensionType_Other), ZarrDimensionType_Other); + py::enum_(m, "LogLevel") + .value(log_level_to_str(ZarrLogLevel_Debug), ZarrLogLevel_Debug) + .value(log_level_to_str(ZarrLogLevel_Info), ZarrLogLevel_Info) + .value(log_level_to_str(ZarrLogLevel_Warning), ZarrLogLevel_Warning) + .value(log_level_to_str(ZarrLogLevel_Error), ZarrLogLevel_Error) + .value(log_level_to_str(ZarrLogLevel_None), ZarrLogLevel_None); + py::class_(m, "S3Settings", py::dynamic_attr()) .def(py::init([](py::kwargs kwargs) { PyZarrS3Settings settings; @@ -671,4 +698,30 @@ PYBIND11_MODULE(acquire_zarr, m) .def(py::init()) .def("append", &PyZarrStream::append) .def("is_active", &PyZarrStream::is_active); + + m.def( + "set_log_level", + [](ZarrLogLevel level) { + auto status = Zarr_set_log_level(level); + if (status != ZarrStatusCode_Success) { + std::string err = "Failed to set log level: " + + std::string(Zarr_get_status_message(status)); + PyErr_SetString(PyExc_RuntimeError, err.c_str()); + throw py::error_already_set(); + } + }, + "Set the log level for the Zarr API", + py::arg("level")); + + m.def( + "get_log_level", + []() { return Zarr_get_log_level(); }, + "Get the current log level for the Zarr API"); + + auto init_status = Zarr_set_log_level(ZarrLogLevel_Info); + if (init_status != ZarrStatusCode_Success) { + // Log the error but don't throw, as that would prevent module import + std::cerr << "Warning: Failed to set initial log level: " + << Zarr_get_status_message(init_status) << std::endl; + } } diff --git a/python/tests/test_stream.py b/python/tests/test_stream.py index fdf2b4e..114db8e 100644 --- a/python/tests/test_stream.py +++ b/python/tests/test_stream.py @@ -14,7 +14,6 @@ import pytest import zarr from numcodecs import blosc -import re import s3fs from acquire_zarr import ( @@ -28,6 +27,9 @@ DimensionType, ZarrVersion, DataType, + LogLevel, + set_log_level, + get_log_level ) @@ -397,3 +399,12 @@ def test_stream_data_to_s3( # cleanup s3.rm(store.root, recursive=True) + + +@pytest.mark.parametrize( + ("level",), + [(LogLevel.DEBUG,), (LogLevel.INFO,), (LogLevel.WARNING,), (LogLevel.ERROR,), (LogLevel.NONE,)], +) +def test_set_log_level(level: LogLevel): + set_log_level(level) + assert get_log_level() == level diff --git a/src/logger/logger.cpp b/src/logger/logger.cpp index c7af558..4f7b291 100644 --- a/src/logger/logger.cpp +++ b/src/logger/logger.cpp @@ -10,7 +10,7 @@ std::mutex Logger::log_mutex_{}; void Logger::set_log_level(LogLevel level) { - if (level < LogLevel_Debug || level > LogLevel_Error) { + if (level < LogLevel_Debug || level > LogLevel_None) { throw std::invalid_argument("Invalid log level"); } diff --git a/src/logger/logger.hh b/src/logger/logger.hh index c504699..4efaaf0 100644 --- a/src/logger/logger.hh +++ b/src/logger/logger.hh @@ -20,6 +20,10 @@ class Logger { namespace fs = std::filesystem; + if (level < current_level_) { + return ""; + } + std::scoped_lock lock(log_mutex_); std::string prefix; diff --git a/src/logger/logger.types.h b/src/logger/logger.types.h index 86d9c0b..f0adb7a 100644 --- a/src/logger/logger.types.h +++ b/src/logger/logger.types.h @@ -4,5 +4,6 @@ typedef enum { LogLevel_Debug, LogLevel_Info, LogLevel_Warning, - LogLevel_Error + LogLevel_Error, + LogLevel_None, } LogLevel; diff --git a/src/streaming/acquire.zarr.cpp b/src/streaming/acquire.zarr.cpp index 91f7f5f..3e9b183 100644 --- a/src/streaming/acquire.zarr.cpp +++ b/src/streaming/acquire.zarr.cpp @@ -27,6 +27,9 @@ extern "C" case ZarrLogLevel_Error: level = LogLevel_Error; break; + case ZarrLogLevel_None: + level = LogLevel_None; + break; default: return ZarrStatusCode_InvalidArgument; } @@ -53,9 +56,12 @@ extern "C" case LogLevel_Warning: level = ZarrLogLevel_Warning; break; - default: + case LogLevel_Error: level = ZarrLogLevel_Error; break; + case LogLevel_None: + level = ZarrLogLevel_None; + break; } return level; }