~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Foundation/include/Poco/LogStream.h

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// LogStream.h
 
3
//
 
4
// $Id: //poco/1.2/Foundation/include/Poco/LogStream.h#1 $
 
5
//
 
6
// Library: Foundation
 
7
// Package: Logging
 
8
// Module:  LogStream
 
9
//
 
10
// Definition of the LogStream class.
 
11
//
 
12
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
 
13
// and Contributors.
 
14
//
 
15
// Permission is hereby granted, free of charge, to any person or organization
 
16
// obtaining a copy of the software and accompanying documentation covered by
 
17
// this license (the "Software") to use, reproduce, display, distribute,
 
18
// execute, and transmit the Software, and to prepare derivative works of the
 
19
// Software, and to permit third-parties to whom the Software is furnished to
 
20
// do so, all subject to the following:
 
21
// 
 
22
// The copyright notices in the Software and this entire statement, including
 
23
// the above license grant, this restriction and the following disclaimer,
 
24
// must be included in all copies of the Software, in whole or in part, and
 
25
// all derivative works of the Software, unless such copies or derivative
 
26
// works are solely in the form of machine-executable object code generated by
 
27
// a source language processor.
 
28
// 
 
29
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
30
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
31
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 
32
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 
33
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 
34
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
35
// DEALINGS IN THE SOFTWARE.
 
36
//
 
37
 
 
38
 
 
39
#ifndef Foundation_LogStream_INCLUDED
 
40
#define Foundation_LogStream_INCLUDED
 
41
 
 
42
 
 
43
#include "Poco/Foundation.h"
 
44
#include "Poco/Logger.h"
 
45
#include "Poco/UnbufferedStreamBuf.h"
 
46
#include <istream>
 
47
 
 
48
 
 
49
namespace Poco {
 
50
 
 
51
 
 
52
class Foundation_API LogStreamBuf: public UnbufferedStreamBuf
 
53
        /// This class implements a streambuf interface
 
54
        /// to a Logger.
 
55
        ///
 
56
        /// The streambuf appends all characters written to it
 
57
        /// to a string. As soon as a CR or LF (std::endl) is written,
 
58
        /// the string is sent to the Logger, with the set
 
59
        /// priority.
 
60
{
 
61
public:
 
62
        LogStreamBuf(Logger& logger, Message::Priority priority);
 
63
                /// Creates the LogStream.
 
64
 
 
65
        ~LogStreamBuf();
 
66
                /// Destroys the LogStream.
 
67
                
 
68
        void setPriority(Message::Priority priority);
 
69
                /// Sets the priority for log messages.
 
70
                
 
71
        Message::Priority getPriority() const;
 
72
                /// Returns the priority for log messages.
 
73
 
 
74
private:
 
75
        int writeToDevice(char c);
 
76
 
 
77
private:
 
78
        Logger&           _logger;
 
79
        Message::Priority _priority;
 
80
        std::string       _message;
 
81
};
 
82
 
 
83
 
 
84
class Foundation_API LogIOS: public virtual std::ios
 
85
        /// The base class for LogStream.
 
86
        ///
 
87
        /// This class is needed to ensure the correct initialization
 
88
        /// order of the stream buffer and base classes.
 
89
{
 
90
public:
 
91
        LogIOS(Logger& logger, Message::Priority priority);
 
92
        ~LogIOS();
 
93
        LogStreamBuf* rdbuf();
 
94
 
 
95
protected:
 
96
        LogStreamBuf _buf;
 
97
};
 
98
 
 
99
 
 
100
class Foundation_API LogStream: public LogIOS, public std::ostream
 
101
        /// This class implements an ostream interface
 
102
        /// to a Logger.
 
103
        ///
 
104
        /// The stream's buffer appends all characters written to it
 
105
        /// to a string. As soon as a CR or LF (std::endl) is written,
 
106
        /// the string is sent to the Logger, with the current
 
107
        /// priority.
 
108
        ///
 
109
        /// Usage example:
 
110
        ///     LogStream ls(someLogger);
 
111
        ///     ls << "Some informational message" << std::endl;
 
112
        ///     ls.error() << "Some error message" << std::endl;
 
113
{
 
114
public:
 
115
        LogStream(Logger& logger, Message::Priority priority = Message::PRIO_INFORMATION);
 
116
                /// Creates the LogStream, using the given logger and priority.
 
117
 
 
118
        LogStream(const std::string& loggerName, Message::Priority priority = Message::PRIO_INFORMATION);
 
119
                /// Creates the LogStream, using the logger identified
 
120
                /// by loggerName, and sets the priority.
 
121
                
 
122
        ~LogStream();
 
123
                /// Destroys the LogStream.
 
124
                
 
125
        LogStream& fatal();
 
126
                /// Sets the priority for log messages to Message::PRIO_FATAL.
 
127
                
 
128
        LogStream& critical();
 
129
                /// Sets the priority for log messages to Message::PRIO_CRITICAL.
 
130
 
 
131
        LogStream& error();
 
132
                /// Sets the priority for log messages to Message::PRIO_ERROR.
 
133
 
 
134
        LogStream& warning();
 
135
                /// Sets the priority for log messages to Message::PRIO_WARNING.
 
136
 
 
137
        LogStream& notice();
 
138
                /// Sets the priority for log messages to Message::PRIO_NOTICE.
 
139
 
 
140
        LogStream& information();
 
141
                /// Sets the priority for log messages to Message::PRIO_INFORMATION.
 
142
 
 
143
        LogStream& debug();
 
144
                /// Sets the priority for log messages to Message::PRIO_DEBUG.
 
145
 
 
146
        LogStream& trace();
 
147
                /// Sets the priority for log messages to Message::PRIO_TRACE.
 
148
 
 
149
        LogStream& priority(Message::Priority priority);
 
150
                /// Sets the priority for log messages.
 
151
};
 
152
 
 
153
 
 
154
//
 
155
// inlines
 
156
//
 
157
inline Message::Priority LogStreamBuf::getPriority() const
 
158
{
 
159
        return _priority;
 
160
}
 
161
 
 
162
 
 
163
} // namespace Poco
 
164
 
 
165
 
 
166
#endif // Foundation_LogStream_INCLUDED