Browse Source

Added auto-colourising DBGC() macro

tags/v0.9.3
Michael Brown 17 years ago
parent
commit
2494625702
2 changed files with 98 additions and 40 deletions
  1. 56
    0
      src/core/debug.c
  2. 42
    40
      src/include/compiler.h

+ 56
- 0
src/core/debug.c View File

@@ -1,4 +1,5 @@
1 1
 #include <stdint.h>
2
+#include <stdarg.h>
2 3
 #include <io.h>
3 4
 #include <console.h>
4 5
 
@@ -85,3 +86,58 @@ int check_region ( void *region, size_t len ) {
85 86
 	}
86 87
 	return corrupted;
87 88
 }
89
+
90
+#define NUM_AUTO_COLOURS 6
91
+
92
+struct autocolour {
93
+	void * id;
94
+	unsigned long last_used;
95
+};
96
+
97
+static int autocolourise ( void *id ) {
98
+	static struct autocolour acs[NUM_AUTO_COLOURS];
99
+	static unsigned long use;
100
+	unsigned int i;
101
+	unsigned int oldest;
102
+	unsigned int oldest_last_used;
103
+
104
+	/* Increment usage iteration counter */
105
+	use++;
106
+
107
+	/* Scan through list for a currently assigned colour */
108
+	for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
109
+		if ( acs[i].id == id ) {
110
+			acs[i].last_used = use;
111
+			return i;
112
+		}
113
+	}
114
+
115
+	/* No colour found; evict the oldest from the list */
116
+	oldest = 0;
117
+	oldest_last_used = use;
118
+	for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
119
+		if ( acs[i].last_used < oldest_last_used ) {
120
+			oldest_last_used = acs[i].last_used;
121
+			oldest = i;
122
+		}
123
+	}
124
+	acs[oldest].id = id;
125
+	acs[oldest].last_used = use;
126
+	return oldest;
127
+}
128
+
129
+/** printf() for debugging with automatic colourisation
130
+ *
131
+ * @v id		Message stream ID
132
+ * @v fmt		printf() format
133
+ * @v ...		printf() argument list
134
+ */
135
+void dbg_printf_autocolour ( void *id, const char *fmt, ... ) {
136
+	va_list args;
137
+
138
+	printf ( "\033[%dm", ( id ? ( 31 + autocolourise ( id ) ) : 0 ) );
139
+	va_start ( args, fmt );
140
+	vprintf ( fmt, args );
141
+	va_end ( args );
142
+	printf ( "\033[0m" );
143
+}

+ 42
- 40
src/include/compiler.h View File

@@ -100,15 +100,6 @@ __asm__ ( ".equ\t" OBJECT_SYMBOL_STR ", 0" );
100 100
  *
101 101
  */
102 102
 
103
-/** @def DBG2
104
- *
105
- * Print a level 2 debugging message.
106
- *
107
- * As for DBG().  DBG2() takes effect only when the debugging level is
108
- * 2 or greater.
109
- *
110
- */
111
-
112 103
 /*
113 104
  * If debug_OBJECT is set to a true value, the macro DBG(...) will
114 105
  * expand to printf(...) when compiling OBJECT, and the symbol
@@ -120,36 +111,57 @@ __asm__ ( ".equ\t" OBJECT_SYMBOL_STR ", 0" );
120 111
 #if DEBUG_SYMBOL
121 112
 #include "console.h"
122 113
 #define DEBUG_SYMBOL_STR _XSTR ( DEBUG_SYMBOL )
123
-__asm__ ( ".equ\tDEBUG_LEVEL, " DEBUG_SYMBOL_STR );
114
+__asm__ ( ".equ\tDBGLVL, " DEBUG_SYMBOL_STR );
124 115
 #endif
125 116
 
126
-/** Do not print
117
+/** printf() for debugging
118
+ *
119
+ * This function exists so that the DBG() macros can expand to
120
+ * printf() calls without dragging the printf() prototype into scope.
127 121
  *
128
- * This function is used only for printf()-style format string
129
- * checking.  The function body does not exist, and no reference to it
130
- * should ever appear in any compiled object.
122
+ * As far as the compiler is concerned, dbg_printf() and printf() are
123
+ * completely unrelated calls; it's only at the assembly stage that
124
+ * references to the dbg_printf symbol are collapsed into references
125
+ * to the printf symbol.
131 126
  */
132
-extern int __attribute__ (( format ( printf, 1, 2 ) ))
133
-__do_not_printf ( const char *fmt, ... );
127
+extern int __attribute__ (( format ( printf, 1, 2 ) )) 
128
+dbg_printf ( const char *fmt, ... ) asm ( "printf" );
134 129
 
135
-#define DBG_PRINT(...) printf ( __VA_ARGS__ )
136
-#define DBG_DISCARD(...) do {					\
137
-		if ( 0 ) __do_not_printf ( __VA_ARGS__ );	\
138
-	} while ( 0 )
139
-
140
-#define DBG  DBG_DISCARD
141
-#define DBG2 DBG_DISCARD
130
+extern void __attribute__ (( format ( printf, 2, 3 ) )) 
131
+dbg_printf_autocolour ( void *id, const char *fmt, ... );
142 132
 
133
+/* Compatibility with existing Makefile */
143 134
 #if DEBUG_SYMBOL >= 1
144
-#undef DBG
145
-#define DBG DBG_PRINT
146
-#endif
147
-
148 135
 #if DEBUG_SYMBOL >= 2
149
-#undef DBG2
150
-#define DBG2 DBG_PRINT
136
+#define DBGLVL 3
137
+#else
138
+#define DBGLVL 1
139
+#endif
140
+#else
141
+#define DBGLVL 0
151 142
 #endif
152 143
 
144
+#define DBGLVL_LOG	1
145
+#define DBG_LOG		( DBGLVL & DBGLVL_LOG )
146
+#define DBGLVL_EXTRA	2
147
+#define DBG_EXTRA	( DBGLVL & DBGLVL_EXTRA )
148
+
149
+#define DBG_IF( level, ... ) do {				\
150
+		if ( DBG_ ## level ) {				\
151
+			dbg_printf ( __VA_ARGS__ );		\
152
+		}						\
153
+	} while ( 0 )
154
+
155
+#define DBGC_IF( level, ... ) do {				\
156
+		if ( DBG_ ## level ) {				\
157
+			dbg_printf_autocolour ( __VA_ARGS__ );	\
158
+		}						\
159
+	} while ( 0 )
160
+
161
+#define DBG( ... )	DBG_IF ( LOG, __VA_ARGS__ )
162
+#define DBG2( ... )	DBG_IF ( EXTRA, __VA_ARGS__ )
163
+#define DBGC( ... )	DBGC_IF ( LOG, __VA_ARGS__ )
164
+
153 165
 #if DEBUG_SYMBOL == 0
154 166
 #define NDEBUG
155 167
 #endif
@@ -157,23 +169,13 @@ __do_not_printf ( const char *fmt, ... );
157 169
 /** Declare a data structure as packed. */
158 170
 #define PACKED __attribute__ (( packed ))
159 171
 
160
-/** 
161
- * Declare a variable or data structure as unused.
162
- *
163
- * Note that using #__unused on a static global variable (such as a
164
- * table structure as mentioned in tables.h) is necessary in order to
165
- * inhibit compiler warnings.
166
- *
167
- */
172
+/** Declare a variable or data structure as unused. */
168 173
 #define __unused __attribute__ (( unused ))
169 174
 
170 175
 /**
171 176
  * Declare a function as used.
172 177
  *
173 178
  * Necessary only if the function is called only from assembler code.
174
- * You cannot use this attribute for static global variables; use
175
- * #__unused instead.
176
- *
177 179
  */
178 180
 #define __used __attribute__ (( used ))
179 181
 

Loading…
Cancel
Save