| 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" |
| 20 |
#ifdef linux |
| 21 |
#include "../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 */ std::mutex 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 |
conststring8 MelderTrace::_peek32to8 (conststring32 string) { |
| 114 |
if (! string) |
| 115 |
return ""; |
| 116 |
static char *buffer { nullptr }; |
| 117 |
static int64 bufferSize { 0 }; |
| 118 |
const int64 n = Melder_length (string); |
| 119 |
int64 sizeNeeded = n * 4 + 1; |
| 120 |
if ((bufferSize - sizeNeeded) * (int64) sizeof (char) >= 10'000) { |
| 121 |
free (buffer); |
| 122 |
buffer = nullptr; // undangle |
| 123 |
bufferSize = 0; |
| 124 |
} |
| 125 |
if (sizeNeeded > bufferSize) { |
| 126 |
sizeNeeded = (int64) floor (sizeNeeded * 1.61803) + 100; |
| 127 |
buffer = (char *) realloc (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 |
int64 i, j; |
| 135 |
for (i = 0, j = 0; i < n; i ++) { |
| 136 |
char32 kar = string [i]; |
| 137 |
if (kar <= 0x00'007F) { // 7 bits |
| 138 |
buffer [j ++] = (char) (char8) kar; // guarded truncation |
| 139 |
} else if (kar <= 0x00'07FF) { // 11 bits |
| 140 |
buffer [j ++] = (char) (char8) (0x00'00C0 | (kar >> 6)); // the upper 5 bits yield a number between 0xC4 and 0xDF |
| 141 |
buffer [j ++] = (char) (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) (char8) (0x00'00E0 | (kar >> 12)); // the upper 4 bits yield a number between 0xE0 and 0xEF |
| 144 |
buffer [j ++] = (char) (char8) (0x00'0080 | ((kar >> 6) & 0x00'003F)); |
| 145 |
buffer [j ++] = (char) (char8) (0x00'0080 | (kar & 0x00'003F)); |
| 146 |
} else { // 21 bits |
| 147 |
buffer [j ++] = (char) (char8) (0x00'00F0 | (kar >> 18)); // the upper 3 bits yield a number between 0xF0 and 0xF4 (0x10FFFF >> 18 == 4) |
| 148 |
buffer [j ++] = (char) (char8) (0x00'0080 | ((kar >> 12) & 0x00'003F)); // the next 6 bits |
| 149 |
buffer [j ++] = (char) (char8) (0x00'0080 | ((kar >> 6) & 0x00'003F)); // the third 6 bits |
| 150 |
buffer [j ++] = (char) (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 |
conststring32 MelderTrace::_peek8to32 (conststring8 string8) { |
| 200 |
static MelderString buffers [19]; |
| 201 |
static int ibuffer = 0; |
| 202 |
if (++ ibuffer == 19) |
| 203 |
ibuffer = 0; |
| 204 |
MelderString_8to32 (& buffers [ibuffer], string8); |
| 205 |
return buffers [ibuffer].string; |
| 206 |
} |
| 207 |
|
| 208 |
/********** TRACE **********/ |
| 209 |
|
| 210 |
void Melder_tracingToFile (MelderFile file) { |
| 211 |
MelderFile_copy (file, & MelderTrace::_file); |
| 212 |
MelderFile_delete (& MelderTrace::_file); |
| 213 |
} |
| 214 |
|
| 215 |
FILE * MelderTrace::_open (conststring8 sourceCodeFileName, int lineNumber, 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_MAXPATH+1]; |
| 221 |
Melder_32to8_fileSystem_inplace (MelderTrace::_file. path, utf8path); // this Melder_xxx() function is OK to call |
| 222 |
f = fopen ((char *) utf8path, "a"); |
| 223 |
#endif |
| 224 |
if (! f) |
| 225 |
f = 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_SEPARATOR); |
| 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 MelderTrace::_close (FILE *f) { |
| 236 |
fprintf (f, "\n"); |
| 237 |
if (f != Melder_stderr) |
| 238 |
fclose (f); |
| 239 |
} |
| 240 |
|
| 241 |
#if defined (linux) && ! defined (NO_GUI) |
| 242 |
static void theGtkLogHandler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer unused_data) { |
| 243 |
FILE *f = MelderTrace::_open (nullptr, 0, "GTK"); |
| 244 |
fprintf (f, "%s", message); |
| 245 |
MelderTrace::_close (f); |
| 246 |
} |
| 247 |
static void theGlibLogHandler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer unused_data) { |
| 248 |
FILE *f = MelderTrace::_open (nullptr, 0, "GLib"); |
| 249 |
fprintf (f, "%s", message); |
| 250 |
MelderTrace::_close (f); |
| 251 |
} |
| 252 |
static void theGlibGobjectLogHandler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer unused_data) { |
| 253 |
FILE *f = MelderTrace::_open (nullptr, 0, "GLib-GObject"); |
| 254 |
fprintf (f, "%s", message); |
| 255 |
MelderTrace::_close (f); |
| 256 |
} |
| 257 |
#endif |
| 258 |
|
| 259 |
void Melder_setTracing (bool tracing) { |
| 260 |
time_t today = time (nullptr); |
| 261 |
if (! tracing) |
| 262 |
trace (U"switch tracing off" |
| 263 |
U" in Praat version ", Melder_appVersionSTR(), |
| 264 |
U" at ", Melder_peek8to32_u (ctime (& today)) |
| 265 |
); |
| 266 |
Melder_isTracingGlobally = tracing; |
| 267 |
#if defined (linux) && ! 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), theGtkLogHandler, nullptr); |
| 271 |
handler_id2 = g_log_set_handler ("GLib", (GLogLevelFlags) (G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION), theGlibLogHandler, nullptr); |
| 272 |
handler_id3 = g_log_set_handler ("GLib-GObject", (GLogLevelFlags) (G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION), theGlibGobjectLogHandler, 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 |
trace (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 */ |