| 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"cxx . . $ `<file>/melder/melder.h`/ |
| 20 | |
| 21 | /* global */ stdcxx . . $ std/::mutexcxx . . $ std/mutex# theMelder_error_mutexcxx . . $ theMelder_error_mutex.; |
| 22 | |
| 23 | static void defaultErrorProccxx . . $ defaultErrorProc(ff00d524ab0355ec). (conststring32cxx . . $ conststring32# message) { |
| 24 | MelderConsolecxx . . $ MelderConsole/::writecxx . . $ MelderConsole/write(7004b68cc6c23ea2). (str32strcxx . . $ str32str(2de17cd8f7c584a6). (message, U"You interrupted ") ? U"User interrupt: " : U"Error: ", true); |
| 25 | MelderConsolecxx . . $ MelderConsole/::writecxx . . $ MelderConsole/write(7004b68cc6c23ea2). (message, true); |
| 26 | MelderConsolecxx . . $ MelderConsole/::writecxx . . $ MelderConsole/write(7004b68cc6c23ea2). (U"\n", true); |
| 27 | } |
| 28 | static void defaultCrashProccxx . . $ defaultCrashProc(ff00d524ab0355ec). (conststring32cxx . . $ conststring32# message) { |
| 29 | MelderConsolecxx . . $ MelderConsole/::writecxx . . $ MelderConsole/write(7004b68cc6c23ea2). (U"Crashing bug: ", true); |
| 30 | MelderConsolecxx . . $ MelderConsole/::writecxx . . $ MelderConsole/write(7004b68cc6c23ea2). (message, true); |
| 31 | MelderConsolecxx . . $ MelderConsole/::writecxx . . $ MelderConsole/write(7004b68cc6c23ea2). (U"\n", true); |
| 32 | } |
| 33 | |
| 34 | static void (*p_theErrorProccxx . . $ p_theErrorProc.) (conststring32cxx . . $ conststring32# message) = & defaultErrorProccxx . . $ defaultErrorProc(ff00d524ab0355ec).; // initial setting after start-up; will stay if program is run from batch |
| 35 | static void (*p_theCrashProccxx . . $ p_theCrashProc.) (conststring32cxx . . $ conststring32# message) = & defaultCrashProccxx . . $ defaultCrashProc(ff00d524ab0355ec).; // initial setting after start-up; will stay if program is run from batch |
| 36 | |
| 37 | void Melder_setErrorProccxx . . $ Melder_setErrorProc(f441b548652834d9). (void (*p_errorProc) (conststring32cxx . . $ conststring32#)) { |
| 38 | p_theErrorProccxx . . $ p_theErrorProc. = p_errorProc ? p_errorProc : & defaultErrorProccxx . . $ defaultErrorProc(ff00d524ab0355ec).; |
| 39 | } |
| 40 | void Melder_setCrashProccxx . . $ Melder_setCrashProc(f441b548652834d9). (void (*p_crashProc) (conststring32cxx . . $ conststring32#)) { |
| 41 | p_theCrashProccxx . . $ p_theCrashProc. = p_crashProc ? p_crashProc : & defaultCrashProccxx . . $ defaultCrashProc(ff00d524ab0355ec).; |
| 42 | } |
| 43 | |
| 44 | constexpr integercxx . . $ integer# BUFFER_LENGTHcxx . . $ BUFFER_LENGTH. = 2000; |
| 45 | static char32cxx . . $ char32# theErrorBuffercxx . . $ theErrorBuffer. [BUFFER_LENGTHcxx . . $ BUFFER_LENGTH.]; // safe in low-memory situations |
| 46 | |
| 47 | void MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (conststring32cxx . . $ conststring32# message) { |
| 48 | if (! message) |
| 49 | return; |
| 50 | const integercxx . . $ integer# length = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (theErrorBuffercxx . . $ theErrorBuffer.), messageLength = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (message); |
| 51 | if (length + messageLength >= BUFFER_LENGTHcxx . . $ BUFFER_LENGTH.) |
| 52 | return; |
| 53 | str32cpycxx . . $ str32cpy(6ab934322573b179). (theErrorBuffercxx . . $ theErrorBuffer. + length, message); |
| 54 | } |
| 55 | |
| 56 | bool Melder_hasErrorcxx . . $ Melder_hasError(c9857a73d68e59ba). () { |
| 57 | return theErrorBuffercxx . . $ theErrorBuffer. [0] != U'\0'; |
| 58 | } |
| 59 | |
| 60 | bool Melder_hasErrorcxx . . $ Melder_hasError(2df281243f91fe91). (conststring32cxx . . $ conststring32# partialError) { |
| 61 | return !! str32strcxx . . $ str32str(2de17cd8f7c584a6). (theErrorBuffercxx . . $ theErrorBuffer., partialError); |
| 62 | } |
| 63 | |
| 64 | #define CRASH_SEMAPHOREcxx . . $ `melder/melder_error.cpp:64:9`! U" will crash. Please notify the authors (" |
| 65 | |
| 66 | static conststring32cxx . . $ conststring32# crashMessagecxx . . $ crashMessage(1912f6684645c851). () { |
| 67 | static char32cxx . . $ char32# crashMessageBuffer [1000]; |
| 68 | str32cpycxx . . $ str32cpy(6ab934322573b179). (crashMessageBuffer, Melder_upperCaseAppNamecxx . . $ Melder_upperCaseAppName(1912f6684645c851).()); |
| 69 | str32catcxx . . $ str32cat(6ab934322573b179). (crashMessageBuffer, CRASH_SEMAPHOREcxx . . $ `melder/melder_error.cpp:64:9`!); |
| 70 | str32catcxx . . $ str32cat(6ab934322573b179). (crashMessageBuffer, Melder_appContactAddresscxx . . $ Melder_appContactAddress(1912f6684645c851).()); |
| 71 | str32catcxx . . $ str32cat(6ab934322573b179). (crashMessageBuffer, U") with all of the following information"); |
| 72 | str32catcxx . . $ str32cat(6ab934322573b179). (crashMessageBuffer, ( Melder_batchcxx . . $ Melder_batch. ? U"" : U", before closing this window" )); |
| 73 | str32catcxx . . $ str32cat(6ab934322573b179). (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_hasCrashcxx . . $ Melder_hasCrash(c9857a73d68e59ba). () { |
| 79 | const char32cxx . . $ char32# *firstSpace = str32chrcxx . . $ str32chr(328a9ede0fba853a). (theErrorBuffercxx . . $ theErrorBuffer., U' '); |
| 80 | if (! firstSpace) |
| 81 | return false; |
| 82 | return Melder_startsWithcxx . . $ Melder_startsWith(1652f10ed0f82783). (firstSpace, CRASH_SEMAPHOREcxx . . $ `melder/melder_error.cpp:64:9`!); |
| 83 | } |
| 84 | |
| 85 | void Melder_clearErrorcxx . . $ Melder_clearError(49f6e7a06ebc5aa8). () { |
| 86 | if (Melder_hasCrashcxx . . $ Melder_hasCrash(c9857a73d68e59ba). ()) |
| 87 | throw MelderErrorcxx . . $ MelderError# (); |
| 88 | theErrorBuffercxx . . $ theErrorBuffer. [0] = U'\0'; |
| 89 | } |
| 90 | |
| 91 | conststring32cxx . . $ conststring32# Melder_getErrorcxx . . $ Melder_getError(1912f6684645c851). () { |
| 92 | return & theErrorBuffercxx . . $ theErrorBuffer. [0]; |
| 93 | } |
| 94 | |
| 95 | void Melder_appendError_noLinecxx . . $ Melder_appendError_noLine(afb30bc799649a62). (const MelderArgcxx . . $ MelderArg#& arg) { |
| 96 | stdcxx . . $ std/::lock_guard lockcxx . . $ std/lock_guard#lock_guard(8e935d9c2e576e3a). (theMelder_error_mutexcxx . . $ theMelder_error_mutex.); |
| 97 | if (Melder_hasErrorcxx . . $ Melder_hasError(c9857a73d68e59ba). ()) { |
| 98 | if (Melder_thisThread_getUniqueIDcxx . . $ Melder_thisThread_getUniqueID(da6335ded91d79c2). () != theMelder_error_threadIdcxx . . $ theMelder_error_threadId.) |
| 99 | return; |
| 100 | } else { |
| 101 | theMelder_error_threadIdcxx . . $ theMelder_error_threadId. =cxx . . $ std/__atomic_base#`operator=`(73de6b30e1938209). Melder_thisThread_getUniqueIDcxx . . $ Melder_thisThread_getUniqueID(da6335ded91d79c2). (); |
| 102 | } |
| 103 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (arg._argcxx . . $ MelderArg#_arg.); |
| 104 | } |
| 105 | |
| 106 | void Melder_flushErrorcxx . . $ Melder_flushError(49f6e7a06ebc5aa8). () { |
| 107 | if (Melder_hasCrashcxx . . $ Melder_hasCrash(c9857a73d68e59ba). ()) { |
| 108 | (*p_theCrashProccxx . . $ p_theCrashProc.) (theErrorBuffercxx . . $ theErrorBuffer.); |
| 109 | abortcxx . . $ abort(15d318a79ddce9e0). (); |
| 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 char32cxx . . $ char32# temp [BUFFER_LENGTHcxx . . $ BUFFER_LENGTH.]; |
| 119 | str32cpycxx . . $ str32cpy(6ab934322573b179). (temp, theErrorBuffercxx . . $ theErrorBuffer.); |
| 120 | theErrorBuffercxx . . $ theErrorBuffer. [0] = U'\0'; |
| 121 | (*p_theErrorProccxx . . $ p_theErrorProc.) (temp); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static stdcxx . . $ std/::mutexcxx . . $ std/mutex# theMelder_crash_mutexcxx . . $ theMelder_crash_mutex.; // to guard against simultaneous crashes in multiple threads |
| 126 | |
| 127 | void _private_Melder_crashcxx . . $ _private_Melder_crash(6658699d5eee445d). (const MelderArgcxx . . $ MelderArg#& arg1, |
| 128 | const MelderArgcxx . . $ MelderArg#& arg2, const MelderArgcxx . . $ MelderArg#& arg3, const MelderArgcxx . . $ MelderArg#& arg4, |
| 129 | const MelderArgcxx . . $ MelderArg#& arg5, const MelderArgcxx . . $ MelderArg#& arg6, const MelderArgcxx . . $ MelderArg#& arg7, |
| 130 | const MelderArgcxx . . $ MelderArg#& arg8, const MelderArgcxx . . $ MelderArg#& arg9, const MelderArgcxx . . $ MelderArg#& arg10, |
| 131 | const MelderArgcxx . . $ MelderArg#& arg11, const MelderArgcxx . . $ MelderArg#& arg12, const MelderArgcxx . . $ MelderArg#& arg13) |
| 132 | { |
| 133 | stdcxx . . $ std/::lock_guardcxx . . $ std/lock_guard# <stdcxx . . $ std/::mutexcxx . . $ std/mutex#> lockcxx . . $ std/lock_guard#lock_guard(8e935d9c2e576e3a). (theMelder_crash_mutexcxx . . $ theMelder_crash_mutex.); |
| 134 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (crashMessagecxx . . $ crashMessage(1912f6684645c851). ()); |
| 135 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (arg1. _argcxx . . $ MelderArg#_arg. ? arg1. _argcxx . . $ MelderArg#_arg. : U""); |
| 136 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (arg2. _argcxx . . $ MelderArg#_arg. ? arg2. _argcxx . . $ MelderArg#_arg. : U""); |
| 137 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (arg3. _argcxx . . $ MelderArg#_arg. ? arg3. _argcxx . . $ MelderArg#_arg. : U""); |
| 138 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (arg4. _argcxx . . $ MelderArg#_arg. ? arg4. _argcxx . . $ MelderArg#_arg. : U""); |
| 139 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (arg5. _argcxx . . $ MelderArg#_arg. ? arg5. _argcxx . . $ MelderArg#_arg. : U""); |
| 140 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (arg6. _argcxx . . $ MelderArg#_arg. ? arg6. _argcxx . . $ MelderArg#_arg. : U""); |
| 141 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (arg7. _argcxx . . $ MelderArg#_arg. ? arg7. _argcxx . . $ MelderArg#_arg. : U""); |
| 142 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (arg8. _argcxx . . $ MelderArg#_arg. ? arg8. _argcxx . . $ MelderArg#_arg. : U""); |
| 143 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (arg9. _argcxx . . $ MelderArg#_arg. ? arg9. _argcxx . . $ MelderArg#_arg. : U""); |
| 144 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (arg10._argcxx . . $ MelderArg#_arg. ? arg10._argcxx . . $ MelderArg#_arg. : U""); |
| 145 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (arg11._argcxx . . $ MelderArg#_arg. ? arg11._argcxx . . $ MelderArg#_arg. : U""); |
| 146 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (arg12._argcxx . . $ MelderArg#_arg. ? arg12._argcxx . . $ MelderArg#_arg. : U""); |
| 147 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (arg13._argcxx . . $ MelderArg#_arg. ? arg13._argcxx . . $ MelderArg#_arg. : U""); |
| 148 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (U"\n"); |
| 149 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"CRASH: ", theErrorBuffer); |
| 150 | throw MelderErrorcxx . . $ MelderError# (); |
| 151 | } |
| 152 | |
| 153 | static void _assertcxx . . $ _assert(483eeb0b41699f56). (const char *pathName, int lineNumber, const char *condition, conststring32cxx . . $ 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 | stdcxx . . $ std/::lock_guardcxx . . $ std/lock_guard# <stdcxx . . $ std/::mutexcxx . . $ std/mutex#> lockcxx . . $ std/lock_guard#lock_guard(8e935d9c2e576e3a). (theMelder_crash_mutexcxx . . $ theMelder_crash_mutex.); // to guard against simultaneous crashes in multiple threads |
| 160 | static char32cxx . . $ char32# pathNameBuffer [1000], conditionBuffer [1000], lineNumberBuffer [40]; |
| 161 | Melder_8to32_inplace_ecxx . . $ Melder_8to32_inplace_e(b9d82cb7d165907c). (pathName, pathNameBuffer, kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::UTF8cxx . . $ kMelder_textInputEncoding#UTF8.); |
| 162 | const char32cxx . . $ char32# *p_lastFolderSeparator = str32rchrcxx . . $ str32rchr(328a9ede0fba853a). (pathNameBuffer, U'/'); |
| 163 | if (! p_lastFolderSeparator) |
| 164 | p_lastFolderSeparator = str32rchrcxx . . $ str32rchr(328a9ede0fba853a). (pathNameBuffer, U'\\'); |
| 165 | const conststring32cxx . . $ conststring32# fileName = ( p_lastFolderSeparator ? p_lastFolderSeparator + 1 : pathNameBuffer ); |
| 166 | Melder_8to32_inplace_ecxx . . $ Melder_8to32_inplace_e(b9d82cb7d165907c). (condition, conditionBuffer, kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::UTF8cxx . . $ kMelder_textInputEncoding#UTF8.); |
| 167 | static char lineNumberBuffer8 [40]; |
| 168 | snprintf (lineNumberBuffer8,40, "%d", lineNumber); |
| 169 | Melder_8to32_inplace_ecxx . . $ Melder_8to32_inplace_e(b9d82cb7d165907c). (lineNumberBuffer8, lineNumberBuffer, kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::UTF8cxx . . $ kMelder_textInputEncoding#UTF8.); |
| 170 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (crashMessagecxx . . $ crashMessage(1912f6684645c851). ()); |
| 171 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (intro); |
| 172 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (Melder_appVersionSTRcxx . . $ Melder_appVersionSTR(1912f6684645c851).()); |
| 173 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (U" in file \""); |
| 174 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (fileName); |
| 175 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (U"\" at line "); |
| 176 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (lineNumberBuffer); |
| 177 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (U":\n "); |
| 178 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (conditionBuffer); |
| 179 | MelderError__appendOneStringcxx . . $ MelderError__appendOneString(ff00d524ab0355ec). (U"\n\n"); |
| 180 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"CRASH: ", theErrorBuffer); |
| 181 | throw MelderErrorcxx . . $ MelderError# (); |
| 182 | } |
| 183 | |
| 184 | void _private_Melder_assertcxx . . $ _private_Melder_assert(1e8c11a11d48e725). (const char *pathName, int lineNumber, const char *condition) { |
| 185 | _assertcxx . . $ _assert(483eeb0b41699f56). (pathName, lineNumber, condition, U"Assertion failed in Praat "); |
| 186 | } |
| 187 | void _private_Melder_precxx . . $ _private_Melder_pre(1e8c11a11d48e725). (const char *pathName, int lineNumber, const char *condition) { |
| 188 | _assertcxx . . $ _assert(483eeb0b41699f56). (pathName, lineNumber, condition, U"Precondition failed in Praat "); |
| 189 | } |
| 190 | void _private_Melder_postcxx . . $ _private_Melder_post(1e8c11a11d48e725). (const char *pathName, int lineNumber, const char *condition) { |
| 191 | _assertcxx . . $ _assert(483eeb0b41699f56). (pathName, lineNumber, condition, U"Postcondition failed in Praat "); |
| 192 | } |
| 193 | |
| 194 | /* End of file melder_error.cpp */ |