OpenStructure
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-2011 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 <iostream>
24 #include <fstream>
25 #include <stack>
26 #include <vector>
27 
28 #include <boost/shared_ptr.hpp>
29 
30 #include <ost/module_config.hh>
31 
32 namespace ost {
33 
34 class DLLEXPORT LogSink {
35 public:
36  LogSink(){};
37  virtual ~LogSink() { }
38  virtual void LogMessage(const String& message, int severity=0)=0;
39 };
40 
41 typedef boost::shared_ptr<LogSink> LogSinkPtr;
42 
43 class DLLEXPORT StreamLogSink : public LogSink {
44 public:
45  StreamLogSink(std::ostream& stream):stream_(stream){}
46  virtual void LogMessage(const String& message, int severity){
47  stream_ << message;
48  }
49 
50 private:
51  std::ostream& stream_;
52 };
53 
54 class DLLEXPORT FileLogSink : public LogSink {
55 public:
56  FileLogSink(const String& file_name):stream_(file_name.c_str(), std::ios::out){}
57  virtual void LogMessage(const String& message, int severity){
58  if (stream_.is_open()){
59  stream_ << message;
60  stream_.flush();
61  }
62  }
63 
65  stream_.flush();
66  }
67 private:
68  std::ofstream stream_;
69 };
70 
71 typedef boost::shared_ptr<FileLogSink> FileLogSinkPtr;
72 
73 
75 public:
76  MultiLogSink();
77  bool AddSink(LogSinkPtr& observer);
78  bool RemoveSink(LogSinkPtr& observer);
79  void LogMessage(const String& message, int severity);
80 private:
81  std::vector<LogSinkPtr> sinks_;
82 };
83 
84 typedef boost::shared_ptr<MultiLogSink> MultiLogSinkPtr;
85 
86 }
87 #endif