• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *          Copyright Andrey Semashev 2007 - 2015.
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)
6  */
7 /*!
8  * \file   main.cpp
9  * \author Andrey Semashev
10  * \date   01.12.2012
11  *
12  * \brief  An example of using attribute keywords.
13  */
14 
15 // #define BOOST_LOG_USE_CHAR
16 // #define BOOST_ALL_DYN_LINK 1
17 // #define BOOST_LOG_DYN_LINK 1
18 
19 #include <iostream>
20 
21 #include <boost/log/common.hpp>
22 #include <boost/log/expressions.hpp>
23 
24 #include <boost/log/utility/setup/file.hpp>
25 #include <boost/log/utility/setup/console.hpp>
26 #include <boost/log/utility/setup/common_attributes.hpp>
27 
28 #include <boost/log/attributes/timer.hpp>
29 #include <boost/log/attributes/named_scope.hpp>
30 
31 #include <boost/log/sources/logger.hpp>
32 
33 #include <boost/log/support/date_time.hpp>
34 
35 namespace logging = boost::log;
36 namespace sinks = boost::log::sinks;
37 namespace attrs = boost::log::attributes;
38 namespace src = boost::log::sources;
39 namespace expr = boost::log::expressions;
40 namespace keywords = boost::log::keywords;
41 
42 using boost::shared_ptr;
43 
44 // Here we define our application severity levels.
45 enum severity_level
46 {
47     normal,
48     notification,
49     warning,
50     error,
51     critical
52 };
53 
54 // The formatting logic for the severity level
55 template< typename CharT, typename TraitsT >
operator <<(std::basic_ostream<CharT,TraitsT> & strm,severity_level lvl)56 inline std::basic_ostream< CharT, TraitsT >& operator<< (
57     std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
58 {
59     static const char* const str[] =
60     {
61         "normal",
62         "notification",
63         "warning",
64         "error",
65         "critical"
66     };
67     if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))
68         strm << str[lvl];
69     else
70         strm << static_cast< int >(lvl);
71     return strm;
72 }
73 
74 // Declare attribute keywords
75 BOOST_LOG_ATTRIBUTE_KEYWORD(_severity, "Severity", severity_level)
76 BOOST_LOG_ATTRIBUTE_KEYWORD(_timestamp, "TimeStamp", boost::posix_time::ptime)
77 BOOST_LOG_ATTRIBUTE_KEYWORD(_uptime, "Uptime", attrs::timer::value_type)
78 BOOST_LOG_ATTRIBUTE_KEYWORD(_scope, "Scope", attrs::named_scope::value_type)
79 
main(int argc,char * argv[])80 int main(int argc, char* argv[])
81 {
82     // This is a simple tutorial/example of Boost.Log usage
83 
84     // The first thing we have to do to get using the library is
85     // to set up the logging sinks - i.e. where the logs will be written to.
86     logging::add_console_log(std::clog, keywords::format = "%TimeStamp%: %_%");
87 
88     // One can also use lambda expressions to setup filters and formatters
89     logging::add_file_log
90     (
91         "sample.log",
92         keywords::filter = _severity >= warning,
93         keywords::format = expr::stream
94             << expr::format_date_time(_timestamp, "%Y-%m-%d, %H:%M:%S.%f")
95             << " [" << expr::format_date_time(_uptime, "%O:%M:%S")
96             << "] [" << expr::format_named_scope(_scope, keywords::format = "%n (%f:%l)")
97             << "] <" << _severity
98             << "> " << expr::message
99 /*
100         keywords::format = expr::format("%1% [%2%] [%3%] <%4%> %5%")
101             % expr::format_date_time(_timestamp, "%Y-%m-%d, %H:%M:%S.%f")
102             % expr::format_date_time(_uptime, "%O:%M:%S")
103             % expr::format_named_scope(_scope, keywords::format = "%n (%f:%l)")
104             % _severity
105             % expr::message
106 */
107     );
108 
109     // Also let's add some commonly used attributes, like timestamp and record counter.
110     logging::add_common_attributes();
111     logging::core::get()->add_thread_attribute("Scope", attrs::named_scope());
112 
113     BOOST_LOG_FUNCTION();
114 
115     // Now our logs will be written both to the console and to the file.
116     // Let's do a quick test and output something. We have to create a logger for this.
117     src::logger lg;
118 
119     // And output...
120     BOOST_LOG(lg) << "Hello, World!";
121 
122     // Now, let's try logging with severity
123     src::severity_logger< severity_level > slg;
124 
125     // Let's pretend we also want to profile our code, so add a special timer attribute.
126     slg.add_attribute("Uptime", attrs::timer());
127 
128     BOOST_LOG_SEV(slg, normal) << "A normal severity message, will not pass to the file";
129     BOOST_LOG_SEV(slg, warning) << "A warning severity message, will pass to the file";
130     BOOST_LOG_SEV(slg, error) << "An error severity message, will pass to the file";
131 
132     return 0;
133 }
134