SIRIUS 7.5.0
Electronic structure library and applications
rte.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2023 Anton Kozhevnikov, Thomas Schulthess
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without modification, are permitted provided that
5// the following conditions are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
8// following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
10// and the following disclaimer in the documentation and/or other materials provided with the distribution.
11//
12// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
13// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
15// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
18// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
20/** \file rte.hpp
21 *
22 * \brief Eror and warning handling during run-time execution.
23 *
24 */
25
26#ifndef __RTE_HPP__
27#define __RTE_HPP__
28
29#include <stdexcept>
30#include <sstream>
31#include <ostream>
32#include <vector>
33#include <iostream>
34#include "core/string_tools.hpp"
35
36namespace sirius {
37
38/// Run-time error and warning handling.
39namespace rte {
40
41inline void message_impl(bool fatal__, const char* func__, const char* file__, int line__, std::string const& msg__)
42{
43 std::stringstream s;
44
45 if (!fatal__) {
46 s << "Warning";
47 } else {
48 s << "Exception";
49 }
50 s << " in function \"" << func__ << "\" at " << file__ << ":" << line__ << std::endl;
51 s << msg__;
52
53 if (fatal__) {
54 throw std::runtime_error(s.str());
55 } else {
56 std::cout << s.str() << std::endl;
57 }
58}
59
60inline void message_impl(bool fatal__, const char* func__, const char* file__, int line__, std::stringstream const& msg__)
61{
62 message_impl(fatal__, func__, file__, line__, msg__.str());
63}
64
65class ostream : public std::ostringstream
66{
67 private:
68 std::ostream* out_{nullptr};
69 std::string prefix_;
70 public:
71 ostream()
72 {
73 }
74 ostream(std::ostream& out__, std::string prefix__)
75 : out_(&out__)
76 , prefix_(prefix__)
77 {
78 }
79 ostream(ostream&& src__)
80 : std::ostringstream(std::move(src__))
81 {
82 out_ = src__.out_;
83 src__.out_ = nullptr;
84 prefix_ = src__.prefix_;
85 }
86 ~ostream()
87 {
88 if (out_) {
89 auto strings = split(this->str(), '\n');
90 for (size_t i = 0; i < strings.size(); i++) {
91 if (!(i == strings.size() - 1 && strings[i].size() == 0)) {
92 (*out_) << "[" << prefix_ << "] " << strings[i];
93 }
94 if (i != strings.size() - 1) {
95 (*out_) << std::endl;
96 }
97 }
98 }
99 }
100};
101
102#define FILE_LINE std::string(__FILE__) + ":" + std::to_string(__LINE__)
103
104#define RTE_THROW(...) \
105{\
106 ::sirius::rte::message_impl(true, __func__, __FILE__, __LINE__, __VA_ARGS__);\
107}
108
109#define RTE_WARNING(...) \
110{\
111 ::sirius::rte::message_impl(false, __func__, __FILE__, __LINE__, __VA_ARGS__);\
112}
113
114#ifdef NDEBUG
115#define RTE_ASSERT(condition__)
116#else
117#define RTE_ASSERT(condition__) \
118{ \
119 if (!(condition__)) { \
120 std::stringstream _s; \
121 _s << "Assertion (" << #condition__ << ") failed " \
122 << "at " << __FILE__ << ":" << __LINE__; \
123 RTE_THROW(_s); \
124 } \
125}
126#endif
127
128#define RTE_OUT(_out) rte::ostream(_out, std::string(__func__))
129
130} // rte
131
132} // sirius
133
134#endif
Namespace of the SIRIUS library.
Definition: sirius.f90:5
auto split(std::string const str__, char delim__)
Split multi-line string into a list of strings.
Extra functions to work with std::strings.