| 1 | /* melder_error.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2026 Paul Boersma |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "melder.h" |
| 20 | |
| 21 | /* global */ std::mutex theMelder_error_mutex; |
| 22 | |
| 23 | static void defaultErrorProc (conststring32 message) { |
| 24 | MelderConsole::write (str32str (message, U"You interrupted ") ? U"User interrupt: " : U"Error: ", true); |
| 25 | MelderConsole::write (message, true); |
| 26 | MelderConsole::write (U"\n", true); |
| 27 | } |
| 28 | static void defaultCrashProc (conststring32 message) { |
| 29 | MelderConsole::write (U"Crashing bug: ", true); |
| 30 | MelderConsole::write (message, true); |
| 31 | MelderConsole::write (U"\n", true); |
| 32 | } |
| 33 | |
| 34 | static void (*p_theErrorProc) (conststring32 message) = & defaultErrorProc; // initial setting after start-up; will stay if program is run from batch |
| 35 | static void (*p_theCrashProc) (conststring32 message) = & defaultCrashProc; // initial setting after start-up; will stay if program is run from batch |
| 36 | |
| 37 | void Melder_setErrorProc (void (*p_errorProc) (conststring32)) { |
| 38 | p_theErrorProc = p_errorProc ? p_errorProc : & defaultErrorProc; |
| 39 | } |
| 40 | void Melder_setCrashProc (void (*p_crashProc) (conststring32)) { |
| 41 | p_theCrashProc = p_crashProc ? p_crashProc : & defaultCrashProc; |
| 42 | } |
| 43 | |
| 44 | constexpr integer BUFFER_LENGTH = 2000; |
| 45 | static char32 theErrorBuffer [BUFFER_LENGTH]; // safe in low-memory situations |
| 46 | |
| 47 | void MelderError__appendOneString (conststring32 message) { |
| 48 | if (! message) |
| 49 | return; |
| 50 | const integer length = Melder_length (theErrorBuffer), messageLength = Melder_length (message); |
| 51 | if (length + messageLength >= BUFFER_LENGTH) |
| 52 | return; |
| 53 | str32cpy (theErrorBuffer + length, message); |
| 54 | } |
| 55 | |
| 56 | bool Melder_hasError () { |
| 57 | return theErrorBuffer [0] != U'\0'; |
| 58 | } |
| 59 | |
| 60 | bool Melder_hasError (conststring32 partialError) { |
| 61 | return !! str32str (theErrorBuffer, partialError); |
| 62 | } |
| 63 | |
| 64 | #define CRASH_SEMAPHORE U" will crash. Please notify the authors (" |
| 65 | |
| 66 | static conststring32 crashMessage () { |
| 67 | static char32 crashMessageBuffer [1000]; |
| 68 | str32cpy (crashMessageBuffer, Melder_upperCaseAppName()); |
| 69 | str32cat (crashMessageBuffer, CRASH_SEMAPHORE); |
| 70 | str32cat (crashMessageBuffer, Melder_appContactAddress()); |
| 71 | str32cat (crashMessageBuffer, U") with all of the following information"); |
| 72 | str32cat (crashMessageBuffer, ( Melder_batch ? U"" : U", before closing this window" )); |
| 73 | str32cat (crashMessageBuffer, U" (and please mention in your email precisely" |
| 74 | " what you were doing when this crash occurred):\n\n"); |
| 75 | return crashMessageBuffer; |
| 76 | } |
| 77 | |
| 78 | bool Melder_hasCrash () { |
| 79 | const char32 *firstSpace = str32chr (theErrorBuffer, U' '); |
| 80 | if (! firstSpace) |
| 81 | return false; |
| 82 | return Melder_startsWith (firstSpace, CRASH_SEMAPHORE); |
| 83 | } |
| 84 | |
| 85 | void Melder_clearError () { |
| 86 | if (Melder_hasCrash ()) |
| 87 | throw MelderError (); |
| 88 | theErrorBuffer [0] = U'\0'; |
| 89 | } |
| 90 | |
| 91 | conststring32 Melder_getError () { |
| 92 | return & theErrorBuffer [0]; |
| 93 | } |
| 94 | |
| 95 | void Melder_appendError_noLine (const MelderArg& arg) { |
| 96 | std::lock_guard lock (theMelder_error_mutex); |
| 97 | if (Melder_hasError ()) { |
| 98 | if (Melder_thisThread_getUniqueID () != theMelder_error_threadId) |
| 99 | return; |
| 100 | } else { |
| 101 | theMelder_error_threadId = Melder_thisThread_getUniqueID (); |
| 102 | } |
| 103 | MelderError__appendOneString (arg._arg); |
| 104 | } |
| 105 | |
| 106 | void Melder_flushError () { |
| 107 | if (Melder_hasCrash ()) { |
| 108 | (*p_theCrashProc) (theErrorBuffer); |
| 109 | abort (); |
| 110 | } else { |
| 111 | /* |
| 112 | "errors" has to be cleared *before* the message is put on the screen. |
| 113 | This is because on some platforms the message dialog is synchronous |
| 114 | (Melder_flushError will wait until the message dialog is closed), |
| 115 | and some operating systems may force an immediate redraw event as soon as |
| 116 | the message dialog is closed. We want "errors" to be empty when redrawing! |
| 117 | */ |
| 118 | static char32 temp [BUFFER_LENGTH]; |
| 119 | str32cpy (temp, theErrorBuffer); |
| 120 | theErrorBuffer [0] = U'\0'; |
| 121 | (*p_theErrorProc) (temp); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static std::mutex theMelder_crash_mutex; // to guard against simultaneous crashes in multiple threads |
| 126 | |
| 127 | void _private_Melder_crash (const MelderArg& arg1, |
| 128 | const MelderArg& arg2, const MelderArg& arg3, const MelderArg& arg4, |
| 129 | const MelderArg& arg5, const MelderArg& arg6, const MelderArg& arg7, |
| 130 | const MelderArg& arg8, const MelderArg& arg9, const MelderArg& arg10, |
| 131 | const MelderArg& arg11, const MelderArg& arg12, const MelderArg& arg13) |
| 132 | { |
| 133 | std::lock_guard <std::mutex> lock (theMelder_crash_mutex); |
| 134 | MelderError__appendOneString (crashMessage ()); |
| 135 | MelderError__appendOneString (arg1. _arg ? arg1. _arg : U""); |
| 136 | MelderError__appendOneString (arg2. _arg ? arg2. _arg : U""); |
| 137 | MelderError__appendOneString (arg3. _arg ? arg3. _arg : U""); |
| 138 | MelderError__appendOneString (arg4. _arg ? arg4. _arg : U""); |
| 139 | MelderError__appendOneString (arg5. _arg ? arg5. _arg : U""); |
| 140 | MelderError__appendOneString (arg6. _arg ? arg6. _arg : U""); |
| 141 | MelderError__appendOneString (arg7. _arg ? arg7. _arg : U""); |
| 142 | MelderError__appendOneString (arg8. _arg ? arg8. _arg : U""); |
| 143 | MelderError__appendOneString (arg9. _arg ? arg9. _arg : U""); |
| 144 | MelderError__appendOneString (arg10._arg ? arg10._arg : U""); |
| 145 | MelderError__appendOneString (arg11._arg ? arg11._arg : U""); |
| 146 | MelderError__appendOneString (arg12._arg ? arg12._arg : U""); |
| 147 | MelderError__appendOneString (arg13._arg ? arg13._arg : U""); |
| 148 | MelderError__appendOneString (U"\n"); |
| 149 | trace (U"CRASH: ", theErrorBuffer); |
| 150 | throw MelderError (); |
| 151 | } |
| 152 | |
| 153 | static void _assert (const char *pathName, int lineNumber, const char *condition, conststring32 intro) { |
| 154 | /* |
| 155 | This function tries to make sure that it allocates no heap memory. |
| 156 | Hence, character conversion is done in place rather than with Melder_peek8to32_u(), |
| 157 | and Melder_integer() is also avoided. |
| 158 | */ |
| 159 | std::lock_guard <std::mutex> lock (theMelder_crash_mutex); // to guard against simultaneous crashes in multiple threads |
| 160 | static char32 pathNameBuffer [1000], conditionBuffer [1000], lineNumberBuffer [40]; |
| 161 | Melder_8to32_inplace_e (pathName, pathNameBuffer, kMelder_textInputEncoding::UTF8); |
| 162 | const char32 *p_lastFolderSeparator = str32rchr (pathNameBuffer, U'/'); |
| 163 | if (! p_lastFolderSeparator) |
| 164 | p_lastFolderSeparator = str32rchr (pathNameBuffer, U'\\'); |
| 165 | const conststring32 fileName = ( p_lastFolderSeparator ? p_lastFolderSeparator + 1 : pathNameBuffer ); |
| 166 | Melder_8to32_inplace_e (condition, conditionBuffer, kMelder_textInputEncoding::UTF8); |
| 167 | static char lineNumberBuffer8 [40]; |
| 168 | snprintf (lineNumberBuffer8,40, "%d", lineNumber); |
| 169 | Melder_8to32_inplace_e (lineNumberBuffer8, lineNumberBuffer, kMelder_textInputEncoding::UTF8); |
| 170 | MelderError__appendOneString (crashMessage ()); |
| 171 | MelderError__appendOneString (intro); |
| 172 | MelderError__appendOneString (Melder_appVersionSTR()); |
| 173 | MelderError__appendOneString (U" in file \""); |
| 174 | MelderError__appendOneString (fileName); |
| 175 | MelderError__appendOneString (U"\" at line "); |
| 176 | MelderError__appendOneString (lineNumberBuffer); |
| 177 | MelderError__appendOneString (U":\n "); |
| 178 | MelderError__appendOneString (conditionBuffer); |
| 179 | MelderError__appendOneString (U"\n\n"); |
| 180 | trace (U"CRASH: ", theErrorBuffer); |
| 181 | throw MelderError (); |
| 182 | } |
| 183 | |
| 184 | void _private_Melder_assert (const char *pathName, int lineNumber, const char *condition) { |
| 185 | _assert (pathName, lineNumber, condition, U"Assertion failed in Praat "); |
| 186 | } |
| 187 | void _private_Melder_pre (const char *pathName, int lineNumber, const char *condition) { |
| 188 | _assert (pathName, lineNumber, condition, U"Precondition failed in Praat "); |
| 189 | } |
| 190 | void _private_Melder_post (const char *pathName, int lineNumber, const char *condition) { |
| 191 | _assert (pathName, lineNumber, condition, U"Postcondition failed in Praat "); |
| 192 | } |
| 193 | |
| 194 | /* End of file melder_error.cpp */ |