OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
log_sink.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2020 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef OST_LOG_SINK_HH
20 #define OST_LOG_SINK_HH
21 
22 #include <ostream>
23 #include <sstream>
24 #include <iostream>
25 #include <fstream>
26 #include <stack>
27 #include <vector>
28 
29 #include <boost/shared_ptr.hpp>
30 
31 #include <ost/module_config.hh>
32 
33 namespace ost {
34 
35 class DLLEXPORT LogSink {
36 public:
37  LogSink(){};
38  virtual ~LogSink() { }
39  virtual void LogMessage(const String& message, int severity=0) {};
40 };
41 
42 typedef boost::shared_ptr<LogSink> LogSinkPtr;
43 
44 class DLLEXPORT StreamLogSink : public LogSink {
45 public:
46  StreamLogSink(std::ostream& stream):stream_(stream){}
47  virtual void LogMessage(const String& message, int severity){
48  stream_ << message;
49  }
50 
51 private:
52  std::ostream& stream_;
53 };
54 
55 class DLLEXPORT StringLogSink : public LogSink {
56 public:
57  StringLogSink():LogSink(),stream_(){}
58  virtual void LogMessage(const String& message, int severity){
59  stream_ << message;
60  }
61  String GetLog() const
62  {
63  return stream_.str();
64  }
65 
66 private:
67  std::ostringstream stream_;
68 };
69 
70 typedef boost::shared_ptr<StringLogSink> StringLogSinkPtr;
71 
72 class DLLEXPORT FileLogSink : public LogSink {
73 public:
74  FileLogSink(const String& file_name):stream_(file_name.c_str(), std::ios::out){}
75  virtual void LogMessage(const String& message, int severity){
76  if (stream_.is_open()){
77  stream_ << message;
78  stream_.flush();
79  }
80  }
81 
83  stream_.flush();
84  }
85 private:
86  std::ofstream stream_;
87 };
88 
89 typedef boost::shared_ptr<FileLogSink> FileLogSinkPtr;
90 
91 
93 public:
94  MultiLogSink();
95  bool AddSink(LogSinkPtr& observer);
96  bool RemoveSink(LogSinkPtr& observer);
97  void LogMessage(const String& message, int severity);
98 private:
99  std::vector<LogSinkPtr> sinks_;
100 };
101 
102 typedef boost::shared_ptr<MultiLogSink> MultiLogSinkPtr;
103 
104 }
105 #endif
boost::shared_ptr< FileLogSink > FileLogSinkPtr
Definition: log_sink.hh:89
virtual void LogMessage(const String &message, int severity)
Definition: log_sink.hh:47
std::string String
Definition: base.hh:54
FileLogSink(const String &file_name)
Definition: log_sink.hh:74
StreamLogSink(std::ostream &stream)
Definition: log_sink.hh:46
boost::shared_ptr< MultiLogSink > MultiLogSinkPtr
Definition: log_sink.hh:102
boost::shared_ptr< StringLogSink > StringLogSinkPtr
Definition: log_sink.hh:70
virtual void LogMessage(const String &message, int severity)
Definition: log_sink.hh:75
#define DLLEXPORT_OST_BASE
virtual void LogMessage(const String &message, int severity)
Definition: log_sink.hh:58
String GetLog() const
Definition: log_sink.hh:61
virtual ~LogSink()
Definition: log_sink.hh:38
virtual void LogMessage(const String &message, int severity=0)
Definition: log_sink.hh:39
boost::shared_ptr< LogSink > LogSinkPtr
Definition: log_sink.hh:42