Fawkes API  Fawkes Development Version
fd_redirect.h
1 
2 /***************************************************************************
3  * fd_redirect.h - Redirect file descriptor writes to log
4  *
5  * Created: Thu Aug 21 14:59:57 2014
6  * Copyright 2014 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __LOGGING_FD_REDIRECT_H_
25 #define __LOGGING_FD_REDIRECT_H_
26 
27 #include <logging/logger.h>
28 
29 #include <boost/asio.hpp>
30 #include <thread>
31 #include <string>
32 
33 namespace fawkes {
34 #if 0 /* just to make Emacs auto-indent happy */
35 }
36 #endif
37 
39  public:
40  LogFileDescriptorToLog(int fd, Logger *logger, const char *logname, Logger::LogLevel log_level);
42 
43  private:
44  void start_log(const char *logname, Logger::LogLevel log_level,
45  boost::asio::posix::stream_descriptor &sd, boost::asio::streambuf &buf);
46  void handle_log_line(const char *logname, Logger::LogLevel log_level,
47  boost::asio::posix::stream_descriptor &sd, boost::asio::streambuf &buf,
48  boost::system::error_code ec, size_t bytes_read);
49 
50  private:
51  int log_fd_;
52  int old_fd_;
53  int old_fd_dup_;
54 
55  boost::asio::io_service io_service_;
56  std::thread io_service_thread_;
57  boost::asio::io_service::work io_service_work_;
58 
59  boost::asio::posix::stream_descriptor stream_;
60  boost::asio::streambuf buffer_;
61 
62  Logger *logger_;
63  std::string log_name_;
64  Logger::LogLevel log_level_;
65 
66 };
67 
68 } // end namespace fawkes
69 
70 #endif
71 
LogLevel
Log level.
Definition: logger.h:45
Fawkes library namespace.
~LogFileDescriptorToLog()
Destructor.
Definition: fd_redirect.cpp:76
Redirect a file descriptor to the log.
Definition: fd_redirect.h:38
LogFileDescriptorToLog(int fd, Logger *logger, const char *logname, Logger::LogLevel log_level)
Constructor.
Definition: fd_redirect.cpp:46
Interface for logging.
Definition: logger.h:34