Skip to content

Commit

Permalink
Set log level from Python (#17)
Browse files Browse the repository at this point in the history
* Fix bugs in multipart upload

* Test S3 streaming

* Run S3 tests

* Test S3 in Python from dedicated S3 test job

* Fix s3_settings fixture

* Replace brackets in store path on S3

* add python-dotenv to testing dependencies

* Add LogLevel_None to supress logs.

* Bind and test setting/getting log level.

* Suppress logs lower than current log level.
  • Loading branch information
aliddell authored Nov 5, 2024
1 parent 4f0a89d commit 8779f14
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 4 deletions.
53 changes: 53 additions & 0 deletions python/acquire-zarr-py.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <iostream>
#include <memory>

#include <pybind11/pybind11.h>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -444,6 +464,13 @@ PYBIND11_MODULE(acquire_zarr, m)
.value(dimension_type_to_str(ZarrDimensionType_Other),
ZarrDimensionType_Other);

py::enum_<ZarrLogLevel>(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_<PyZarrS3Settings>(m, "S3Settings", py::dynamic_attr())
.def(py::init([](py::kwargs kwargs) {
PyZarrS3Settings settings;
Expand Down Expand Up @@ -671,4 +698,30 @@ PYBIND11_MODULE(acquire_zarr, m)
.def(py::init<PyZarrStreamSettings>())
.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;
}
}
13 changes: 12 additions & 1 deletion python/tests/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import pytest
import zarr
from numcodecs import blosc
import re
import s3fs

from acquire_zarr import (
Expand All @@ -28,6 +27,9 @@
DimensionType,
ZarrVersion,
DataType,
LogLevel,
set_log_level,
get_log_level
)


Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/logger/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
4 changes: 4 additions & 0 deletions src/logger/logger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class Logger
{
namespace fs = std::filesystem;

if (level < current_level_) {
return "";
}

std::scoped_lock lock(log_mutex_);

std::string prefix;
Expand Down
3 changes: 2 additions & 1 deletion src/logger/logger.types.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ typedef enum {
LogLevel_Debug,
LogLevel_Info,
LogLevel_Warning,
LogLevel_Error
LogLevel_Error,
LogLevel_None,
} LogLevel;
8 changes: 7 additions & 1 deletion src/streaming/acquire.zarr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ extern "C"
case ZarrLogLevel_Error:
level = LogLevel_Error;
break;
case ZarrLogLevel_None:
level = LogLevel_None;
break;
default:
return ZarrStatusCode_InvalidArgument;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 8779f14

Please sign in to comment.