• 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   26.04.2008
11  *
12  * \brief  An example of initializing the library from a settings file.
13  *         See the library tutorial for expanded comments on this code.
14  *         It may also be worthwhile reading the Wiki requirements page:
15  *         http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?Boost.Logging
16  */
17 
18 // #define BOOST_ALL_DYN_LINK 1
19 
20 #include <exception>
21 #include <iostream>
22 #include <fstream>
23 
24 #include <boost/log/trivial.hpp>
25 #include <boost/log/common.hpp>
26 #include <boost/log/attributes.hpp>
27 #include <boost/log/utility/setup/from_stream.hpp>
28 
29 namespace logging = boost::log;
30 namespace attrs = boost::log::attributes;
31 
try_logging()32 void try_logging()
33 {
34     BOOST_LOG_TRIVIAL(trace) << "This is a trace severity record";
35     BOOST_LOG_TRIVIAL(debug) << "This is a debug severity record";
36     BOOST_LOG_TRIVIAL(info) << "This is an info severity record";
37     BOOST_LOG_TRIVIAL(warning) << "This is a warning severity record";
38     BOOST_LOG_TRIVIAL(error) << "This is an error severity record";
39     BOOST_LOG_TRIVIAL(fatal) << "This is a fatal severity record";
40 }
41 
main(int argc,char * argv[])42 int main(int argc, char* argv[])
43 {
44     try
45     {
46         // Open the file
47         std::ifstream settings("settings.txt");
48         if (!settings.is_open())
49         {
50             std::cout << "Could not open settings.txt file" << std::endl;
51             return 1;
52         }
53 
54         // Read the settings and initialize logging library
55         logging::init_from_stream(settings);
56 
57         // Add some attributes
58         logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
59 
60         // Try logging
61         try_logging();
62 
63         // Now enable tagging and try again
64         BOOST_LOG_SCOPED_THREAD_TAG("Tag", "TAGGED");
65         try_logging();
66 
67         return 0;
68     }
69     catch (std::exception& e)
70     {
71         std::cout << "FAILURE: " << e.what() << std::endl;
72         return 1;
73     }
74 }
75