// 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 <string.h>
extern int strucmp(const char *s1, const char *s2);
extern int strpos(const char *Str, char Ch);
extern int WriteUTF8(Uint8 *str, Uint32 Val);
#define CheckString(str) (1)
#define CheckMem(mem,sz) (1)
+#include <ctype.h>
// 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;
#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, ...);
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
#include <stdlib.h>
#include <stdint.h>
#include <acess_logging.h>
+#include <ctype.h>
+
+#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 ===
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();
+}
+