Tag Archives: boost

Using Boost Logging

Simple std::cout works great but there are few problems:

  1. Logging output only goes to console
  2. If program runs for a long time via cron / scheduler then output has to be redirected to a file
  3. Doing so might cause a large log with filesystem lock for which the program has to be stopped before it can be cleaned

Here’s how to use the Boost Logging library:

#include 

#include "boost/log/trivial.hpp"
#include "boost/log/utility/setup.hpp"

using namespace std;

int main() {
  // Output message to console
  boost::log::add_console_log(
    cout, 
    boost::log::keywords::format = "[%TimeStamp%]: %Message%",
    boost::log::keywords::auto_flush = true
  );

  // Output message to file, rotates when file reached 1mb or at midnight every day. Each log file
  // is capped at 1mb and total is 20mb
  boost::log::add_file_log (
    boost::log::keywords::file_name = "MyApp_%3N.log",
    boost::log::keywords::rotation_size = 1 * 1024 * 1024,
    boost::log::keywords::max_size = 20 * 1024 * 1024,
    boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0),
    boost::log::keywords::format = "[%TimeStamp%]: %Message%",
    boost::log::keywords::auto_flush = true
  );

  boost::log::add_common_attributes();

  // Only output message with INFO or higher severity
  boost::log::core::get()->set_filter(
    boost::log::trivial::severity >= boost::log::trivial::info
  );

  // Output some simple log message
  BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
  BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
  BOOST_LOG_TRIVIAL(info) << "An informational severity message";
  BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
  BOOST_LOG_TRIVIAL(error) << "An error severity message";
  BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";
}

See Also

Using Boost On Visual Studio Project

To use Boost libraries, set following configurations on Visual Studio project properties:

  1. Check if boost is already installed (eg: C:\Program Files (x86)\boost\boost_1_51_0) if not, go through the installation process on http://www.boost.org/doc/libs/1_52_0/more/getting_started/windows.html
  2. Ensure BOOST_HOME environment variable exist and points to the installation path above
  3. On project properties, under C/C++ -> General, add $(BOOST_HOME) to Additional Include Directories
  4. Under Linker -> General, add $(BOOST_HOME)\lib to Additional Library Directories
  5. Ensure C/C++ -> Code Generations -> Runtime Library is set to /MD or /MDd so linker can find boost lib files

Some boost component such as boost log need to be built first before it can be linked:

  1. Unarchive the downloaded compressed file
  2. Open command prompt in administrator mode and cd into the unarchived directory. Run bootstrap.bat to build b2
  3. Run b2 install --prefix=PREFIX --toolset=msvc-10.0 --build-type=complete stage. This will take about 30 minutes, be patient. Note: “PREFIX” is the directory you want to install boost (eg: lib). “toolset=msvc-10.0″ means compile boost by using visual studio 2010.