Skip to content

Commit

Permalink
updated logger to be thread safe.
Browse files Browse the repository at this point in the history
  • Loading branch information
finger563 committed Apr 9, 2018
1 parent 593f9ee commit b162fb6
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 44 deletions.
5 changes: 4 additions & 1 deletion src/rosmod_actor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ project(rosmod_actor)

## Check C++11 / C++0x
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++17" COMPILER_SUPPORTS_CXX17)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
if(COMPILER_SUPPORTS_CXX17)
set(CMAKE_CXX_FLAGS "-std=c++17")
elseif(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "-std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "-std=c++0x")
Expand Down
135 changes: 92 additions & 43 deletions src/rosmod_actor/include/rosmod_actor/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
#ifndef LOGGER_HPP
#define LOGGER_HPP

#include <mutex>
#include <thread>
#include <iostream>
#include <vector>
#include <functional>
#include <chrono>
#include <string>
#include <iostream>
#include <stdarg.h>
#include <fstream>
Expand Down Expand Up @@ -38,57 +45,79 @@ class Logger
* @param[in] is_periodic boolean indicating whether logging is periodic.
*/
void set_is_periodic(bool is_periodic) {
is_periodic_ = is_periodic;
{
std::lock_guard<std::recursive_mutex> lk(settings_mutex);
is_periodic_ = is_periodic;
}
}

/**
* @brief Set maximum log unit in bytes during periodic logging.
* @param[in] max_log_unit maximum periodic logging unit in bytes.
*/
void set_max_log_unit(int max_log_unit) {
max_log_unit_ = max_log_unit;
{
std::lock_guard<std::recursive_mutex> lk(settings_mutex);
max_log_unit_ = max_log_unit;
}
}

/**
* @brief Log to file instead of stdout.
* @param[in] logs_to_file boolean requiring logging on file.
*/
void set_logs_to_file(bool logs_to_file) {
logs_to_file_ = logs_to_file;
{
std::lock_guard<std::recursive_mutex> lk(settings_mutex);
logs_to_file_ = logs_to_file;
}
}

/**
* @brief Enable this logger
*/
void enable_logging() {
logging_enabled_ = true;
{
std::lock_guard<std::recursive_mutex> lk(settings_mutex);
logging_enabled_ = true;
}
}

/**
* @brief Disable this logger
*/
void disable_logging() {
logging_enabled_ = false;
{
std::lock_guard<std::recursive_mutex> lk(settings_mutex);
logging_enabled_ = false;
}
}

/**
* @brief Writes out the remainder of the logs and closes logfile.
*/
~Logger() {
write();
log_stream_.close();
{
std::lock_guard<std::recursive_mutex> lk(io_mutex);
log_stream_.close();
}
}

/**
* @brief Create log file
* @param[in] log_path path to log file.
*/
bool create_file(std::string log_path) {
if (logging_enabled_) {
log_path_ = log_path;
log_stream_.open(log_path_, std::ios::out | std::ios::app);
logs_to_file_ = true;
return true;
{
std::lock_guard<std::recursive_mutex> lk0(settings_mutex);
std::lock_guard<std::recursive_mutex> lk1(io_mutex);
if (logging_enabled_) {
log_path_ = log_path;
log_stream_.open(log_path_, std::ios::out | std::ios::app);
logs_to_file_ = true;
return true;
}
}
return false;
}
Expand All @@ -97,27 +126,34 @@ class Logger
* @brief Write logged bytes to file
*/
bool write() {
if (logging_enabled_) {
if (logs_to_file_) {
log_stream_ << log_content_;
log_stream_.flush();
{
std::lock_guard<std::recursive_mutex> lk0(settings_mutex);
std::lock_guard<std::recursive_mutex> lk1(io_mutex);
if (logging_enabled_) {
if (logs_to_file_) {
log_stream_ << log_content_;
log_stream_.flush();
}
else
printf("%s", log_content_.c_str());
return true;
}
else
printf("%s", log_content_.c_str());
return true;
return false;
}
else
return false;
}

/**
* @brief Flush out to file.
*/
bool flush() {
if (is_periodic_ && size() > max_log_unit_) {
write();
log_content_ = "";
return true;
{
std::lock_guard<std::recursive_mutex> lk(settings_mutex);
if (is_periodic_ && size() > max_log_unit_) {
write();
log_content_ = "";
return true;
}
}
return false;
}
Expand All @@ -133,16 +169,20 @@ class Logger
*
*/
bool log(std::string log_level, const char * format, ...) {
if (logging_enabled_) {
va_list args;
va_start (args, format);
char log_entry[1024];
vsprintf (log_entry, format, args);
std::string log_entry_string(log_entry);
va_end (args);
log_content_ += "ROSMOD::" + log_level + "::" + clock() +
"::" + log_entry_string + "\n";
flush();
{
std::lock_guard<std::recursive_mutex> lk0(settings_mutex);
std::lock_guard<std::recursive_mutex> lk1(io_mutex);
if (logging_enabled_) {
va_list args;
va_start (args, format);
char log_entry[1024];
vsprintf (log_entry, format, args);
std::string log_entry_string(log_entry);
va_end (args);
log_content_ += "ROSMOD::" + log_level + "::" + clock() +
"::" + log_entry_string + "\n";
flush();
}
}
}

Expand All @@ -151,23 +191,30 @@ class Logger
* @param[in] format varargs input to logger.
*/
bool raw_log(const char * format, ...) {
if (logging_enabled_) {
va_list args;
va_start (args, format);
char log_entry[1024];
vsprintf (log_entry, format, args);
std::string log_entry_string(log_entry);
va_end (args);
log_content_ += log_entry_string + "\n";
flush();
{
std::lock_guard<std::recursive_mutex> lk0(settings_mutex);
std::lock_guard<std::recursive_mutex> lk1(io_mutex);
if (logging_enabled_) {
va_list args;
va_start (args, format);
char log_entry[1024];
vsprintf (log_entry, format, args);
std::string log_entry_string(log_entry);
va_end (args);
log_content_ += log_entry_string + "\n";
flush();
}
}
}

/**
* @brief Return the current size of the log in bytes.
*/
int size() {
return log_content_.size();
{
std::lock_guard<std::recursive_mutex> lk(io_mutex);
return log_content_.size();
}
}

/**
Expand All @@ -180,6 +227,8 @@ class Logger
}

private:
std::recursive_mutex io_mutex; /*!< Mutex for writing to the log */
std::recursive_mutex settings_mutex; /*!< Mutex for controlling the settings */
std::ofstream log_stream_; /*!< Output log stream */
std::string log_content_; /*!< Log contents */
std::string log_path_; /*!< Log file path */
Expand Down

0 comments on commit b162fb6

Please sign in to comment.