OpenStructure
Loading...
Searching...
No Matches
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
33namespace ost {
34
35class DLLEXPORT LogSink {
36public:
38 virtual ~LogSink() { }
39 virtual void LogMessage(const String& message, int severity=0) {};
40};
41
42typedef boost::shared_ptr<LogSink> LogSinkPtr;
43
44class DLLEXPORT StreamLogSink : public LogSink {
45public:
46 StreamLogSink(std::ostream& stream):stream_(stream){}
47 virtual void LogMessage(const String& message, int severity){
48 stream_ << message;
49 }
50
51private:
52 std::ostream& stream_;
53};
54
55class DLLEXPORT StringLogSink : public LogSink {
56public:
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
66private:
67 std::ostringstream stream_;
68};
69
70typedef boost::shared_ptr<StringLogSink> StringLogSinkPtr;
71
72class DLLEXPORT FileLogSink : public LogSink {
73public:
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 }
85private:
86 std::ofstream stream_;
87};
88
89typedef boost::shared_ptr<FileLogSink> FileLogSinkPtr;
90
91
93public:
95 bool AddSink(LogSinkPtr& observer);
96 bool RemoveSink(LogSinkPtr& observer);
97 void LogMessage(const String& message, int severity);
98private:
99 std::vector<LogSinkPtr> sinks_;
100};
101
102typedef boost::shared_ptr<MultiLogSink> MultiLogSinkPtr;
103
104}
105#endif
FileLogSink(const String &file_name)
Definition log_sink.hh:74
virtual void LogMessage(const String &message, int severity)
Definition log_sink.hh:75
virtual void LogMessage(const String &message, int severity=0)
Definition log_sink.hh:39
virtual ~LogSink()
Definition log_sink.hh:38
bool AddSink(LogSinkPtr &observer)
void LogMessage(const String &message, int severity)
bool RemoveSink(LogSinkPtr &observer)
virtual void LogMessage(const String &message, int severity)
Definition log_sink.hh:47
StreamLogSink(std::ostream &stream)
Definition log_sink.hh:46
virtual void LogMessage(const String &message, int severity)
Definition log_sink.hh:58
String GetLog() const
Definition log_sink.hh:61
#define DLLEXPORT_OST_BASE
std::string String
Definition base.hh:54
Definition base.dox:1
boost::shared_ptr< StringLogSink > StringLogSinkPtr
Definition log_sink.hh:70
boost::shared_ptr< FileLogSink > FileLogSinkPtr
Definition log_sink.hh:89
boost::shared_ptr< MultiLogSink > MultiLogSinkPtr
Definition log_sink.hh:102
boost::shared_ptr< LogSink > LogSinkPtr
Definition log_sink.hh:42