Tools/NetTest - TCP stack testing, going well
[tpg/acess2.git] / Tools / nativelib / logging.c
1 /*
2  * Acess2 libnative (Kernel Simulation Library)
3  * - By John Hodge (thePowersGang)
4  *
5  * logging.c
6  * - Logging functions
7  */
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <stdlib.h>
11 #include <stdint.h>
12 #include <acess_logging.h>
13 #include <ctype.h>
14 #include <inttypes.h>
15 #include <shortlock.h>
16
17 extern int      Threads_GetTID();
18
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")
21
22 #define LOG_LOCK_ACQUIRE()      do{ \
23         if(!gbThreadInLog)      SHORTLOCK(&glDebugLock); \
24         gbThreadInLog ++; \
25 }while(0)
26 #define LOG_LOCK_RELEASE()      do {\
27         gbThreadInLog --; \
28         if(!gbThreadInLog)      SHORTREL(&glDebugLock); \
29 } while(0)
30
31 #define PUTERR(col,type)        {\
32         LOG_LOCK_ACQUIRE(); \
33         LOGHDR(col,type);\
34         va_list args; va_start(args, Message);\
35         vfprintf(stderr, Message, args);\
36         va_end(args);\
37         LOGTAIL();\
38         LOG_LOCK_RELEASE(); \
39 }
40
41 // === GLOBALS ===
42 int __thread    gbThreadInLog;
43 tShortSpinlock  glDebugLock;
44
45 // === CODE ===
46 void Log_KernelPanic(const char *Ident, const char *Message, ...) {
47         PUTERR("35", "k")
48         exit(-1);
49 }
50 void Log_Panic(const char *Ident, const char *Message, ...)
51         PUTERR("34", "p")
52 void Log_Error(const char *Ident, const char *Message, ...)
53         PUTERR("31", "e")
54 void Log_Warning(const char *Ident, const char *Message, ...)
55         PUTERR("33", "w")
56 void Log_Notice(const char *Ident, const char *Message, ...)
57         PUTERR("32", "n")
58 void Log_Log(const char *Ident, const char *Message, ...)
59         PUTERR("37", "l")
60 void Log_Debug(const char *Ident, const char *Message, ...)
61         PUTERR("37", "d")
62
63 void Panic(const char *Message, ...) {
64         const char *Ident = "";
65         PUTERR("35", "k")
66         exit(-1);
67 }
68 void Warning(const char *Message, ...) {
69         const char *Ident = "";
70         PUTERR("33", "W")
71 }
72 void Log(const char *Message, ...) {
73         const char *Ident = "";
74         PUTERR("37", "L")
75 }
76 void Debug(const char *Message, ...) {
77         const char *Ident = "";
78         PUTERR("38", "D")
79 }
80
81 void Debug_HexDump(const char *Prefix, const void *Data, size_t Length)
82 {
83         const uint8_t *data = Data;
84         size_t  ofs;
85         LOG_LOCK_ACQUIRE();
86         fprintf(stderr, "[HexDump ]d %s: %i bytes\n", Prefix, (int)Length);
87         for( ofs = 0; ofs + 16 <= Length; ofs += 16 )
88         {
89                 fprintf(stderr, "[HexDump ]d %s:", Prefix);
90                 fprintf(stderr, "  %02x %02x %02x %02x %02x %02x %02x %02x",
91                         data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
92                 data += 8;
93                 fprintf(stderr, "  %02x %02x %02x %02x %02x %02x %02x %02x",
94                         data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
95                 data += 8;
96                 fprintf(stderr, "\n");
97         }
98         
99         fprintf(stderr, "[HexDump ]d %s:", Prefix);
100         for( ; ofs < Length; ofs ++ )
101         {
102                 if( ofs % 8 == 0 )      fprintf(stderr, " ");
103                 fprintf(stderr, " %02x", data[ofs%16]);
104         }
105         fprintf(stderr, "\n");
106         LOG_LOCK_RELEASE();
107 }
108
109  int    giDebug_TraceLevel = 0;
110
111 void Debug_TraceEnter(const char *Function, const char *Format, ...)
112 {
113         LOG_LOCK_ACQUIRE();
114         //const char *Ident = "Trace";
115         //LOGHDR("37","T");
116         for( int i = 0; i < giDebug_TraceLevel; i ++ )
117                 fprintf(stderr, " ");
118         fprintf(stderr, "%s: (", Function);
119
120         va_list args;
121         va_start(args, Format);
122         
123         int hasBeenPrev = 0;
124         while(*Format)
125         {
126                 while( *Format && isblank(*Format) )
127                         Format ++;
128                 if( !*Format )  break;
129                 
130                 char type = *Format++;
131                 const char *start = Format;
132                 while( *Format && !isblank(*Format) )
133                         Format ++;
134                 
135                 if(hasBeenPrev)
136                         fprintf(stderr, ",");
137                 hasBeenPrev = 1;
138                 
139                 fprintf(stderr, "%.*s=", (int)(Format-start), start);
140                 switch(type)
141                 {
142                 case 'p':
143                         fprintf(stderr, "%p", va_arg(args,const void *));
144                         break;
145                 case 's':
146                         fprintf(stderr, "\"%s\"", va_arg(args,const char *));
147                         break;
148                 case 'i':
149                         fprintf(stderr, "%i", va_arg(args,int));
150                         break;
151                 case 'x':
152                         fprintf(stderr, "0x%x", va_arg(args,unsigned int));
153                         break;
154                 case 'X':
155                         fprintf(stderr, "0x%"PRIx64, va_arg(args,uint64_t));
156                         break;
157                 default:
158                         va_arg(args,uintptr_t);
159                         fprintf(stderr, "?");
160                         break;
161                 }
162         }
163
164         va_end(args);
165
166         fprintf(stderr, ")");
167         LOGTAIL();
168         giDebug_TraceLevel ++;
169         LOG_LOCK_RELEASE();
170 }
171
172 void Debug_TraceLog(const char *Function, const char *Format, ...)
173 {
174         LOG_LOCK_ACQUIRE();
175         //const char *Ident = "Trace";
176         //LOGHDR("37","T");
177         
178         for( int i = 0; i < giDebug_TraceLevel; i ++ )
179                 fprintf(stderr, " ");
180         fprintf(stderr, "%s: ", Function);
181         
182         va_list args;
183         va_start(args, Format);
184
185         vfprintf(stderr, Format, args);
186         
187         va_end(args);
188         LOGTAIL();
189         LOG_LOCK_RELEASE();
190 }
191
192 void Debug_TraceLeave(const char *Function, char Type, ...)
193 {
194         if( giDebug_TraceLevel == 0 ) {
195                 Log_Error("Debug", "Function %s called LEAVE without ENTER", Function);
196         }
197         
198         LOG_LOCK_ACQUIRE();
199         //const char *Ident = "Trace";
200         //LOGHDR("37","T");
201         
202         va_list args;
203         va_start(args, Type);
204
205         if( giDebug_TraceLevel > 0 )
206         {       
207                 giDebug_TraceLevel --;
208                 for( int i = 0; i < giDebug_TraceLevel; i ++ )
209                         fprintf(stderr, " ");
210         }
211         fprintf(stderr, "%s: RETURN", Function);
212         switch(Type)
213         {
214         case '-':
215                 break;
216         case 'i':
217                 fprintf(stderr, " %i", va_arg(args, int));
218                 break;
219         case 'x':
220                 fprintf(stderr, " 0x%x", va_arg(args, unsigned int));
221                 break;
222         case 'X':
223                 fprintf(stderr, " 0x%"PRIx64, va_arg(args,uint64_t));
224                 break;
225         case 's':
226                 fprintf(stderr, " \"%s\"", va_arg(args, const char *));
227                 break;
228         case 'p':
229                 fprintf(stderr, " %p", va_arg(args, const void *));
230                 break;
231         case 'n':
232                 fprintf(stderr, " NULL");
233                 break;
234         default:
235                 fprintf(stderr, " ?");
236                 break;
237         }
238         
239         va_end(args);
240         LOGTAIL();
241         LOG_LOCK_RELEASE();
242 }
243

UCC git Repository :: git.ucc.asn.au