Skip to content

Commit

Permalink
logger: Support logging using log4cpp framework
Browse files Browse the repository at this point in the history
Supports logging using the log4cpp framework, which replaces logging
both to file and via printf. The log4cpp option defaults off. This
fix also ensures that application code is informed of the build option.
  • Loading branch information
snrkiwi committed Apr 4, 2014
1 parent c200326 commit ab04b27
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 4 deletions.
36 changes: 36 additions & 0 deletions config/FindLog4cpp.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
################################################################################
#
# CMake script for finding Log4cpp.
# The default CMake search process is used to locate files.
#
# This script creates the following variables:
# LOG4CPP_FOUND: Boolean that indicates if the package was found
# LOG4CPP_INCLUDE_DIRS: Paths to the necessary header files
# LOG4CPP_LIBRARIES: Package libraries
# LOG4CPP_LIBRARY_DIRS: Path to package libraries
#
################################################################################

include(FindPackageHandleStandardArgs)

# Find headers and libraries
find_path(LOG4CPP_INCLUDE_DIR NAMES log4cpp/Category.hh)
find_library(LOG4CPP_LIBRARY NAMES log4cpp)

# Set LOG4CPP_FOUND honoring the QUIET and REQUIRED arguments
find_package_handle_standard_args(LOG4CPP DEFAULT_MSG LOG4CPP_LIBRARY LOG4CPP_INCLUDE_DIR)

# Output variables
if(LOG4CPP_FOUND)
# Include dirs
set(LOG4CPP_INCLUDE_DIRS ${LOG4CPP_INCLUDE_DIR})

# Libraries
set(LOG4CPP_LIBRARIES ${LOG4CPP_LIBRARY})

# Link dirs
get_filename_component(LOG4CPP_LIBRARY_DIRS ${LOG4CPP_LIBRARY} PATH)
endif()

# Advanced options for not cluttering the cmake UIs
mark_as_advanced(LOG4CPP_INCLUDE_DIR LOG4CPP_LIBRARY)
15 changes: 14 additions & 1 deletion rtt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,26 @@ OPTION(OROBLD_DISABLE_LOGGING "Disable Logging Infrastructure" OFF)
CMAKE_DEPENDENT_OPTION(OROSEM_PRINTF_LOGGING "Logger uses printf()/fprintf() instead of std::iostream." OFF "NOT OROBLD_DISABLE_LOGGING" OFF)
CMAKE_DEPENDENT_OPTION(OROSEM_FILE_LOGGING "Logger logs to orocos.log file." ON "NOT OROBLD_DISABLE_LOGGING" OFF)
CMAKE_DEPENDENT_OPTION(OROSEM_REMOTE_LOGGING "Logger allows remote log retrieval." ON "NOT OROBLD_DISABLE_LOGGING" OFF)
CMAKE_DEPENDENT_OPTION(OROSEM_LOG4CPP_LOGGING "Logger logs to log4cpp infrastructure (replaces file and printf logging) ." OFF "NOT OROBLD_DISABLE_LOGGING;OROSEM_FILE_LOGGING" OFF)

IF (OROSEM_REMOTE_LOGGING AND NOT OROBLD_DISABLE_LOGGING)
SET(ORONUM_LOGGING_BUFSIZE 1000 CACHE STRING "Maximum number of lines kept for remote log retrieval.")
ELSE (OROSEM_REMOTE_LOGGING AND NOT OROBLD_DISABLE_LOGGING)
SET(ORONUM_LOGGING_BUFSIZE CACHE INTERNAL "")
ENDIF (OROSEM_REMOTE_LOGGING AND NOT OROBLD_DISABLE_LOGGING)

IF (OROSEM_LOG4CPP_LOGGING)
FIND_PACKAGE(Log4cpp REQUIRED)

INCLUDE_DIRECTORIES(${LOG4CPP_INCLUDE_DIRS})
list(APPEND OROCOS-RTT_INCLUDE_DIRS ${LOG4CPP_INCLUDE_DIRS} )
# add to list of libraries in pkgconfig file
LIST(APPEND OROCOS-RTT_USER_LINK_LIBS ${LOG4CPP_LIBRARIES})
# add to list of dependent link libraries
LIST(APPEND EXTRA_LIBRARIES ${LOG4CPP_LIBRARIES})

ENDIF (OROSEM_LOG4CPP_LOGGING)

### Execution Engine
OPTION(OROPKG_EXECUTION_ADVANCED "Advanced Execution Engine configuration" OFF)
CMAKE_DEPENDENT_OPTION(OROPKG_EXECUTION_ENGINE_EVENTS "Enable Event Processing." ON "OROPKG_EXECUTION_ADVANCED" ON)
Expand Down Expand Up @@ -205,7 +218,7 @@ ENDIF ( BUILD_STATIC )
ENDIF ()


target_link_libraries(orocos-rtt-${OROCOS_TARGET}_dynamic ${OROCOS-RTT_LIBRARIES})
target_link_libraries(orocos-rtt-${OROCOS_TARGET}_dynamic ${OROCOS-RTT_LIBRARIES} ${EXTRA_LIBRARIES})
IF (BUILD_ENABLE_COVERAGE)
target_link_libraries(orocos-rtt-${OROCOS_TARGET}_dynamic gcov)
ENDIF (BUILD_ENABLE_COVERAGE)
Expand Down
46 changes: 43 additions & 3 deletions rtt/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
# include <ostream>
# ifdef OROSEM_FILE_LOGGING
# include <fstream>
# ifdef OROSEM_LOG4CPP_LOGGING
# include <log4cpp/Category.hh>
# endif
# endif
# ifdef OROSEM_REMOTE_LOGGING
# include <sstream>
Expand Down Expand Up @@ -86,6 +89,31 @@ namespace RTT

#ifndef OROBLD_DISABLE_LOGGING

#ifdef OROSEM_LOG4CPP_LOGGING

const std::string Logger::log4cppCategoryName = "org.orocos.rtt";

log4cpp::Priority::Value level2Priority(const int logLevel)
{
log4cpp::Priority::Value value = log4cpp::Priority::NOTSET;
switch (logLevel)
{
case Never: value = log4cpp::Priority::NOTSET; break;
case Fatal: value = log4cpp::Priority::FATAL; break;
case Critical: value = log4cpp::Priority::CRIT; break;
case Error: value = log4cpp::Priority::ERROR; break;
case Warning: value = log4cpp::Priority::WARN; break;
case Info: value = log4cpp::Priority::INFO; break;
case Debug: value = log4cpp::Priority::DEBUG; break;
// best we can do!?
case RealTime: value = log4cpp::Priority::DEBUG; break;
default: value = log4cpp::Priority::NOTSET; break;
}
return value;
}

#endif

Logger& Logger::log() {
return *Instance();
}
Expand All @@ -107,8 +135,12 @@ namespace RTT
#ifdef OROSEM_REMOTE_LOGGING
messagecnt(0),
#endif
#if defined(OROSEM_FILE_LOGGING) && !defined(OROSEM_PRINTF_LOGGING)
#if defined(OROSEM_FILE_LOGGING)
#if defined(OROSEM_LOG4CPP_LOGGING)
category(log4cpp::Category::getInstance(RTT::Logger::log4cppCategoryName)),
#elif !defined(OROSEM_PRINTF_LOGGING)
logfile(logfile_name ? logfile_name : "orocos.log"),
#endif
#endif
inloglevel(Info),
outloglevel(Warning),
Expand All @@ -117,7 +149,7 @@ namespace RTT
mlogStdOut(true), mlogFile(true),
moduleptr("Logger")
{
#if defined(OROSEM_FILE_LOGGING) && defined(OROSEM_PRINTF_LOGGING)
#if defined(OROSEM_FILE_LOGGING) && !defined(OROSEM_LOG4CPP_LOGGING) && defined(OROSEM_PRINTF_LOGGING)
logfile = fopen(logfile_name ? logfile_name : "orocos.log","w");
#endif
}
Expand Down Expand Up @@ -163,7 +195,9 @@ namespace RTT

if ( maylogFile() ) {
#ifdef OROSEM_FILE_LOGGING
#ifndef OROSEM_PRINTF_LOGGING
#if defined(OROSEM_LOG4CPP_LOGGING)
category.log(level2Priority(inloglevel), fileline.str());
#elif !defined(OROSEM_PRINTF_LOGGING)
logfile << res << fileline.str() << pf;
#else
fprintf( logfile, "%s%s\n", res.c_str(), fileline.str().c_str() );
Expand Down Expand Up @@ -197,6 +231,9 @@ namespace RTT
unsigned int messagecnt;
#endif
#if defined(OROSEM_FILE_LOGGING)
#if defined(OROSEM_LOG4CPP_LOGGING)
log4cpp::Category& category;
#endif
# ifndef OROSEM_PRINTF_LOGGING
std::ofstream logfile;
# else
Expand Down Expand Up @@ -575,6 +612,9 @@ namespace RTT

void Logger::setLogLevel( LogLevel ll ) {
d->outloglevel = ll;
#if defined(OROSEM_LOG4CPP_LOGGING)
d->category.setPriority(level2Priority(ll));
#endif
}

Logger::LogLevel Logger::getLogLevel() const {
Expand Down
7 changes: 7 additions & 0 deletions rtt/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,13 @@ namespace RTT
*/
void lognl();

#ifdef OROSEM_LOG4CPP_LOGGING
/**
* Name of the log4cpp category that RTT logs to
*/
static const std::string log4cppCategoryName;
#endif

private:
/**
* Returns true if the next message will be logged.
Expand Down
1 change: 1 addition & 0 deletions rtt/os/targets/target-config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#cmakedefine OROSEM_PRINTF_LOGGING
#cmakedefine OROSEM_FILE_LOGGING
#cmakedefine OROSEM_REMOTE_LOGGING
#cmakedefine OROSEM_LOG4CPP_LOGGING
#define ORONUM_LOGGING_BUFSIZE @ORONUM_LOGGING_BUFSIZE@

#define OROPKG_OS
Expand Down

0 comments on commit ab04b27

Please sign in to comment.