Debian Codegraph @

melder/melder_debug.cpp

raw symbols SCIP
1 /* melder_debug.cpp
2 *
3 * Copyright (C) 2000-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 #ifdef linuxcxx . . $ `<built-in>:480:9`!
21 #include "../sys/GuiP.h"cxx . . $ `<file>/sys/GuiP.h`/
22 #endif
23 #include <time.h>
24 #ifdef _WIN32
25 #include "../kar/UnicodeData.h"
26 #include <windows.h>
27 #endif
28
29 /* global */ stdcxx . . $ std/::mutexcxx . . $ std/mutex# theMelder_trace_mutexcxx . . $ theMelder_trace_mutex.;
30
31 /*
32 Melder_debug will always be set to 0 when Praat starts up.
33 If Melder_debug is temporarily set to the following values
34 (preferably with the "Debug..." command under Praat->Technical,
35 which you can use from a script),
36 the behaviour of Praat will temporarily change in the following ways:
37
38 1: Windows: use C-clock instead of multimedia-clock in melder_audio.cpp.
39 2: Windows: always reset waveOut, even when played to end, in melder_audio.cpp.
40 3: Windows: reset waveOut if unprepareHeader fails, and retry, in melder_audio.cpp. STAY: 20010214
41 4: Windows: fewer callbacks during sound play, in melder_audio.cpp. STAY: 20010214
42 6: Windows: info on metafile properties in Picture.cpp.
43 8: Windows: don't reset waveIn, in SoundRecorder.cpp.
44 9: flush Error in FunctionEditor_Sound_draw
45 10: geometric pens
46 11: clicked item in option menu in Ui.cpp
47 14: switches off the progress window in melder.cpp
48 15: don't use TrueType IPA fonts, but always bitmaps instead
49 16: Linux: open /dev/dsp without O_NDELAY
50 17: debugging on in Formula.cpp
51 18: no endian assumptions in abcio.cpp
52 19: show path name in UiOutfile_do
53 20: trace PortAudio
54 21: Mac: list supported document formats when printing
55 22: UTF-8 tests in logo
56 23: recognize special chunks in WAV files
57 24: measure buttons in drawing area
58 25: read crooked Manipulation files (January 2008)
59 26: force OT-GLA
60 27: force HG-GLA
61 28: don't use GSL in NUMfisherQ
62 29: use GSL in NUMinvFisherQ
63 30: pitch path finder: use octave jump cost across voiceless parts
64 31: Pitch analysis: formant pulling on
65 32: show info on file names in ExperimentMFC
66 33: trace the Pitch path finder
67 34: trace memory allocation and deallocation
68 35: debugging on for QuickTime movie file opening
69 37: debug autoThing
70 39: debug autostring
71 40: debug Thing_new
72 41: OTGrammar_getWinner: always first choice rather than random choice
73 42: OTGrammar_getWinner: always last choice rather than random choice
74 43: trace class table initialization
75 44: trace Collection
76 45: tracing structMatrix :: read ()
77 46: trace GTK parent sizes in _GuiObject_position ()
78 47: force resampling in OTGrammar RIP
79 48: compute sum, mean, stdev with naive implementation in double (64 bits)
80 49: compute sum, mean, stdev with naive implementation in longdouble (80 bits on Intel64)
81 50: compute sum, mean, stdev with first-element offset (80 bits on Intel64)
82 51: compute sum, mean, stdev with two cycles, as in R (80 bits on Intel64)
83 (other numbers than 48-51: compute sum, mean, stdev with simple pairwise algorithm, base case 64 [80 bits on Intel64])
84 52: debug Discriminant_TableOfReal_to_ClassificationTable
85 53: trace running cursor
86 54: ignore gdk_cairo_reset_clip
87 55: trace Gui init, draw, destroy
88 56: trace text styles
89 57: no parabolic interpolation in Sound_Pitch_to_PointProcess_cc (March 2024)
90 58: list font replacements at start-up (December 2025)
91 181: read and write native-endian real64
92 900: use DG Meta Serif Science instead of Palatino
93 1264: Mac: Sound_record_fixedTime uses microphone "FW Solo (1264)"
94 2001: trace timing for whisper.cpp
95 2002: use greedy sampling strategy for whisper.cpp (instead of the beam search)
96 2004: trace timing for diarization
97
98 (negative values are for David)
99
100 */
101
102 /*
103 In order to make sure that Melder_casual() and trace() can be called from anywhere,
104 including e.g. from Melder_realloc() or Melder_free(),
105 they cannot use any Melder_xxx() functions.
106 */
107
108 /*
109 peek32to8 substitutes for Melder_peek32to8(),
110 which can call Melder_realloc() and Melder_free();
111 also, we need no newline nativization, as Melder_32to8_inplace() does.
112 */
113 conststring8cxx . . $ conststring8# MelderTracecxx . . $ MelderTrace/::_peek32to8cxx . . $ MelderTrace/_peek32to8(c6a971952cbb8e31). (conststring32cxx . . $ conststring32# string) {
114 if (! string)
115 return "";
116 static char *buffer { nullptr };
117 static int64cxx . . $ int64# bufferSize { 0 };
118 const int64cxx . . $ int64# n = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (string);
119 int64cxx . . $ int64# sizeNeeded = n * 4 + 1;
120 if ((bufferSize - sizeNeeded) * (int64cxx . . $ int64#) sizeof (char) >= 10'000) {
121 freecxx . . $ free(bd21765a0afc8e3c). (buffer);
122 buffer = nullptr; // undangle
123 bufferSize = 0;
124 }
125 if (sizeNeeded > bufferSize) {
126 sizeNeeded = (int64cxx . . $ int64#) floorcxx . . $ floor(fee4f1d22bc8e617). (sizeNeeded * 1.61803) + 100;
127 buffer = (char *) realloccxx . . $ realloc(42b2a9dd8f084bf2). (buffer, (size_t) sizeNeeded * sizeof (char));
128 if (buffer == nullptr) {
129 bufferSize = 0;
130 return "(out of memory during tracing)";
131 }
132 bufferSize = sizeNeeded;
133 }
134 int64cxx . . $ int64# i, j;
135 for (i = 0, j = 0; i < n; i ++) {
136 char32cxx . . $ char32# kar = string [i];
137 if (kar <= 0x00'007F) { // 7 bits
138 buffer [j ++] = (char) (char8cxx . . $ char8#) kar; // guarded truncation
139 } else if (kar <= 0x00'07FF) { // 11 bits
140 buffer [j ++] = (char) (char8cxx . . $ char8#) (0x00'00C0 | (kar >> 6)); // the upper 5 bits yield a number between 0xC4 and 0xDF
141 buffer [j ++] = (char) (char8cxx . . $ char8#) (0x00'0080 | (kar & 0x00'003F)); // the lower 6 bits yield a number between 0x80 and 0xBF
142 } else if (kar <= 0x00'FFFF) { // 16 bits
143 buffer [j ++] = (char) (char8cxx . . $ char8#) (0x00'00E0 | (kar >> 12)); // the upper 4 bits yield a number between 0xE0 and 0xEF
144 buffer [j ++] = (char) (char8cxx . . $ char8#) (0x00'0080 | ((kar >> 6) & 0x00'003F));
145 buffer [j ++] = (char) (char8cxx . . $ char8#) (0x00'0080 | (kar & 0x00'003F));
146 } else { // 21 bits
147 buffer [j ++] = (char) (char8cxx . . $ char8#) (0x00'00F0 | (kar >> 18)); // the upper 3 bits yield a number between 0xF0 and 0xF4 (0x10FFFF >> 18 == 4)
148 buffer [j ++] = (char) (char8cxx . . $ char8#) (0x00'0080 | ((kar >> 12) & 0x00'003F)); // the next 6 bits
149 buffer [j ++] = (char) (char8cxx . . $ char8#) (0x00'0080 | ((kar >> 6) & 0x00'003F)); // the third 6 bits
150 buffer [j ++] = (char) (char8cxx . . $ char8#) (0x00'0080 | (kar & 0x00'003F)); // the lower 6 bits
151 }
152 }
153 buffer [j] = '\0';
154 return buffer;
155 }
156 #ifdef _WIN32
157 conststring16 MelderTrace::_peek32to16 (conststring32 string) {
158 if (! string)
159 return u"";
160 static char16 *buffer { nullptr };
161 static int64 bufferSize { 0 };
162 const int64 n = Melder_length (string);
163 int64 sizeNeeded = n * 2 + 1;
164 if ((bufferSize - sizeNeeded) * (int64) sizeof (char16) >= 10'000) {
165 free (buffer);
166 bufferSize = 0;
167 }
168 if (sizeNeeded > bufferSize) {
169 sizeNeeded = (int64) floor (sizeNeeded * 1.61803) + 100;
170 buffer = (char16 *) realloc (buffer, (size_t) sizeNeeded * sizeof (char16));
171 if (! buffer) {
172 bufferSize = 0;
173 return u"(out of memory during tracing)";
174 }
175 bufferSize = sizeNeeded;
176 }
177 int64 i, j;
178 for (i = 0, j = 0; i < n; i ++) {
179 char32 kar = string [i];
180 if (kar <= 0x00'D7FF) { // 16 bits
181 buffer [j ++] = (char16) kar; // guarded truncation
182 } else if (kar <= 0x00'DFFF) { // 16 bits
183 buffer [j ++] = (char16) UNICODE_REPLACEMENT_CHARACTER; // forbidden for UTF-32
184 } else if (kar <= 0x00'FFFF) { // 16 bits
185 buffer [j ++] = (char16) kar; // guarded truncation
186 } else if (kar <= 0x10'FFFF) { // 21 bits
187 kar -= 0x01'0000;
188 buffer [j ++] = (char16) (0x00'D800 | (kar >> 10)); // guarded truncation
189 buffer [j ++] = (char16) (0x00'DC00 | (kar & 0x00'03FF)); // guarded truncation
190 } else { // 21 bits
191 buffer [j ++] = (char16) UNICODE_REPLACEMENT_CHARACTER;
192 }
193 }
194 buffer [j] = u'\0';
195 return buffer;
196 }
197 #endif
198
199 conststring32cxx . . $ conststring32# MelderTracecxx . . $ MelderTrace/::_peek8to32cxx . . $ MelderTrace/_peek8to32(4cad97a093ddcc1d). (conststring8cxx . . $ conststring8# string8) {
200 static MelderStringcxx . . $ MelderString# buffers [19];
201 static int ibuffer = 0;
202 if (++ ibuffer == 19)
203 ibuffer = 0;
204 MelderString_8to32cxx . . $ MelderString_8to32(908334b4533925d1). (& buffers [ibuffer], string8);
205 return buffers [ibuffer].stringcxx . . $ MelderString#string.;
206 }
207
208 /********** TRACE **********/
209
210 void Melder_tracingToFilecxx . . $ Melder_tracingToFile(1145552d769d7b5d). (MelderFilecxx . . $ MelderFile# file) {
211 MelderFile_copycxx . . $ MelderFile_copy(b298201f6d539a65). (file, & MelderTracecxx . . $ MelderTrace/::_filecxx . . $ MelderTrace/_file.);
212 MelderFile_deletecxx . . $ MelderFile_delete(1145552d769d7b5d). (& MelderTracecxx . . $ MelderTrace/::_filecxx . . $ MelderTrace/_file.);
213 }
214
215 FILE * MelderTracecxx . . $ MelderTrace/::_opencxx . . $ MelderTrace/_open(e3ed0bbe2bd3b468). (conststring8cxx . . $ conststring8# sourceCodeFileName, int lineNumber, conststring8cxx . . $ conststring8# functionName) {
216 FILE *f;
217 #if defined (_WIN32) && ! defined (__CYGWIN__)
218 f = _wfopen ((const wchar_t *) MelderTrace::_peek32to16 (MelderTrace::_file. path), L"a");
219 #else
220 char utf8path [kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1];
221 Melder_32to8_fileSystem_inplacecxx . . $ Melder_32to8_fileSystem_inplace(d86258d5722a82b2). (MelderTracecxx . . $ MelderTrace/::_filecxx . . $ MelderTrace/_file.. pathcxx . . $ structMelderFile#path., utf8path); // this Melder_xxx() function is OK to call
222 f = fopen ((char *) utf8path, "a");
223 #endif
224 if (! f)
225 f = Melder_stderrcxx . . $ Melder_stderr.; // if the file cannot be opened, we can still trace to stderr!
226 if (sourceCodeFileName) {
227 const char *slashLocation = strrchr (sourceCodeFileName, Melder_DIRECTORY_SEPARATORcxx . . $ `melder/melder_files.h:24:10`!);
228 fprintf (f, "%s (%s:%d): ", functionName, slashLocation ? slashLocation + 1 : sourceCodeFileName, lineNumber);
229 } else {
230 fprintf (f, "%s: ", functionName);
231 }
232 return f;
233 }
234
235 void MelderTracecxx . . $ MelderTrace/::_closecxx . . $ MelderTrace/_close(8d4262d45fc644ab). (FILE *f) {
236 fprintf (f, "\n");
237 if (f != Melder_stderrcxx . . $ Melder_stderr.)
238 fclose (f);
239 }
240
241 #if defined (linuxcxx . . $ `<built-in>:480:9`!) && ! defined (NO_GUI)
242 static void theGtkLogHandlercxx . . $ theGtkLogHandler(bb9ba2a667c00213). (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer unused_data) {
243 FILE *f = MelderTracecxx . . $ MelderTrace/::_opencxx . . $ MelderTrace/_open(e3ed0bbe2bd3b468). (nullptr, 0, "GTK");
244 fprintf (f, "%s", message);
245 MelderTracecxx . . $ MelderTrace/::_closecxx . . $ MelderTrace/_close(8d4262d45fc644ab). (f);
246 }
247 static void theGlibLogHandlercxx . . $ theGlibLogHandler(bb9ba2a667c00213). (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer unused_data) {
248 FILE *f = MelderTracecxx . . $ MelderTrace/::_opencxx . . $ MelderTrace/_open(e3ed0bbe2bd3b468). (nullptr, 0, "GLib");
249 fprintf (f, "%s", message);
250 MelderTracecxx . . $ MelderTrace/::_closecxx . . $ MelderTrace/_close(8d4262d45fc644ab). (f);
251 }
252 static void theGlibGobjectLogHandlercxx . . $ theGlibGobjectLogHandler(bb9ba2a667c00213). (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer unused_data) {
253 FILE *f = MelderTracecxx . . $ MelderTrace/::_opencxx . . $ MelderTrace/_open(e3ed0bbe2bd3b468). (nullptr, 0, "GLib-GObject");
254 fprintf (f, "%s", message);
255 MelderTracecxx . . $ MelderTrace/::_closecxx . . $ MelderTrace/_close(8d4262d45fc644ab). (f);
256 }
257 #endif
258
259 void Melder_setTracingcxx . . $ Melder_setTracing(b45ddcbc8423892c). (bool tracing) {
260 time_t today = time (nullptr);
261 if (! tracing)
262 tracecxx . . $ `melder/melder_debug.h:60:10`! (U"switch tracing off"
263 U" in Praat version ", Melder_appVersionSTR(),
264 U" at ", Melder_peek8to32_u (ctime (& today))
265 );
266 Melder_isTracingGloballycxx . . $ Melder_isTracingGlobally. = tracing;
267 #if defined (linuxcxx . . $ `<built-in>:480:9`!) && ! defined (NO_GUI)
268 static guint handler_id1, handler_id2, handler_id3;
269 if (tracing) {
270 handler_id1 = g_log_set_handler ("Gtk", (GLogLevelFlags) (G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION), theGtkLogHandlercxx . . $ theGtkLogHandler(bb9ba2a667c00213)., nullptr);
271 handler_id2 = g_log_set_handler ("GLib", (GLogLevelFlags) (G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION), theGlibLogHandlercxx . . $ theGlibLogHandler(bb9ba2a667c00213)., nullptr);
272 handler_id3 = g_log_set_handler ("GLib-GObject", (GLogLevelFlags) (G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION), theGlibGobjectLogHandlercxx . . $ theGlibGobjectLogHandler(bb9ba2a667c00213)., nullptr);
273 } else {
274 if (handler_id1) g_log_remove_handler ("Gtk", handler_id1);
275 if (handler_id2) g_log_remove_handler ("GLib", handler_id2);
276 if (handler_id3) g_log_remove_handler ("GLib-GObject", handler_id3);
277 handler_id1 = handler_id2 = handler_id3 = 0;
278 }
279 #endif
280 if (tracing)
281 tracecxx . . $ `melder/melder_debug.h:60:10`! (U"switch tracing on"
282 U" in Praat version ", Melder_appVersionSTR(),
283 U" at ", Melder_peek8to32_u (ctime (& today))
284 );
285 }
286
287 /* End of file melder_debug.cpp */