diff --git a/include/librealsense2/rs.h b/include/librealsense2/rs.h index e7d7fdf909..aa6f82f734 100644 --- a/include/librealsense2/rs.h +++ b/include/librealsense2/rs.h @@ -80,6 +80,7 @@ void rs2_log_to_callback_cpp( rs2_log_severity min_severity, rs2_log_callback * void rs2_log_to_callback( rs2_log_severity min_severity, rs2_log_callback_ptr callback, void * arg, rs2_error** error ); +void rs2_reset_logger( rs2_error ** error); unsigned rs2_get_log_message_line_number( rs2_log_message const * msg, rs2_error** error ); const char * rs2_get_log_message_filename( rs2_log_message const * msg, rs2_error** error ); diff --git a/include/librealsense2/rs.hpp b/include/librealsense2/rs.hpp index 8922881300..5a09a68388 100644 --- a/include/librealsense2/rs.hpp +++ b/include/librealsense2/rs.hpp @@ -30,6 +30,12 @@ namespace rs2 error::handle(e); } + inline void reset_logger() + { + rs2_error* e = nullptr; + rs2_reset_logger(&e); + error::handle(e); + } /* Interface to the log message data we expose. */ diff --git a/src/log.cpp b/src/log.cpp index aef102219a..0e1f9be02d 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -29,6 +29,11 @@ void librealsense::log_to_callback( rs2_log_severity min_severity, log_callback_ logger.log_to_callback( min_severity, callback ); } +void librealsense::reset_logger() +{ + logger.reset_logger(); +} + #else // BUILD_EASYLOGGINGPP void librealsense::log_to_console(rs2_log_severity min_severity) @@ -43,5 +48,9 @@ void librealsense::log_to_callback(rs2_log_severity min_severity, log_callback_p { throw std::runtime_error("log_to_callback is not supported without BUILD_EASYLOGGINGPP"); } + +void librealsense::reset_logger() +{ +} #endif // BUILD_EASYLOGGINGPP diff --git a/src/log.h b/src/log.h index 671884c39c..2005586441 100644 --- a/src/log.h +++ b/src/log.h @@ -243,6 +243,18 @@ namespace librealsense //el::Helpers::uninstallLogDispatchCallback< el::base::DefaultLogDispatchCallback >( "DefaultLogDispatchCallback" ); } } + + //Stop logging and reset logger to initial configurations + void reset_logger() + { + el::Loggers::reconfigureLogger(log_id, el::ConfigurationType::ToFile, "false"); + el::Loggers::reconfigureLogger(log_id, el::ConfigurationType::ToStandardOutput, "false"); + remove_callbacks(); + + minimum_log_severity = RS2_LOG_SEVERITY_NONE; + minimum_console_severity = RS2_LOG_SEVERITY_NONE; + minimum_file_severity = RS2_LOG_SEVERITY_NONE; + } }; #else //BUILD_EASYLOGGINGPP struct log_message diff --git a/src/realsense.def b/src/realsense.def index 5ba76f1343..3ac921b9bc 100644 --- a/src/realsense.def +++ b/src/realsense.def @@ -147,6 +147,7 @@ EXPORTS rs2_log_to_file rs2_log_to_callback rs2_log_to_callback_cpp + rs2_reset_logger rs2_get_log_message_line_number rs2_get_log_message_filename diff --git a/src/rs.cpp b/src/rs.cpp index fda9939abd..f79afb75ca 100644 --- a/src/rs.cpp +++ b/src/rs.cpp @@ -1304,6 +1304,12 @@ void rs2_log_to_callback_cpp( rs2_log_severity min_severity, rs2_log_callback * } HANDLE_EXCEPTIONS_AND_RETURN( , min_severity, callback ) +void rs2_reset_logger( rs2_error** error) BEGIN_API_CALL +{ + librealsense::reset_logger(); +} +NOARGS_HANDLE_EXCEPTIONS_AND_RETURN() + // librealsense wrapper around a C function class on_log_callback : public rs2_log_callback { diff --git a/src/types.h b/src/types.h index 82506acf5b..0928d224a9 100644 --- a/src/types.h +++ b/src/types.h @@ -204,6 +204,7 @@ namespace librealsense void log_to_console(rs2_log_severity min_severity); void log_to_file( rs2_log_severity min_severity, const char* file_path ); void log_to_callback( rs2_log_severity min_severity, log_callback_ptr callback ); + void reset_logger(); #if BUILD_EASYLOGGINGPP diff --git a/unit-tests/log/test-c-reset-logger.cpp b/unit-tests/log/test-c-reset-logger.cpp new file mode 100644 index 0000000000..8b91f5c528 --- /dev/null +++ b/unit-tests/log/test-c-reset-logger.cpp @@ -0,0 +1,56 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2020 Intel Corporation. All Rights Reserved. + +//#cmake:add-file log-common.h +#include "log-common.h" + + +// See log_callback_function_ptr +size_t c_n_callbacks = 0; +void c_callback(rs2_log_severity severity, rs2_log_message const* msg, void * arg) +{ + REQUIRE(!arg); + ++c_n_callbacks; + rs2_error* e = nullptr; + char const* str = rs2_get_full_log_message(msg, &e); + REQUIRE_NOTHROW(rs2::error::handle(e)); + TRACE(str); +} + +TEST_CASE("RESET C LOGGER", "[log]") { + c_n_callbacks = 0; + rs2_error* e = nullptr; + + rs2_log_to_callback(RS2_LOG_SEVERITY_INFO, c_callback, nullptr, &e); + REQUIRE_NOTHROW(rs2::error::handle(e)); + REQUIRE(!c_n_callbacks); + rs2_reset_logger(&e); + REQUIRE_NOTHROW(rs2::error::handle(e)); + log_all(); + REQUIRE(c_n_callbacks == 0); + + rs2_log_to_callback(RS2_LOG_SEVERITY_INFO, c_callback, nullptr, &e); + REQUIRE_NOTHROW(rs2::error::handle(e)); + log_all(); + REQUIRE(c_n_callbacks == 3); + + rs2_reset_logger(&e); + REQUIRE_NOTHROW(rs2::error::handle(e)); + c_n_callbacks = 0; + + rs2_log_to_callback(RS2_LOG_SEVERITY_DEBUG, c_callback, nullptr, &e); + REQUIRE_NOTHROW(rs2::error::handle(e)); + REQUIRE(!c_n_callbacks); + log_all(); + REQUIRE(c_n_callbacks == 4); + + rs2_reset_logger(&e); + REQUIRE_NOTHROW(rs2::error::handle(e)); + c_n_callbacks = 0; + + rs2_log_to_callback(RS2_LOG_SEVERITY_NONE, c_callback, nullptr, &e); + REQUIRE_NOTHROW(rs2::error::handle(e)); + REQUIRE(!c_n_callbacks); + log_all(); + REQUIRE(c_n_callbacks == 0); +} diff --git a/unit-tests/log/test-cpp-reset-logger.cpp b/unit-tests/log/test-cpp-reset-logger.cpp new file mode 100644 index 0000000000..6320c26cd8 --- /dev/null +++ b/unit-tests/log/test-cpp-reset-logger.cpp @@ -0,0 +1,42 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2020 Intel Corporation. All Rights Reserved. + +//#cmake:add-file log-common.h +#include "log-common.h" + + +TEST_CASE("RESET C++ LOGGER", "[log]") { + size_t n_callbacks = 0; + auto callback = [&](rs2_log_severity severity, rs2::log_message const& msg) + { + ++n_callbacks; + TRACE(severity << ' ' << msg.filename() << '+' << msg.line_number() << ": " << msg.raw()); + }; + + rs2::log_to_callback(RS2_LOG_SEVERITY_INFO, callback); + REQUIRE(!n_callbacks); + rs2::reset_logger(); + log_all(); + REQUIRE(n_callbacks == 0); + + rs2::log_to_callback(RS2_LOG_SEVERITY_INFO, callback); + REQUIRE(!n_callbacks); + log_all(); + REQUIRE(n_callbacks == 3); + + rs2::reset_logger(); + log_all(); + REQUIRE(n_callbacks == 3); + + n_callbacks = 0; + rs2::log_to_callback(RS2_LOG_SEVERITY_DEBUG, callback); + REQUIRE(!n_callbacks); + log_all(); + REQUIRE(n_callbacks == 4); + + n_callbacks = 0; + rs2::reset_logger(); + log_all(); + REQUIRE(n_callbacks == 0); + +} diff --git a/wrappers/python/python.cpp b/wrappers/python/python.cpp index a0d6228a6b..ab8a2338a3 100644 --- a/wrappers/python/python.cpp +++ b/wrappers/python/python.cpp @@ -41,6 +41,7 @@ PYBIND11_MODULE(NAME, m) { m.def("log_to_console", &rs2::log_to_console, "min_severity"_a); m.def("log_to_file", &rs2::log_to_file, "min_severity"_a, "file_path"_a); + m.def("reset_logger", &rs2::reset_logger); // Access to log_message is only from a callback (see log_to_callback below) and so already // should have the GIL acquired