Skip to content

Commit

Permalink
Add cmake option to disable console logfile (#348)
Browse files Browse the repository at this point in the history
The sdf::Console class logs some messages to a file
in `~/.sdformat/sdformat.log` by default.
This wraps the code in the constructor of the `sdf::Console`
class that opens this file in an `#ifndef` and adds
a cmake option `SDFORMAT_DISABLE_CONSOLE_LOGFILE`
to easily disable the use of this logfile.

Initial approach to #334.

Signed-off-by: Steve Peters <scpeters@openrobotics.org>
  • Loading branch information
scpeters authored and brawner committed Jan 9, 2021
1 parent b491e9b commit 08f9cca
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ set(sdf_import_target_name ${PROJECT_EXPORT_NAME}::${sdf_target})
set(sdf_target_output_filename "${sdf_target}-targets.cmake")


OPTION(SDFORMAT_DISABLE_CONSOLE_LOGFILE "Disable the sdformat console logfile" OFF)

if (USE_FULL_RPATH)
# use, i.e. don't skip the full RPATH for the build tree
set(CMAKE_SKIP_BUILD_RPATH FALSE)
Expand Down
1 change: 1 addition & 0 deletions cmake/sdf_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#cmakedefine BUILD_TYPE_RELEASE 1
#cmakedefine HAVE_URDFDOM 1
#cmakedefine USE_INTERNAL_URDF 1
#cmakedefine SDFORMAT_DISABLE_CONSOLE_LOGFILE 1

#define SDF_SHARE_PATH "${CMAKE_INSTALL_FULL_DATAROOTDIR}/"
#define SDF_VERSION_PATH "${CMAKE_INSTALL_FULL_DATAROOTDIR}/sdformat${SDF_MAJOR_VERSION}/${SDF_PKG_VERSION}"
5 changes: 4 additions & 1 deletion include/sdf/Console.hh
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ namespace sdf
const std::string &file,
unsigned int line, int color);

/// \brief Use this to output a message to a log file
/// \brief Use this to output a message to a log file at
/// `$HOME/.sdformat/sdformat.log`.
/// To disable this log file, define the following symbol when
/// compiling: SDFORMAT_DISABLE_CONSOLE_LOGFILE
/// \return Reference to output stream
public: ConsoleStream &Log(const std::string &lbl,
const std::string &file,
Expand Down
3 changes: 3 additions & 0 deletions src/Console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "sdf/Console.hh"
#include "sdf/Filesystem.hh"
#include "sdf/Types.hh"
#include "sdf/sdf_config.h"

using namespace sdf;

Expand All @@ -45,6 +46,7 @@ static Console::ConsoleStream g_NullStream(nullptr);
Console::Console()
: dataPtr(new ConsolePrivate)
{
#ifndef SDFORMAT_DISABLE_CONSOLE_LOGFILE
// Set up the file that we'll log to.
#ifndef _WIN32
const char *home = std::getenv("HOME");
Expand Down Expand Up @@ -72,6 +74,7 @@ Console::Console()
}
std::string logFile = sdf::filesystem::append(logDir, "sdformat.log");
this->dataPtr->logFileStream.open(logFile.c_str(), std::ios::out);
#endif
}

//////////////////////////////////////////////////
Expand Down

0 comments on commit 08f9cca

Please sign in to comment.