MeVisLab Toolbox Reference
mlErrorOutput.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2007, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef ML_ERROR_OUTPUT_H
14 #define ML_ERROR_OUTPUT_H
15 
17 
18 #include "mlUtilsSystem.h"
19 #include "mlUtilsAPI.h"
20 #include "mlMutex.h"
21 #include "mlTrace.h"
22 
23 
24 //-----------------------------------------------------------------------------------
25 // Forward declarations.
26 //-----------------------------------------------------------------------------------
27 ML_UTILS_START_NAMESPACE
28 
30  class ErrorOutput;
31 
32 ML_UTILS_END_NAMESPACE
33 
34 
35 ML_START_NAMESPACE
38  class State;
39 ML_END_NAMESPACE
40 
41 
42 
43 //-----------------------------------------------------------------------------------
44 // Stream output for std::ostream
45 //-----------------------------------------------------------------------------------
46 namespace std {
47 
49  ML_UTILS_EXPORT ostream& operator<<(ostream& s, const ML_UTILS_NAMESPACE::ErrorOutput &errorOutput);
50 }
51 
52 
53 
54 
55 ML_UTILS_START_NAMESPACE
56 
57  //---------------------------------------------------------------------------------------------
59  //---------------------------------------------------------------------------------------------
60  class ErrorOutputInfos;
61 
62  //---------------------------------------------------------------------------------------------
64  //---------------------------------------------------------------------------------------------
65  class RuntimeType;
66 
67  //---------------------------------------------------------------------------------------------
69 
92  //---------------------------------------------------------------------------------------------
94 
95  public:
96 
97  //---------------------------------------------------------------------------------------------
101  //---------------------------------------------------------------------------------------------
102  typedef void ErrorOutputCB(void *usrData,
103  const char* errStr,
104  const ErrorOutputInfos &infos);
105 
106  //---------------------------------------------------------------------------------------------
120  //---------------------------------------------------------------------------------------------
121  typedef char *DumpCB(const void *dumpObj, const RuntimeType *dumpObjRT);
122 
123  //---------------------------------------------------------------------------------------------
125  //---------------------------------------------------------------------------------------------
127 
128  //---------------------------------------------------------------------------------------------
130  //---------------------------------------------------------------------------------------------
132 
133  //-----------------------------------------------------------------------------------
135  //-----------------------------------------------------------------------------------
136  void reset();
137 
138  //---------------------------------------------------------------------------------------------
141  //---------------------------------------------------------------------------------------------
147  void init();
148 
151  void destroy();
153 
154 
155  //---------------------------------------------------------------------------------------------
161  //---------------------------------------------------------------------------------------------
162 
166 
170 
171  //---------------------------------------------------------------------------------------------
174  //---------------------------------------------------------------------------------------------
180  void setMessageFilter(MLuint32 messageType);
181 
186 
187 
188  //-----------------------------------------------------------------------------------
191  //-----------------------------------------------------------------------------------
195  void setDumpCB(DumpCB *dumpCB);
196 
198  DumpCB *getDumpCB() const;
200 
201 
202  //---------------------------------------------------------------------------------------------
210  //---------------------------------------------------------------------------------------------
212  void setFullDebuggingOn(bool on);
213 
215  bool isFullDebuggingOn() const;
217 
218 
219  //---------------------------------------------------------------------------------------------
222  //---------------------------------------------------------------------------------------------
228  void addErrorOutputCB(void *userData, ErrorOutputCB *callback);
229 
232  void removeErrorOutputCB(void *userData, ErrorOutputCB *callback);
233 
237  bool hasErrorOutputCB(void *userData, ErrorOutputCB *callback) const;
238 
243 
245  size_t getNumOutputCBs() const { return _errorOutputCBList.size(); }
247 
248 
249  //---------------------------------------------------------------------------------------------
252  //---------------------------------------------------------------------------------------------
254  void addDebugEnvName(const std::string &envName);
255 
257  void removeDebugEnvName(const std::string &envName);
258 
260  bool hasDebugEnvName(const std::string &envName) const;
261 
263  const std::vector<std::string> &getDebugEnvNames() const;
264 
268 
270  size_t getNumDebugEnvNames() const;
272 
273  //---------------------------------------------------------------------------------------------
276  //---------------------------------------------------------------------------------------------
277 
279  void setOutputMessagesInVisualStudio(bool flag) { _outputMessagesInVisualStudio = flag; }
280 
282  bool shouldOutputMessagesInVisualStudio() const { return _outputMessagesInVisualStudio; }
283 
285 
286 
287  //---------------------------------------------------------------------------------------------
290  //---------------------------------------------------------------------------------------------
292  bool areMessagesSentToCout() const { return _sendToCout; }
293 
296  void sendMessagesToCout(bool on);
297 
299  bool areMessagesSentToCerr() const { return _sendToCerr; }
300 
303  void sendMessagesToCerr(bool on);
305 
306  //---------------------------------------------------------------------------------------------
309  //---------------------------------------------------------------------------------------------
311  MLuint32 getMaxNumTraceListDumps() const { return _maxNumTraceListDumps; }
312 
315 
317  MLuint32 getMaxNumTraceStackDumps() const { return _maxNumTraceStackDumps; }
318 
321 
324  MLuint32 getTraceDumpMessageBits() const { return _traceDumpMessageBits; }
325 
330 
331 
332  // -------------------------------------------------------------------------------------------
338  // -------------------------------------------------------------------------------------------
339 
363  // ErrorOutput::printAndNotify(ML_WARNING, // message type
364  // "ML_", // Library prefix
365  // "Function=", // function prefix
366  // "MyFunction", // functionName
367  // "MyFunction had an overflow", // reason
368  // "I will do nothing to handle it", // handling
369  // __FILE__, // file
370  // __LINE__); // line
371  // NULL, // no runtime object to be dumped
372  // NULL, // no runtime type of any object
373  // ML_CALCULATION_ERROR); // line
377  void printAndNotify(MLMessageType messageType,
378  const std::string &libraryPrefix,
379  const std::string &fPrefix,
380  const std::string &functionName,
381  const std::string &reason,
382  const std::string &handling = "",
383  const std::string &file = "",
384  int line = -1,
385  const void *dumpObj = nullptr,
386  const RuntimeType *dumpObjRT= nullptr,
387  MLErrorCode errCode = ML_RESULT_OK) const;
388 
392  void printAndNotify(MLMessageType messageType,
393  const std::string &libraryPrefix,
394  const std::string &fPrefix,
395  const std::string &functionName,
396  MLErrorCode reason,
397  const std::string &handling = "",
398  const std::string &file = "",
399  int line = -1,
400  const void *dumpObj = nullptr,
401  const RuntimeType *dumpObjRT= nullptr
402  ) const;
403 
414  void handleDebugPrint(const std::string &envVar,
415  const std::string &libraryPrefix,
416  std::stringstream &reason,
417  const char * file,
418  int line) const;
419 
431  void handleDebugPrint(const std::string &envVar,
432  const std::string &libraryPrefix,
433  const char * reason,
434  const char * file,
435  int line) const;
436 
438 
439 
440  protected:
441 
443  friend class ML_NAMESPACE::State;
444 
446  ErrorOutput(const ErrorOutput &errorOutput);
447 
449  ErrorOutput& operator=(const ErrorOutput &errorOutput);
450 
451  private:
452 
454  friend std::ostream& std::operator<<(std::ostream& s, const ErrorOutput &errorOutput);
455 
457  void _init();
458 
460  void _printConsole(const std::string &errStr, const ErrorOutputInfos &infos) const;
461 
465  std::string getTraceDump() const;
466 
470  std::streambuf *_defaultCoutBuffer;
471 
475  std::streambuf *_defaultCerrBuffer;
476 
478  bool _sendToCout;
479 
481  bool _sendToCerr;
482 
483  //---------------------------------------------------------------------------------------------
486  //---------------------------------------------------------------------------------------------
487 
489  bool _isFullDebuggingOn;
490 
492  MLuint32 _messageTypeBits;
493 
496  DumpCB *_dumpCB;
497 
498 
500  MLTerminator _infoTerm;
501 
503  MLTerminator _warnTerm;
504 
506  MLTerminator _errorTerm;
507 
510  MLTerminator _fatalTerm;
511 
513  MLTerminator _debugTerm;
514 
516  MLTerminator _coutTerm;
517 
519  MLTerminator _cerrTerm;
520 
522  MLTerminator _otherTerm;
523 
526  MLGlobalTraceBufferType _maxNumTraceListDumps;
527 
530  MLGlobalTraceBufferType _maxNumTraceStackDumps;
531 
534  MLuint32 _traceDumpMessageBits;
535 
537  std::vector<void*> _errorUserDataList;
538 
540  std::vector<ErrorOutputCB*> _errorOutputCBList;
541 
543  std::vector<std::string> _debugEnvStrings;
544 
549  mutable RecursiveMutex _mutex;
550 
552  bool _outputMessagesInVisualStudio;
553 
555  };
556 
559 
560 ML_UTILS_END_NAMESPACE
561 
562 #endif // End of __mlErrorOutput_H
563 
564 
Struct which contains all information about errors, fatal errors, warnings, information or debug prin...
Class to handle all ML debug prints, errors, warnings, and information.
Definition: mlErrorOutput.h:93
void sendMessagesToCout(bool on)
If on is passed as true, message sending to std::cout is enabled.
size_t getNumOutputCBs() const
Returns the number of registered callbacks.
void init()
Only to force an access to instances of this class to make the objects automatically initialized duri...
ErrorOutput(const ErrorOutput &errorOutput)
Copy constructor, only to be used by state class of ML.
void destroy()
Destroys global settings; to be called during ML or mlUtils destruction, not to be called by applicat...
void setFullDebuggingOn(bool on)
Enables/disables full debugging by passing true / false as on.
MLTerminator getTerminationType(MLMessageType level) const
Return the way how the ML shall handle messages.
bool hasErrorOutputCB(void *userData, ErrorOutputCB *callback) const
Returns true if the output function is registered, i.e., if both parameters are identical with their ...
MLuint32 getTraceDumpMessageBits() const
Returns the bit mask describing all message types which shall lead to a trace list and trace stack du...
void removeAllErrorOutputCBs()
Removes all registered error print functions from list.
ErrorOutput()
Constructor.
void setOutputMessagesInVisualStudio(bool flag)
Sets if messages should be printed in Visual Studio (turned on by default in debug mode).
~ErrorOutput()
Destructor.
void addDebugEnvName(const std::string &envName)
Adds debug environment string whose information shall be printed.
void setMaxNumTraceListDumps(MLGlobalTraceBufferType num)
Sets the maximum number of trace list entries to be shown on fatal error outputs.
bool areMessagesSentToCout() const
Messages are only sent to std::cout if this flag is true.
bool areMessagesSentToCerr() const
Messages are only sent to std::cerr if this flag is true.
void sendMessagesToCerr(bool on)
If on is passed as true message sending to std::cerr is enabled.
void setDumpCB(DumpCB *dumpCB)
Registers a dump function dumpCB which will be called by the ML to dump object structures e....
ErrorOutput & operator=(const ErrorOutput &errorOutput)
Assignment operator, only to be used by state class of ML.
void removeDebugEnvName(const std::string &envName)
Removes a registered debug environment string whose information shall not be printed any more.
size_t getNumDebugEnvNames() const
Return number of registered debug environment names.
void setTerminationType(MLMessageType level, MLTerminator term)
Sets the way how the ML shall handle a specific message type, note that ML_ALL_MESSAGES is not a vali...
void addErrorOutputCB(void *userData, ErrorOutputCB *callback)
Registers a function called when any message is sent MLErrorOutput.
bool hasDebugEnvName(const std::string &envName) const
Returns true if the debug environment name exists.
void setMessageFilter(MLuint32 messageType)
Sets messages to be handled by MLErrorOutput as set bits in messageType.
bool shouldOutputMessagesInVisualStudio() const
Returns wheter messages should be printed in Visual Studio.
MLuint32 getMaxNumTraceStackDumps() const
Returns the maximum number of trace stack entries to be shown on fatal error outputs.
void removeErrorOutputCB(void *userData, ErrorOutputCB *callback)
Removes error print function from function list if both parameters are identical with their correspon...
void ErrorOutputCB(void *usrData, const char *errStr, const ErrorOutputInfos &infos)
Function type which can be passed to the ErrorOutput class.
void removeAllDebugEnvNames()
Removes all registered debug environment strings from list.
MLuint32 getMessageFilter()
Returns the bit mask specifying the message filter which is composed from binary ORed MLMessageTypes ...
void setMaxNumTraceStackDumps(MLGlobalTraceBufferType num)
Sets the maximum number of trace stack entries to be shown on fatal error outputs.
void setTraceDumpMessageBits(MLuint32 bitMask)
Sets the bit mask describing all message types which shall lead to a trace list and trace stack dump.
const std::vector< std::string > & getDebugEnvNames() const
Returns a list of debug environment names.
DumpCB * getDumpCB() const
Returns the currently registered dump callback. Default is NULL.
MLuint32 getMaxNumTraceListDumps() const
Reutrns the maximum number of trace list entries to be shown on fatal error outputs.
char * DumpCB(const void *dumpObj, const RuntimeType *dumpObjRT)
Definition of a callback type which can be registered at the ML C-API.
void reset()
Resets instance to construction state.
bool isFullDebuggingOn() const
Returns true if full debugging is enabled, otherwise false.
RuntimeType contains type and inheritance information of a class and a static dictionary with informa...
Definition: mlRuntimeType.h:53
MLint32 MLErrorCode
Type of an ML Error code.
Definition: mlTypeDefs.h:818
#define ML_RESULT_OK
No error. Everything seems to be okay.
Definition: mlTypeDefs.h:826
void printAndNotify(MLMessageType messageType, const std::string &libraryPrefix, const std::string &fPrefix, const std::string &functionName, const std::string &reason, const std::string &handling="", const std::string &file="", int line=-1, const void *dumpObj=nullptr, const RuntimeType *dumpObjRT=nullptr, MLErrorCode errCode=ML_RESULT_OK) const
Core error printing function used by all other functions.
boost::recursive_mutex RecursiveMutex
Defines a recursive mutex.
Definition: mlMutex.h:32
void handleDebugPrint(const std::string &envVar, const std::string &libraryPrefix, const char *reason, const char *file, int line) const
Function called from mlDebugPrint.
void handleDebugPrint(const std::string &envVar, const std::string &libraryPrefix, std::stringstream &reason, const char *file, int line) const
Function called from mlDebugPrint.
void printAndNotify(MLMessageType messageType, const std::string &libraryPrefix, const std::string &fPrefix, const std::string &functionName, MLErrorCode reason, const std::string &handling="", const std::string &file="", int line=-1, const void *dumpObj=nullptr, const RuntimeType *dumpObjRT=nullptr) const
Like the other printAndNotify() function with the difference that reason is an MLErrorCode which is a...
MLEXPORT std::ostream & operator<<(std::ostream &s, const ml::Field &v)
Overloads the operator "<<" for stream output of Field objects.
MLTerminator
Termination types for error handling.
Definition: mlTypeDefs.h:1162
unsigned int MLuint32
Definition: mlTypeDefs.h:191
MLMessageType
Message types handled by the ErrorOutput class.
Definition: mlTypeDefs.h:798
#define ML_UTILS_EXPORT
Defines platform dependent DLL export macro for mlUtils.
Definition: mlUtilities.h:20
MLCSO_EXPORT void level(CSO *cso)
Determines the normal and the center of gravity of the given CSO.
ML_UTILS_EXPORT ErrorOutput MLErrorOutput
This is a singleton used for all ML Error input and output;.
MLuint16 MLGlobalTraceBufferType
The type used in the MLGlobalTraceBuffer.
Definition: mlTrace.h:238