1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
/* Copyright (c) 2008-2022 the MRtrix3 contributors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Covered Software is provided under this License on an "as is"
* basis, without warranty of any kind, either expressed, implied, or
* statutory, including, without limitation, warranties that the
* Covered Software is free of defects, merchantable, fit for a
* particular purpose or non-infringing.
* See the Mozilla Public License v. 2.0 for more details.
*
* For more details, see http://www.mrtrix.org/.
*/
#ifndef __mrtrix_exception_h__
#define __mrtrix_exception_h__
#include <cerrno>
#include <iostream>
#include <string>
#include "types.h"
#ifdef MRTRIX_AS_R_LIBRARY
# include "wrap_r.h"
#endif
namespace MR
{
namespace App
{
extern int log_level;
extern int exit_error_code;
}
//! print primary output to stdout as-is.
/*! This function is intended for cases where the command's primary output is text, not
* image data, etc. It is \e not designed for error or status reports: it
* prints to stdout, whereas all reporting functions print to stderr. This is
* to allow the output of the command to be used directly in text processing
* pipeline or redirected to file.
* \note the use of stdout is normally reserved for piping data files (or at
* least their filenames) between MRtrix commands. This function should
* therefore never be used in commands that produce output images, as the two
* different types of output may then interfere and cause unexpected issues. */
extern void (*print) (const std::string& msg);
//! \cond skip
// for internal use only
inline void __print_stderr (const std::string& text)
{
#ifdef MRTRIX_AS_R_LIBRARY
REprintf (text.c_str());
#else
std::cerr << text;
#endif
}
//! \endcond
//! display error, warning, debug, etc. message to user
/*! types are: 0: error; 1: warning; 2: additional information; 3:
* debugging information; anything else: none. */
extern void (*report_to_user_func) (const std::string& msg, int type);
#define CONSOLE(msg) if (MR::App::log_level >= 1) report_to_user_func (msg, -1)
#define FAIL(msg) if (MR::App::log_level >= 0) report_to_user_func (msg, 0)
#define WARN(msg) if (MR::App::log_level >= 1) report_to_user_func (msg, 1)
#define INFO(msg) if (MR::App::log_level >= 2) report_to_user_func (msg, 2)
#define DEBUG(msg) if (MR::App::log_level >= 3) report_to_user_func (msg, 3)
class Exception { NOMEMALIGN
public:
Exception () { }
Exception (const std::string& msg) {
description.push_back (msg);
}
Exception (const Exception& previous_exception, const std::string& msg) :
description (previous_exception.description) {
description.push_back (msg);
}
void display (int log_level = 0) const {
display_func (*this, log_level);
}
size_t num () const {
return description.size();
}
const std::string& operator[] (size_t n) const {
return description[n];
}
void push_back (const std::string& s) {
description.push_back (s);
}
void push_back (const Exception& e) {
for (auto s : e.description)
push_back (s);
}
static void (*display_func) (const Exception& E, int log_level);
vector<std::string> description;
};
class InvalidImageException : public Exception { NOMEMALIGN
public:
InvalidImageException (const std::string& msg) : Exception(msg) {}
InvalidImageException (const Exception& previous_exception, const std::string& msg)
: Exception(previous_exception, msg) {}
};
class CancelException : public Exception { NOMEMALIGN
public:
CancelException () : Exception ("operation cancelled by user") { }
};
void display_exception_cmdline (const Exception& E, int log_level);
void cmdline_print_func (const std::string& msg);
void cmdline_report_to_user_func (const std::string& msg, int type);
class LogLevelLatch { NOMEMALIGN
public:
LogLevelLatch (const int new_level) :
prev_level (App::log_level)
{
App::log_level = new_level;
}
~LogLevelLatch () {
App::log_level = prev_level;
}
private:
const int prev_level;
};
void check_app_exit_code();
}
#endif
|