X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Tools%2Fnativelib%2Flogging.c;h=959a9cc615cbcdbe496a32f1c3d763d40373694d;hb=13078002b01ee4f63eb2001d2ef479a2a006ea32;hp=0cb3ed32a8e98485b6f6ea9baadcf71080eaa343;hpb=7ba570fe3cc5418f42decf5b72ac2295cce9e60f;p=tpg%2Facess2.git diff --git a/Tools/nativelib/logging.c b/Tools/nativelib/logging.c index 0cb3ed32..959a9cc6 100644 --- a/Tools/nativelib/logging.c +++ b/Tools/nativelib/logging.c @@ -1,5 +1,9 @@ /* - * + * Acess2 libnative (Kernel Simulation Library) + * - By John Hodge (thePowersGang) + * + * logging.c + * - Logging functions */ #include #include @@ -8,22 +12,40 @@ #include #include #include +#include -#define LOGHDR(col,type) fprintf(stderr, "\e["col"m[%-8.8s]"type" ", Ident) +extern int Threads_GetTID(); + +#define LOGHDR(col,type) fprintf(stderr, "\e["col"m[%-8.8s]"type"%2i ", Ident, Threads_GetTID()) #define LOGTAIL() fprintf(stderr, "\e[0m\n") +#define LOG_LOCK_ACQUIRE() do{ \ + if(!gbThreadInLog) SHORTLOCK(&glDebugLock); \ + gbThreadInLog ++; \ +}while(0) +#define LOG_LOCK_RELEASE() do {\ + gbThreadInLog --; \ + if(!gbThreadInLog) SHORTREL(&glDebugLock); \ +} while(0) + #define PUTERR(col,type) {\ + LOG_LOCK_ACQUIRE(); \ LOGHDR(col,type);\ va_list args; va_start(args, Message);\ vfprintf(stderr, Message, args);\ va_end(args);\ LOGTAIL();\ + LOG_LOCK_RELEASE(); \ } +// === GLOBALS === +int __thread gbThreadInLog; +tShortSpinlock glDebugLock; + // === CODE === void Log_KernelPanic(const char *Ident, const char *Message, ...) { PUTERR("35", "k") - abort(); + exit(-1); } void Log_Panic(const char *Ident, const char *Message, ...) PUTERR("34", "p") @@ -38,6 +60,11 @@ void Log_Log(const char *Ident, const char *Message, ...) void Log_Debug(const char *Ident, const char *Message, ...) PUTERR("37", "d") +void Panic(const char *Message, ...) { + const char *Ident = ""; + PUTERR("35", "k") + exit(-1); +} void Warning(const char *Message, ...) { const char *Ident = ""; PUTERR("33", "W") @@ -46,39 +73,71 @@ void Log(const char *Message, ...) { const char *Ident = ""; PUTERR("37", "L") } +void Debug(const char *Message, ...) { + const char *Ident = ""; + PUTERR("38", "D") +} +char _prn(char byte) +{ + return (' ' <= byte && byte <= 'z') ? byte : '.'; +} void Debug_HexDump(const char *Prefix, const void *Data, size_t Length) { const uint8_t *data = Data; size_t ofs; + LOG_LOCK_ACQUIRE(); fprintf(stderr, "[HexDump ]d %s: %i bytes\n", Prefix, (int)Length); for( ofs = 0; ofs + 16 <= Length; ofs += 16 ) { + const uint8_t *d = data + ofs; fprintf(stderr, "[HexDump ]d %s:", Prefix); fprintf(stderr, " %02x %02x %02x %02x %02x %02x %02x %02x", - data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]); - data += 8; + d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7]); fprintf(stderr, " %02x %02x %02x %02x %02x %02x %02x %02x", - data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]); - data += 8; + d[8], d[9], d[10], d[11], d[12], d[13], d[14], d[15]); + fprintf(stderr, " |%c%c%c%c""%c%c%c%c %c%c%c%c""%c%c%c%c", + _prn(d[ 0]), _prn(d[ 1]), _prn(d[ 2]), _prn(d[ 3]), + _prn(d[ 4]), _prn(d[ 5]), _prn(d[ 6]), _prn(d[ 7]), + _prn(d[ 8]), _prn(d[ 9]), _prn(d[10]), _prn(d[11]), + _prn(d[12]), _prn(d[13]), _prn(d[14]), _prn(d[15]) + ); fprintf(stderr, "\n"); } - fprintf(stderr, "[HexDump ]d %s:", Prefix); - for( ; ofs < Length; ofs ++ ) + if( ofs < Length ) { - if( ofs % 8 == 0 ) fprintf(stderr, " "); - fprintf(stderr, " %02x", data[ofs%16]); + const uint8_t *d = data + ofs; + fprintf(stderr, "[HexDump ]d %s: ", Prefix); + for( int i = 0; i < Length - ofs; i ++ ) + { + if( i == 8 ) fprintf(stderr, " "); + fprintf(stderr, " %02x", d[i]); + } + for( int i = Length - ofs; i < 16; i ++ ) + { + if( i == 8 ) fprintf(stderr, " "); + fprintf(stderr, " "); + } + fprintf(stderr, " |"); + for( int i = 0; i < Length - ofs; i ++ ) + { + if( i == 8 ) fprintf(stderr, " "); + fprintf(stderr, "%c", _prn(d[i])); + } + + fprintf(stderr, "\n"); } - fprintf(stderr, "\n"); + LOG_LOCK_RELEASE(); } int giDebug_TraceLevel = 0; void Debug_TraceEnter(const char *Function, const char *Format, ...) { - const char *Ident = "Trace"; - LOGHDR("37","T"); + LOG_LOCK_ACQUIRE(); + //const char *Ident = "Trace"; + //LOGHDR("37","T"); for( int i = 0; i < giDebug_TraceLevel; i ++ ) fprintf(stderr, " "); fprintf(stderr, "%s: (", Function); @@ -132,12 +191,14 @@ void Debug_TraceEnter(const char *Function, const char *Format, ...) fprintf(stderr, ")"); LOGTAIL(); giDebug_TraceLevel ++; + LOG_LOCK_RELEASE(); } void Debug_TraceLog(const char *Function, const char *Format, ...) { - const char *Ident = "Trace"; - LOGHDR("37","T"); + LOG_LOCK_ACQUIRE(); + //const char *Ident = "Trace"; + //LOGHDR("37","T"); for( int i = 0; i < giDebug_TraceLevel; i ++ ) fprintf(stderr, " "); @@ -150,6 +211,7 @@ void Debug_TraceLog(const char *Function, const char *Format, ...) va_end(args); LOGTAIL(); + LOG_LOCK_RELEASE(); } void Debug_TraceLeave(const char *Function, char Type, ...) @@ -158,8 +220,9 @@ void Debug_TraceLeave(const char *Function, char Type, ...) Log_Error("Debug", "Function %s called LEAVE without ENTER", Function); } - const char *Ident = "Trace"; - LOGHDR("37","T"); + LOG_LOCK_ACQUIRE(); + //const char *Ident = "Trace"; + //LOGHDR("37","T"); va_list args; va_start(args, Type); @@ -200,5 +263,6 @@ void Debug_TraceLeave(const char *Function, char Type, ...) va_end(args); LOGTAIL(); + LOG_LOCK_RELEASE(); }