BOSS 7.0.6
BESIII Offline Software System
Loading...
Searching...
No Matches
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/Core.h"
14#include "ers/Context.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 used by the system.
24 * It implements the singleton pattern and the factory pattern.
25 * There should normally only be one instance of this class per process at any time, each instance
26 * handles a table of the different stream attached to each severity.
27 * When issues occurs, they can be dispatched using this instance.
28 * In general, the following method should be used to dispatch issues
29 * <ul>
30 * <li> \c dispatch if the severity_t of the issue is set correctly. </li>
31 * <li> \c fatal, \c error, \c warning, \c debug for setting the severity_t and dispatch.</li>
32 * </ul>
33 * The default stream are set up in the following way:
34 * <ol>
35 * <li>The system checks if an environnement variable with the same name as severity_t is defined.
36 * If this is the case, the value of this variable is parsed as a <b>key</b> for building the stream.
37 * Key are basically URLs, either they start with the \c file protocol, or a keyword that
38 * identifies a different type of protocol followed by a colon.
39 * Certain protocols accepts more information after the colon, for instance for file streams,
40 * the rest of the URL is used to specify the file path, and deduce the file format from the
41 * file extension.<br>
42 * <b>Examples</b><br>
43 * <ul>
44 * <li><code>file:/tmp/out.xml</code> represents a stream that writes
45 * issues into file <tt>/tmp/out.xml</tt> in the XML format.</li>
46 * <li><code>cerr:tab</code> represents a stream that writes in tabulated format
47 * on the standard output.
48 * </ul></li>
49 * <li>If the stream cannot be determined out of the environement variable, a default stream is built.
50 * The actual type of this default stream is determined by the content of the key in \c DEFAULT_STREAMS.
51 * The content is a key with the format described above.y</li>
52 * <li>The user can provide another stream for any severity_t simply by calling method \c set, either
53 * by specifying the stream directly, or by providing a key.</li>
54 * </ol>
55 * Different packages offer different stream implementations.
56 * To use a specific stream type, the relevant library needs to be linked in.
57 * If the key for a type of stream that is not available is used, the system will revert to the default stream.
58 *
59 * <b>Supported Keys - ERS package</b>
60 *
61 * The ERS package supports by default the following keys:<ul>
62 * <li><tt>null:</tt> issues are discarded </li>
63 * <li><tt>default:</tt> issues are displayed on standard error stream </li>
64 * <li><tt>fifo:</tt> writes into a FIFO stream, to be actually used the issues need to be read by another entity </li>
65 * <li><tt>filter:</tt> filter stream, the Issues are filtered and if the filter accept them, passed on to another stream
66 (specified by another key in the stream description).
67 * </ul>
68 *
69 * <b>Supported Keys - System package:</b>
70 *
71 * The System package supports the following keys:<ul>
72 * <li>file: writes into a file</li>
73 * <li>cerr: writes into the standard error stream.<br>
74 * The file: and cerr: protocol support the following data encodings:<ul>
75 * <li>xml - issues are serialized as XML data </li>
76 * <li>tab - issues are serialized as tabulated data </li>
77 * <li>txt - issues are serialized as single line of text </li>
78 * </ul>
79 * <li>syslog: issues are sent to syslog </li>
80 * </ul>
81 *
82 * <b>Supported Keys - ers-ipc package:</b>
83 *
84 * The ersipc package supports the following keys:<ul>
85 * <li>mrs: writes issues into the MRS system (q.v.) the rest of the uri in this case designates the IPC partition to use
86 * for instance mrs://data
87 * </ul>
88 *
89 * In order to add new stream implementations, one needs to register a factory function of type \c create_stream_callback.
90 * This method takes two parameters, a protocol parameter, and an uri parameter, that basically contains everything except
91 * the protocol part of the key. If the implementation can offer a stream that corresponds to the key it should return a
92 * heap allocated stream, if not it should return a null pointer.
93 * This method should be registered using the \c register_factory method
94 * on the default instance.
95 *
96 * <b>Macros</b><br>
97 * This file also defines a set of macros that send messages to the relevant streams in the factory.
98 * Those macros are intendred for logging out debug messages.
99 * They can compiled out by the way of the \c DEBUG_LEVEL macro.
100 * This macro should be defined with an integer level.
101 * A debugging macro is disabled if its number is higher than the value of \c DEBUG_LEVEL.
102 * For instance if DEBUG_LEVEL=2 then ERS_DEBUG_3 is compiled out.
103 * ERS_DEBUG_0 is never compiled out. If no debug level is specified, level 3 (maximum) is assumed.
104 * If the macro ERS_FAST_DEBUG is defined then ERS_DEBUG_0 macro directly maps to a fprintf statement on stderr.
105 *
106 * \author Matthias Wiesmann
107 * \version 1.2
108 * \brief Factory for Stream objects and repository of default streams.
109 * \see ers::Stream
110 * \see ers::FIFOStream
111 * \see ers::FilterStream
112 * \see System::STLStream
113 * \see System::MessageStream
114 * \see System::SyslogStream
115 * \see System::TabStream
116 * \see System::XercesStream
117 */
118
120
121public:
122 typedef Stream* (*create_stream_callback)(const std::string &, const std::string &);
123 typedef std::map<std::string, create_stream_callback> stream_factory_collection ;
124protected:
125 static StreamFactory *s_instance ; /**< \brief singleton instance */
126 static const char* DEFAULT_STREAMS[] ; /**< \brief keys for default streams */
127 static const char *key_for_severity(severity_t s) ; /**< \brief finds key for severity_t */
128 static Stream *get_default_stream(severity_t s) ; /**< \brief builds default stream for severity_t */
129
131 StreamFactory(const StreamFactory &other);
132
133 Stream *m_streams[severity_max] ; /**< \brief array of pointers to streams per severity_t */
134 stream_factory_collection m_factories ; /**< \brief collection of factories to build streams */
135
136 Stream *create_stream(severity_t s) ; /**< \brief create a stream for severity_t */
137public:
138 static StreamFactory *instance(); /**< \brief return the singleton */
139 static void print_registered();
140 static void fatal(Issue *i) ; /**< \brief sends an issue to the fatal stream */
141 static void error(Issue *i) ; /**< \brief sends an issue to the error stream */
142 static void warning(Issue *i); /**< \brief sends an issue to the warning stream */
143 static void warning(const Context &c, const std::string &message); /**< \brief sends a warning message */
144 static void debug(Issue *i, severity_t) ; /**< \brief sends an Issue to the debug stream */
145 static void debug(const Context &c, const std::string &message, severity_t s); /**< \brief sends a debug message */
146 static void dispatch(Issue *i, bool throw_error = false) ; /**< \brief Sends an issue to the appropriate stream according to its severity_t */
147 static void dispatch(Issue &i, bool throw_error = false) ;
148 static void set_stream(severity_t, const std::string &key) ;
149 ~StreamFactory() ;
150 Stream *create_stream(const std::string &key) const ; /**< \brief create a stream from a key */
151 Stream *get_stream(severity_t s) ; /**< \brief get stream for severity_t */
152 void set(severity_t severity, Stream *s) ; /**< \brief Sets the stream for a given severity_t */
153 void set(severity_t severity, const char* key) ; /**< \brief Setup a stream for a given severity_t based on a key */
154 Stream *fatal() ; /**< \brief Fatal stream */
155 Stream *error() ; /**< \brief Error stream */
156 Stream *warning() ; /**< \brief Warning stream */
157 Stream* debug(severity_t s) ; /**< \brief Debug stream for level*/
158 bool register_factory(const std::string &name, create_stream_callback callback); /**< \brief register a factory method */
159 void write_to(std::ostream& stream) const ; /**< \brief write content of factory to stream */
160 } ;
161 std::ostream& operator<<(std::ostream&, const ers::StreamFactory& factory); /**< \brief streaming operator */
162} // ers
163
164#ifndef DEBUG_LEVEL
165 /** If no debug level is defined, we assume the highest level */
166#define DEBUG_LEVEL 3
167#endif
168
169
170 /** Sends a debug message with level 0, the first parameter is a \c printf like pattern, the next are parameters for it */
171
172#if ! defined(ERS_FAST_DEBUG)
173#define ERS_DEBUG_0(...) { char ers_debug_buf[256] ; snprintf(ers_debug_buf,sizeof(ers_debug_buf),__VA_ARGS__) ; ers::StreamFactory::debug(ERS_HERE,ers_debug_buf,ers::debug_0) ; }
174#else
175#define ERS_DEBUG_0(...) { fprintf(stderr,"debug-0 ") ; fprintf(stderr,__VA_ARGS__) ; fprintf(stderr,"\n") ; }
176#endif
177
178
179 /** Sends a debug message with level 1, the first parameter is a \c printf like pattern, the next are parameters for it */
180#if (DEBUG_LEVEL>0)
181#if (! defined(ERS_FAST_DEBUG))
182#define ERS_DEBUG_1(...) { char ers_debug_buf[256] ; snprintf(ers_debug_buf,sizeof(ers_debug_buf),__VA_ARGS__) ; ers::StreamFactory::debug(ERS_HERE,ers_debug_buf,ers::debug_1) ; }
183#else
184#define ERS_DEBUG_1(...) { fprintf(stderr,"debug-1 ") ; fprintf(stderr,__VA_ARGS__) ; fprintf(stderr,"\n") ; }
185#endif
186#else
187#define ERS_DEBUG_1(...)
188#endif
189
190 /** Sends a debug message with level 2, the first parameter is a \c printf like pattern, the next are parameters for it */
191#if DEBUG_LEVEL>1
192#define ERS_DEBUG_2(...) { char ers_debug_buf[256] ; snprintf(ers_debug_buf,sizeof(ers_debug_buf),__VA_ARGS__) ; ers::StreamFactory::debug(ERS_HERE,ers_debug_buf,ers::debug_2) ; }
193#else
194#define ERS_DEBUG_2(...)
195#endif
196
197 /** Sends a debug message with level 3, the first parameter is a \c printf like pattern, the next are parameters for it */
198#if DEBUG_LEVEL>2
199#define ERS_DEBUG_3(...) { char ers_debug_buf[256] ; snprintf(ers_debug_buf,sizeof(ers_debug_buf),__VA_ARGS__) ; ers::StreamFactory::debug(ERS_HERE,ers_debug_buf,ers::debug_3) ; }
200#else
201#define ERS_DEBUG_3(...)
202#endif
203
204 /** Sends a warning, the first parameter is a \c printf like pattern, the next are parameters for it */
205#define ERS_WARN(...) { char ers_warn_buf[256] ; snprintf(ers_warn_buf,sizeof(ers_warn_buf),__VA_ARGS__) ; ers::StreamFactory::warning(ERS_HERE,ers_warn_buf) ; }
206
207
208#endif
209
XmlRpcServer s
Definition: HelloServer.cpp:11
*************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.
Definition: Context.h:42
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 StreamFactory * s_instance
singleton instance
static const char * DEFAULT_STREAMS[]
keys for default streams
static void print_registered()
Stream * m_streams[severity_max]
array of pointers to streams per severity_t
static StreamFactory * instance()
return the singleton
Stream * fatal()
Fatal stream.
stream_factory_collection m_factories
collection of factories to build streams
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
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.
Definition: Stream.h:35
efhlt::Interface * factory(void)
Definition: factory.cxx:17
std::ostream & operator<<(std::ostream &, const Issue &)
@ severity_max
Definition: Core.h:24
enum ers::_severity_t severity_t