Fawkes API Fawkes Development Version
log_stream.h
1
2/***************************************************************************
3 * log_stream.h - Route Plexil log output to Fawkes log
4 *
5 * Created: Tue Aug 14 15:04:57 2018
6 * Copyright 2006-2018 Tim Niemueller [www.niemueller.de]
7 ****************************************************************************/
8
9/* This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Library General Public License for more details.
18 *
19 * Read the full text in the LICENSE.GPL file in the doc directory.
20 */
21
22#include <logging/logger.h>
23
24#include <iostream>
25#include <sstream>
26
27/** Log Plexil log output to Fawkes logger.
28 * @author Tim Niemueller
29 */
30class PlexilLogStreamBuffer : public std::basic_streambuf<char>
31{
32public:
33 /** Constructor.
34 * @param logger logger to write output to
35 */
36 PlexilLogStreamBuffer(fawkes::Logger *logger) : logger_(logger)
37 {
38 }
39
40 /** Consume characters.
41 * @param ch character to add
42 * @return always zero
43 */
44 int_type
45 overflow(int_type ch)
46 {
47 if (ch == '\n') {
48 std::string s = ss_.str();
49 std::string::size_type opening, closing;
50 opening = s.find('[');
51 closing = s.find(']');
52 if (opening != std::string::npos && closing != std::string::npos) {
53 std::string comp = "Plexil" + s.substr(opening, closing - opening + 1);
54 std::string text = s.substr(closing + 1);
55 logger_->log_info(comp.c_str(), "%s", text.c_str());
56 } else {
57 logger_->log_info("Plexil", "%s", s.c_str());
58 }
59 ss_.str("");
60 } else {
61 ss_.put(ch);
62 }
63 return 0;
64 }
65
66private:
67 fawkes::Logger * logger_;
68 std::stringstream ss_;
69};
Log Plexil log output to Fawkes logger.
Definition: log_stream.h:31
PlexilLogStreamBuffer(fawkes::Logger *logger)
Constructor.
Definition: log_stream.h:36
int_type overflow(int_type ch)
Consume characters.
Definition: log_stream.h:45
Interface for logging.
Definition: logger.h:42
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.