Create a gist now

Instantly share code, notes, and snippets.

@xiongjia /0_main.cxx
Last active Mar 14, 2017

What would you like to do?
A simple sample of Boost Log
/**
* A simple sample of Boost Log
*/
#pragma warning(push)
#pragma warning(disable:4819)
# include <boost/shared_ptr.hpp>
# include <boost/date_time/posix_time/posix_time_types.hpp>
# include <boost/log/trivial.hpp>
# include <boost/log/core.hpp>
# include <boost/log/expressions.hpp>
# include <boost/log/sources/logger.hpp>
# include <boost/log/utility/setup/file.hpp>
# include <boost/log/utility/setup/console.hpp>
# include <boost/log/utility/setup/common_attributes.hpp>
# include <boost/log/support/date_time.hpp>
# include <boost/log/sinks/sync_frontend.hpp>
# include <boost/log/sinks/text_file_backend.hpp>
# include <boost/log/sinks/text_ostream_backend.hpp>
# include <boost/log/attributes/named_scope.hpp>
#pragma warning(pop)
#pragma warning(disable:4503)
static void init_log(void)
{
/* init boost log
* 1. Add common attributes
* 2. set log filter to trace
*/
boost::log::add_common_attributes();
boost::log::core::get()->add_global_attribute("Scope",
boost::log::attributes::named_scope());
boost::log::core::get()->set_filter(
boost::log::trivial::severity >= boost::log::trivial::trace
);
/* log formatter:
* [TimeStamp] [ThreadId] [Severity Level] [Scope] Log message
*/
auto fmtTimeStamp = boost::log::expressions::
format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S.%f");
auto fmtThreadId = boost::log::expressions::
attr<boost::log::attributes::current_thread_id::value_type>("ThreadID");
auto fmtSeverity = boost::log::expressions::
attr<boost::log::trivial::severity_level>("Severity");
auto fmtScope = boost::log::expressions::format_named_scope("Scope",
boost::log::keywords::format = "%n(%f:%l)",
boost::log::keywords::iteration = boost::log::expressions::reverse,
boost::log::keywords::depth = 2);
boost::log::formatter logFmt =
boost::log::expressions::format("[%1%] (%2%) [%3%] [%4%] %5%")
% fmtTimeStamp % fmtThreadId % fmtSeverity % fmtScope
% boost::log::expressions::smessage;
/* console sink */
auto consoleSink = boost::log::add_console_log(std::clog);
consoleSink->set_formatter(logFmt);
/* fs sink */
auto fsSink = boost::log::add_file_log(
boost::log::keywords::file_name = "test_%Y-%m-%d_%H-%M-%S.%N.log",
boost::log::keywords::rotation_size = 10 * 1024 * 1024,
boost::log::keywords::min_free_space = 30 * 1024 * 1024,
boost::log::keywords::open_mode = std::ios_base::app);
fsSink->set_formatter(logFmt);
fsSink->locked_backend()->auto_flush(true);
}
static void Test(void)
{
BOOST_LOG_FUNCTION();
BOOST_LOG_TRIVIAL(info) << "Info Log in Test()";
}
int main(int argc, char **argv)
{
init_log();
BOOST_LOG_NAMED_SCOPE("main");
BOOST_LOG_TRIVIAL(info) << "Info Log";
Test();
BOOST_LOG_TRIVIAL(info) << "app exiting";
return 0;
}
# CMake build script
cmake_minimum_required(VERSION 2.8)
# project name & version
project(ProgLogTest)
# common settings (Boost libraries)
if (MSVC)
# Enable the static libraries on Windows
foreach (flag_var
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
# update to the static version of the run time library
string(REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endforeach()
set(CMAKE_C_STANDARD_LIBRARIES "")
set(CMAKE_CXX_STANDARD_LIBRARIES "")
endif()
# enable boost static flag
unset(Boost_LIBRARIES)
set(Boost_USE_STATIC ON)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME ON)
# boost components
find_package(Boost REQUIRED COMPONENTS
log log_setup system filesystem date_time thread chrono)
include_directories("${PROJECT_SOURCE_DIR}"
"${Boost_INCLUDE_DIRS}")
set(proglog_dbg_libs
"${Boost_LOG_LIBRARY_DEBUG}"
"${Boost_LOG_SETUP_LIBRARY_DEBUG}"
"${Boost_THREAD_LIBRARY_DEBUG}"
"${Boost_DATE_TIME_LIBRARY_DEBUG}"
"${Boost_CHRONO_LIBRARY_DEBUG}"
"${Boost_FILESYSTEM_LIBRARY_DEBUG}"
"${Boost_SYSTEM_LIBRARY_DEBUG}")
set(proglog_opt_libs
"${Boost_LOG_LIBRARY_RELEASE}"
"${Boost_LOG_SETUP_LIBRARY_RELEASE}"
"${Boost_THREAD_LIBRARY_RELEASE}"
"${Boost_DATE_TIME_LIBRARY_RELEASE}"
"${Boost_CHRONO_LIBRARY_RELEASE}"
"${Boost_FILESYSTEM_LIBRARY_RELEASE}"
"${Boost_SYSTEM_LIBRARY_RELEASE}")
add_executable(prog-log
"${PROJECT_SOURCE_DIR}/0_main.cxx")
target_link_libraries(prog-log
debug "${proglog_dbg_libs}"
optimized "${proglog_opt_libs}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment