~christopher-hunt08/maus/maus_mc_control

« back to all changes in this revision

Viewing changes to src/common_cpp/Utils/Exception.hh

  • Committer: Durga Rajaram
  • Date: 2013-07-24 00:19:08 UTC
  • mfrom: (659.1.72 release-candidate)
  • Revision ID: durga@fnal.gov-20130724001908-hw36h7kefglgaw3l
Tags: MAUS-v0.6.0
MAUS-v0.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// MAUS WARNING: THIS IS LEGACY CODE.
2
1
/* This file is part of MAUS: http://micewww.pp.rl.ac.uk/projects/maus
3
2
 *
4
3
 * MAUS is free software: you can redistribute it and/or modify
16
15
 *
17
16
 */
18
17
 
19
 
#ifndef Squeal_hh
20
 
#define Squeal_hh 1
 
18
#ifndef SRC_COMMON_CPP_UTILS_EXCEPTION_HH
 
19
#define SRC_COMMON_CPP_UTILS_EXCEPTION_HH 1
21
20
 
22
21
#include <cstdio>
23
22
#include <cstring>
26
25
#include <string>
27
26
#include <vector>
28
27
 
29
 
/// Exception handler class
30
 
///
31
 
/// Exception handler has a severity (exceptionLevel), an error message and a
32
 
/// location. Also function to return the stack trace (gcc only).
33
 
///
34
 
/// If you aren't using gcc, you need to define NO_STACKTRACE
35
 
 
36
 
// note the throw() directives are there to explicitly declare that we cannot
37
 
// throw an exception in this stuff
38
 
 
39
 
class Squeal : public std::exception {
 
28
 
 
29
namespace MAUS {
 
30
/** MAUS exception class
 
31
 *
 
32
 *  Exception has a severity (exceptionLevel), an error message and a
 
33
 *  location. Also function to return the stack trace (gcc only).
 
34
 * 
 
35
 *  If you aren't using gcc, you need to define NO_STACKTRACE at compile time
 
36
 * 
 
37
 *  Note the throw() directives are there to explicitly declare that we cannot
 
38
 *  throw an exception in this stuff
 
39
 */
 
40
class Exception : public std::exception {
40
41
 public:
41
 
  /// ExceptionLevel enumerates the severity of the exception.
42
 
 
43
 
  /// I use an enumeration to distinguish between different error levels.
44
 
  /// * nonRecoverable means the internal state of the programme is no longer
45
 
  ///   well-defined i.e. some memory problem or so.
46
 
  /// * recoverable means that in principle we could keep on running, although
47
 
  ///   most of the time this results in end of run (usually indicates typo in
48
 
  ///   an input file).
49
 
  /// If we start doing GUIs, then the distinction becomes important. Most stuff
50
 
  /// should be recoverable.
 
42
  /** ExceptionLevel enumerates the severity of the exception.
 
43
   *
 
44
   *  I use an enumeration to distinguish between different error levels.
 
45
   *  - nonRecoverable means the internal state of the programme is no longer
 
46
   *    well-defined i.e. some memory problem or so.
 
47
   *  - recoverable means that in principle we could keep on running, although
 
48
   *    most of the time this results in end of run (usually indicates typo in
 
49
   *    an input file).
 
50
   *  If we start doing GUIs, then the distinction becomes important. Most stuff
 
51
   *  should be recoverable.
 
52
   */
51
53
  enum exceptionLevel {recoverable, nonRecoverable};
52
54
 
53
 
  /// constructor - with error level, error message and location
54
 
 
55
 
  /// note this makes a stack trace which can be slow.
56
 
  Squeal(exceptionLevel level, std::string errorMessage, std::string location)
 
55
  /** constructor - with error level, error message and location
 
56
   *
 
57
   *  note this makes a stack trace which can be slow.
 
58
   */
 
59
  Exception(exceptionLevel level, std::string errorMessage, std::string location)
57
60
                                                                        throw();
58
 
  /// constructor - does nothing
59
 
  Squeal() throw();
60
 
 
61
 
  /// destructor - does nothing
62
 
  ~Squeal() throw() {}
63
 
  /// Return char buffer of  message+" at "+location
 
61
  /** constructor - does nothing */
 
62
  Exception() throw();
 
63
 
 
64
  /** destructor - does nothing */
 
65
  ~Exception() throw() {}
 
66
 
 
67
  /** Return char buffer of  message+" at "+location 
 
68
   *
 
69
   *  Memory remains owned by Exception
 
70
   */
64
71
  const char* what() const throw() {return &_what[0];}
65
 
  /// Print the Message to Squeak::mout(Squeak::error) and Location to
66
 
  /// Squeak::mout(Squeak::debug)
 
72
 
 
73
  /** Print the error message and location
 
74
   *
 
75
   *  Prints to error message to Squeak::mout(Squeak::error) and Location to
 
76
   *  Squeak::mout(Squeak::debug)
 
77
   */
67
78
  void Print();
68
 
  /// Get the severity of the exception
69
 
  exceptionLevel GetErrorLevel() const {return _level;}
70
 
  /// Get the error message (as defined by constructor)
71
 
  std::string    GetMessage() const {return _message;}
72
 
  /// Set the error message
73
 
  void           SetMessage(std::string new_message) {
74
 
    _message = new_message;
75
 
    SetWhat(_message+" at "+_location);
76
 
  }
77
 
  /// Get the location (as defined by constructor) of the error
78
 
  std::string    GetLocation() const {return _location;}
79
 
  /// Return the stack trace if it was stored
80
 
  std::string    GetStackTrace() const {return _stacktrace;}
81
 
  /// Gcc-specific code to recover the stack trace as a string.
82
 
 
83
 
  /// Will skip traces below skipTrace (so, for example, if we want to know
84
 
  /// where the Squeal was thrown we might set skipTrace to 2, to skip
85
 
  /// MakeStackTrace and Squeal constructor).
86
 
  static std::string    MakeStackTrace(size_t skipTrace);
 
79
 
 
80
  /** Get the severity of the exception */
 
81
  inline exceptionLevel GetErrorLevel() const {return _level;}
 
82
 
 
83
  /** Get the error message (as defined by constructor) */
 
84
  inline std::string GetMessage() const {return _message;}
 
85
 
 
86
  /** Set the error message */
 
87
  inline void SetMessage(std::string new_message);
 
88
 
 
89
  /** Get the location (as defined by Exception constructor) of the error */
 
90
  std::string GetLocation() const {return _location;}
 
91
 
 
92
  /** Return the stack trace if it was stored */
 
93
  std::string GetStackTrace() const {return _stacktrace;}
 
94
 
 
95
  /** Gcc-specific code to recover the stack trace as a string.
 
96
   *
 
97
   *  Will skip traces below skipTrace (so, for example, if we want to know
 
98
   *  where the Exception was thrown we might set skipTrace to 2, to skip
 
99
   *  MakeStackTrace and Exception constructor).
 
100
   */
 
101
  static std::string MakeStackTrace(size_t skipTrace);
 
102
 
 
103
  /** Set that it makes a stack trace on exception */
 
104
  static void SetWillDoStackTrace(bool willDoStackTrace);
 
105
 
 
106
  /** Return whether makes a stack trace on exception */
 
107
  static bool GetWillDoStackTrace();
87
108
 
88
109
 private:
89
 
 
90
 
  void SetWhat(std::string what_str) {
91
 
    _what = std::vector<char>(what_str.size()+1);
92
 
    snprintf(&_what[0], what_str.size()+1, "%s", what_str.c_str());
93
 
  }
94
 
 
95
 
  static const size_t    _maxStackSize;
 
110
  inline void SetWhat(std::string what_str);
 
111
 
 
112
  static const size_t _maxStackSize;
 
113
  static bool _willDoStackTrace;
96
114
 
97
115
  std::string       _message;
98
116
  std::string       _location;
101
119
  exceptionLevel    _level;
102
120
};
103
121
 
 
122
void Exception::SetMessage(std::string new_message) {
 
123
    _message = new_message;
 
124
    SetWhat(_message+" at "+_location);
 
125
}
 
126
 
 
127
void Exception::SetWhat(std::string what_str) {
 
128
    _what = std::vector<char>(what_str.size()+1);
 
129
    snprintf(&_what[0], what_str.size()+1, "%s", what_str.c_str());
 
130
}
 
131
}
104
132
 
105
133
#endif
106
134