diff options
author | Michael Brown | 2006-12-29 04:05:21 +0100 |
---|---|---|
committer | Michael Brown | 2006-12-29 04:05:21 +0100 |
commit | 2494625702c08e2e76252e1a7d94f1bf6c48d43a (patch) | |
tree | 906e71a3466907098bc666bcd618d5f99ab610ed /src/core/debug.c | |
parent | Redefine TCP state to include "flags that have been sent" rather than (diff) | |
download | ipxe-2494625702c08e2e76252e1a7d94f1bf6c48d43a.tar.gz ipxe-2494625702c08e2e76252e1a7d94f1bf6c48d43a.tar.xz ipxe-2494625702c08e2e76252e1a7d94f1bf6c48d43a.zip |
Added auto-colourising DBGC() macro
Diffstat (limited to 'src/core/debug.c')
-rw-r--r-- | src/core/debug.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/core/debug.c b/src/core/debug.c index 3facad29..9ef25b89 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" ); +} |