LogChannel.h
1 /*
2  * Copyright (C) 2005-2010 by Dr. Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 #ifndef Pt_System_LogChannel_h
29 #define Pt_System_LogChannel_h
30 
31 #include <Pt/System/Api.h>
32 #include <Pt/NonCopyable.h>
33 #include <string>
34 #include <cstddef>
35 
36 namespace Pt {
37 
38 namespace System {
39 
51 class PT_SYSTEM_API LogChannel : protected Pt::NonCopyable
52 {
53  protected:
57  : _refs(0)
58  {}
59 
60  public:
63  virtual ~LogChannel()
64  {}
65 
66  std::size_t ref()
67  { return ++_refs; }
68 
69  std::size_t unref()
70  { return --_refs; }
71 
74  const std::string& url() const
75  { return _url; }
76 
82  void open(const std::string& urlstr)
83  {
84  _url = urlstr;
85  this->onOpen(urlstr);
86  }
87 
90  void close()
91  {
92  this->onClose();
93  _url.clear();
94  }
95 
98  void write(const std::string& message)
99  { this->onWrite( message.data(), message.size() ); }
100 
101  protected:
107  virtual void onOpen(const std::string& url) = 0;
108 
111  virtual void onClose() = 0;
112 
115  virtual void onWrite(const char* msg, std::size_t msglen) = 0;
116 
117  private:
118  std::string _url;
119  std::size_t _refs;
120 };
121 
122 } // namespace System
123 
124 } // namespace Pt
125 
126 #endif // Pt_System_LogChannel_h
LogChannel()
Default constructor.
Definition: LogChannel.h:56
Protects derived classes from being copied.
Definition: NonCopyable.h:54
void close()
Closes the channel.
Definition: LogChannel.h:90
void open(const std::string &urlstr)
Open the channel from URL.
Definition: LogChannel.h:82
void write(const std::string &message)
Writes data to the channel.
Definition: LogChannel.h:98
virtual ~LogChannel()
Destructor.
Definition: LogChannel.h:63
Logging channel.
Definition: LogChannel.h:51
const std::string & url() const
Returns the URL used to open the channel.
Definition: LogChannel.h:74