exception.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_EXCEPTION_HPP
27 #define CASADI_EXCEPTION_HPP
28 
29 #include <chrono>
30 #include <ctime>
31 #include <exception>
32 #include <iomanip>
33 #include <iostream>
34 #include <sstream>
35 #include <stdexcept>
36 #include <string>
37 #include <vector>
38 
39 #include <casadi/core/casadi_export.h>
40 
41 // Disable some Visual studio warnings
42 #ifdef _MSC_VER
43 
44 // warning C4251: Need a dll interface?
45 #pragma warning(disable:4251)
46 
47 // warning C4275: non dll-interface class 'std::exception' used as base for dll-interface
48 // class 'casadi::CasadiException'
49 #pragma warning(disable:4275)
50 
51 // warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s
52 // instead
53 #pragma warning(disable:4996)
54 
55 #endif // _MSC_VER
56 
57 namespace casadi {
58 
77 class CASADI_EXPORT CasadiException : public std::exception {
78  public:
81  }
82 
84  explicit CasadiException(const std::string& msg) : msg_(msg) {}
85 
87  ~CasadiException() throw() {}
88 
90  const char* what() const throw() override {
91  return msg_.c_str();
92  }
93 
94  protected:
95  std::string msg_;
96 };
97 
98 class CASADI_EXPORT KeyboardInterruptException : public CasadiException {
99  public:
101  KeyboardInterruptException() : CasadiException("KeyboardInterrupt") {}
104 };
105 
106 // Strip path prefix
107 inline std::string trim_path(const std::string& full_path) {
108  size_t found = full_path.rfind("/casadi/");
109  if (found == std::string::npos) {
110  return full_path;
111  } else {
112  std::string ret = full_path;
113  ret.replace(0, found, "...");
114  return ret;
115  }
116 }
117 
118 // Current time as a string
119 inline std::ostream& message_prefix(std::ostream &stream) {
120  // CasADi prefix
121  stream << "CasADi - ";
122  // Get current time
123  auto now = std::chrono::system_clock::now();
124  std::time_t tt = std::chrono::system_clock::to_time_t(now);
125  auto local_tm = *std::localtime(&tt); // NOLINT(runtime/threadsafe_fn)
126  // Convert to YYYY-MM-DD HH:MM:SS format
127  stream << local_tm.tm_year + 1900 << '-'; // YYYY-
128  stream << std::setfill('0') << std::setw(2) << local_tm.tm_mon + 1 << '-'; // MM-
129  stream << std::setfill('0') << std::setw(2) << local_tm.tm_mday << ' '; // DD
130  stream << std::setfill('0') << std::setw(2) << local_tm.tm_hour << ':'; // hh:
131  stream << std::setfill('0') << std::setw(2) << local_tm.tm_min << ':'; // mm:
132  stream << std::setfill('0') << std::setw(2) << local_tm.tm_sec; // ss
133  return stream;
134 }
135 
136 // String denoting where the macro is situated
137 #define CASADI_WHERE casadi::trim_path(__FILE__ ":" CASADI_STR(__LINE__))
138 
139 // Throw an exception with information about source code location
140 #define casadi_error(msg, ...) \
141 throw casadi::CasadiException(CASADI_WHERE + ": "\
142  + casadi::fmtstr(msg, casadi::strvec(__VA_ARGS__)))
143 
144 // This assertion checks for illegal user inputs
145 #define casadi_assert(x, msg, ...) \
146 if (!(x)) casadi_error("Assertion \"" CASADI_STR(x) "\" failed:\n"\
147  + std::string(msg), __VA_ARGS__)
148 
149 // This assertion if for internal errors caused by bugs in CasADi
150 #define casadi_assert_dev(x) casadi_assert(x, "Notify the CasADi developers.")
151 
152 // This assertion if for internal errors caused by bugs in CasADi
153 #define casadi_report() casadi_error("Notify the CasADi developers.")
154 
155 // Issue a warning, including location in the source code
156 #define casadi_warning(msg) \
157  casadi::message_prefix(casadi::uerr()) \
158  << " WARNING(\"" << msg << "\") [" << CASADI_WHERE << "]\n" << std::flush;
159 
160 // Issue a message, including location in the source code
161 #define casadi_message(msg) \
162  casadi::message_prefix(casadi::uout()) \
163  << " MESSAGE(\"" << msg << "\") [" << CASADI_WHERE << "]\n" << std::flush;
164 
165 } // namespace casadi
166 
167 #endif // CASADI_EXCEPTION_HPP
Casadi exception class.
Definition: exception.hpp:77
~CasadiException()
Destructor.
Definition: exception.hpp:87
const char * what() const override
Display error.
Definition: exception.hpp:90
CasadiException(const std::string &msg)
Form message string.
Definition: exception.hpp:84
CasadiException()
Default constructor.
Definition: exception.hpp:80
KeyboardInterruptException()
Default constructor.
Definition: exception.hpp:101
The casadi namespace.
std::ostream & message_prefix(std::ostream &stream)
Definition: exception.hpp:119
std::string trim_path(const std::string &full_path)
Definition: exception.hpp:107