BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Event/ers/include/ers/StreamFactory.h
Go to the documentation of this file.
1/*
2 * StreamFactory.h
3 * ers
4 *
5 * Created by Matthias Wiesmann on 21.01.05.
6 * Copyright 2005 CERN. All rights reserved.
7 *
8 */
9
10#ifndef ERS_STREAM_FACTORY
11#define ERS_STREAM_FACTORY
12
13#include "ers/Context.h"
14#include "ers/Core.h"
15
16#include <map>
17
18namespace ers {
19
20 class Stream;
21 class Issue;
22
23 /** The \c StreamFactory class is responsible for creating and handling all the ers stream
24 used by the system.
25 * It implements the singleton pattern and the factory pattern.
26 * There should normally only be one instance of this class per process at any time, each
27 instance
28 * handles a table of the different stream attached to each severity.
29 * When issues occurs, they can be dispatched using this instance.
30 * In general, the following method should be used to dispatch issues
31 * <ul>
32 * <li> \c dispatch if the severity_t of the issue is set correctly. </li>
33 * <li> \c fatal, \c error, \c warning, \c debug for setting the severity_t and
34 dispatch.</li>
35 * </ul>
36 * The default stream are set up in the following way:
37 * <ol>
38 * <li>The system checks if an environnement variable with the same name as severity_t is
39 defined.
40 * If this is the case, the value of this variable is parsed as a <b>key</b> for
41 building the stream.
42 * Key are basically URLs, either they start with the \c file protocol, or a keyword
43 that
44 * identifies a different type of protocol followed by a colon.
45 * Certain protocols accepts more information after the colon, for instance for file
46 streams,
47 * the rest of the URL is used to specify the file path, and deduce the file format from
48 the
49 * file extension.<br>
50 * <b>Examples</b><br>
51 * <ul>
52 * <li><code>file:/tmp/out.xml</code> represents a stream that writes
53 * issues into file <tt>/tmp/out.xml</tt> in the XML format.</li>
54 * <li><code>cerr:tab</code> represents a stream that writes in tabulated format
55 * on the standard output.
56 * </ul></li>
57 * <li>If the stream cannot be determined out of the environement variable, a default stream
58 is built.
59 * The actual type of this default stream is determined by the content of the key in \c
60 DEFAULT_STREAMS.
61 * The content is a key with the format described above.y</li>
62 * <li>The user can provide another stream for any severity_t simply by calling method \c
63 set, either
64 * by specifying the stream directly, or by providing a key.</li>
65 * </ol>
66 * Different packages offer different stream implementations.
67 * To use a specific stream type, the relevant library needs to be linked in.
68 * If the key for a type of stream that is not available is used, the system will revert to
69 the default stream.
70 *
71 * <b>Supported Keys - ERS package</b>
72 *
73 * The ERS package supports by default the following keys:<ul>
74 * <li><tt>null:</tt> issues are discarded </li>
75 * <li><tt>default:</tt> issues are displayed on standard error stream </li>
76 * <li><tt>fifo:</tt> writes into a FIFO stream, to be actually used the issues need to be
77 read by another entity </li>
78 * <li><tt>filter:</tt> filter stream, the Issues are filtered and if the filter accept
79 them, passed on to another stream (specified by another key in the stream description).
80 * </ul>
81 *
82 * <b>Supported Keys - System package:</b>
83 *
84 * The System package supports the following keys:<ul>
85 * <li>file: writes into a file</li>
86 * <li>cerr: writes into the standard error stream.<br>
87 * The file: and cerr: protocol support the following data encodings:<ul>
88 * <li>xml - issues are serialized as XML data </li>
89 * <li>tab - issues are serialized as tabulated data </li>
90 * <li>txt - issues are serialized as single line of text </li>
91 * </ul>
92 * <li>syslog: issues are sent to syslog </li>
93 * </ul>
94 *
95 * <b>Supported Keys - ers-ipc package:</b>
96 *
97 * The ersipc package supports the following keys:<ul>
98 * <li>mrs: writes issues into the MRS system (q.v.) the rest of the uri in this case
99 designates the IPC partition to use
100 * for instance mrs://data
101 * </ul>
102 *
103 * In order to add new stream implementations, one needs to register a factory function of
104 type \c create_stream_callback.
105 * This method takes two parameters, a protocol parameter, and an uri parameter, that
106 basically contains everything except
107 * the protocol part of the key. If the implementation can offer a stream that corresponds
108 to the key it should return a
109 * heap allocated stream, if not it should return a null pointer.
110 * This method should be registered using the \c register_factory method
111 * on the default instance.
112 *
113 * <b>Macros</b><br>
114 * This file also defines a set of macros that send messages to the relevant streams in the
115 factory.
116 * Those macros are intendred for logging out debug messages.
117 * They can compiled out by the way of the \c DEBUG_LEVEL macro.
118 * This macro should be defined with an integer level.
119 * A debugging macro is disabled if its number is higher than the value of \c DEBUG_LEVEL.
120 * For instance if DEBUG_LEVEL=2 then ERS_DEBUG_3 is compiled out.
121 * ERS_DEBUG_0 is never compiled out. If no debug level is specified, level 3 (maximum) is
122 assumed.
123 * If the macro ERS_FAST_DEBUG is defined then ERS_DEBUG_0 macro directly maps to a fprintf
124 statement on stderr.
125 *
126 * \author Matthias Wiesmann
127 * \version 1.2
128 * \brief Factory for Stream objects and repository of default streams.
129 * \see ers::Stream
130 * \see ers::FIFOStream
131 * \see ers::FilterStream
132 * \see System::STLStream
133 * \see System::MessageStream
134 * \see System::SyslogStream
135 * \see System::TabStream
136 * \see System::XercesStream
137 */
138
140
141 public:
142 typedef Stream* ( *create_stream_callback )( const std::string&, const std::string& );
143 typedef std::map<std::string, create_stream_callback> stream_factory_collection;
144
145 protected:
146 static StreamFactory* s_instance; /**< \brief singleton instance */
147 static const char* DEFAULT_STREAMS[]; /**< \brief keys for default streams */
148 static const char* key_for_severity( severity_t s ); /**< \brief finds key for severity_t
149 */
150 static Stream* get_default_stream( severity_t s ); /**< \brief builds default stream for
151 severity_t */
152
154 StreamFactory( const StreamFactory& other );
155
156 Stream* m_streams[severity_max]; /**< \brief array of pointers to streams per severity_t */
157 stream_factory_collection m_factories; /**< \brief collection of factories to build streams
158 */
159
160 Stream* create_stream( severity_t s ); /**< \brief create a stream for severity_t */
161 public:
162 static StreamFactory* instance(); /**< \brief return the singleton */
163 static void print_registered();
164 static void fatal( Issue* i ); /**< \brief sends an issue to the fatal stream */
165 static void error( Issue* i ); /**< \brief sends an issue to the error stream */
166 static void warning( Issue* i ); /**< \brief sends an issue to the warning stream */
167 static void warning( const Context& c,
168 const std::string& message ); /**< \brief sends a warning message */
169 static void debug( Issue* i,
170 severity_t ); /**< \brief sends an Issue to the debug stream */
171 static void debug( const Context& c, const std::string& message,
172 severity_t s ); /**< \brief sends a debug message */
173 static void dispatch( Issue* i, bool throw_error = false ); /**< \brief Sends an issue to
174 the appropriate stream
175 according to its severity_t */
176 static void dispatch( Issue& i, bool throw_error = false );
177 static void set_stream( severity_t, const std::string& key );
179 Stream* create_stream( const std::string& key ) const; /**< \brief create a stream from a
180 key */
181 Stream* get_stream( severity_t s ); /**< \brief get stream for severity_t */
182 void set( severity_t severity,
183 Stream* s ); /**< \brief Sets the stream for a given severity_t */
184 void set( severity_t severity, const char* key ); /**< \brief Setup a stream for a given
185 severity_t based on a key */
186 Stream* fatal(); /**< \brief Fatal stream */
187 Stream* error(); /**< \brief Error stream */
188 Stream* warning(); /**< \brief Warning stream */
189 Stream* debug( severity_t s ); /**< \brief Debug stream for level*/
190 bool register_factory( const std::string& name,
191 create_stream_callback callback ); /**< \brief register a factory
192 method */
193 void write_to( std::ostream& stream ) const; /**< \brief write content of factory to stream
194 */
195 };
196 std::ostream& operator<<( std::ostream&,
197 const ers::StreamFactory& factory ); /**< \brief streaming operator
198 */
199} // namespace ers
200
201#ifndef DEBUG_LEVEL
202/** If no debug level is defined, we assume the highest level */
203# define DEBUG_LEVEL 3
204#endif
205
206/** Sends a debug message with level 0, the first parameter is a \c printf like pattern, the
207 * next are parameters for it */
208
209#if !defined( ERS_FAST_DEBUG )
210# define ERS_DEBUG_0( ... ) \
211 { \
212 char ers_debug_buf[256]; \
213 snprintf( ers_debug_buf, sizeof( ers_debug_buf ), __VA_ARGS__ ); \
214 ers::StreamFactory::debug( ERS_HERE, ers_debug_buf, ers::debug_0 ); \
215 }
216#else
217# define ERS_DEBUG_0( ... ) \
218 { \
219 fprintf( stderr, "debug-0 " ); \
220 fprintf( stderr, __VA_ARGS__ ); \
221 fprintf( stderr, "\n" ); \
222 }
223#endif
224
225/** Sends a debug message with level 1, the first parameter is a \c printf like pattern, the
226 * next are parameters for it */
227#if ( DEBUG_LEVEL > 0 )
228# if ( !defined( ERS_FAST_DEBUG ) )
229# define ERS_DEBUG_1( ... ) \
230 { \
231 char ers_debug_buf[256]; \
232 snprintf( ers_debug_buf, sizeof( ers_debug_buf ), __VA_ARGS__ ); \
233 ers::StreamFactory::debug( ERS_HERE, ers_debug_buf, ers::debug_1 ); \
234 }
235# else
236# define ERS_DEBUG_1( ... ) \
237 { \
238 fprintf( stderr, "debug-1 " ); \
239 fprintf( stderr, __VA_ARGS__ ); \
240 fprintf( stderr, "\n" ); \
241 }
242# endif
243#else
244# define ERS_DEBUG_1( ... )
245#endif
246
247/** Sends a debug message with level 2, the first parameter is a \c printf like pattern, the
248 * next are parameters for it */
249#if DEBUG_LEVEL > 1
250# define ERS_DEBUG_2( ... ) \
251 { \
252 char ers_debug_buf[256]; \
253 snprintf( ers_debug_buf, sizeof( ers_debug_buf ), __VA_ARGS__ ); \
254 ers::StreamFactory::debug( ERS_HERE, ers_debug_buf, ers::debug_2 ); \
255 }
256#else
257# define ERS_DEBUG_2( ... )
258#endif
259
260/** Sends a debug message with level 3, the first parameter is a \c printf like pattern, the
261 * next are parameters for it */
262#if DEBUG_LEVEL > 2
263# define ERS_DEBUG_3( ... ) \
264 { \
265 char ers_debug_buf[256]; \
266 snprintf( ers_debug_buf, sizeof( ers_debug_buf ), __VA_ARGS__ ); \
267 ers::StreamFactory::debug( ERS_HERE, ers_debug_buf, ers::debug_3 ); \
268 }
269#else
270# define ERS_DEBUG_3( ... )
271#endif
272
273/** Sends a warning, the first parameter is a \c printf like pattern, the next are parameters
274 * for it */
275#define ERS_WARN( ... ) \
276 { \
277 char ers_warn_buf[256]; \
278 snprintf( ers_warn_buf, sizeof( ers_warn_buf ), __VA_ARGS__ ); \
279 ers::StreamFactory::warning( ERS_HERE, ers_warn_buf ); \
280 }
281
282#endif
XmlRpcServer s
*************DOUBLE PRECISION m_pi *DOUBLE PRECISION m_HvecTau2 DOUBLE PRECISION m_HvClone2 DOUBLE PRECISION m_gamma1 DOUBLE PRECISION m_gamma2 DOUBLE PRECISION m_thet1 DOUBLE PRECISION m_thet2 INTEGER m_IFPHOT *COMMON c_Taupair $ !Spin Polarimeter vector first Tau $ !Spin Polarimeter vector second Tau $ !Clone Spin Polarimeter vector first Tau $ !Clone Spin Polarimeter vector second Tau $ !Random Euler angle for cloning st tau $ !Random Euler angle for cloning st tau $ !Random Euler angle for cloning st tau $ !Random Euler angle for cloning nd tau $ !Random Euler angle for cloning nd tau $ !Random Euler angle for cloning nd tau $ !phi of HvecTau1 $ !theta of HvecTau1 $ !phi of HvecTau2 $ !theta of HvecTau2 $ !super key
Definition Taupair.h:42
Source context for Issue.
Root Issue class.
Factory for Stream objects and repository of default streams.
static const char * key_for_severity(severity_t s)
finds key for severity_t
static void set_stream(severity_t, const std::string &key)
Stream * create_stream(severity_t s)
create a stream for severity_t
Stream * warning()
Warning stream.
static void dispatch(Issue *i, bool throw_error=false)
Sends an issue to the appropriate stream according to its severity_t.
static Stream * get_default_stream(severity_t s)
builds default stream for severity_t
bool register_factory(const std::string &name, create_stream_callback callback)
register a factory method
Stream *(* create_stream_callback)(const std::string &, const std::string &)
static void print_registered()
static const char * DEFAULT_STREAMS[]
keys for default streams
static StreamFactory * instance()
return the singleton
Stream * fatal()
Fatal stream.
stream_factory_collection m_factories
collection of factories to build streams
static StreamFactory * s_instance
singleton instance
void set(severity_t severity, Stream *s)
Sets the stream for a given severity_t.
Stream * get_stream(severity_t s)
get stream for severity_t
Stream * m_streams[severity_max]
array of pointers to streams per severity_t
void write_to(std::ostream &stream) const
write content of factory to stream
std::map< std::string, create_stream_callback > stream_factory_collection
static void debug(Issue *i, severity_t)
sends an Issue to the debug stream
Stream * error()
Error stream.
Root/Null issue stream.
efhlt::Interface * factory(void)
Definition factory.cxx:15
std::ostream & operator<<(std::ostream &, const Issue &)
enum ers::_severity_t severity_t