acado_message_handling.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
32 #ifndef ACADO_TOOLKIT_ACADO_MESSAGE_HANDLING_HPP
33 #define ACADO_TOOLKIT_ACADO_MESSAGE_HANDLING_HPP
34 
37 
38 #include <cstdlib>
39 #include <iostream>
40 #include <vector>
41 #include <string>
42 
43 #ifdef __MATLAB__
44  #include <mex.h>
45  #include <iostream>
46 #endif
47 
49 
52 {
55 };
56 
58 #ifndef __MATLAB__
59 
60 #define COL_DEBUG "\033[1;34m"
61 #define COL_FATAL "\033[0;31m"
62 #define COL_ERROR "\033[1;31m"
63 #define COL_WARNING "\033[1;33m"
64 #define COL_INFO "\033[0m"
65 
66 #else
67 
68 #define COL_DEBUG ""
69 #define COL_FATAL ""
70 #define COL_ERROR ""
71 #define COL_WARNING ""
72 #define COL_INFO ""
73 
74 #endif /* __MATLAB__ */
75 
80 };
81 
84 
87 
88 // define DEBUG macros if necessary
89 #ifndef __FUNCTION__
90  #define __FUNCTION__ 0
91 #endif
92 
93 #ifndef __FILE__
94  #define __FILE__ 0
95 #endif
96 
97 #ifndef __LINE__
98  #define __LINE__ 0
99 #endif
100 
102 #define QUOTE_(x) #x
103 #define QUOTE(x) QUOTE_(x)
104 
106 #define ACADOERROR(retval) \
107  returnValue("Code: ("#retval") \n File: " __FILE__ "\n Line: " QUOTE(__LINE__), LVL_ERROR, retval)
108 
110 #define ACADOERRORTEXT(retval, text) \
111  returnValue("Message: "#text"\n Code: ("#retval") \n File: " __FILE__ "\n Line: " QUOTE(__LINE__), LVL_ERROR, retval)
112 
114 #define ACADOFATAL(retval) \
115  returnValue("Code: ("#retval") \n File: " __FILE__ "\n Line: " QUOTE(__LINE__), LVL_FATAL, retval)
116 
118 #define ACADOFATALTEXT(retval, text) \
119  returnValue("Message: "#text"\n Code: ("#retval") \n File: " __FILE__ "\n Line: " QUOTE(__LINE__), LVL_FATAL, retval)
120 
122 #define ACADOWARNING(retval) \
123  returnValue("Code: ("#retval") \n File: " __FILE__ "\n Line: " QUOTE(__LINE__), LVL_WARNING, retval)
124 
126 #define ACADOWARNINGTEXT(retval,text) \
127  returnValue("Message: "#text"\n Code: ("#retval") \n File: " __FILE__ "\n Line: " QUOTE(__LINE__), LVL_WARNING, retval)
128 
130 #define ACADOINFO(retval) \
131  returnValue("", LVL_INFO, retval)
132 
134 #define ACADOINFOTEXT(retval,text) \
135  returnValue("Message: "#text"\n Code: ("#retval") \n File: " __FILE__ "\n Line: " QUOTE(__LINE__), LVL_INFO, retval)
136 
137 
144 #define ACADO_TRY(X) for(returnValue ACADO_R = X; !ACADO_R;) return ACADO_R
145 
152 class Logger
153 {
154 public:
156  static Logger& instance()
157  {
158  static Logger instance;
159  return instance;
160  }
161 
164  {
165  logLevel = level;
166 
167  return *this;
168  }
169 
172  {
173  return logLevel;
174  }
175 
177  std::ostream& get(returnValueLevel level);
178 
179 private:
181 
183  : logLevel( LVL_FATAL )
184  {}
185 
186  Logger(const Logger&);
187  Logger& operator=(const Logger&);
189  {}
190 };
191 
193 #define LOG( level ) \
194  if (level < Logger::instance().getLogLevel()); \
195  else Logger::instance().get( level )
196 
197 
198 
199 #ifdef __MATLAB__
200 // Writes character stream to MATLAB console.
201 class MatlabConsoleStreamBuf : public std::basic_streambuf<char>
202 {
203 protected:
204  int_type overflow( int_type ch = traits_type::eof() )
205  {
206  if (!traits_type::eq_int_type(ch, traits_type::eof()))
207  return mexPrintf("%c", traits_type::to_char_type(ch)) > 0 ? 0 : traits_type::eof();
208 
209  return 0;
210  }
211 };
212 
213 // Automatically restores previous rdbuf() when goes out of scope.
214 class RedirectStream
215 {
216 public:
217  RedirectStream(std::ostream& stream, std::streambuf& new_streambuf)
218  : _stream(stream)
219  , _old_streambuf(stream.rdbuf())
220  {
221  _stream.rdbuf(&new_streambuf);
222  }
223 
224  ~RedirectStream()
225  {
226  _stream.rdbuf(_old_streambuf);
227  }
228 
229 private:
230  std::ostream& _stream;
231  std::streambuf * _old_streambuf;
232 };
233 #endif
234 
236 
237 
238 
239 #endif // ACADO_TOOLKIT_ACADO_MESSAGE_HANDLING_HPP
240 
241 /*
242  * end of file
243  */
A very simple logging class.
returnValue was not yet handled by user
#define CLOSE_NAMESPACE_ACADO
const char * returnValueTypeToString(returnValueType type)
returnValueType
Defines all symbols for global return values.
returnValueLevel logLevel
returnValueLevel
static Logger & instance()
Returned value is a fatal error, assert like use, aborts execution is unhandled.
returnValueLevel getLogLevel()
#define BEGIN_NAMESPACE_ACADO
returnValue was handled by user
Logger & setLogLevel(returnValueLevel level)
Logger & operator=(const Logger &)
const char * returnValueLevelToString(returnValueLevel level)


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:27