2 * Copyright Andrey Semashev 2007 - 2014.
3 * Distributed under the Boost Software License, Version 1.0.
4 * (See accompanying file LICENSE_1_0.txt or copy at
5 * http://www.boost.org/LICENSE_1_0.txt)
9 * \author Andrey Semashev
12 * \brief An example of basic library usage. See the library tutorial for expanded
13 * comments on this code. It may also be worthwhile reading the Wiki requirements page:
14 * http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?Boost.Logging
17 // #define BOOST_LOG_USE_CHAR
18 // #define BOOST_ALL_DYN_LINK 1
19 // #define BOOST_LOG_DYN_LINK 1
23 #include <boost/log/common.hpp>
24 #include <boost/log/expressions.hpp>
26 #include <boost/log/utility/setup/file.hpp>
27 #include <boost/log/utility/setup/console.hpp>
28 #include <boost/log/utility/setup/common_attributes.hpp>
30 #include <boost/log/attributes/timer.hpp>
31 #include <boost/log/attributes/named_scope.hpp>
33 #include <boost/log/sources/logger.hpp>
35 #include <boost/log/support/date_time.hpp>
37 namespace logging = boost::log;
38 namespace sinks = boost::log::sinks;
39 namespace attrs = boost::log::attributes;
40 namespace src = boost::log::sources;
41 namespace expr = boost::log::expressions;
42 namespace keywords = boost::log::keywords;
44 using boost::shared_ptr;
46 // Here we define our application severity levels.
56 // The formatting logic for the severity level
57 template< typename CharT, typename TraitsT >
58 inline std::basic_ostream< CharT, TraitsT >& operator<< (
59 std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
61 static const char* const str[] =
69 if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))
72 strm << static_cast< int >(lvl);
76 int main(int argc, char* argv[])
78 // This is a simple tutorial/example of Boost.Log usage
80 // The first thing we have to do to get using the library is
81 // to set up the logging sinks - i.e. where the logs will be written to.
82 logging::add_console_log(std::clog, keywords::format = "%TimeStamp%: %Message%");
84 // One can also use lambda expressions to setup filters and formatters
88 keywords::filter = expr::attr< severity_level >("Severity") >= warning,
89 keywords::format = expr::stream
90 << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d, %H:%M:%S.%f")
91 << " [" << expr::format_date_time< attrs::timer::value_type >("Uptime", "%O:%M:%S")
92 << "] [" << expr::format_named_scope("Scope", keywords::format = "%n (%f:%l)")
93 << "] <" << expr::attr< severity_level >("Severity")
94 << "> " << expr::message
96 keywords::format = expr::format("%1% [%2%] [%3%] <%4%> %5%")
97 % expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d, %H:%M:%S.%f")
98 % expr::format_date_time< attrs::timer::value_type >("Uptime", "%O:%M:%S")
99 % expr::format_named_scope("Scope", keywords::format = "%n (%f:%l)")
100 % expr::attr< severity_level >("Severity")
105 // Also let's add some commonly used attributes, like timestamp and record counter.
106 logging::add_common_attributes();
107 logging::core::get()->add_thread_attribute("Scope", attrs::named_scope());
109 BOOST_LOG_FUNCTION();
111 // Now our logs will be written both to the console and to the file.
112 // Let's do a quick test and output something. We have to create a logger for this.
116 BOOST_LOG(lg) << "Hello, World!";
118 // Now, let's try logging with severity
119 src::severity_logger< severity_level > slg;
121 // Let's pretend we also want to profile our code, so add a special timer attribute.
122 slg.add_attribute("Uptime", attrs::timer());
124 BOOST_LOG_SEV(slg, normal) << "A normal severity message, will not pass to the file";
125 BOOST_LOG_SEV(slg, warning) << "A warning severity message, will pass to the file";
126 BOOST_LOG_SEV(slg, error) << "An error severity message, will pass to the file";