OpcUaCanOpen
CANopen OPC-UA server
BoostRotatingFileLog.cpp
Go to the documentation of this file.
1 /* © Copyright CERN, 2015. All rights not expressly granted are reserved.
2  * BoostRotatingFileLog.cpp
3  *
4  * Created on: Aug 18, 2015
5  * Author: Benjamin Farnham <benjamin.farnham@cern.ch>
6  *
7  * This file is part of Quasar.
8  *
9  * Quasar is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public Licence as published by
11  * the Free Software Foundation, either version 3 of the Licence.
12  *
13  * Quasar is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public Licence for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with Quasar. If not, see <http://www.gnu.org/licenses/>.
20  */
21 #include "BoostRotatingFileLog.h"
22 
23 #include <boost/move/move.hpp>
24 #include <boost/shared_ptr.hpp>
25 #include <boost/lambda/lambda.hpp>
26 #include <boost/log/core/core.hpp>
27 
28 #include <boost/log/sources/record_ostream.hpp>
29 #include <boost/log/core/record.hpp>
30 #include <boost/log/sources/logger.hpp>
31 #include <boost/log/common.hpp>
32 
33 #include <boost/log/sinks/text_ostream_backend.hpp>
34 #include <boost/log/sinks/text_file_backend.hpp>
35 #include <boost/log/sinks/text_multifile_backend.hpp>
36 #include <boost/log/sinks/sync_frontend.hpp>
37 
38 namespace sinks = boost::log::sinks;
39 namespace keywords = boost::log::keywords;
40 
41 using sinks::text_ostream_backend;
42 using sinks::text_file_backend;
43 
44 const std::string fileNamePattern = "file_%5N.log";
45 const std::string logDirectory = "log";
46 const size_t maxLogFileSizeBytes = 5000000;
47 const size_t maxLogFileCount = 10;
48 
50 {
51  addFileSink();
52  return true;
53 }
54 
56 {
57  boost::log::record record = m_boostLogger.open_record();
58 
59  if(record)
60  {
61  boost::log::record_ostream stream(record);
62  stream << logEntry;
63  stream.flush();
64  m_boostLogger.push_record(boost::move(record));
65  }
66 }
67 
69 {
70  boost::shared_ptr<text_file_backend> fileBackend = boost::make_shared<text_file_backend>(
71  keywords::file_name = fileNamePattern,
72  keywords::open_mode = std::ios_base::app, // append
73  keywords::rotation_size = maxLogFileSizeBytes,
74  keywords::time_based_rotation = sinks::file::rotation_at_time_point(12, 0, 0) );
75 
76  typedef sinks::synchronous_sink<text_file_backend> sink_t;
77  boost::shared_ptr<sink_t> fileSink(new sink_t(fileBackend));
78  fileSink->locked_backend()->auto_flush(true);
79 
80  // manage logfile rolling and deleting oldest file
81  fileSink->locked_backend()->set_file_collector(sinks::file::make_collector(
82  keywords::target = logDirectory,
83  keywords::max_size = maxLogFileCount * maxLogFileSizeBytes));
84 
85  // takes account of files already in the target log directory (otherwise old log files are overwritten).
86  fileSink->locked_backend()->scan_for_files();
87 
88  boost::log::core::get()->add_sink(fileSink);
89 }