-
Couldn't load subscription status.
- Fork 4.9k
LibRS API to enable rolling log files #8207
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,13 @@ namespace rs2 | |
| rs2_reset_logger(&e); | ||
| error::handle(e); | ||
| } | ||
|
|
||
| inline void enable_rolling_files(std::size_t max_size ) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not clear what the unit of measure is for max_size -- is it bytes? KB? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| { | ||
| rs2_error* e = nullptr; | ||
| rs2_enable_rolling_files(max_size,&e); | ||
| error::handle(e); | ||
| } | ||
| /* | ||
| Interface to the log message data we expose. | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,11 @@ void librealsense::reset_logger() | |
| logger.reset_logger(); | ||
| } | ||
|
|
||
| void librealsense::enable_rolling_files(std::size_t max_size ) | ||
| { | ||
| logger.enable_rolling_files(max_size); | ||
| } | ||
|
|
||
| #else // BUILD_EASYLOGGINGPP | ||
|
|
||
| void librealsense::log_to_console(rs2_log_severity min_severity) | ||
|
|
@@ -52,5 +57,9 @@ void librealsense::log_to_callback(rs2_log_severity min_severity, log_callback_p | |
| void librealsense::reset_logger() | ||
| { | ||
| } | ||
|
|
||
| void librealsense::enable_rolling_files(std::size_t max_size ) | ||
| { | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it needs to throw There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why? if so, shouldn't all the other functions also throw? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| #endif // BUILD_EASYLOGGINGPP | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -251,10 +251,45 @@ namespace librealsense | |
| el::Loggers::reconfigureLogger(log_id, el::ConfigurationType::ToStandardOutput, "false"); | ||
| remove_callbacks(); | ||
|
|
||
| if(el::Loggers::hasFlag(el::LoggingFlag::StrictLogFileSizeCheck)) | ||
| { | ||
| el::Loggers::removeFlag(el::LoggingFlag::StrictLogFileSizeCheck); | ||
| el::Loggers::reconfigureLogger(log_id, el::ConfigurationType::MaxLogFileSize, "0"); | ||
| } | ||
|
|
||
| minimum_log_severity = RS2_LOG_SEVERITY_NONE; | ||
| minimum_console_severity = RS2_LOG_SEVERITY_NONE; | ||
| minimum_file_severity = RS2_LOG_SEVERITY_NONE; | ||
| } | ||
|
|
||
| //Renames current log file to "log_name.log.old", after this function | ||
| //log file will be truncated and re-initiated. | ||
| static void rolloutHandler(const char* filename, std::size_t size) | ||
| { | ||
| std::string file_str(filename); | ||
| std::string old_file = file_str + ".old"; | ||
|
|
||
| const char* old_filename = old_file.c_str(); | ||
| std::ifstream exists(old_filename); | ||
| if (exists.is_open()) { | ||
| exists.close(); | ||
| std::remove(old_filename); | ||
| } | ||
|
|
||
| rename(filename, old_filename); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| } | ||
|
|
||
| //Since log file will be truncated upon reaching max_size, MaxLogFileSize is configured to be half of the original max_size | ||
| //another file that contains previous half will be created in rolloutHandler. | ||
| //log file directory should have permissions of removing/renaming files. | ||
| //@param max_size max file size in bytes | ||
| void enable_rolling_files(std::size_t max_size) | ||
| { | ||
| std::string size = std::to_string( max_size/2 ); | ||
| el::Loggers::addFlag(el::LoggingFlag::StrictLogFileSizeCheck); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please explain -- have you tried without? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| el::Loggers::reconfigureLogger(log_id, el::ConfigurationType::MaxLogFileSize, size.c_str()); | ||
| el::Helpers::installPreRollOutCallback(rolloutHandler); | ||
| } | ||
| }; | ||
| #else //BUILD_EASYLOGGINGPP | ||
| struct log_message | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // 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" | ||
| #include<fstream> | ||
|
|
||
| TEST_CASE("ROLLING C++ LOGGER", "[log]") { | ||
|
|
||
| std::string log_filename = "rolling-log.log"; | ||
| rs2::log_to_file(RS2_LOG_SEVERITY_INFO, log_filename.c_str()); | ||
|
|
||
| int max_size = 1024; | ||
| rs2::enable_rolling_files(max_size); | ||
|
|
||
| for (int i = 0; i < 100; ++i) | ||
| log_all(); | ||
|
|
||
| std::ifstream log_file(log_filename.c_str(), std::ios::binary); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| log_file.seekg(0, std::ios::end); | ||
| int log_size = log_file.tellg(); | ||
|
|
||
| std::string old_filename = log_filename + ".old"; | ||
| std::ifstream old_file(old_filename.c_str(), std::ios::binary); | ||
| old_file.seekg(0, std::ios::end); | ||
| int old_size = old_file.tellg(); | ||
|
|
||
| int size = log_size + old_size; | ||
| REQUIRE(size <= max_size); | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a
/** ... */descriptionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rs2_enable_rolling_log_fileThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done