Monday 2 December 2013

Use boost log step1

I get used to log library from CppCMS, it's simple. Boost releases the official log library since 1.54 version. After reading some doc, I decide to change to boost::log because the following reasons:
1. There are about 2,000 devices which connect to my TCP server, the TCP server saves all detail information. For example, when the device logs into the server, when it quits, what message is sent by device or server. And the amount of devices will increase in the future. Thus cause a few very large log files are created.

2. We will analyze the log file for profiling the problems of TCP server, also find out how our customers use their devices. When a problem occurs, administrator should receive waning email. And the product people could see the user behavior analysis report on web page to help them improve our product.

Obviously, boost new log library is designed to do the above jobs. That's what I expected.

Here is a example, most of code from boost log document:
http://www.boost.org/doc/libs/1_55_0/libs/log/doc/html/log/tutorial.html#log.tutorial.trivial

I am using Ubuntu 13.10 installed clang 3.4, also I built boost 1.55 libraries with clang & c++11 option.
Refer to my build command below:

./bootstrap.sh --with-libraries=system,filesystem,log,thread --with-toolset=clang
./b2 toolset=clang cxxflags="-std=c++11"

Now, create a main.cc file, write some code like this:
#include <iostream>
#include <boost/log/trivial.hpp>

using namespace std;

int main () {
  cout << "hello, world" << endl;
  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";
}

Compile code and link it to binary:
c++ -g -std=c++11 -Wall -DBOOST_LOG_DYN_LINK -o ./main.o -c ./main.cc
c++ -g -std=c++11 ./main.o -o main -rdynamic -lpthread -lboost_log -lboost_system -lboost_thread -lboost_filesystem

Now, execute ./main binary, it outputs:
$ ./main
hello, world
[2013-12-03 14:35:14.309093] [0x000007f362c17d74] [trace]   A trace severity message
[2013-12-03 14:35:14.309208] [0x000007f362c17d74] [debug]   A debug severity message
[2013-12-03 14:35:14.309224] [0x000007f362c17d74] [info]    An informational severity message
[2013-12-03 14:35:14.309237] [0x000007f362c17d74] [warning] A warning severity message
[2013-12-03 14:35:14.309250] [0x000007f362c17d74] [error]   An error severity message
[2013-12-03 14:35:14.309262] [0x000007f362c17d74] [fatal]   A fatal severity message


By default, the BOOST_LOG_TRIVIAL outputs all information to console. Some people ran into the problem
 undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'

Add -DBOOST_LOG_DYN_LINK to solve this.


No comments:

Followers

Contributors