1 /* 2 COMMON TRACE 3 4 Common header file for tracing. 5 6 Copyright (C) Dmitri Pal <dpal@redhat.com> 2009 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU Lesser General Public License as published by 10 the Free Software Foundation; either version 3 of the License, or 11 (at your option) any later version. 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 You should have received a copy of the GNU Lesser General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #ifndef COMMON_TRACE_H 21 #define COMMON_TRACE_H 22 23 #ifdef TRACE_LEVEL 24 #define HAVE_TRACE 25 #include <stdio.h> 26 27 /* The trace level is a bit mask */ 28 #define TRACE_FLOW 0x0000001 /* - trace messages that are entry exit into functions */ 29 #define TRACE_ERROR 0x0000002 /* - trace messages that are errors */ 30 #define TRACE_INFO 0x0000004 /* - trace things that are informational */ 31 32 33 #ifdef TRACE_HOME /* Define this in the module that contains main */ 34 unsigned trace_level = TRACE_LEVEL; 35 #else 36 extern unsigned trace_level; 37 #endif /* TRACE_HOME */ 38 #endif /* TRACE_LEVEL */ 39 40 41 42 #ifdef HAVE_TRACE 43 /* Tracing strings */ 44 #define TRACE_STRING(level, msg, str) \ 45 do { \ 46 if (level & trace_level) { \ 47 printf("[DEBUG] %40s (%4d) %s%s %s\n", \ 48 __FILE__, __LINE__, \ 49 (level == TRACE_ERROR) ? "ERROR-> " : "", \ 50 (msg != NULL) ? msg : "MISSING MESSAGE", \ 51 (str != NULL) ? str : "(null)"); \ 52 } \ 53 } while(0) 54 55 /* Tracing unsigned numbers */ 56 #define TRACE_NUMBER(level, msg, num) \ 57 do { \ 58 if (level & trace_level) { \ 59 printf("[DEBUG] %40s (%4d) %s%s %lu\n", \ 60 __FILE__, __LINE__, \ 61 (level == TRACE_ERROR) ? "ERROR-> " : "", \ 62 (msg != NULL) ? msg : "MISSING MESSAGE", \ 63 (unsigned long int)(num)); \ 64 } \ 65 } while(0) 66 67 /* Tracing signed numbers */ 68 #define TRACE_SNUMBER(level, msg, num) \ 69 do { \ 70 if (level & trace_level) { \ 71 printf("[DEBUG] %40s (%4d) %s%s %ld\n", \ 72 __FILE__, __LINE__, \ 73 (level == TRACE_ERROR) ? "ERROR-> " : "", \ 74 (msg != NULL) ? msg : "MISSING MESSAGE", \ 75 (long int)(num)); \ 76 } \ 77 } while(0) 78 79 /* Tracing long numbers */ 80 #define TRACE_LNUMBER(level, msg, num) \ 81 do { \ 82 if (level & trace_level) { \ 83 printf("[DEBUG] %40s (%4d) %s%s %llu\n", \ 84 __FILE__, __LINE__, \ 85 (level == TRACE_ERROR) ? "ERROR-> " : "", \ 86 (msg != NULL) ? msg : "MISSING MESSAGE", \ 87 (unsigned long long int)(num)); \ 88 } \ 89 } while(0) 90 91 /* Tracing signed long numbers */ 92 #define TRACE_SLNUMBER(level, msg, num) \ 93 do { \ 94 if (level & trace_level) { \ 95 printf("[DEBUG] %40s (%4d) %s%s %lld\n", \ 96 __FILE__, __LINE__, \ 97 (level == TRACE_ERROR) ? "ERROR-> " : "", \ 98 (msg != NULL) ? msg : "MISSING MESSAGE", \ 99 (long long int)(num)); \ 100 } \ 101 } while(0) 102 103 /* Tracing doubles */ 104 #define TRACE_DOUBLE(level, msg, num) \ 105 do { \ 106 if (level & trace_level) { \ 107 printf("[DEBUG] %40s (%4d) %s%s %e\n", \ 108 __FILE__, __LINE__, \ 109 (level == TRACE_ERROR) ? "ERROR-> " : "", \ 110 (msg != NULL) ? msg : "MISSING MESSAGE", \ 111 (double)(num)); \ 112 } \ 113 } while(0) 114 115 #define TRACE_RETURN(flow, val) \ 116 do { \ 117 char mstr[200]; \ 118 sprintf(mstr, "%s returning:", __FUNCTION__); \ 119 flow(mstr, val); \ 120 } while(0) 121 122 123 /* Assertion */ 124 #define TRACE_ASSERT(expression) expression ? : printf("ASSERTION FAILED\n") 125 #else /* HAVE_TRACE */ 126 127 /* Noop in case the tracing is disabled */ 128 #define TRACE_STRING(level, msg, str) 129 #define TRACE_NUMBER(level, msg, num) 130 #define TRACE_SNUMBER(level, msg, num) 131 #define TRACE_LNUMBER(level, msg, num) 132 #define TRACE_SLNUMBER(level, msg, num) 133 #define TRACE_DOUBLE(level, msg, num) 134 #define TRACE_RETURN(flow, val) 135 #define TRACE_ASSERT(expression) 136 #endif /* HAVE_TRACE */ 137 138 139 /* Convenience wrappers for strings */ 140 #define TRACE_FLOW_STRING(msg, str) TRACE_STRING(TRACE_FLOW, msg, str) 141 #define TRACE_ERROR_STRING(msg, str) TRACE_STRING(TRACE_ERROR, msg, str) 142 #define TRACE_INFO_STRING(msg, str) TRACE_STRING(TRACE_INFO, msg, str) 143 144 /* Convenience wrappers for unsigned numbers */ 145 #define TRACE_FLOW_NUMBER(msg, num) TRACE_NUMBER(TRACE_FLOW, msg, num) 146 #define TRACE_ERROR_NUMBER(msg, num) TRACE_NUMBER(TRACE_ERROR, msg, num) 147 #define TRACE_INFO_NUMBER(msg, num) TRACE_NUMBER(TRACE_INFO, msg, num) 148 149 /* Convenience wrappers for signed numbers */ 150 #define TRACE_FLOW_SNUMBER(msg, num) TRACE_SNUMBER(TRACE_FLOW, msg, num) 151 #define TRACE_ERROR_SNUMBER(msg, num) TRACE_SNUMBER(TRACE_ERROR, msg, num) 152 #define TRACE_INFO_SNUMBER(msg, num) TRACE_SNUMBER(TRACE_INFO, msg, num) 153 154 /* Convenience wrappers for 64-bit long unsigned numbers */ 155 #define TRACE_FLOW_LNUMBER(msg, num) TRACE_LNUMBER(TRACE_FLOW, msg, num) 156 #define TRACE_ERROR_LNUMBER(msg, num) TRACE_LNUMBER(TRACE_ERROR, msg, num) 157 #define TRACE_INFO_LNUMBER(msg, num) TRACE_LNUMBER(TRACE_INFO, msg, num) 158 159 /* Convenience wrappers for 64-bit long signed numbers */ 160 #define TRACE_FLOW_SLNUMBER(msg, num) TRACE_SLNUMBER(TRACE_FLOW, msg, num) 161 #define TRACE_ERROR_SLNUMBER(msg, num) TRACE_SLNUMBER(TRACE_ERROR, msg, num) 162 #define TRACE_INFO_SLNUMBER(msg, num) TRACE_SLNUMBER(TRACE_INFO, msg, num) 163 164 /* Convenience wrappers for numbers */ 165 #define TRACE_FLOW_DOUBLE(msg, num) TRACE_DOUBLE(TRACE_FLOW, msg, num) 166 #define TRACE_ERROR_DOUBLE(msg, num) TRACE_DOUBLE(TRACE_ERROR, msg, num) 167 #define TRACE_INFO_DOUBLE(msg, num) TRACE_DOUBLE(TRACE_INFO, msg, num) 168 169 /* Some other nice wrappers for function entry and exit */ 170 #define TRACE_FLOW_ENTRY() TRACE_FLOW_STRING(__FUNCTION__, "Entry") 171 #define TRACE_FLOW_EXIT() TRACE_FLOW_STRING(__FUNCTION__, "Exit") 172 #define TRACE_FLOW_RETURN(val) TRACE_RETURN(TRACE_FLOW_NUMBER, val) 173 174 175 #endif /* COMMON_TRACE_H */ 176