1/** 2 * \file 3 * \brief Debug makro definitions 4 */ 5/* 6 * Copyright (c) 2009, 2010, ETH Zurich. 7 * All rights reserved. 8 * 9 * This file is distributed under the terms in the attached LICENSE file. 10 * If you do not find this file, copies can be found by writing to: 11 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group. 12 */ 13 14#ifndef __DEBUG_H__ 15#define __DEBUG_H__ 16 17// Debug 18 19 20/** 21 * \brief Debug level 22 * \arg 0: No messages will be printed on stdout. 23 * \arg 1: Only error messages will be printed. 24 * \arg 2: Error and information messages will be printed. 25 * \arg 3: Information, errors and packet data as ascii will be printed. 26 * \arg 4: Information, errors and packet data as ascii and hex will be printed. 27 */ 28#define DEBUG_LEVEL 2 29 30/** 31 * \brief Set the current function name for well-arranged debug messages. 32 */ 33#define PDEBUG_FNAME(x) char* __DEBUG__CURRENT_FUNCTION_NAME = x; int __DEBUG__OMIT = 0; if (__DEBUG__OMIT) {}; // avoids 34 // compilers 35 // 36 // 37 // 'unused 38 // 39 // 40 // variable' 41 // 42 // 43 // warning 44 // 45 // 46 // in 47 // 48 // 49 // functions 50 // 51 // 52 // only 53 // 54 // 55 // using 56 // 57 // 58 // error 59 // 60 // 61 // debug 62 // 63 // 64 // messages 65 66/** 67 * \brief When calling this makro, all debug messages for the caller function will be omitted. 68 */ 69#define PDEBUG_OMIT __DEBUG__OMIT = 1; 70 71/** 72 * \brief This prints a debug header e.g: libbfdmux.c:regster_app: *** Hello world debug message *** 73 */ 74#define PDEBUG_HEADER(x) if ((DEBUG_LEVEL >= 2) && (!__DEBUG__OMIT)) { \ 75 printf("> %s: *** ", __DEBUG__CURRENT_FUNCTION_NAME); \ 76 printf x; \ 77 printf(" ***\n"); \ 78 }; 79/** 80 * \brief This prints a debug footer line e.g: libbfdmux.c:regiser_app ### Foo bar footer ### 81 */ 82#define PDEBUG_FOOTER(x) if ((DEBUG_LEVEL >= 2) && (!__DEBUG__OMIT)) { \ 83 printf("> %s: ### ", __DEBUG__CURRENT_FUNCTION_NAME); \ 84 printf x; \ 85 printf(" ###\n"); \ 86 }; 87/** 88 * \brief This makro is used to print error messages. 89 */ 90#define PDEBUG_ERROR(x) if (DEBUG_LEVEL >= 1) { \ 91 printf("E %s: ", __DEBUG__CURRENT_FUNCTION_NAME); \ 92 printf x; \ 93 printf("\n"); \ 94 }; 95 96/** 97 * \brief This makro is used to print additional information 98 */ 99#define PDEBUG_INFO(x) if ((DEBUG_LEVEL >= 2) && (!__DEBUG__OMIT)) { \ 100 printf(" %s: ", __DEBUG__CURRENT_FUNCTION_NAME); \ 101 printf x; \ 102 printf("\n"); \ 103 }; 104 105/** 106 * \brief This makro is used to dump a memory segment to the screen as hex and 107 * 108 * (if the debug level allows it) as characters. 109 */ 110#define PDEBUG_RAW(arr,cnt) if ((DEBUG_LEVEL >= 4) && (!__DEBUG__OMIT)) { \ 111 int __debug_i; __debug_i = 0; \ 112 char __debug_c; __debug_c = ' '; \ 113 printf(" %s: ", __DEBUG__CURRENT_FUNCTION_NAME); \ 114 printf("Address: %p, size: %u Bytes\n", arr, (unsigned) (cnt)); \ 115 printf(" %s: ", __DEBUG__CURRENT_FUNCTION_NAME); \ 116 for(__debug_i = 0; __debug_i < (cnt); __debug_i++) { \ 117 printf("%02x ",*( (uint8_t*) (arr) + __debug_i )); \ 118 if (!((__debug_i+1)%20)) printf("\n %s: ", __DEBUG__CURRENT_FUNCTION_NAME); \ 119 }; \ 120 }; \ 121 if ((DEBUG_LEVEL >= 3) && (!__DEBUG__OMIT)) { \ 122 int __debug_j; __debug_j = 0; \ 123 char __debug_d; __debug_d = ' '; \ 124 printf("\n %s: ", __DEBUG__CURRENT_FUNCTION_NAME); \ 125 for(__debug_j = 0; __debug_j < (cnt); __debug_j++) { \ 126 __debug_d = *((uint8_t*) (arr) + __debug_j ); \ 127 if (__debug_d < 0x20 || __debug_d > 0x7e) __debug_d = '*'; \ 128 printf("%c",__debug_d); \ 129 if (!((__debug_j+1)%60)) printf("\n %s: ", __DEBUG__CURRENT_FUNCTION_NAME); \ 130 }; \ 131 printf("\n"); \ 132 if (FLUSH_AND_SYNC) { \ 133 fflush(stdin); \ 134 } \ 135 }; 136 137#endif 138