From: John Hodge Date: Thu, 12 Jul 2012 07:44:07 +0000 (+0800) Subject: DiskTool - Added endianness swaps and better logging X-Git-Tag: rel0.15~611^2~41^2~16 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=c6944db0d69feec15477318c21c9019392f4be73;p=tpg%2Facess2.git DiskTool - Added endianness swaps and better logging --- diff --git a/Tools/DiskTool/src/include/acess.h b/Tools/DiskTool/src/include/acess.h index 8493078c..c63582d0 100644 --- a/Tools/DiskTool/src/include/acess.h +++ b/Tools/DiskTool/src/include/acess.h @@ -82,6 +82,33 @@ extern tGID Threads_GetGID(void); // Kinda hacky way of not colliding with native errno #define errno (*(Threads_GetErrno())) +/** + * \name Endianness Swapping + * \{ + */ +#ifdef __BIG_ENDIAN__ +#define LittleEndian16(_val) SwapEndian16(_val) +#define LittleEndian32(_val) SwapEndian32(_val) +#define LittleEndian64(_val) SwapEndian32(_val) +#define BigEndian16(_val) (_val) +#define BigEndian32(_val) (_val) +#define BigEndian64(_val) (_val) +#else +#define LittleEndian16(_val) (_val) +#define LittleEndian32(_val) (_val) +#define LittleEndian64(_val) (_val) +#define BigEndian16(_val) SwapEndian16(_val) +#define BigEndian32(_val) SwapEndian32(_val) +#define BigEndian64(_val) SwapEndian64(_val) +#endif +extern Uint16 SwapEndian16(Uint16 Val); +extern Uint32 SwapEndian32(Uint32 Val); +extern Uint64 SwapEndian64(Uint64 Val); +/** + * \} + */ + + #include extern int strucmp(const char *s1, const char *s2); extern int strpos(const char *Str, char Ch); @@ -92,23 +119,12 @@ extern int ReadUTF8(const Uint8 *str, Uint32 *Val); extern int WriteUTF8(Uint8 *str, Uint32 Val); #define CheckString(str) (1) #define CheckMem(mem,sz) (1) +#include // TODO: Move out? extern int DivUp(int value, int divisor); extern uint64_t DivMod64U(uint64_t Num, uint64_t Den, uint64_t *Rem); -#if DEBUG -# define ENTER(str, v...) Log("%s:%i: ENTER "str, __func__, __LINE__) -# define LOG(fmt, v...) Log("%s:%i: "fmt, __func__, __LINE__, ##v) -# define LEAVE(...) do{}while(0) -# define LEAVE_RET(t,v) return v; -#else -# define ENTER(...) do{}while(0) -# define LOG(...) do{}while(0) -# define LEAVE(...) do{}while(0) -# define LEAVE_RET(t,v) return v; -#endif - static inline int Mutex_Acquire(tMutex *m) { if(*m) Log_KernelPanic("---", "Double mutex lock"); *m = 1; diff --git a/Tools/DiskTool/src/include/acess_logging.h b/Tools/DiskTool/src/include/acess_logging.h index 3be9e956..f8577dd5 100644 --- a/Tools/DiskTool/src/include/acess_logging.h +++ b/Tools/DiskTool/src/include/acess_logging.h @@ -2,6 +2,18 @@ #ifndef _DISKTOOL__ACESS_LOGGING_H_ #define _DISKTOOL__ACESS_LOGGING_H_ +#if DEBUG +# define ENTER(str, v...) Debug_TraceEnter(__func__, str, ##v) +# define LOG(fmt, v...) Debug_TraceLog(__func__, fmt, ##v) +# define LEAVE(t, v...) Debug_TraceLeave(__func__, t, ##v) +# define LEAVE_RET(t,v) do{LEAVE('-');return v;}while(0) +#else +# define ENTER(...) do{}while(0) +# define LOG(...) do{}while(0) +# define LEAVE(...) do{}while(0) +# define LEAVE_RET(t,v) return v; +#endif + extern void Log_KernelPanic(const char *Ident, const char *Message, ...) __attribute__((noreturn)); extern void Log_Panic(const char *Ident, const char *Message, ...); extern void Log_Error(const char *Ident, const char *Message, ...); @@ -13,5 +25,10 @@ extern void Log_Debug(const char *Ident, const char *Message, ...); extern void Warning(const char *Message, ...); extern void Log(const char *Message, ...); extern void Debug_HexDump(const char *Prefix, const void *Data, size_t Length); + +extern void Debug_TraceEnter(const char *Function, const char *Format, ...); +extern void Debug_TraceLog(const char *Function, const char *Format, ...); +extern void Debug_TraceLeave(const char *Function, char Type, ...); + #endif diff --git a/Tools/DiskTool/src/logging.c b/Tools/DiskTool/src/logging.c index a7a934e1..3a0d9f1e 100644 --- a/Tools/DiskTool/src/logging.c +++ b/Tools/DiskTool/src/logging.c @@ -6,13 +6,17 @@ #include #include #include +#include + +#define LOGHDR(col,type) fprintf(stderr, "\e["col"m[%-8.8s]"type" ", Ident) +#define LOGTAIL() fprintf(stderr, "\e[0m\n") #define PUTERR(col,type) {\ - fprintf(stderr, "\e["col"m[%-8.8s]"type" ", Ident); \ + LOGHDR(col,type);\ va_list args; va_start(args, Message);\ vfprintf(stderr, Message, args);\ va_end(args);\ - fprintf(stderr, "\e[0m\n"); \ + LOGTAIL();\ } // === CODE === @@ -68,3 +72,117 @@ void Debug_HexDump(const char *Prefix, const void *Data, size_t Length) fprintf(stderr, "\n"); } + int giDebug_TraceLevel = 0; + +void Debug_TraceEnter(const char *Function, const char *Format, ...) +{ + const char *Ident = "Trace"; + LOGHDR("37","T"); + for( int i = 0; i < giDebug_TraceLevel; i ++ ) + fprintf(stderr, " "); + fprintf(stderr, "%s: (", Function); + + va_list args; + va_start(args, Format); + + int hasBeenPrev = 0; + while(*Format) + { + while( *Format && isblank(*Format) ) + Format ++; + if( !*Format ) break; + + char type = *Format++; + const char *start = Format; + while( *Format && !isblank(*Format) ) + Format ++; + + if(hasBeenPrev) + fprintf(stderr, ","); + hasBeenPrev = 1; + + fprintf(stderr, "%.*s=", (int)(Format-start), start); + switch(type) + { + case 'p': + fprintf(stderr, "%p", va_arg(args,const void *)); + break; + case 's': + fprintf(stderr, "\"%s\"", va_arg(args,const char *)); + break; + case 'i': + fprintf(stderr, "%i", va_arg(args,int)); + break; + case 'x': + fprintf(stderr, "0x%x", va_arg(args,unsigned int)); + break; + default: + va_arg(args,uintptr_t); + fprintf(stderr, "?"); + break; + } + } + + va_end(args); + + fprintf(stderr, ")"); + LOGTAIL(); + giDebug_TraceLevel ++; +} + +void Debug_TraceLog(const char *Function, const char *Format, ...) +{ + const char *Ident = "Trace"; + LOGHDR("37","T"); + + for( int i = 0; i < giDebug_TraceLevel; i ++ ) + fprintf(stderr, " "); + fprintf(stderr, "%s: ", Function); + + va_list args; + va_start(args, Format); + + vfprintf(stderr, Format, args); + + va_end(args); + LOGTAIL(); +} + +void Debug_TraceLeave(const char *Function, char Type, ...) +{ + if( giDebug_TraceLevel == 0 ) { + Log_Error("Debug", "Function %s called LEAVE without ENTER", Function); + } + + const char *Ident = "Trace"; + LOGHDR("37","T"); + + va_list args; + va_start(args, Type); + + if( giDebug_TraceLevel > 0 ) + { + giDebug_TraceLevel --; + for( int i = 0; i < giDebug_TraceLevel; i ++ ) + fprintf(stderr, " "); + } + fprintf(stderr, "%s: RETURN", Function); + switch(Type) + { + case '-': + break; + case 'i': + fprintf(stderr, " %i", va_arg(args, int)); + break; + case 'p': + fprintf(stderr, " \"%s\"", va_arg(args, const char *)); + break; + default: + fprintf(stderr, " ?"); + break; + } + + va_end(args); + LOGTAIL(); +} +