DataFormatException.h
Go to the documentation of this file.
1 #ifndef _DAQDATAFORMATEXCEPTION_H
2 #define _DAQDATAFORMATEXCEPTION_H
3 #include "DAQDataFormats/RawDAQData.h" //< using DEBUGGING variable from there. Otherwise, we don't need it
4 
5 #include <stdexcept>
6 #include <execinfo.h>
7 #include <sstream>
8 #include <cstdlib>
9 
10 #ifdef DEBUGGING
11 #define GENERATE_DATAFORMAT_EXCEPTION(msg) \
12  throw daqdataformats::DataFormatException(__PRETTY_FUNCTION__, __FILE__, __LINE__, msg, _dataFormatClassName, _dataFormatConstructor )
13 #else
14 #define GENERATE_DATAFORMAT_EXCEPTION(msg) \
15  throw daqdataformats::DataFormatException(__PRETTY_FUNCTION__, __FILE__, __LINE__, msg )
16 #endif
17 
18 #define GENERATE_DATAFORMAT_WRONG_VERSION_EXCEPTION GENERATE_DATAFORMAT_EXCEPTION(generateExceptionForWrongVersion(MAXIMUM_VERSION))
19 
20 namespace daqdataformats {
21 
22 /**
23  * Class to report exceptions in the DAQDataFormats package.
24  *
25  * @author Kurt Biery
26  * @version $Revision: 1.7.12.1 $ $Date: 2019/09/27 00:06:51 $
27  */
28 
30 
31  public:
32 
33  /**
34  * Constructs a data format exception instance from the specified
35  * file, line number, and message.
36  */
37  DataFormatException( const std::string& pretty_function
38  ,const std::string& file_name
39  ,int line
40  ,const std::string& message
41  ,const std::string dataformat_class_name = ""
42  ,const std::string dataformat_constructor = ""
43  )
44  : _fullText(constructMessage(&pretty_function,
45  &file_name,
46  line,
47  &message,
48  &dataformat_class_name,
49  &dataformat_constructor))
50  {
51  }
52 
54  : _fullText(constructMessage(nullptr,
55  nullptr,
56  0,
57  &message,
58  nullptr,
59  nullptr))
60  {
61  }
62 
63  const char* what() const noexcept override
64  {
65  return _fullText.c_str();
66  }
67 
68  private:
69  //--------------------------------------------------------------------------
70  /// Construct exception message
71  std::string constructMessage(std::string const * const pretty_function,
72  std::string const * const file_name,
73  int line,
74  std::string const * const message,
75  std::string const * const dataformat_class_name,
76  std::string const * const dataformat_constructor)
77  {
79  /// build up the exception text
80  result = *message + "\n" + " DataFormatException generated";
81 
82  /// Add function name
83  if(pretty_function && pretty_function->size() > 0){
84  result.append(" in function" + *pretty_function);
85  }
86 
87  /// Add File name
88  if(file_name && file_name->size() > 0){
89  result += " in File " + *file_name;
90  }
91 
92  /// Add Line number
93  if(line > 0){
94  result += " Line:" + std::to_string(line) + '.';
95  }
96 
97  /// if dataformat class name is given, print it
98  if(dataformat_class_name && dataformat_class_name->size() > 0){
99  result += "\nDataFormat Class: " + *dataformat_class_name + ".";
100  }
101 
102  /// if dataformat constructor name is given, print it
103  if(dataformat_constructor && dataformat_constructor->size() > 0){
104  result += " DataFormat Constructor: " + *dataformat_constructor + ".";
105  }// end if dataformat class name is given
106 
107  result.append(".");
108 
109  /// add some limited stack trace information
110  void *array[10];
111  const size_t size = backtrace (array, 10);
112 
113  char** strings = backtrace_symbols (array, size);
114 
115  std::stringstream tmpStream;
116  for (size_t idx = 0; idx < size; idx++) {
117  tmpStream << " " << strings[idx] << std::endl;
118  }
119  free (strings);
120 
121  result += "\n Stack trace:\n" + tmpStream.str();
122 
123  //std::cout<<_fullText<<"\n";
124 
125  return result;
126  }
127 
128  private:
130 
131 };
132 
133 } // end of namespace
134 
135 #endif
std::string constructMessage(std::string const *const pretty_function, std::string const *const file_name, int line, std::string const *const message, std::string const *const dataformat_class_name, std::string const *const dataformat_constructor)
Construct exception message.
DataFormatException(const std::string &message)
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
const char * what() const noexcept override
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
Class to hold the data from the FEBs in correct formats.
DataFormatException(const std::string &pretty_function, const std::string &file_name, int line, const std::string &message, const std::string dataformat_class_name="", const std::string dataformat_constructor="")
std::string to_string(ModuleType mt)
Definition: ModuleType.h:32