Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Debug.h@0:63d45df56584, 2012-07-08 (annotated)
- Committer:
- RichardUK
- Date:
- Sun Jul 08 20:18:58 2012 +0000
- Revision:
- 0:63d45df56584
Fixed memory leak where endpoints were being allocated over and over again. Now only allocates on first use.
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| RichardUK | 0:63d45df56584 | 1 | /* mbed Debug library used by all my developed program | 
| RichardUK | 0:63d45df56584 | 2 | * Copyright (c) 2010-2011 ygarcia | 
| RichardUK | 0:63d45df56584 | 3 | * | 
| RichardUK | 0:63d45df56584 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | 
| RichardUK | 0:63d45df56584 | 5 | * of this software and associated documentation files (the "Software"), to deal | 
| RichardUK | 0:63d45df56584 | 6 | * in the Software without restriction, including without limitation the rights | 
| RichardUK | 0:63d45df56584 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
| RichardUK | 0:63d45df56584 | 8 | * copies of the Software, and to permit persons to whom the Software is | 
| RichardUK | 0:63d45df56584 | 9 | * furnished to do so, subject to the following conditions: | 
| RichardUK | 0:63d45df56584 | 10 | * | 
| RichardUK | 0:63d45df56584 | 11 | * The above copyright notice and this permission notice shall be included in | 
| RichardUK | 0:63d45df56584 | 12 | * all copies or substantial portions of the Software. | 
| RichardUK | 0:63d45df56584 | 13 | * | 
| RichardUK | 0:63d45df56584 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
| RichardUK | 0:63d45df56584 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
| RichardUK | 0:63d45df56584 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
| RichardUK | 0:63d45df56584 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
| RichardUK | 0:63d45df56584 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
| RichardUK | 0:63d45df56584 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
| RichardUK | 0:63d45df56584 | 20 | * THE SOFTWARE. | 
| RichardUK | 0:63d45df56584 | 21 | */ | 
| RichardUK | 0:63d45df56584 | 22 | #if !defined(__DEBUG_H__) | 
| RichardUK | 0:63d45df56584 | 23 | #define __DEBUG_H__ | 
| RichardUK | 0:63d45df56584 | 24 | |
| RichardUK | 0:63d45df56584 | 25 | #include "mbed.h" | 
| RichardUK | 0:63d45df56584 | 26 | |
| RichardUK | 0:63d45df56584 | 27 | /** The steps below describe how to use this library: | 
| RichardUK | 0:63d45df56584 | 28 | * 1. Import this library to your project 'As file', because you will need to modify this file as described in step 2 | 
| RichardUK | 0:63d45df56584 | 29 | * 2. Edit this library | 
| RichardUK | 0:63d45df56584 | 30 | * 3. Remove comment from line 66 (search for '//#define __DEBUG' in this file) to get the DEBUG macro defimed properly. By default, __DEBUG flahg is undef | 
| RichardUK | 0:63d45df56584 | 31 | * 4. Rebuild this library and use the debug macro as decribe in sample code | 
| RichardUK | 0:63d45df56584 | 32 | * | 
| RichardUK | 0:63d45df56584 | 33 | * IMPORTANT: If you modify this libray, please keep this comment up to date for future users | 
| RichardUK | 0:63d45df56584 | 34 | * | 
| RichardUK | 0:63d45df56584 | 35 | * Please refer to handbook (http://mbed.org/handbook/Debugging) for mbed Debugging support | 
| RichardUK | 0:63d45df56584 | 36 | * | 
| RichardUK | 0:63d45df56584 | 37 | * @code | 
| RichardUK | 0:63d45df56584 | 38 | * #include "Debug.h" // This header alway includes mbed.h file | 
| RichardUK | 0:63d45df56584 | 39 | * | 
| RichardUK | 0:63d45df56584 | 40 | * DigitalOut myled(LED1); | 
| RichardUK | 0:63d45df56584 | 41 | * | 
| RichardUK | 0:63d45df56584 | 42 | * int main() { | 
| RichardUK | 0:63d45df56584 | 43 | * DEBUG_ENTER("main") // Log the entry of the C function 'main' | 
| RichardUK | 0:63d45df56584 | 44 | * | 
| RichardUK | 0:63d45df56584 | 45 | * std::string str("This is a sample for heaxdecimal dump using DebugLibrary"); | 
| RichardUK | 0:63d45df56584 | 46 | * DEBUG(">>> Example:"); | 
| RichardUK | 0:63d45df56584 | 47 | * HEXADUMP((unsigned char *)str.c_str(), str.length()); | 
| RichardUK | 0:63d45df56584 | 48 | * DEBUG("==="); | 
| RichardUK | 0:63d45df56584 | 49 | * HEXADUMP_OFFSET((unsigned char *)str.c_str(), str.length() - 19, 19); | 
| RichardUK | 0:63d45df56584 | 50 | * DEBUG("<<<"); | 
| RichardUK | 0:63d45df56584 | 51 | * | 
| RichardUK | 0:63d45df56584 | 52 | * int counter = 0; | 
| RichardUK | 0:63d45df56584 | 53 | * while(1) { | 
| RichardUK | 0:63d45df56584 | 54 | * DEBUG("In loop [%d]", counter++) // A sample message | 
| RichardUK | 0:63d45df56584 | 55 | * myled = 1; | 
| RichardUK | 0:63d45df56584 | 56 | * wait(0.2); | 
| RichardUK | 0:63d45df56584 | 57 | * myled = 0; | 
| RichardUK | 0:63d45df56584 | 58 | * wait(0.2); | 
| RichardUK | 0:63d45df56584 | 59 | * DEBUG_BREAK() // Wait for any key pressed to continue | 
| RichardUK | 0:63d45df56584 | 60 | * } // End of 'while' statement | 
| RichardUK | 0:63d45df56584 | 61 | * DEBUG_LEAVE("main, never reached") // Log the end of the C function 'main' | 
| RichardUK | 0:63d45df56584 | 62 | * } | 
| RichardUK | 0:63d45df56584 | 63 | * @endcode | 
| RichardUK | 0:63d45df56584 | 64 | */ | 
| RichardUK | 0:63d45df56584 | 65 | //#undef __DEBUG //<! Undefined debug flah, default value | 
| RichardUK | 0:63d45df56584 | 66 | #define __DEBUG //<! Uncomment this line to activate debug macros | 
| RichardUK | 0:63d45df56584 | 67 | |
| RichardUK | 0:63d45df56584 | 68 | // Undefined DEBUG symbols to be sure to use mine | 
| RichardUK | 0:63d45df56584 | 69 | #undef DEBUG_ENTER | 
| RichardUK | 0:63d45df56584 | 70 | #undef DEBUG_LEAVE | 
| RichardUK | 0:63d45df56584 | 71 | #undef DEBUG | 
| RichardUK | 0:63d45df56584 | 72 | #undef HEXADUMP | 
| RichardUK | 0:63d45df56584 | 73 | #undef HEXADUMP_OFFSET | 
| RichardUK | 0:63d45df56584 | 74 | #undef DEBUG_ERROR | 
| RichardUK | 0:63d45df56584 | 75 | #undef DEBUG_WARNING | 
| RichardUK | 0:63d45df56584 | 76 | #undef DEBUG_BREAK | 
| RichardUK | 0:63d45df56584 | 77 | #undef DEBUG_FATAL | 
| RichardUK | 0:63d45df56584 | 78 | |
| RichardUK | 0:63d45df56584 | 79 | #ifdef __DEBUG | 
| RichardUK | 0:63d45df56584 | 80 | |
| RichardUK | 0:63d45df56584 | 81 | /** This class implements debug functionalities based on USB console interface. V0.0.0.8 | 
| RichardUK | 0:63d45df56584 | 82 | * | 
| RichardUK | 0:63d45df56584 | 83 | * Note that this class is based on Helper pattern | 
| RichardUK | 0:63d45df56584 | 84 | */ | 
| RichardUK | 0:63d45df56584 | 85 | class DebugHelper | 
| RichardUK | 0:63d45df56584 | 86 | { | 
| RichardUK | 0:63d45df56584 | 87 | /** Convert the specified digit into hexadecimal number (0x30..0x39 (0..9), 0x47..x4c (A..F)) | 
| RichardUK | 0:63d45df56584 | 88 | * | 
| RichardUK | 0:63d45df56584 | 89 | * @param p_digit The digit to convert | 
| RichardUK | 0:63d45df56584 | 90 | * @return An hexadecimal digit (0..9-A..F) | 
| RichardUK | 0:63d45df56584 | 91 | */ | 
| RichardUK | 0:63d45df56584 | 92 | static inline unsigned char ToHexDigit(unsigned char p_digit) { return ((p_digit < 10) ? (p_digit + 0x30) : (p_digit + 0x37)); }; | 
| RichardUK | 0:63d45df56584 | 93 | /** Convert the specified hexadecimal digit into a character if it is printable, or replace by a '.' otherwise | 
| RichardUK | 0:63d45df56584 | 94 | * | 
| RichardUK | 0:63d45df56584 | 95 | * @param p_digit The hexadecimal digit to convert | 
| RichardUK | 0:63d45df56584 | 96 | * @return A character is it's printable, '.' otherwise | 
| RichardUK | 0:63d45df56584 | 97 | */ | 
| RichardUK | 0:63d45df56584 | 98 | static inline char ToCharDigit(unsigned char p_digit) { return (((p_digit < 0x20) || (p_digit > 0x80)) ? '.' : (char)p_digit); }; | 
| RichardUK | 0:63d45df56584 | 99 | public: | 
| RichardUK | 0:63d45df56584 | 100 | /** Standard log method | 
| RichardUK | 0:63d45df56584 | 101 | * @param p_format Format string compliant with C 'printf' format string | 
| RichardUK | 0:63d45df56584 | 102 | */ | 
| RichardUK | 0:63d45df56584 | 103 | static void Debug(const char* p_format, ...); | 
| RichardUK | 0:63d45df56584 | 104 | /** Log an hexadecimal buffer | 
| RichardUK | 0:63d45df56584 | 105 | * | 
| RichardUK | 0:63d45df56584 | 106 | * Note that parameters 'p_offset' and 'p_length' are not supported yet | 
| RichardUK | 0:63d45df56584 | 107 | * | 
| RichardUK | 0:63d45df56584 | 108 | * @param p_buffer The buffer to dump | 
| RichardUK | 0:63d45df56584 | 109 | * @param p_count Number of bytes to dump | 
| RichardUK | 0:63d45df56584 | 110 | * @param p_offset Offset to start the dump. Default: 0 | 
| RichardUK | 0:63d45df56584 | 111 | */ | 
| RichardUK | 0:63d45df56584 | 112 | static void HexaDump(unsigned char* p_buffer, int p_count, int p_offset = 0); | 
| RichardUK | 0:63d45df56584 | 113 | /** Break point method based on getchar() C function | 
| RichardUK | 0:63d45df56584 | 114 | */ | 
| RichardUK | 0:63d45df56584 | 115 | static void BreakPoint(const char* p_file, int p_line); | 
| RichardUK | 0:63d45df56584 | 116 | }; // End of class DebugHelper | 
| RichardUK | 0:63d45df56584 | 117 | |
| RichardUK | 0:63d45df56584 | 118 | /** Used to log function/method entry (>> ) | 
| RichardUK | 0:63d45df56584 | 119 | * | 
| RichardUK | 0:63d45df56584 | 120 | * Note that \ for multiline macro is not supported yet | 
| RichardUK | 0:63d45df56584 | 121 | */ | 
| RichardUK | 0:63d45df56584 | 122 | #define DEBUG_ENTER(...) do { DebugHelper::Debug(">> "); DebugHelper::Debug(__VA_ARGS__); DebugHelper::Debug("\r\n"); } while(false); | 
| RichardUK | 0:63d45df56584 | 123 | /** Used to log function end of function/method (<< ) | 
| RichardUK | 0:63d45df56584 | 124 | * | 
| RichardUK | 0:63d45df56584 | 125 | * Note that \ for multiline macro is not supported yet | 
| RichardUK | 0:63d45df56584 | 126 | */ | 
| RichardUK | 0:63d45df56584 | 127 | #define DEBUG_LEAVE(...) do { DebugHelper::Debug("<< "); DebugHelper::Debug(__VA_ARGS__); DebugHelper::Debug("\r\n"); } while(false); | 
| RichardUK | 0:63d45df56584 | 128 | /** Used to log a standard message | 
| RichardUK | 0:63d45df56584 | 129 | * | 
| RichardUK | 0:63d45df56584 | 130 | * Note that \ for multiline macro is not supported yet | 
| RichardUK | 0:63d45df56584 | 131 | */ | 
| RichardUK | 0:63d45df56584 | 132 | #define DEBUG(...) do { DebugHelper::Debug(__VA_ARGS__); DebugHelper::Debug("\r\n"); } while(false); | 
| RichardUK | 0:63d45df56584 | 133 | /** Used to dump an hexadecimal buffer | 
| RichardUK | 0:63d45df56584 | 134 | * | 
| RichardUK | 0:63d45df56584 | 135 | * Note that \ for multiline macro is not supported yet | 
| RichardUK | 0:63d45df56584 | 136 | */ | 
| RichardUK | 0:63d45df56584 | 137 | #define HEXADUMP(p_buffer, p_count) DebugHelper::HexaDump(p_buffer, p_count); | 
| RichardUK | 0:63d45df56584 | 138 | /** Used to dump an hexadecimal buffer with an offset | 
| RichardUK | 0:63d45df56584 | 139 | * | 
| RichardUK | 0:63d45df56584 | 140 | * Note that \ for multiline macro is not supported yet | 
| RichardUK | 0:63d45df56584 | 141 | */ | 
| RichardUK | 0:63d45df56584 | 142 | #define HEXADUMP_OFFSET(p_buffer, p_count, p_offset) DebugHelper::HexaDump(p_buffer, p_count, p_offset); | 
| RichardUK | 0:63d45df56584 | 143 | /** Used to log an error message (?? ) | 
| RichardUK | 0:63d45df56584 | 144 | * | 
| RichardUK | 0:63d45df56584 | 145 | * Note that \ for multiline macro is not supported yet | 
| RichardUK | 0:63d45df56584 | 146 | */ | 
| RichardUK | 0:63d45df56584 | 147 | #define DEBUG_ERROR(...) do { DebugHelper::Debug("?? "); DebugHelper::Debug(__VA_ARGS__); DebugHelper::Debug("\r\n"); } while(false); | 
| RichardUK | 0:63d45df56584 | 148 | /** Used to log a warning message (!! ) | 
| RichardUK | 0:63d45df56584 | 149 | * | 
| RichardUK | 0:63d45df56584 | 150 | * Note that \ for multiline macro is not supported yet | 
| RichardUK | 0:63d45df56584 | 151 | */ | 
| RichardUK | 0:63d45df56584 | 152 | #define DEBUG_WARNING(...) do { DebugHelper::Debug("!! "); DebugHelper::Debug(__VA_ARGS__); DebugHelper::Debug("\r\n"); } while(false); | 
| RichardUK | 0:63d45df56584 | 153 | |
| RichardUK | 0:63d45df56584 | 154 | /** Break point macro | 
| RichardUK | 0:63d45df56584 | 155 | */ | 
| RichardUK | 0:63d45df56584 | 156 | #define DEBUG_BREAK() DebugHelper::BreakPoint(__FILE__, __LINE__); | 
| RichardUK | 0:63d45df56584 | 157 | |
| RichardUK | 0:63d45df56584 | 158 | /** Used to stop program on fatal error | 
| RichardUK | 0:63d45df56584 | 159 | */ | 
| RichardUK | 0:63d45df56584 | 160 | #define DEBUG_FATAL(cause) error(cause); | 
| RichardUK | 0:63d45df56584 | 161 | |
| RichardUK | 0:63d45df56584 | 162 | #else // __DEBUG | 
| RichardUK | 0:63d45df56584 | 163 | |
| RichardUK | 0:63d45df56584 | 164 | /** Undefine DEBUG macro, used when __DEBUG is undefined | 
| RichardUK | 0:63d45df56584 | 165 | */ | 
| RichardUK | 0:63d45df56584 | 166 | #define DEBUG_ENTER(...) | 
| RichardUK | 0:63d45df56584 | 167 | /** Undefine DEBUG macro, used when __DEBUG is undefined | 
| RichardUK | 0:63d45df56584 | 168 | */ | 
| RichardUK | 0:63d45df56584 | 169 | #define DEBUG_LEAVE(...) | 
| RichardUK | 0:63d45df56584 | 170 | /** Undefine DEBUG macro, used when __DEBUG is undefined | 
| RichardUK | 0:63d45df56584 | 171 | */ | 
| RichardUK | 0:63d45df56584 | 172 | #define DEBUG(...) | 
| RichardUK | 0:63d45df56584 | 173 | /** Undefine DEBUG macro, used when __DEBUG is undefined | 
| RichardUK | 0:63d45df56584 | 174 | * | 
| RichardUK | 0:63d45df56584 | 175 | * Note that \ for multiline macro is not supported yet | 
| RichardUK | 0:63d45df56584 | 176 | */ | 
| RichardUK | 0:63d45df56584 | 177 | #define HEXADUMP(p_buffer, p_count) | 
| RichardUK | 0:63d45df56584 | 178 | /** Undefine DEBUG macro, used when __DEBUG is undefined | 
| RichardUK | 0:63d45df56584 | 179 | * | 
| RichardUK | 0:63d45df56584 | 180 | * Note that \ for multiline macro is not supported yet | 
| RichardUK | 0:63d45df56584 | 181 | */ | 
| RichardUK | 0:63d45df56584 | 182 | #define HEXADUMP_OFFSET(p_buffer, p_count, p_offset) | 
| RichardUK | 0:63d45df56584 | 183 | /** Undefine DEBUG_ERROR macro, used when __DEBUG is undefined | 
| RichardUK | 0:63d45df56584 | 184 | */ | 
| RichardUK | 0:63d45df56584 | 185 | #define DEBUG_ERROR(...) | 
| RichardUK | 0:63d45df56584 | 186 | /** Undefine DEBUG_WARNING macro, used when __DEBUG is undefined | 
| RichardUK | 0:63d45df56584 | 187 | */ | 
| RichardUK | 0:63d45df56584 | 188 | #define DEBUG_WARNING(...) | 
| RichardUK | 0:63d45df56584 | 189 | |
| RichardUK | 0:63d45df56584 | 190 | /** Undefine DEBUG_BREAK macro, used when __DEBUG is undefined | 
| RichardUK | 0:63d45df56584 | 191 | */ | 
| RichardUK | 0:63d45df56584 | 192 | #define DEBUG_BREAK() | 
| RichardUK | 0:63d45df56584 | 193 | |
| RichardUK | 0:63d45df56584 | 194 | /** Undefine DEBUG_FATAL macro, used when __DEBUG is undefined | 
| RichardUK | 0:63d45df56584 | 195 | */ | 
| RichardUK | 0:63d45df56584 | 196 | #define DEBUG_FATAL(cause) | 
| RichardUK | 0:63d45df56584 | 197 | |
| RichardUK | 0:63d45df56584 | 198 | #endif // __DEBUG | 
| RichardUK | 0:63d45df56584 | 199 | |
| RichardUK | 0:63d45df56584 | 200 | #endif // __DEBUG_H__ |