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 11.11.2007
11 *
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
15 */
16
17 // #define BOOST_LOG_USE_CHAR
18 // #define BOOST_ALL_DYN_LINK 1
19 // #define BOOST_LOG_DYN_LINK 1
20
21 #include <iostream>
22
23 #include <boost/log/common.hpp>
24 #include <boost/log/expressions.hpp>
25
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>
29
30 #include <boost/log/attributes/timer.hpp>
31 #include <boost/log/attributes/named_scope.hpp>
32
33 #include <boost/log/sources/logger.hpp>
34
35 #include <boost/log/support/date_time.hpp>
36
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;
43
44 using boost::shared_ptr;
45
46 // Here we define our application severity levels.
47 enum severity_level
48 {
49 normal,
50 notification,
51 warning,
52 error,
53 critical
54 };
55
56 // The formatting logic for the severity level
57 template< typename CharT, typename TraitsT >
operator <<(std::basic_ostream<CharT,TraitsT> & strm,severity_level lvl)58 inline std::basic_ostream< CharT, TraitsT >& operator<< (
59 std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
60 {
61 static const char* const str[] =
62 {
63 "normal",
64 "notification",
65 "warning",
66 "error",
67 "critical"
68 };
69 if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))
70 strm << str[lvl];
71 else
72 strm << static_cast< int >(lvl);
73 return strm;
74 }
75
main(int argc,char * argv[])76 int main(int argc, char* argv[])
77 {
78 // This is a simple tutorial/example of Boost.Log usage
79
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%");
83
84 // One can also use lambda expressions to setup filters and formatters
85 logging::add_file_log
86 (
87 "sample.log",
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
95 /*
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")
101 % expr::message
102 */
103 );
104
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());
108
109 BOOST_LOG_FUNCTION();
110
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.
113 src::logger lg;
114
115 // And output...
116 BOOST_LOG(lg) << "Hello, World!";
117
118 // Now, let's try logging with severity
119 src::severity_logger< severity_level > slg;
120
121 // Let's pretend we also want to profile our code, so add a special timer attribute.
122 slg.add_attribute("Uptime", attrs::timer());
123
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";
127
128 return 0;
129 }
130