| 1 |
#ifndef _melder_debug_h_ |
| 2 |
#define _melder_debug_h_ |
| 3 |
/* melder_debug.h |
| 4 |
* |
| 5 |
* Copyright (C) 1992-2018,2020,2021,2024 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 2 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 |
|
| 21 |
inline integer Melder_debug = 0; |
| 22 |
|
| 23 |
void Melder_tracingToFile (MelderFile file); |
| 24 |
void Melder_setTracing (bool tracing); |
| 25 |
inline bool Melder_isTracingGlobally = false; |
| 26 |
inline bool Melder_isTracingLocally = false; |
| 27 |
|
| 28 |
namespace MelderTrace { |
| 29 |
inline structMelderFile _file { }; |
| 30 |
FILE * _open (conststring8 sourceCodeFileName, int lineNumber, conststring8 functionName); |
| 31 |
void _close (FILE *f); |
| 32 |
conststring8 _peek32to8 (conststring32 string); |
| 33 |
conststring16 _peek32to16 (conststring32 string); |
| 34 |
conststring32 _peek8to32 (conststring8 string8); // used in the `trace` macro |
| 35 |
|
| 36 |
inline void _appendOneMessageElement (FILE *f, conststring32 string) { |
| 37 |
if (string) |
| 38 |
fprintf (f, "%s", _peek32to8 (string)); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
extern std::mutex theMelder_trace_mutex; |
| 43 |
|
| 44 |
template <typename... Arg> |
| 45 |
void Melder_trace (conststring8 sourceCodeFileName, int lineNumber, conststring8 functionName, const Arg... arg) { |
| 46 |
std::lock_guard lock (theMelder_trace_mutex); |
| 47 |
if (MelderFile_isNull (& MelderTrace::_file)) |
| 48 |
return; |
| 49 |
FILE *f = MelderTrace::_open (sourceCodeFileName, lineNumber, functionName); |
| 50 |
( MelderTrace::_appendOneMessageElement (f, MelderArg { arg }. _arg), ... ); |
| 51 |
MelderTrace::_close (f); |
| 52 |
} |
| 53 |
|
| 54 |
#define TRACE constexpr bool Melder_isTracingLocally = true; // intentionally shadow a global variable; meant for temporary tracing of a function (to stderr) |
| 55 |
#define TRACEF constexpr bool Melder_isTracingGlobally = true; // for temporary tracing of a function (to the tracing file, even if global tracing is off) |
| 56 |
|
| 57 |
#ifdef NDEBUG |
| 58 |
#define trace(...) ((void) 0) |
| 59 |
#else |
| 60 |
#define trace(...) ( \ |
| 61 |
Melder_isTracingGlobally ? Melder_trace (__FILE__, __LINE__, __FUNCTION__, __VA_ARGS__) : \ |
| 62 |
Melder_isTracingLocally ? Melder_casual (U"" __FILE__ " ", __LINE__, U" ", MelderTrace::_peek8to32 (__FUNCTION__), U": ", __VA_ARGS__) : \ |
| 63 |
(void) 0 \ |
| 64 |
) |
| 65 |
#endif |
| 66 |
|
| 67 |
/* End of file melder_debug.h */ |
| 68 |
#endif |