melder_debug.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #ifndef _melder_debug_h_
  2. #define _melder_debug_h_
  3. /* melder_debug.h
  4. *
  5. * Copyright (C) 1992-2018 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. SYNOPSIS
  22. trace (arg1, arg2, arg3...);
  23. extern int Melder_debug;
  24. */
  25. extern int Melder_debug;
  26. void Melder_tracingToFile (MelderFile file);
  27. void Melder_setTracing (bool tracing);
  28. extern bool Melder_isTracing;
  29. namespace MelderTrace {
  30. extern structMelderFile _file;
  31. FILE * _open (conststring8 sourceCodeFileName, int lineNumber, conststring8 functionName);
  32. void _close (FILE *f);
  33. conststring8 _peek32to8 (conststring32 string);
  34. conststring16 _peek32to16 (conststring32 string);
  35. };
  36. inline static void _recursiveTemplate_Melder_trace (FILE *f, const MelderArg& arg) {
  37. if (arg._arg)
  38. fprintf (f, "%s", MelderTrace::_peek32to8 (arg. _arg));
  39. }
  40. template <typename... Args>
  41. void _recursiveTemplate_Melder_trace (FILE *f, const MelderArg& first, Args... rest) {
  42. _recursiveTemplate_Melder_trace (f, first);
  43. _recursiveTemplate_Melder_trace (f, rest...);
  44. }
  45. template <typename... Args>
  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. #ifdef NDEBUG
  54. #define trace(x) ((void) 0)
  55. #else
  56. #define trace(...) (! Melder_isTracing ? (void) 0 : Melder_trace (__FILE__, __LINE__, __FUNCTION__, __VA_ARGS__))
  57. #endif
  58. /* End of file melder_debug.h */
  59. #endif