1 #ifndef _melder_debug_h_
2 #define _melder_debug_h_
3 /* melder_debug.h
4  *
5  * Copyright (C) 1992-2018,2020,2021 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_isTracing = false;
26 
27 namespace MelderTrace {
28 	inline structMelderFile _file { };
29 	FILE * _open (conststring8 sourceCodeFileName, int lineNumber, conststring8 functionName);
30 	void _close (FILE *f);
31 	conststring8  _peek32to8  (conststring32 string);
32 	conststring16 _peek32to16 (conststring32 string);
33 }
34 
_recursiveTemplate_Melder_trace(FILE * f,const MelderArg & arg)35 inline void _recursiveTemplate_Melder_trace (FILE *f, const MelderArg& arg) {
36 	if (arg._arg)
37 		fprintf (f, "%s", MelderTrace::_peek32to8 (arg. _arg));
38 }
39 template <typename... Args>
_recursiveTemplate_Melder_trace(FILE * f,const MelderArg & first,Args...rest)40 void _recursiveTemplate_Melder_trace (FILE *f, const MelderArg& first, Args... rest) {
41 	_recursiveTemplate_Melder_trace (f, first);
42 	_recursiveTemplate_Melder_trace (f, rest...);
43 }
44 
45 template <typename... Args>
Melder_trace(conststring8 sourceCodeFileName,int lineNumber,conststring8 functionName,const MelderArg & first,Args...rest)46 void Melder_trace (conststring8 sourceCodeFileName, int lineNumber, conststring8 functionName, const MelderArg& first, Args... rest) {
47 	if (! Melder_isTracing || MelderFile_isNull (& MelderTrace::_file))
48 		return;
49 	FILE *f = MelderTrace::_open (sourceCodeFileName, lineNumber, functionName);
50 	_recursiveTemplate_Melder_trace (f, first, rest...);
51 	MelderTrace::_close (f);
52 }
53 
54 #ifdef NDEBUG
55 	#define trace(...)   ((void) 0)
56 #else
57 	#define trace(...)   (! Melder_isTracing ? (void) 0 : Melder_trace (__FILE__, __LINE__, __FUNCTION__, __VA_ARGS__))
58 #endif
59 
60 /* End of file melder_debug.h */
61 #endif
62