debug.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @file debug.h
  3. * @brief Debugging facilities
  4. *
  5. * @section License
  6. *
  7. * SPDX-License-Identifier: GPL-2.0-or-later
  8. *
  9. * Copyright (C) 2010-2023 Oryx Embedded SARL. All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software Foundation,
  23. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. *
  25. * @author Oryx Embedded SARL (www.oryx-embedded.com)
  26. * @version 2.2.4
  27. **/
  28. #ifndef _DEBUG_H
  29. #define _DEBUG_H
  30. //Dependencies
  31. #include <stdio.h>
  32. #include "os_port.h"
  33. //Trace level definitions
  34. #define TRACE_LEVEL_OFF 0
  35. #define TRACE_LEVEL_FATAL 1
  36. #define TRACE_LEVEL_ERROR 2
  37. #define TRACE_LEVEL_WARNING 3
  38. #define TRACE_LEVEL_INFO 4
  39. #define TRACE_LEVEL_DEBUG 5
  40. #define TRACE_LEVEL_VERBOSE 6
  41. //Default trace level
  42. #ifndef TRACE_LEVEL
  43. #define TRACE_LEVEL TRACE_LEVEL_DEBUG
  44. #endif
  45. //Trace output redirection
  46. #ifndef TRACE_PRINTF
  47. #define TRACE_PRINTF(...) osSuspendAllTasks(), fprintf(stderr, __VA_ARGS__), osResumeAllTasks()
  48. #endif
  49. #ifndef TRACE_ARRAY
  50. #define TRACE_ARRAY(p, a, n) osSuspendAllTasks(), debugDisplayArray(stderr, p, a, n), osResumeAllTasks()
  51. #endif
  52. #ifndef TRACE_MPI
  53. #define TRACE_MPI(p, a) osSuspendAllTasks(), mpiDump(stderr, p, a), osResumeAllTasks()
  54. #endif
  55. //Debugging macros
  56. #if (TRACE_LEVEL >= TRACE_LEVEL_FATAL)
  57. #define TRACE_FATAL(...) TRACE_PRINTF(__VA_ARGS__)
  58. #define TRACE_FATAL_ARRAY(p, a, n) TRACE_ARRAY(p, a, n)
  59. #define TRACE_FATAL_MPI(p, a) TRACE_MPI(p, a)
  60. #else
  61. #define TRACE_FATAL(...)
  62. #define TRACE_FATAL_ARRAY(p, a, n)
  63. #define TRACE_FATAL_MPI(p, a)
  64. #endif
  65. #if (TRACE_LEVEL >= TRACE_LEVEL_ERROR)
  66. #define TRACE_ERROR(...) TRACE_PRINTF(__VA_ARGS__)
  67. #define TRACE_ERROR_ARRAY(p, a, n) TRACE_ARRAY(p, a, n)
  68. #define TRACE_ERROR_MPI(p, a) TRACE_MPI(p, a)
  69. #else
  70. #define TRACE_ERROR(...)
  71. #define TRACE_ERROR_ARRAY(p, a, n)
  72. #define TRACE_ERROR_MPI(p, a)
  73. #endif
  74. #if (TRACE_LEVEL >= TRACE_LEVEL_WARNING)
  75. #define TRACE_WARNING(...) TRACE_PRINTF(__VA_ARGS__)
  76. #define TRACE_WARNING_ARRAY(p, a, n) TRACE_ARRAY(p, a, n)
  77. #define TRACE_WARNING_MPI(p, a) TRACE_MPI(p, a)
  78. #else
  79. #define TRACE_WARNING(...)
  80. #define TRACE_WARNING_ARRAY(p, a, n)
  81. #define TRACE_WARNING_MPI(p, a)
  82. #endif
  83. #if (TRACE_LEVEL >= TRACE_LEVEL_INFO)
  84. #define TRACE_INFO(...) TRACE_PRINTF(__VA_ARGS__)
  85. #define TRACE_INFO_ARRAY(p, a, n) TRACE_ARRAY(p, a, n)
  86. #define TRACE_INFO_NET_BUFFER(p, b, o, n)
  87. #define TRACE_INFO_MPI(p, a) TRACE_MPI(p, a)
  88. #else
  89. #define TRACE_INFO(...)
  90. #define TRACE_INFO_ARRAY(p, a, n)
  91. #define TRACE_INFO_NET_BUFFER(p, b, o, n)
  92. #define TRACE_INFO_MPI(p, a)
  93. #endif
  94. #if (TRACE_LEVEL >= TRACE_LEVEL_DEBUG)
  95. #define TRACE_DEBUG(...) TRACE_PRINTF(__VA_ARGS__)
  96. #define TRACE_DEBUG_ARRAY(p, a, n) TRACE_ARRAY(p, a, n)
  97. #define TRACE_DEBUG_NET_BUFFER(p, b, o, n)
  98. #define TRACE_DEBUG_MPI(p, a) TRACE_MPI(p, a)
  99. #else
  100. #define TRACE_DEBUG(...)
  101. #define TRACE_DEBUG_ARRAY(p, a, n)
  102. #define TRACE_DEBUG_NET_BUFFER(p, b, o, n)
  103. #define TRACE_DEBUG_MPI(p, a)
  104. #endif
  105. #if (TRACE_LEVEL >= TRACE_LEVEL_VERBOSE)
  106. #define TRACE_VERBOSE(...) TRACE_PRINTF(__VA_ARGS__)
  107. #define TRACE_VERBOSE_ARRAY(p, a, n) TRACE_ARRAY(p, a, n)
  108. #define TRACE_VERBOSE_NET_BUFFER(p, b, o, n)
  109. #define TRACE_VERBOSE_MPI(p, a) TRACE_MPI(p, a)
  110. #else
  111. #define TRACE_VERBOSE(...)
  112. #define TRACE_VERBOSE_ARRAY(p, a, n)
  113. #define TRACE_VERBOSE_NET_BUFFER(p, b, o, n)
  114. #define TRACE_VERBOSE_MPI(p, a)
  115. #endif
  116. //C++ guard
  117. #ifdef __cplusplus
  118. extern "C" {
  119. #endif
  120. //Debug related functions
  121. void debugInit(uint32_t baudrate);
  122. void debugDisplayArray(FILE *stream,
  123. const char_t *prepend, const void *data, size_t length);
  124. //Deprecated definitions
  125. #define TRACE_LEVEL_NO_TRACE TRACE_LEVEL_OFF
  126. //C++ guard
  127. #ifdef __cplusplus
  128. }
  129. #endif
  130. #endif