Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/librealsense2/rs.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ void rs2_log_to_callback( rs2_log_severity min_severity, rs2_log_callback_ptr ca

void rs2_reset_logger( rs2_error ** error);

void rs2_enable_rolling_files(size_t max_size, rs2_error ** error);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a /** ... */ description

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rs2_enable_rolling_log_file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done



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 );
const char * rs2_get_raw_log_message( rs2_log_message const * msg, rs2_error** error );
Expand Down
7 changes: 7 additions & 0 deletions include/librealsense2/rs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ namespace rs2
rs2_reset_logger(&e);
error::handle(e);
}

inline void enable_rolling_files(std::size_t max_size )
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 )
{
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it needs to throw

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why? if so, shouldn't all the other functions also throw?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

#endif // BUILD_EASYLOGGINGPP

35 changes: 35 additions & 0 deletions src/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not std::?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain -- have you tried without?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rollout checking happens when Easylogging++ flushes the log file, or, if you have added the flag el::LoggingFlags::StrictLogFileSizeCheck, at each log output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down
3 changes: 2 additions & 1 deletion src/realsense.def
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ EXPORTS
rs2_log_to_callback
rs2_log_to_callback_cpp
rs2_reset_logger

rs2_enable_rolling_files

rs2_get_log_message_line_number
rs2_get_log_message_filename
rs2_get_raw_log_message
Expand Down
6 changes: 6 additions & 0 deletions src/rs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,12 @@ void rs2_reset_logger( rs2_error** error) BEGIN_API_CALL
}
NOARGS_HANDLE_EXCEPTIONS_AND_RETURN()

void rs2_enable_rolling_files(std::size_t max_size, rs2_error** error) BEGIN_API_CALL
{
librealsense::enable_rolling_files(max_size);
}
HANDLE_EXCEPTIONS_AND_RETURN(, max_size)

// librealsense wrapper around a C function
class on_log_callback : public rs2_log_callback
{
Expand Down
1 change: 1 addition & 0 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ namespace librealsense
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();
void enable_rolling_files(std::size_t max_size);

#if BUILD_EASYLOGGINGPP

Expand Down
31 changes: 31 additions & 0 deletions unit-tests/log/test-cpp-enable-rolling.cpp
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add reset_logger() before to have it flush everything

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);

}
1 change: 1 addition & 0 deletions wrappers/python/python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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);
m.def("enable_rolling_files", &rs2::enable_rolling_files, "max_size"_a);

// Access to log_message is only from a callback (see log_to_callback below) and so already
// should have the GIL acquired
Expand Down