Developer documentation
Version 3.0.3-105-gd3941f44
exception.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __mrtrix_exception_h__
18#define __mrtrix_exception_h__
19
20#include <cerrno>
21#include <iostream>
22#include <string>
23
24#include "types.h"
25
26#ifdef MRTRIX_AS_R_LIBRARY
27# include "wrap_r.h"
28#endif
29
30namespace MR
31{
32 namespace App
33 {
34 extern int log_level;
35 extern int exit_error_code;
36 }
37
39
48 extern void (*print) (const std::string& msg);
49
50
51
53
54 // for internal use only
55
56 inline void __print_stderr (const std::string& text)
57 {
58#ifdef MRTRIX_AS_R_LIBRARY
59 REprintf (text.c_str());
60#else
61 std::cerr << text;
62#endif
63 }
65
67
69 extern void (*report_to_user_func) (const std::string& msg, int type);
70
71#define CONSOLE(msg) if (MR::App::log_level >= 1) report_to_user_func (msg, -1)
72#define FAIL(msg) if (MR::App::log_level >= 0) report_to_user_func (msg, 0)
73#define WARN(msg) if (MR::App::log_level >= 1) report_to_user_func (msg, 1)
74#define INFO(msg) if (MR::App::log_level >= 2) report_to_user_func (msg, 2)
75#define DEBUG(msg) if (MR::App::log_level >= 3) report_to_user_func (msg, 3)
76
77
78
79
81 public:
82 Exception () { }
83
84 Exception (const std::string& msg) {
85 description.push_back (msg);
86 }
87 Exception (const Exception& previous_exception, const std::string& msg) :
88 description (previous_exception.description) {
89 description.push_back (msg);
90 }
91
92 void display (int log_level = 0) const {
93 display_func (*this, log_level);
94 }
95
96 size_t num () const {
97 return description.size();
98 }
99 const std::string& operator[] (size_t n) const {
100 return description[n];
101 }
102 void push_back (const std::string& s) {
103 description.push_back (s);
104 }
105 void push_back (const Exception& e) {
106 for (auto s : e.description)
107 push_back (s);
108 }
109
110 static void (*display_func) (const Exception& E, int log_level);
111
113 };
114
116 public:
117 InvalidImageException (const std::string& msg) : Exception(msg) {}
118 InvalidImageException (const Exception& previous_exception, const std::string& msg)
119 : Exception(previous_exception, msg) {}
120 };
121
122
124 public:
125 CancelException () : Exception ("operation cancelled by user") { }
126 };
127
129 void cmdline_print_func (const std::string& msg);
130 void cmdline_report_to_user_func (const std::string& msg, int type);
131
132
133
135 public:
136 LogLevelLatch (const int new_level) :
137 prev_level (App::log_level)
138 {
139 App::log_level = new_level;
140 }
142 App::log_level = prev_level;
143 }
144 private:
145 const int prev_level;
146 };
147
148
150
151
152}
153
154#endif
155
void push_back(const std::string &s)
Definition: exception.h:102
vector< std::string > description
Definition: exception.h:112
const std::string & operator[](size_t n) const
Definition: exception.h:99
void display(int log_level=0) const
Definition: exception.h:92
Exception(const Exception &previous_exception, const std::string &msg)
Definition: exception.h:87
size_t num() const
Definition: exception.h:96
static void(* display_func)(const Exception &E, int log_level)
Definition: exception.h:110
void push_back(const Exception &e)
Definition: exception.h:105
Exception(const std::string &msg)
Definition: exception.h:84
InvalidImageException(const std::string &msg)
Definition: exception.h:117
InvalidImageException(const Exception &previous_exception, const std::string &msg)
Definition: exception.h:118
LogLevelLatch(const int new_level)
Definition: exception.h:136
constexpr double e
Definition: math.h:39
#define NOMEMALIGN
Definition: memory.h:22
int log_level
Definition: exception.h:34
int exit_error_code
Definition: exception.h:35
Definition: base.h:24
void cmdline_print_func(const std::string &msg)
void(* report_to_user_func)(const std::string &msg, int type)
display error, warning, debug, etc. message to user
void(* print)(const std::string &msg)
print primary output to stdout as-is.
void display_exception_cmdline(const Exception &E, int log_level)
void check_app_exit_code()
void cmdline_report_to_user_func(const std::string &msg, int type)