LogRecord.h
1 /*
2  * Copyright (C) 2005-2012 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 
29 #ifndef Pt_System_LogRecord_h
30 #define Pt_System_LogRecord_h
31 
32 #include <Pt/System/Api.h>
33 #include <Pt/System/LogLevel.h>
34 #include <Pt/SourceInfo.h>
35 #include <Pt/NonCopyable.h>
36 #include <string>
37 #include <iostream>
38 #include <sstream>
39 
40 namespace Pt {
41 
42 namespace System {
43 
90 class LogRecord : protected Pt::NonCopyable
91 {
92  public:
94  explicit LogRecord(const LogLevel& level)
95  : _level(level)
96  , _source("unknown", "unknown", "unknown")
97  { }
98 
101  {}
102 
104  void clear()
105  {
106  _text.str( std::string() );
107  _text.clear();
108  }
109 
111  std::string text() const
112  { return _text.str(); }
113 
116  { return _level; }
117 
119  const Pt::SourceInfo& sourceInfo() const
120  { return _source; }
121 
123  template <typename T>
124  LogRecord& operator<<(const T& value)
125  {
126  _text << value;
127  return *this;
128  }
129 
131  LogRecord& operator<<( std::ostream& (*op)(std::ostream&) )
132  {
133  _text << op;
134  return *this;
135  }
136 
138  LogRecord& operator<<( std::ios& (*op)(std::ios&) )
139  {
140  _text << op;
141  return *this;
142  }
143 
145  LogRecord& operator<<( std::ios_base& (*op)(std::ios_base&) )
146  {
147  _text << op;
148  return *this;
149  }
150 
153  {
154  _source = si;
155  return *this;
156  }
157 
158  private:
159  std::ostringstream _text;
160  LogLevel _level;
161  Pt::SourceInfo _source;
162 };
163 
164 } // namespace System
165 
166 } // namespace Pt
167 
168 #endif // Pt_System_LogRecord_h
LogRecord & operator<<(const Pt::SourceInfo &si)
Sets the source location of the log record.
Definition: LogRecord.h:152
Log records can be added to a log.
Definition: LogRecord.h:90
Protects derived classes from being copied.
Definition: NonCopyable.h:54
LogLevel
Severity of the log-message.
Definition: LogLevel.h:41
LogLevel logLevel() const
Returns the severity level.
Definition: LogRecord.h:115
~LogRecord()
@ Destructor.
Definition: LogRecord.h:100
LogRecord & operator<<(std::ios &(*op)(std::ios &))
Applies op to the log record's text.
Definition: LogRecord.h:138
const Pt::SourceInfo & sourceInfo() const
Returns the location in the source where the record was generated.
Definition: LogRecord.h:119
LogRecord & operator<<(std::ostream &(*op)(std::ostream &))
Applies op to the log record's text.
Definition: LogRecord.h:131
void clear()
Clears all content of the record.
Definition: LogRecord.h:104
Source code info class.
Definition: SourceInfo.h:100
LogRecord & operator<<(const T &value)
Appends value to the log record's text.
Definition: LogRecord.h:124
LogRecord(const LogLevel &level)
Construct a log record with a severity level.
Definition: LogRecord.h:94
LogRecord & operator<<(std::ios_base &(*op)(std::ios_base &))
Applies op to the log record's text.
Definition: LogRecord.h:145
std::string text() const
Returns the textual of the record.
Definition: LogRecord.h:111