| 1 | #ifndef _melder_error_h_ |
| 2 | #define _melder_error_h_ |
| 3 | /* melder_error.h |
| 4 | * |
| 5 | * Copyright (C) 1992-2021,2025 Paul Boersma |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 | * See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | #include <mutex> |
| 21 | |
| 22 | class MelderError { }; // empty exception class |
| 23 | |
| 24 | extern std::mutex theMelder_error_mutex; |
| 25 | extern std::atomic <integer> theMelder_error_threadId; |
| 26 | |
| 27 | bool Melder_hasError (); |
| 28 | bool Melder_hasError (conststring32 partialError); |
| 29 | /* Returns true if there is an error message in store, otherwise false. */ |
| 30 | bool Melder_hasCrash (); |
| 31 | /* Returns true if there is an error message in store, and that error message is a crash message. */ |
| 32 | |
| 33 | void Melder_clearError (); |
| 34 | /* Cancel all stored error messages. */ |
| 35 | |
| 36 | conststring32 Melder_getError (); |
| 37 | /* Returns the error string. Mainly used with str32str. */ |
| 38 | |
| 39 | void Melder_appendError_noLine (const MelderArg& arg1); |
| 40 | |
| 41 | /** |
| 42 | The usual error reporting function is Melder_throw. However, |
| 43 | you may sometimes want to prepend other messages before Melder_throw, |
| 44 | without jumping away from the error-generating location. |
| 45 | In such a special case you can use Melder_appendError. |
| 46 | |
| 47 | Melder_appendError() has to be followed by one of these: |
| 48 | - Melder_throw() (or just `throw`) to prepend the error to a normal exception; |
| 49 | - Melder_flushError() to show the error in the GUI |
| 50 | (this is where a trail of Melder_throw will usually end up as well); |
| 51 | - Melder_clearError() to ignore the error. |
| 52 | |
| 53 | If you don't do this, the error will linger in your error buffer until |
| 54 | the next, probably unrelated, error is generated, |
| 55 | and your prepended error text will be shown to the user out of context, |
| 56 | which is wrong. |
| 57 | */ |
| 58 | |
| 59 | void MelderError__appendOneString (conststring32 message); |
| 60 | |
| 61 | template <typename... Arg> |
| 62 | void Melder_appendError (const Arg... arg) { |
| 63 | std::lock_guard lock (theMelder_error_mutex); |
| 64 | if (Melder_hasError ()) { |
| 65 | if (Melder_thisThread_getUniqueID () != theMelder_error_threadId) |
| 66 | return; |
| 67 | } else { |
| 68 | theMelder_error_threadId = Melder_thisThread_getUniqueID (); |
| 69 | } |
| 70 | (// fold |
| 71 | MelderError__appendOneString (MelderArg { arg }. _arg) |
| 72 | , ... |
| 73 | ); |
| 74 | MelderError__appendOneString (U"\n"); |
| 75 | } |
| 76 | |
| 77 | #define Melder_throw(...) do { Melder_appendError (__VA_ARGS__); throw MelderError (); } while (false) |
| 78 | |
| 79 | void Melder_flushError (); |
| 80 | |
| 81 | template <typename... Arg> |
| 82 | void Melder_flushError (const Arg... arg) { |
| 83 | Melder_appendError (arg...); |
| 84 | Melder_flushError (); |
| 85 | } |
| 86 | /* Send all deferred error messages to stderr (batch) or to an "Error" dialog, */ |
| 87 | /* including, if there are arguments, the error message generated by this function. */ |
| 88 | |
| 89 | void Melder_setErrorProc (void (*p_errorProc) (conststring32)); |
| 90 | |
| 91 | void _private_Melder_crash (const MelderArg&, |
| 92 | const MelderArg& = U"", const MelderArg& = U"", const MelderArg& = U"", |
| 93 | const MelderArg& = U"", const MelderArg& = U"", const MelderArg& = U"", |
| 94 | const MelderArg& = U"", const MelderArg& = U"", const MelderArg& = U"", |
| 95 | const MelderArg& = U"", const MelderArg& = U"", const MelderArg& = U"" |
| 96 | ); |
| 97 | |
| 98 | #define Melder_crash(...) do { _private_Melder_crash (__VA_ARGS__); abort (); } while (0) |
| 99 | |
| 100 | void Melder_setCrashProc (void (*p_crashProc) (conststring32)); |
| 101 | |
| 102 | /* End of file melder_error.h */ |
| 103 | #endif |