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
40namespace Pt {
41
42namespace System {
43
93class LogRecord : protected Pt::NonCopyable
94{
95 public:
97 explicit LogRecord(const LogLevel& level)
98 : _level(level)
99 , _source("unknown", "unknown", "unknown")
100 { }
101
104 {}
105
107 void clear()
108 {
109 _text.str( std::string() );
110 _text.clear();
111 }
112
114 std::string text() const
115 { return _text.str(); }
116
119 { return _level; }
120
123 { return _source; }
124
126 template <typename T>
127 LogRecord& operator<<(const T& value)
128 {
129 _text << value;
130 return *this;
131 }
132
134 LogRecord& operator<<( std::ostream& (*op)(std::ostream&) )
135 {
136 _text << op;
137 return *this;
138 }
139
141 LogRecord& operator<<( std::ios& (*op)(std::ios&) )
142 {
143 _text << op;
144 return *this;
145 }
146
148 LogRecord& operator<<( std::ios_base& (*op)(std::ios_base&) )
149 {
150 _text << op;
151 return *this;
152 }
153
156 {
157 _source = si;
158 return *this;
159 }
160
161 private:
162 std::ostringstream _text;
163 LogLevel _level;
164 Pt::SourceInfo _source;
165};
166
167} // namespace System
168
169} // namespace Pt
170
171#endif // Pt_System_LogRecord_h
Base class that disables copy construction and assignment.
Definition NonCopyable.h:59
Source code info class.
Definition SourceInfo.h:101
std::string text() const
Returns the textual of the record.
Definition LogRecord.h:114
const Pt::SourceInfo & sourceInfo() const
Returns the location in the source where the record was generated.
Definition LogRecord.h:122
LogLevel logLevel() const
Returns the severity level.
Definition LogRecord.h:118
LogRecord & operator<<(std::ios_base &(*op)(std::ios_base &))
Applies op to the log record's text.
Definition LogRecord.h:148
LogRecord & operator<<(std::ios &(*op)(std::ios &))
Applies op to the log record's text.
Definition LogRecord.h:141
LogRecord & operator<<(std::ostream &(*op)(std::ostream &))
Applies op to the log record's text.
Definition LogRecord.h:134
~LogRecord()
@ Destructor.
Definition LogRecord.h:103
LogRecord(const LogLevel &level)
Construct a log record with a severity level.
Definition LogRecord.h:97
void clear()
Clears all content of the record.
Definition LogRecord.h:107
LogRecord & operator<<(const T &value)
Appends value to the log record's text.
Definition LogRecord.h:127
LogRecord & operator<<(const Pt::SourceInfo &si)
Sets the source location of the log record.
Definition LogRecord.h:155
LogLevel
Severity of the log-message.
Definition LogLevel.h:41
System programming
Definition Connection.h:26
Core module.
Definition Allocator.h:33