2 * Acess2 libnative (Kernel Simulation Library)
3 * - By John Hodge (thePowersGang)
12 #include <acess_logging.h>
15 #include <shortlock.h>
17 extern int Threads_GetTID();
19 #define LOGHDR(col,type) fprintf(stderr, "\e["col"m[%-8.8s]"type"%2i ", Ident, Threads_GetTID())
20 #define LOGTAIL() fprintf(stderr, "\e[0m\n")
22 #define LOG_LOCK_ACQUIRE() do{ \
23 if(!gbThreadInLog) SHORTLOCK(&glDebugLock); \
26 #define LOG_LOCK_RELEASE() do {\
28 if(!gbThreadInLog) SHORTREL(&glDebugLock); \
31 #define PUTERR(col,type) {\
34 va_list args; va_start(args, Message);\
35 vfprintf(stderr, Message, args);\
42 int __thread gbThreadInLog;
43 tShortSpinlock glDebugLock;
46 void Log_KernelPanic(const char *Ident, const char *Message, ...) {
50 void Log_Panic(const char *Ident, const char *Message, ...)
52 void Log_Error(const char *Ident, const char *Message, ...)
54 void Log_Warning(const char *Ident, const char *Message, ...)
56 void Log_Notice(const char *Ident, const char *Message, ...)
58 void Log_Log(const char *Ident, const char *Message, ...)
60 void Log_Debug(const char *Ident, const char *Message, ...)
63 void Panic(const char *Message, ...) {
64 const char *Ident = "";
68 void Warning(const char *Message, ...) {
69 const char *Ident = "";
72 void Log(const char *Message, ...) {
73 const char *Ident = "";
76 void Debug(const char *Message, ...) {
77 const char *Ident = "";
83 return (' ' <= byte && byte <= 'z') ? byte : '.';
85 void Debug_HexDump(const char *Prefix, const void *Data, size_t Length)
87 const uint8_t *data = Data;
90 fprintf(stderr, "[HexDump ]d %s: %i bytes\n", Prefix, (int)Length);
91 for( ofs = 0; ofs + 16 <= Length; ofs += 16 )
93 const uint8_t *d = data + ofs;
94 fprintf(stderr, "[HexDump ]d %s:", Prefix);
95 fprintf(stderr, " %02x %02x %02x %02x %02x %02x %02x %02x",
96 d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7]);
97 fprintf(stderr, " %02x %02x %02x %02x %02x %02x %02x %02x",
98 d[8], d[9], d[10], d[11], d[12], d[13], d[14], d[15]);
99 fprintf(stderr, " |%c%c%c%c""%c%c%c%c %c%c%c%c""%c%c%c%c",
100 _prn(d[ 0]), _prn(d[ 1]), _prn(d[ 2]), _prn(d[ 3]),
101 _prn(d[ 4]), _prn(d[ 5]), _prn(d[ 6]), _prn(d[ 7]),
102 _prn(d[ 8]), _prn(d[ 9]), _prn(d[10]), _prn(d[11]),
103 _prn(d[12]), _prn(d[13]), _prn(d[14]), _prn(d[15])
105 fprintf(stderr, "\n");
110 const uint8_t *d = data + ofs;
111 fprintf(stderr, "[HexDump ]d %s: ", Prefix);
112 for( int i = 0; i < Length - ofs; i ++ )
114 if( i == 8 ) fprintf(stderr, " ");
115 fprintf(stderr, " %02x", d[i]);
117 for( int i = Length - ofs; i < 16; i ++ )
119 if( i == 8 ) fprintf(stderr, " ");
120 fprintf(stderr, " ");
122 fprintf(stderr, " |");
123 for( int i = 0; i < Length - ofs; i ++ )
125 if( i == 8 ) fprintf(stderr, " ");
126 fprintf(stderr, "%c", _prn(d[i]));
129 fprintf(stderr, "\n");
134 int giDebug_TraceLevel = 0;
136 void Debug_TraceEnter(const char *Function, const char *Format, ...)
139 //const char *Ident = "Trace";
141 for( int i = 0; i < giDebug_TraceLevel; i ++ )
142 fprintf(stderr, " ");
143 fprintf(stderr, "%s: (", Function);
146 va_start(args, Format);
151 while( *Format && isblank(*Format) )
153 if( !*Format ) break;
155 char type = *Format++;
156 const char *start = Format;
157 while( *Format && !isblank(*Format) )
161 fprintf(stderr, ",");
164 fprintf(stderr, "%.*s=", (int)(Format-start), start);
168 fprintf(stderr, "%p", va_arg(args,const void *));
171 fprintf(stderr, "\"%s\"", va_arg(args,const char *));
174 fprintf(stderr, "%i", va_arg(args,int));
177 fprintf(stderr, "0x%x", va_arg(args,unsigned int));
180 fprintf(stderr, "0x%"PRIx64, va_arg(args,uint64_t));
183 va_arg(args,uintptr_t);
184 fprintf(stderr, "?");
191 fprintf(stderr, ")");
193 giDebug_TraceLevel ++;
197 void Debug_TraceLog(const char *Function, const char *Format, ...)
200 //const char *Ident = "Trace";
203 for( int i = 0; i < giDebug_TraceLevel; i ++ )
204 fprintf(stderr, " ");
205 fprintf(stderr, "%s: ", Function);
208 va_start(args, Format);
210 vfprintf(stderr, Format, args);
217 void Debug_TraceLeave(const char *Function, char Type, ...)
219 if( giDebug_TraceLevel == 0 ) {
220 Log_Error("Debug", "Function %s called LEAVE without ENTER", Function);
224 //const char *Ident = "Trace";
228 va_start(args, Type);
230 if( giDebug_TraceLevel > 0 )
232 giDebug_TraceLevel --;
233 for( int i = 0; i < giDebug_TraceLevel; i ++ )
234 fprintf(stderr, " ");
236 fprintf(stderr, "%s: RETURN", Function);
242 fprintf(stderr, " %i", va_arg(args, int));
245 fprintf(stderr, " 0x%x", va_arg(args, unsigned int));
248 fprintf(stderr, " 0x%"PRIx64, va_arg(args,uint64_t));
251 fprintf(stderr, " \"%s\"", va_arg(args, const char *));
254 fprintf(stderr, " %p", va_arg(args, const void *));
257 fprintf(stderr, " NULL");
260 fprintf(stderr, " ?");