[go: up one dir, main page]

Menu

[r40]: / src / message.h  Maximize  Restore  History

Download this file

202 lines (162 with data), 6.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/***************************************************************************
message.h - Print messages
-------------------
begin : Wed Sep 15 2004
copyright : (C) 2004 by Jan Rheinlaender
email : jrheinlaender@users.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef MESSAGE_H
#define MESSAGE_H
#include <iostream>
#include <fstream>
#include <string>
enum message_mode { // *** added in 1.3.1
m_stdc, // Use std::c streams
m_func, // Use a user-defined function
m_ostream // Use a user-defined output stream
};
enum message_type {
t_msg_error, // Stream type for printing error messages
t_msg_warn, // warning messages
t_msg_info, // progress messages
t_msg_null // Rubbish bin
};
typedef void (*msgfuncp)(const std::string&, const std::string&);
#define OUTPUTF *outputfunctionp
#define CONSTRUCTOR \
message::message(msgfuncp f, const message_type t, const int l) { \
mode = m_func; \
type = t; \
level = l; \
strm = new std::ostringstream; \
outputfunctionp = f; \
} \
#define INITDECL static void init(msgfuncp f);
#define INIT \
void msg::init(msgfuncp f) { \
msg_error = message(f, t_msg_error, 1); \
msg_warn = message(f, t_msg_warn, 1); \
msg_info = message(f, t_msg_info, -1); \
devnullstream.open("/dev/null"); \
devnull = message(devnullstream, t_msg_info, 0); \
} \
/// Helper class to initialize class message
class message;
class message_init {
public:
message_init();
private:
/// A boolean used to ensure that init() is only called once in the lifetime of the class
static bool called;
static message_init message_initializer;
};
enum manip_type {
endline, // End of line
endstring // End of string
};
/** *** added in 0.7
Every stream has a priority level, where smaller numbers mean higher priority.
Messages are printed when their priority is smaller or equal to the priority level of the
stream. For example: Stream message::msg_err() has priority 1. This
means that a message with priority 1 will be printed, but a message with priority 2 not.
There are 8 priority levels (0 to 7). Larger priority levels are interpreted in such a way
that, for example, a stream with priority 9 prints messages of level 9 and 1 (9 modulo 1).
@author Jan Rheinlaender
@short Class for printing messages: Errors, warnings and info
**/
class message {
friend class message_init;
public:
/**
* Create a message stream of the given type that will output messages of priority level
* or less. This construction uses the std::cerr etc. output streams
* @param t The type of the messages that will be printed
* @param l The priority level up to which messages will be printed
*/
message(const message_type t, const int l);
/**
* Create a message stream using a user-defined output file stream
* @param t The type of the messages that will be printed
* @param l The priority level up to which messages will be printed
*/
message(std::ostream& of, const message_type t, const int l); // *** added in 1.3.1
/**
* Create a message stream using a user-defined output function
* @param t The type of the messages that will be printed
* @param l The priority level up to which messages will be printed
*/
message(msgfuncp f, const message_type t, const int l); // *** added in 1.3.1
/// Clean up
~message();
/// Set the priority level of the stream
void setlevel(const int l);
/// Return the underlying stream. Use only if absolutely necessary!
//inline std::ostream &stream() { return *strm; } // *** removed in 1.3.1
/**
Return the stream to print messages of priority p. Returns the /dev/null stream
if the priority is not high enough
@param p The priority of the messages
@returns This object
*/
// message& prio(const int p); *** removed in 1.3.1
/**
Check if messages of a certain priority will be printed on this stream. Returns true
if (p % 8) <= the priority level of the stream.
@param p The priority
@returns True or false
*/
inline const bool checkprio(const int p) const { // *** changed in 0.8, inlined in 1.0
if ((level >> 3) > 0) // i.e. level > 7 *** changed in 1.0
return ((p % 8) <= (level % 8));
else
return (p <= level);
}
/// Print a message on the stream
message &operator<<(const char *cp);
/// Print an unsigned int on the stream
message &operator<<(const unsigned int i);
/// Print a long unsigned int on the stream
message &operator<<(const long unsigned int i);
/// Print a signed int on the stream
message &operator<<(const int i);
/// Print a long int on the stream
message &operator<<(const long int i);
/// Print a double on the stream
message &operator<<(const double d);
/// Print a string on the stream
message &operator<<(const std::string &s);
/// manipulators
// TODO: Is there a better way to implement this?
// compare std::ostream & dflt(std::ostream & os); from GiNac
message &operator<<(manip_type m);
/// endline manipulator
message &endl(message &m) { (*strm) << std::endl; return m; } // *** added in 0.9
private:
/// The messaging mode
message_mode mode;
/// The type of the stream
message_type type;
/// The priority level of the stream
int level;
/// The underlying stream for printing the messages
std::ostream *strm;
/// The user-defined output function
msgfuncp outputfunctionp;
// Class methods
private:
/// Initialize
static void init();
};
// Global message printing streams *** removed in 1.3.1
//extern message msg_error;
//extern message msg_warn;
//extern message msg_info;
#endif