• 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 wide character logging.
13  */
14 
15 #include <iostream>
16 #include <boost/locale/generator.hpp>
17 #include <boost/date_time/posix_time/posix_time_types.hpp>
18 
19 #include <boost/log/common.hpp>
20 #include <boost/log/expressions.hpp>
21 #include <boost/log/utility/setup/file.hpp>
22 #include <boost/log/utility/setup/console.hpp>
23 #include <boost/log/utility/setup/common_attributes.hpp>
24 #include <boost/log/sources/logger.hpp>
25 #include <boost/log/support/date_time.hpp>
26 
27 namespace logging = boost::log;
28 namespace sinks = boost::log::sinks;
29 namespace attrs = boost::log::attributes;
30 namespace src = boost::log::sources;
31 namespace expr = boost::log::expressions;
32 namespace keywords = boost::log::keywords;
33 
34 //[ example_wide_char_severity_level_definition
35 enum severity_level
36 {
37     normal,
38     notification,
39     warning,
40     error,
41     critical
42 };
43 
44 template< typename CharT, typename TraitsT >
operator <<(std::basic_ostream<CharT,TraitsT> & strm,severity_level lvl)45 inline std::basic_ostream< CharT, TraitsT >& operator<< (
46     std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
47 {
48     static const char* const str[] =
49     {
50         "normal",
51         "notification",
52         "warning",
53         "error",
54         "critical"
55     };
56     if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))
57         strm << str[lvl];
58     else
59         strm << static_cast< int >(lvl);
60     return strm;
61 }
62 //]
63 
64 //[ example_wide_char_logging_initialization
65 // Declare attribute keywords
66 BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
67 BOOST_LOG_ATTRIBUTE_KEYWORD(timestamp, "TimeStamp", boost::posix_time::ptime)
68 
init_logging()69 void init_logging()
70 {
71     boost::shared_ptr< sinks::synchronous_sink< sinks::text_file_backend > > sink = logging::add_file_log
72     (
73         "sample.log",
74         keywords::format = expr::stream
75             << expr::format_date_time(timestamp, "%Y-%m-%d, %H:%M:%S.%f")
76             << " <" << severity.or_default(normal)
77             << "> " << expr::message
78     );
79 
80     // The sink will perform character code conversion as needed, according to the locale set with imbue()
81     std::locale loc = boost::locale::generator()("en_US.UTF-8");
82     sink->imbue(loc);
83 
84     // Let's add some commonly used attributes, like timestamp and record counter.
85     logging::add_common_attributes();
86 }
87 //]
88 
89 //[ example_wide_char_logging
test_narrow_char_logging()90 void test_narrow_char_logging()
91 {
92     // Narrow character logging still works
93     src::logger lg;
94     BOOST_LOG(lg) << "Hello, World! This is a narrow character message.";
95 }
96 
test_wide_char_logging()97 void test_wide_char_logging()
98 {
99     src::wlogger lg;
100     BOOST_LOG(lg) << L"Hello, World! This is a wide character message.";
101 
102     // National characters are also supported
103     const wchar_t national_chars[] = { 0x041f, 0x0440, 0x0438, 0x0432, 0x0435, 0x0442, L',', L' ', 0x043c, 0x0438, 0x0440, L'!', 0 };
104     BOOST_LOG(lg) << national_chars;
105 
106     // Now, let's try logging with severity
107     src::wseverity_logger< severity_level > slg;
108     BOOST_LOG_SEV(slg, normal) << L"A normal severity message, will not pass to the file";
109     BOOST_LOG_SEV(slg, warning) << L"A warning severity message, will pass to the file";
110     BOOST_LOG_SEV(slg, error) << L"An error severity message, will pass to the file";
111 }
112 //]
113 
main(int argc,char * argv[])114 int main(int argc, char* argv[])
115 {
116     init_logging();
117     test_narrow_char_logging();
118     test_wide_char_logging();
119 
120     return 0;
121 }
122