• Main Page
  • Modules
  • Files
  • File List

trace.h

00001 /*
00002     COMMON TRACE
00003 
00004     Common header file for tracing.
00005 
00006     Copyright (C) Dmitri Pal <dpal@redhat.com> 2009
00007 
00008     This program is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 3 of the License, or
00011     (at your option) any later version.
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016     You should have received a copy of the GNU General Public License
00017     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #ifndef COMMON_TRACE_H
00021 #define COMMON_TRACE_H
00022 
00023 #ifdef TRACE_LEVEL
00024 #define HAVE_TRACE
00025 #include <stdio.h>
00026 
00027 /* The trace level is a bit mask */
00028 #define TRACE_FLOW      0x0000001  /* - trace messages that are entry exit into functions */
00029 #define TRACE_ERROR     0x0000002  /* - trace messages that are errors */
00030 #define TRACE_INFO      0x0000004  /* - trace things that are informational */
00031 
00032 
00033 #ifdef TRACE_HOME           /* Define this in the module that contains main */
00034 unsigned trace_level = TRACE_LEVEL;
00035 #else
00036 extern unsigned trace_level;
00037 #endif /* TRACE_HOME */
00038 #endif /* TRACE_LEVEL */
00039 
00040 
00041 
00042 #ifdef HAVE_TRACE
00043 /* Tracing strings */
00044 #define TRACE_STRING(level, msg, str) \
00045     do { \
00046         if (level & trace_level) { \
00047             printf("[DEBUG] %40s (%4d) %s%s %s\n", \
00048                    __FILE__, __LINE__, \
00049                    (level == TRACE_ERROR) ? "ERROR-> " : "", \
00050                    (msg != NULL) ? msg : "MISSING MESSAGE", \
00051                    (str != NULL) ? str : "(null)"); \
00052         } \
00053     } while(0)
00054 
00055 /* Tracing unsigned numbers */
00056 #define TRACE_NUMBER(level, msg, num) \
00057     do { \
00058         if (level & trace_level) { \
00059             printf("[DEBUG] %40s (%4d) %s%s %lu\n", \
00060                    __FILE__, __LINE__, \
00061                    (level == TRACE_ERROR) ? "ERROR-> " : "", \
00062                    (msg != NULL) ? msg : "MISSING MESSAGE", \
00063                    (unsigned long int)(num)); \
00064         } \
00065     } while(0)
00066 
00067 /* Tracing signed numbers */
00068 #define TRACE_SNUMBER(level, msg, num) \
00069     do { \
00070         if (level & trace_level) { \
00071             printf("[DEBUG] %40s (%4d) %s%s %ld\n", \
00072                    __FILE__, __LINE__, \
00073                    (level == TRACE_ERROR) ? "ERROR-> " : "", \
00074                    (msg != NULL) ? msg : "MISSING MESSAGE", \
00075                    (long int)(num)); \
00076         } \
00077     } while(0)
00078 
00079 /* Tracing long numbers */
00080 #define TRACE_LNUMBER(level, msg, num) \
00081     do { \
00082         if (level & trace_level) { \
00083             printf("[DEBUG] %40s (%4d) %s%s %llu\n", \
00084                    __FILE__, __LINE__, \
00085                    (level == TRACE_ERROR) ? "ERROR-> " : "", \
00086                    (msg != NULL) ? msg : "MISSING MESSAGE", \
00087                    (unsigned long long int)(num)); \
00088         } \
00089     } while(0)
00090 
00091 /* Tracing signed long numbers */
00092 #define TRACE_SLNUMBER(level, msg, num) \
00093     do { \
00094         if (level & trace_level) { \
00095             printf("[DEBUG] %40s (%4d) %s%s %lld\n", \
00096                    __FILE__, __LINE__, \
00097                    (level == TRACE_ERROR) ? "ERROR-> " : "", \
00098                    (msg != NULL) ? msg : "MISSING MESSAGE", \
00099                    (long long int)(num)); \
00100         } \
00101     } while(0)
00102 
00103 /* Tracing doubles */
00104 #define TRACE_DOUBLE(level, msg, num) \
00105     do { \
00106         if (level & trace_level) { \
00107             printf("[DEBUG] %40s (%4d) %s%s %e\n", \
00108                    __FILE__, __LINE__, \
00109                    (level == TRACE_ERROR) ? "ERROR-> " : "", \
00110                    (msg != NULL) ? msg : "MISSING MESSAGE", \
00111                    (double)(num)); \
00112         } \
00113     } while(0)
00114 
00115 /* Assertion */
00116 #define TRACE_ASSERT(expression) expression ? : printf("ASSERTION FAILED\n")
00117 #else /* HAVE_TRACE */
00118 
00119 /* Noop in case the tracing is disabled */
00120 #define TRACE_STRING(level, msg, str)
00121 #define TRACE_NUMBER(level, msg, num)
00122 #define TRACE_SNUMBER(level, msg, num)
00123 #define TRACE_LNUMBER(level, msg, num)
00124 #define TRACE_SLNUMBER(level, msg, num)
00125 #define TRACE_DOUBLE(level, msg, num)
00126 #endif /* HAVE_TRACE */
00127 
00128 
00129 /* Convenience wrappers for strings */
00130 #define TRACE_FLOW_STRING(msg, str)  TRACE_STRING(TRACE_FLOW, msg, str)
00131 #define TRACE_ERROR_STRING(msg, str) TRACE_STRING(TRACE_ERROR, msg, str)
00132 #define TRACE_INFO_STRING(msg, str)  TRACE_STRING(TRACE_INFO, msg, str)
00133 
00134 /* Convenience wrappers for unsigned numbers */
00135 #define TRACE_FLOW_NUMBER(msg, num)  TRACE_NUMBER(TRACE_FLOW, msg, num)
00136 #define TRACE_ERROR_NUMBER(msg, num) TRACE_NUMBER(TRACE_ERROR, msg, num)
00137 #define TRACE_INFO_NUMBER(msg, num)  TRACE_NUMBER(TRACE_INFO, msg, num)
00138 
00139 /* Convenience wrappers for signed numbers */
00140 #define TRACE_FLOW_SNUMBER(msg, num)  TRACE_SNUMBER(TRACE_FLOW, msg, num)
00141 #define TRACE_ERROR_SNUMBER(msg, num) TRACE_SNUMBER(TRACE_ERROR, msg, num)
00142 #define TRACE_INFO_SNUMBER(msg, num)  TRACE_SNUMBER(TRACE_INFO, msg, num)
00143 
00144 /* Convenience wrappers for 64-bit long unsigned numbers */
00145 #define TRACE_FLOW_LNUMBER(msg, num)  TRACE_LNUMBER(TRACE_FLOW, msg, num)
00146 #define TRACE_ERROR_LNUMBER(msg, num) TRACE_LNUMBER(TRACE_ERROR, msg, num)
00147 #define TRACE_INFO_LNUMBER(msg, num)  TRACE_LNUMBER(TRACE_INFO, msg, num)
00148 
00149 /* Convenience wrappers for 64-bit long signed numbers */
00150 #define TRACE_FLOW_SLNUMBER(msg, num)  TRACE_SLNUMBER(TRACE_FLOW, msg, num)
00151 #define TRACE_ERROR_SLNUMBER(msg, num) TRACE_SLNUMBER(TRACE_ERROR, msg, num)
00152 #define TRACE_INFO_SLNUMBER(msg, num)  TRACE_SLNUMBER(TRACE_INFO, msg, num)
00153 
00154 /* Convenience wrappers for numbers */
00155 #define TRACE_FLOW_DOUBLE(msg, num)  TRACE_DOUBLE(TRACE_FLOW, msg, num)
00156 #define TRACE_ERROR_DOUBLE(msg, num) TRACE_DOUBLE(TRACE_ERROR, msg, num)
00157 #define TRACE_INFO_DOUBLE(msg, num)  TRACE_DOUBLE(TRACE_INFO, msg, num)
00158 
00159 #endif /* COMMON_TRACE_H */

Generated on Fri Aug 13 2010 for libref_array by  doxygen 1.7.1