sta_error.h
1 /*#############################################################################
2  *
3  * Copyright 2011 by Henrik Skibbe and Marco Reisert
4  *
5  *
6  * This file is part of the STA-ImageAnalysisToolbox
7  *
8  * STA-ImageAnalysisToolbox is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * STA-ImageAnalysisToolbox is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with STA-ImageAnalysisToolbox.
20  * If not, see <http://www.gnu.org/licenses/>.
21  *
22  *
23 *#############################################################################*/
24 
25 #ifndef STA_COMMON_ERROR_H
26 #define STA_COMMON_ERROR_H
27 #include <ctime>
28 #include <list>
29 #include <sstream>
30 #include <string>
31 #include <iomanip>
32 #include "stensor.h"
33 #include <assert.h>
34 
35 
36 #ifdef __linux__
37  #include <sys/time.h>
38 #endif
39 
40 
41 //mxAssert sometimes didn't work, so we use this workaround
42 
43 #ifdef _SUPPORT_MATLAB_
44  #define sta_assert( S ) \
45  { \
46  if (!( S )) \
47  {\
48  std::stringstream s; \
49  s<<"Assertion \""<<#S<<"\" failed (line "<<__LINE__<<" in "<<__FILE__<<")";\
50  mexErrMsgTxt(s.str().c_str()); \
51  }\
52  }
53 #else
54  #define sta_assert( S ) \
55  { \
56  if (!( S )) \
57  {\
58  assert(S); \
59  }\
60  }
61 #endif
62 
63 
64 namespace hanalysis
65 {
66 
68 class STAError
69 {
70 public:
71  STAError() {}
72 
73  STAError(const std::string& message, STA_RESULT error_code=STA_RESULT_FAILED)
74  :_message(message),_error_code(error_code)
75  {}
76 
77  STAError(STA_RESULT error_code)
78  :_error_code(error_code)
79  {_message=errortostring(error_code);}
80 
81 
82  template<class T>
83  STAError & operator<<(const T &data)
84  {
85  std::ostringstream os;
86  os << data;
87  _message += os.str();
88  return *this;
89  }
90 
91  void setCode(STA_RESULT error_code)
92  {
93  _error_code=error_code;
94  }
95 
96  STA_RESULT getCode() const
97  {
98  return _error_code;
99  }
100 
102  std::string str() const
103  {
104  return _message;
105  }
106 
107 
109  const char* what() const
110  {
111  return _message.c_str();
112  }
113 
114 private:
115  std::string _message;
116  STA_RESULT _error_code;
117 };
118 
119 
121 {
122 private:
123  double _startSecond;
124  double _lastSecond;
125  int steps;
126  std::string startevent;
127  std::list<std::string> event_names;
128  std::list<double> event_times;
129  std::list<double> event_all_times;
130 public:
131  CtimeStopper(std::string event="")
132  {
133  startevent=event;
134  steps=0;
135 #ifdef __linux__
136  struct timeval _Time;
137  gettimeofday(&_Time,NULL);
138  _startSecond=_Time.tv_sec + 0.000001*_Time.tv_usec;
139 #else
140  _startSecond=0;
141 #endif
142  _lastSecond=_startSecond;
143  if (startevent!="")
144  {
145  addEvent(startevent);
146  //printEvents();
147  }
148  }
149 // ~CtimeStopper()
150 // {
151 // if (startevent!="")
152 // {
153 // addEvent("done");
154 // printEvents();
155 // }
156 // }
157 
158 
159  void addEvent(std::string event)
160  {
161 #ifdef __linux__
162  struct timeval currentTime;
163  gettimeofday(&currentTime,NULL);
164  double currentSeconds=currentTime.tv_sec + 0.000001*currentTime.tv_usec;
165 #else
166  double currentSeconds=0;
167 #endif
168 
169  event_times.push_back(currentSeconds-_lastSecond);
170  event_all_times.push_back(currentSeconds-_startSecond);
171  event_names.push_back(event);
172  _lastSecond=currentSeconds;
173  steps++;
174  }
175 
176  void printEvents(int precision=3)
177  {
178  printf("\n computation time (summary):\n");
179  printf(" all step\n");
180  std::list<std::string>::iterator iter2=event_names.begin();
181  std::list<double>::iterator iter3=event_all_times.begin();
182  for (std::list<double>::iterator iter=event_times.begin();
183  iter!=event_times.end();iter++,iter2++,iter3++)
184  {
185  std::stringstream s;
186  s<<std::fixed<<std::setprecision(precision)<<*iter3<<" sec. | "<<*iter<<" sec. |\t("<<*iter2<<")";
187  printf("%s\n",s.str().c_str());
188  }
189  printf("\n");
190  }
191 };
192 
193 
194 }
195 
196 #endif
197 
Definition: sta_error.h:120
the STA error class
Definition: sta_error.h:68
std::string str() const
Definition: sta_error.h:102
STA_RESULT
function return value
Definition: stensor.h:124
const char * what() const
Definition: sta_error.h:109
The STA-ImageAnalysisToolkit namespace.
Definition: stafield.h:55