trace.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 "uart.h"
  42. /*
  43. * Global Definitions
  44. */
  45. /** Softpack Version */
  46. #define SOFTPACK_VERSION "1.1"
  47. #define TRACE_LEVEL_DEBUG 5
  48. #define TRACE_LEVEL_INFO 4
  49. #define TRACE_LEVEL_WARNING 3
  50. #define TRACE_LEVEL_ERROR 2
  51. #define TRACE_LEVEL_FATAL 1
  52. #define TRACE_LEVEL_NO_TRACE 0
  53. #define TRACE_LEVEL TRACE_LEVEL_NO_TRACE
  54. /* By default, all traces are output except the debug one. */
  55. #if !defined(TRACE_LEVEL)
  56. #define TRACE_LEVEL TRACE_LEVEL_INFO
  57. #endif
  58. /* By default, trace level is static (not dynamic) */
  59. #if !defined(DYN_TRACES)
  60. #define DYN_TRACES 0
  61. #endif
  62. #if defined(NOTRACE)
  63. #error "Error: NOTRACE has to be not defined !"
  64. #endif
  65. #undef NOTRACE
  66. #if (DYN_TRACES==0)
  67. #if (TRACE_LEVEL == TRACE_LEVEL_NO_TRACE)
  68. #define NOTRACE
  69. #endif
  70. #endif
  71. /* ------------------------------------------------------------------------------
  72. * Global Macros
  73. * ------------------------------------------------------------------------------
  74. */
  75. extern void TRACE_CONFIGURE( uint32_t dwBaudRate, uint32_t dwMCk ) ;
  76. /**
  77. * Initializes the DBGU for ISP project
  78. *
  79. * \param mode DBGU mode.
  80. * \param baudrate DBGU baudrate.
  81. * \param mck Master clock frequency.
  82. */
  83. #ifndef DYNTRACE
  84. #define DYNTRACE 0
  85. #endif
  86. #if (TRACE_LEVEL==0) && (DYNTRACE==0)
  87. #define TRACE_CONFIGURE_ISP(mode, baudrate, mck) {}
  88. #else
  89. #define TRACE_CONFIGURE_ISP(mode, baudrate, mck) { \
  90. const Pin pinsDBGU[] = {PINS_DBGU}; \
  91. PIO_Configure(pinsDBGU, PIO_LISTSIZE(pinsDBGU)); \
  92. DBGU_Configure( baudrate, mck ) ; \
  93. }
  94. #endif
  95. /**
  96. * Outputs a formatted string using 'printf' if the log level is high
  97. * enough. Can be disabled by defining TRACE_LEVEL=0 during compilation.
  98. * \param ... Additional parameters depending on formatted string.
  99. */
  100. #if defined(NOTRACE)
  101. /* Empty macro */
  102. #define TRACE_DEBUG(...) { }
  103. #define TRACE_INFO(...) { }
  104. #define TRACE_WARNING(...) { }
  105. #define TRACE_ERROR(...) { }
  106. #define TRACE_FATAL(...) { while(1); }
  107. #define TRACE_DEBUG_WP(...) { }
  108. #define TRACE_INFO_WP(...) { }
  109. #define TRACE_WARNING_WP(...) { }
  110. #define TRACE_ERROR_WP(...) { }
  111. #define TRACE_FATAL_WP(...) { while(1); }
  112. #elif (DYN_TRACES == 1)
  113. /* Trace output depends on dwTraceLevel value */
  114. #define TRACE_DEBUG(...) { if (dwTraceLevel >= TRACE_LEVEL_DEBUG) { printf("-D- " __VA_ARGS__); } }
  115. #define TRACE_INFO(...) { if (dwTraceLevel >= TRACE_LEVEL_INFO) { printf("-I- " __VA_ARGS__); } }
  116. #define TRACE_WARNING(...) { if (dwTraceLevel >= TRACE_LEVEL_WARNING) { printf("-W- " __VA_ARGS__); } }
  117. #define TRACE_ERROR(...) { if (dwTraceLevel >= TRACE_LEVEL_ERROR) { printf("-E- " __VA_ARGS__); } }
  118. #define TRACE_FATAL(...) { if (dwTraceLevel >= TRACE_LEVEL_FATAL) { printf("-F- " __VA_ARGS__); while(1); } }
  119. #define TRACE_DEBUG_WP(...) { if (dwTraceLevel >= TRACE_LEVEL_DEBUG) { printf(__VA_ARGS__); } }
  120. #define TRACE_INFO_WP(...) { if (dwTraceLevel >= TRACE_LEVEL_INFO) { printf(__VA_ARGS__); } }
  121. #define TRACE_WARNING_WP(...) { if (dwTraceLevel >= TRACE_LEVEL_WARNING) { printf(__VA_ARGS__); } }
  122. #define TRACE_ERROR_WP(...) { if (dwTraceLevel >= TRACE_LEVEL_ERROR) { printf(__VA_ARGS__); } }
  123. #define TRACE_FATAL_WP(...) { if (dwTraceLevel >= TRACE_LEVEL_FATAL) { printf(__VA_ARGS__); while(1); } }
  124. #else
  125. /* Trace compilation depends on TRACE_LEVEL value */
  126. #if (TRACE_LEVEL >= TRACE_LEVEL_DEBUG)
  127. #define TRACE_DEBUG(...) { printf("-D- " __VA_ARGS__); }
  128. #define TRACE_DEBUG_WP(...) { printf(__VA_ARGS__); }
  129. #else
  130. #define TRACE_DEBUG(...) { }
  131. #define TRACE_DEBUG_WP(...) { }
  132. #endif
  133. #if (TRACE_LEVEL >= TRACE_LEVEL_INFO)
  134. #define TRACE_INFO(...) { printf("-I- " __VA_ARGS__); }
  135. #define TRACE_INFO_WP(...) { printf(__VA_ARGS__); }
  136. #else
  137. #define TRACE_INFO(...) { }
  138. #define TRACE_INFO_WP(...) { }
  139. #endif
  140. #if (TRACE_LEVEL >= TRACE_LEVEL_WARNING)
  141. #define TRACE_WARNING(...) { printf("-W- " __VA_ARGS__); }
  142. #define TRACE_WARNING_WP(...) { printf(__VA_ARGS__); }
  143. #else
  144. #define TRACE_WARNING(...) { }
  145. #define TRACE_WARNING_WP(...) { }
  146. #endif
  147. #if (TRACE_LEVEL >= TRACE_LEVEL_ERROR)
  148. #define TRACE_ERROR(...) { printf("-E- " __VA_ARGS__); }
  149. #define TRACE_ERROR_WP(...) { printf(__VA_ARGS__); }
  150. #else
  151. #define TRACE_ERROR(...) { }
  152. #define TRACE_ERROR_WP(...) { }
  153. #endif
  154. #if (TRACE_LEVEL >= TRACE_LEVEL_FATAL)
  155. #define TRACE_FATAL(...) { printf("-F- " __VA_ARGS__); while(1); }
  156. #define TRACE_FATAL_WP(...) { printf(__VA_ARGS__); while(1); }
  157. #else
  158. #define TRACE_FATAL(...) { while(1); }
  159. #define TRACE_FATAL_WP(...) { while(1); }
  160. #endif
  161. #endif
  162. /**
  163. * Exported variables
  164. */
  165. /** Depending on DYN_TRACES, dwTraceLevel is a modifable runtime variable or a define */
  166. #if !defined(NOTRACE) && (DYN_TRACES == 1)
  167. extern uint32_t dwTraceLevel ;
  168. #endif
  169. #endif //#ifndef TRACE_H