summaryrefslogtreecommitdiffstats
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/debug.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/core/debug.c b/src/core/debug.c
index 3facad29e..9ef25b89b 100644
--- a/src/core/debug.c
+++ b/src/core/debug.c
@@ -1,4 +1,5 @@
#include <stdint.h>
+#include <stdarg.h>
#include <io.h>
#include <console.h>
@@ -85,3 +86,58 @@ int check_region ( void *region, size_t len ) {
}
return corrupted;
}
+
+#define NUM_AUTO_COLOURS 6
+
+struct autocolour {
+ void * id;
+ unsigned long last_used;
+};
+
+static int autocolourise ( void *id ) {
+ static struct autocolour acs[NUM_AUTO_COLOURS];
+ static unsigned long use;
+ unsigned int i;
+ unsigned int oldest;
+ unsigned int oldest_last_used;
+
+ /* Increment usage iteration counter */
+ use++;
+
+ /* Scan through list for a currently assigned colour */
+ for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
+ if ( acs[i].id == id ) {
+ acs[i].last_used = use;
+ return i;
+ }
+ }
+
+ /* No colour found; evict the oldest from the list */
+ oldest = 0;
+ oldest_last_used = use;
+ for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
+ if ( acs[i].last_used < oldest_last_used ) {
+ oldest_last_used = acs[i].last_used;
+ oldest = i;
+ }
+ }
+ acs[oldest].id = id;
+ acs[oldest].last_used = use;
+ return oldest;
+}
+
+/** printf() for debugging with automatic colourisation
+ *
+ * @v id Message stream ID
+ * @v fmt printf() format
+ * @v ... printf() argument list
+ */
+void dbg_printf_autocolour ( void *id, const char *fmt, ... ) {
+ va_list args;
+
+ printf ( "\033[%dm", ( id ? ( 31 + autocolourise ( id ) ) : 0 ) );
+ va_start ( args, fmt );
+ vprintf ( fmt, args );
+ va_end ( args );
+ printf ( "\033[0m" );
+}