trace.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * \file
  3. *
  4. * \par Purpose
  5. *
  6. * Standard output methods for reporting debug information, warnings and
  7. * errors, which can be easily be turned on/off.
  8. *
  9. * \par Usage
  10. * -# Initialize the DBGU using TRACE_CONFIGURE() if you intend to eventually
  11. * disable ALL traces; otherwise use DBGU_Configure().
  12. * -# Uses the TRACE_DEBUG(), TRACE_INFO(), TRACE_WARNING(), TRACE_ERROR()
  13. * TRACE_FATAL() macros to output traces throughout the program.
  14. * -# Each type of trace has a level : Debug 5, Info 4, Warning 3, Error 2
  15. * and Fatal 1. Disable a group of traces by changing the value of
  16. * TRACE_LEVEL during compilation; traces with a level bigger than TRACE_LEVEL
  17. * are not generated. To generate no trace, use the reserved value 0.
  18. * -# Trace disabling can be static or dynamic. If dynamic disabling is selected
  19. * the trace level can be modified in runtime. If static disabling is selected
  20. * the disabled traces are not compiled.
  21. *
  22. * \par traceLevels Trace level description
  23. * -# TRACE_DEBUG (5): Traces whose only purpose is for debugging the program,
  24. * and which do not produce meaningful information otherwise.
  25. * -# TRACE_INFO (4): Informational trace about the program execution. Should
  26. * enable the user to see the execution flow.
  27. * -# TRACE_WARNING (3): Indicates that a minor error has happened. In most case
  28. * it can be discarded safely; it may even be expected.
  29. * -# TRACE_ERROR (2): Indicates an error which may not stop the program execution,
  30. * but which indicates there is a problem with the code.
  31. * -# TRACE_FATAL (1): Indicates a major error which prevents the program from going
  32. * any further.
  33. */
  34. #ifndef _TRACE_
  35. #define _TRACE_
  36. /*
  37. * Headers
  38. */
  39. //#include "pio.h"
  40. #include <stdio.h>
  41. #include "board.h"
  42. /*
  43. * Global Definitions
  44. */
  45. /** Softpack Version */
  46. #define SOFTPACK_VERSION "1.1"
  47. #if defined(USE_ULOG)
  48. /* using ulog compatible with trace */
  49. #include <ulog.h>
  50. #else
  51. #define TRACE_LEVEL_DEBUG 5
  52. #define TRACE_LEVEL_INFO 4
  53. #define TRACE_LEVEL_WARNING 3
  54. #define TRACE_LEVEL_ERROR 2
  55. #define TRACE_LEVEL_FATAL 1
  56. #define TRACE_LEVEL_NO_TRACE 0
  57. /* By default, all traces are output except the debug one. */
  58. #if !defined(TRACE_LEVEL)
  59. #define TRACE_LEVEL TRACE_LEVEL_INFO
  60. #endif
  61. /* By default, trace level is static (not dynamic) */
  62. #if !defined(DYN_TRACES)
  63. #define DYN_TRACES 0
  64. #endif
  65. #if defined(NOTRACE)
  66. #error "Error: NOTRACE has to be not defined !"
  67. #endif
  68. #undef NOTRACE
  69. #if (DYN_TRACES==0)
  70. #if (TRACE_LEVEL == TRACE_LEVEL_NO_TRACE)
  71. #define NOTRACE
  72. #endif
  73. #endif
  74. /* ------------------------------------------------------------------------------
  75. * Global Macros
  76. * ------------------------------------------------------------------------------
  77. */
  78. extern void TRACE_CONFIGURE( uint32_t dwBaudRate, uint32_t dwMCk ) ;
  79. /**
  80. * Initializes the DBGU for ISP project
  81. *
  82. * \param mode DBGU mode.
  83. * \param baudrate DBGU baudrate.
  84. * \param mck Master clock frequency.
  85. */
  86. #ifndef DYNTRACE
  87. #define DYNTRACE 0
  88. #endif
  89. #if (TRACE_LEVEL==0) && (DYNTRACE==0)
  90. #define TRACE_CONFIGURE_ISP(mode, baudrate, mck) {}
  91. #else
  92. #define TRACE_CONFIGURE_ISP(mode, baudrate, mck) { \
  93. const Pin pinsDBGU[] = {PINS_DBGU}; \
  94. PIO_Configure(pinsDBGU, PIO_LISTSIZE(pinsDBGU)); \
  95. DBGU_Configure( baudrate, mck ) ; \
  96. }
  97. #endif
  98. /**
  99. * Outputs a formatted string using 'printf' if the log level is high
  100. * enough. Can be disabled by defining TRACE_LEVEL=0 during compilation.
  101. * \param ... Additional parameters depending on formatted string.
  102. */
  103. #if defined(NOTRACE)
  104. /* Empty macro */
  105. #define TRACE_DEBUG(...) { }
  106. #define TRACE_INFO(...) { }
  107. #define TRACE_WARNING(...) { }
  108. #define TRACE_ERROR(...) { }
  109. #define TRACE_FATAL(...) { while(1); }
  110. #define TRACE_DEBUG_WP(...) { }
  111. #define TRACE_INFO_WP(...) { }
  112. #define TRACE_WARNING_WP(...) { }
  113. #define TRACE_ERROR_WP(...) { }
  114. #define TRACE_FATAL_WP(...) { while(1); }
  115. #elif (DYN_TRACES == 1)
  116. /* Trace output depends on dwTraceLevel value */
  117. #define TRACE_DEBUG(...) { if (dwTraceLevel >= TRACE_LEVEL_DEBUG) { printf("-D- " __VA_ARGS__); } }
  118. #define TRACE_INFO(...) { if (dwTraceLevel >= TRACE_LEVEL_INFO) { printf("-I- " __VA_ARGS__); } }
  119. #define TRACE_WARNING(...) { if (dwTraceLevel >= TRACE_LEVEL_WARNING) { printf("-W- " __VA_ARGS__); } }
  120. #define TRACE_ERROR(...) { if (dwTraceLevel >= TRACE_LEVEL_ERROR) { printf("-E- " __VA_ARGS__); } }
  121. #define TRACE_FATAL(...) { if (dwTraceLevel >= TRACE_LEVEL_FATAL) { printf("-F- " __VA_ARGS__); while(1); } }
  122. #define TRACE_DEBUG_WP(...) { if (dwTraceLevel >= TRACE_LEVEL_DEBUG) { printf(__VA_ARGS__); } }
  123. #define TRACE_INFO_WP(...) { if (dwTraceLevel >= TRACE_LEVEL_INFO) { printf(__VA_ARGS__); } }
  124. #define TRACE_WARNING_WP(...) { if (dwTraceLevel >= TRACE_LEVEL_WARNING) { printf(__VA_ARGS__); } }
  125. #define TRACE_ERROR_WP(...) { if (dwTraceLevel >= TRACE_LEVEL_ERROR) { printf(__VA_ARGS__); } }
  126. #define TRACE_FATAL_WP(...) { if (dwTraceLevel >= TRACE_LEVEL_FATAL) { printf(__VA_ARGS__); while(1); } }
  127. #else
  128. /* Trace compilation depends on TRACE_LEVEL value */
  129. #if (TRACE_LEVEL >= TRACE_LEVEL_DEBUG)
  130. #define TRACE_DEBUG(...) { printf("-D- " __VA_ARGS__); }
  131. #define TRACE_DEBUG_WP(...) { printf(__VA_ARGS__); }
  132. #else
  133. #define TRACE_DEBUG(...) { }
  134. #define TRACE_DEBUG_WP(...) { }
  135. #endif
  136. #if (TRACE_LEVEL >= TRACE_LEVEL_INFO)
  137. #define TRACE_INFO(...) { printf("-I- " __VA_ARGS__); }
  138. #define TRACE_INFO_WP(...) { printf(__VA_ARGS__); }
  139. #else
  140. #define TRACE_INFO(...) { }
  141. #define TRACE_INFO_WP(...) { }
  142. #endif
  143. #if (TRACE_LEVEL >= TRACE_LEVEL_WARNING)
  144. #define TRACE_WARNING(...) { printf("-W- " __VA_ARGS__); }
  145. #define TRACE_WARNING_WP(...) { printf(__VA_ARGS__); }
  146. #else
  147. #define TRACE_WARNING(...) { }
  148. #define TRACE_WARNING_WP(...) { }
  149. #endif
  150. #if (TRACE_LEVEL >= TRACE_LEVEL_ERROR)
  151. #define TRACE_ERROR(...) { printf("-E- " __VA_ARGS__); }
  152. #define TRACE_ERROR_WP(...) { printf(__VA_ARGS__); }
  153. #else
  154. #define TRACE_ERROR(...) { }
  155. #define TRACE_ERROR_WP(...) { }
  156. #endif
  157. #if (TRACE_LEVEL >= TRACE_LEVEL_FATAL)
  158. #define TRACE_FATAL(...) { printf("-F- " __VA_ARGS__); while(1); }
  159. #define TRACE_FATAL_WP(...) { printf(__VA_ARGS__); while(1); }
  160. #else
  161. #define TRACE_FATAL(...) { while(1); }
  162. #define TRACE_FATAL_WP(...) { while(1); }
  163. #endif
  164. #endif
  165. /**
  166. * Exported variables
  167. */
  168. /** Depending on DYN_TRACES, dwTraceLevel is a modifable runtime variable or a define */
  169. #if !defined(NOTRACE) && (DYN_TRACES == 1)
  170. extern uint32_t dwTraceLevel ;
  171. #endif
  172. #endif //defined(USE_ULOG)
  173. #endif //#ifndef TRACE_H