Stride Reference Manual  - generated for commit 9643b11
Assert.h
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2019, Kuylen E, Willem L, Broeckhove J
14  */
15 
21 #pragma once
22 
23 #include "util/Exception.h"
24 
25 #include <iostream>
26 #include <spdlog/logger.h>
27 #include <sstream>
28 #include <string>
29 
30 namespace stride {
31 namespace util {
32 
33 inline std::string AssertMessage(const char* condition, const std::string& message,
34  const std::shared_ptr<spdlog::logger>& logger, const char* file, int line)
35 {
36  std::ostringstream os;
37  os << "Assert: '" << condition << "'"
38  << " fails in: '" << file << "' line: " << line << " with: " << message;
39  const auto msg = os.str();
40  if (logger) {
41  logger->critical(msg);
42  logger->flush();
43  } else {
44  std::cerr << msg << std::endl;
45  }
46  return msg;
47 }
48 
49 } // namespace util
50 } // namespace stride
51 
52 // Assert that CONDITION evaluates to true, if not produce failure info string
53 // including file and line of the failure and its MESSAGE and
54 // then (for AssertAndLog) log with LOGGER or with std::cerr (iff LOOGER==nullptr),
55 // or (for AssertAndThrow) raise an Exception with infor string as content.
56 #ifdef STRIDE_INCLUDE_STRIDE_ASSERTS
57 #define AssertLog(CONDITION, MESSAGE, LOGGER) \
58  if (!(CONDITION)) { \
59  stride::util::AssertMessage(#CONDITION, MESSAGE, LOGGER, __FILE__, __LINE__); \
60  }
61 #define AssertThrow(CONDITION, MESSAGE, LOGGER) \
62  if (!(CONDITION)) { \
63  throw stride::util::Exception( \
64  stride::util::AssertMessage(#CONDITION, MESSAGE, LOGGER, __FILE__, __LINE__)); \
65  }
66 #else
67 #define AssertLog(CONDITION, MESSAGE, LOGGER) ((void)0)
68 #define AssertThrow(CONDITION, MESSAGE, LOGGER) ((void)0)
69 #endif
std::string AssertMessage(const char *condition, const std::string &message, const std::shared_ptr< spdlog::logger > &logger, const char *file, int line)
Definition: Assert.h:33
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28