-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from nqminds/feat/add-LoggerContextManager
feat: add `LoggerContextManager` Python class
- Loading branch information
Showing
8 changed files
with
140 additions
and
6 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
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
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
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,9 @@ | ||
#include "./logger_context_manager.hpp" | ||
|
||
LoggerContextManager::~LoggerContextManager() = default; | ||
|
||
void LoggerContextManager::start() { | ||
if (!logger_) logger_ = std::make_unique<Logger>(); | ||
} | ||
|
||
void LoggerContextManager::stop() { logger_ = nullptr; } |
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,31 @@ | ||
#ifndef NQM_IRIMAGER_LOGGER_CONTEXT_MANAGER | ||
#define NQM_IRIMAGER_LOGGER_CONTEXT_MANAGER | ||
|
||
#include <memory> | ||
|
||
#include <pybind11/pybind11.h> | ||
|
||
#include "./logger.hpp" | ||
|
||
/** | ||
* Context Manager around a Logger object. | ||
* | ||
* Designed for use with Python's ``with`` syntax. | ||
*/ | ||
class LoggerContextManager { | ||
public: | ||
virtual ~LoggerContextManager(); | ||
|
||
/** | ||
* Starts the logger if it's not already started. | ||
*/ | ||
void start(); | ||
|
||
/** Stops the logger */ | ||
void stop(); | ||
|
||
private: | ||
std::unique_ptr<Logger> logger_; | ||
}; | ||
|
||
#endif /* NQM_IRIMAGER_LOGGER_CONTEXT_MANAGER */ |
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,41 @@ | ||
"""Tests for nqm.irimager.Logging""" | ||
import logging | ||
|
||
import pytest | ||
|
||
from nqm.irimager import LoggerContextManager | ||
|
||
|
||
def test_logger_in_context_manager(caplog): | ||
"""We should be able to wrap logger in a context manager""" | ||
|
||
with caplog.at_level(logging.DEBUG): | ||
with LoggerContextManager(): | ||
assert "Redirecting spdlogs to a callback." in caplog.text | ||
|
||
caplog.clear() | ||
|
||
# creating a second logger should be fine after the first contextmanager | ||
# closes | ||
with LoggerContextManager(): | ||
assert "Redirecting spdlogs to a callback." in caplog.text | ||
|
||
|
||
def test_logger_in_context_manager_handles_exception(caplog): | ||
"""We should be able to wrap logger in a context manager and handle exceptions""" | ||
|
||
with caplog.at_level(logging.DEBUG): | ||
# creating a second logger should be fine, even if the first | ||
# contextmanager broke due to an exception | ||
with pytest.raises( | ||
NotImplementedError, | ||
match="Testing whether contextmanager cleans up after errors", | ||
): | ||
with LoggerContextManager(): | ||
raise NotImplementedError( | ||
"Testing whether contextmanager cleans up after errors" | ||
) | ||
|
||
caplog.clear() | ||
with LoggerContextManager(): | ||
assert "Redirecting spdlogs to a callback." in caplog.text |