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 #include <string>
9 #include <fstream>
10 #include <iostream>
11 #include <boost/smart_ptr/shared_ptr.hpp>
12 #include <boost/log/core.hpp>
13 #include <boost/log/sinks/sync_frontend.hpp>
14 #include <boost/log/sinks/text_file_backend.hpp>
15 #include <boost/log/sources/severity_channel_logger.hpp>
16 #include <boost/log/sources/record_ostream.hpp>
17
18 namespace logging = boost::log;
19 namespace src = boost::log::sources;
20 namespace sinks = boost::log::sinks;
21 namespace keywords = boost::log::keywords;
22
23 //[ example_sinks_file
init_logging()24 void init_logging()
25 {
26 boost::shared_ptr< logging::core > core = logging::core::get();
27
28 boost::shared_ptr< sinks::text_file_backend > backend =
29 boost::make_shared< sinks::text_file_backend >(
30 keywords::file_name = "file.log", /*< active file name pattern >*/
31 keywords::target_file_name = "file_%5N.log", /*< target file name pattern >*/
32 keywords::rotation_size = 5 * 1024 * 1024, /*< rotate the file upon reaching 5 MiB size... >*/
33 keywords::time_based_rotation = sinks::file::rotation_at_time_point(12, 0, 0) /*< ...or every day, at noon, whichever comes first >*/
34 );
35
36 // Wrap it into the frontend and register in the core.
37 // The backend requires synchronization in the frontend.
38 typedef sinks::synchronous_sink< sinks::text_file_backend > sink_t;
39 boost::shared_ptr< sink_t > sink(new sink_t(backend));
40
41 core->add_sink(sink);
42 }
43 //]
44
main(int,char * [])45 int main(int, char*[])
46 {
47 init_logging();
48
49 src::severity_channel_logger< > lg(keywords::channel = "net");
50 BOOST_LOG_SEV(lg, 3) << "Hello world!";
51
52 return 0;
53 }
54