Merge branch 'master' of git://git.ucc.asn.au/tpg/acess2
[tpg/acess2.git] / KernelLand / Kernel / logging.c
1 /*
2  * Acess 2 Kernel
3  * - By John Hodge (thePowersGang)
4  *
5  * logging.c - Kernel Logging Service
6  */
7 #include <acess.h>
8 #include <adt.h>
9
10 #define CACHE_MESSAGES  0
11 #define PRINT_ON_APPEND 1
12 #define USE_RING_BUFFER 1
13 #define RING_BUFFER_SIZE        4096
14
15 // === CONSTANTS ===
16 enum eLogLevels
17 {
18         LOG_LEVEL_KPANIC,
19         LOG_LEVEL_PANIC,
20         LOG_LEVEL_FATAL,
21         LOG_LEVEL_ERROR,
22         LOG_LEVEL_WARNING,
23         LOG_LEVEL_NOTICE,
24         LOG_LEVEL_LOG,
25         LOG_LEVEL_DEBUG,
26         NUM_LOG_LEVELS
27 };
28 const char      *csaLevelColours[] = {
29                 "\x1B[35m", "\x1B[34m", "\x1B[36m", "\x1B[31m",
30                 "\x1B[33m", "\x1B[32m", "\x1B[0m", "\x1B[0m"
31                 };
32 const char      *csaLevelCodes[] =  {"k","p","f","e","w","n","l","d"};
33
34 // === TYPES ===
35 typedef struct sLogEntry
36 {
37         struct sLogEntry        *Next;
38         struct sLogEntry        *LevelNext;
39         Sint64  Time;
40          int    Level;
41          int    Length;
42         char    Ident[9];
43         char    Data[];
44 }       tLogEntry;
45 typedef struct sLogList
46 {
47         tLogEntry       *Head;
48         tLogEntry       *Tail;
49 }       tLogList;
50
51 // === PROTOTYPES ===
52 void    Log_AddEvent(const char *Ident, int Level, const char *Format, va_list Args);
53 static void     Log_Int_PrintMessage(tLogEntry *Entry);
54 //void  Log_KernelPanic(const char *Ident, const char *Message, ...);
55 //void  Log_Panic(const char *Ident, const char *Message, ...);
56 //void  Log_Error(const char *Ident, const char *Message, ...);
57 //void  Log_Warning(const char *Ident, const char *Message, ...);
58 //void  Log_Notice(const char *Ident, const char *Message, ...);
59 //void  Log_Log(const char *Ident, const char *Message, ...);
60 //void  Log_Debug(const char *Ident, const char *Message, ...);
61
62 // === EXPORTS ===
63 EXPORT(Log_Panic);
64 EXPORT(Log_Error);
65 EXPORT(Log_Warning);
66 EXPORT(Log_Notice);
67 EXPORT(Log_Log);
68 EXPORT(Log_Debug);
69
70 // === GLOBALS ===
71 tShortSpinlock  glLogOutput;
72 #if CACHE_MESSAGES
73 # if USE_RING_BUFFER
74 Uint8   gaLog_RingBufferData[sizeof(tRingBuffer)+RING_BUFFER_SIZE];
75 tRingBuffer     *gpLog_RingBuffer = (void*)gaLog_RingBufferData;
76 # else
77 tMutex  glLog;
78 tLogList        gLog;
79 tLogList        gLog_Levels[NUM_LOG_LEVELS];
80 # endif // USE_RING_BUFFER
81 #endif // CACHE_MESSAGES
82
83 // === CODE ===
84 /**
85  * \brief Adds an event to the log
86  */
87 void Log_AddEvent(const char *Ident, int Level, const char *Format, va_list Args)
88 {
89          int    len;
90         tLogEntry       *ent;
91         va_list args_tmp;
92         
93         if( Level >= NUM_LOG_LEVELS )   return;
94
95         va_copy(args_tmp, Args);
96         len = vsnprintf(NULL, 0, Format, args_tmp);
97         
98         #if USE_RING_BUFFER || !CACHE_MESSAGES
99         {
100         char    buf[sizeof(tLogEntry)+len+1];
101         ent = (void*)buf;
102         #else
103         ent = malloc(sizeof(tLogEntry)+len+1);
104         #endif
105         ent->Time = now();
106         strncpy(ent->Ident, Ident, 8);
107         ent->Ident[8] = '\0';
108         ent->Level = Level;
109         ent->Length = len;
110         vsnprintf( ent->Data, len+1, Format, Args );
111
112         #if CACHE_MESSAGES
113         # if USE_RING_BUFFER
114         {
115                 #define LOG_HDR_LEN     (14+1+2+8+2)
116                 char    newData[ LOG_HDR_LEN + len + 2 + 1 ];
117                 char    _ident[9];
118                 strncpy(_ident, Ident, 9);
119                 sprintf( newData, "%014lli%s [%-8s] ",
120                         ent->Time, csaLevelCodes[Level], Ident);
121                 strcpy( newData + LOG_HDR_LEN, ent->Data );
122                 strcpy( newData + LOG_HDR_LEN + len, "\r\n" );
123                 gpLog_RingBuffer->Space = RING_BUFFER_SIZE;     // Needed to init the buffer
124                 RingBuffer_Write( gpLog_RingBuffer, newData, LOG_HDR_LEN + len + 2 );
125         }
126         # else
127         Mutex_Acquire( &glLog );
128         
129         ent->Next = gLog.Tail;
130         if(gLog.Head)
131                 gLog.Tail = ent;
132         else
133                 gLog.Tail = gLog.Head = ent;
134         
135         ent->LevelNext = gLog_Levels[Level].Tail;
136         if(gLog_Levels[Level].Head)
137                 gLog_Levels[Level].Tail = ent;
138         else
139                 gLog_Levels[Level].Tail = gLog_Levels[Level].Head = ent;
140         
141         Mutex_Release( &glLog );
142         # endif
143         #endif
144         
145         #if PRINT_ON_APPEND || !CACHE_MESSAGES
146         Log_Int_PrintMessage( ent );
147         #endif
148         
149         #if USE_RING_BUFFER || !CACHE_MESSAGES
150         }
151         #endif
152 }
153
154 /**
155  * \brief Prints a log message to the debug console
156  */
157 void Log_Int_PrintMessage(tLogEntry *Entry)
158 {
159         if( CPU_HAS_LOCK(&glLogOutput) )
160                 return ;        // TODO: Error?
161         SHORTLOCK( &glLogOutput );
162         LogF("%s%014lli",
163                 csaLevelColours[Entry->Level],
164                 Entry->Time
165                 );
166         LogF("%s [%-8s] %i - %s",
167                 csaLevelCodes[Entry->Level],
168                 Entry->Ident,
169                 Threads_GetTID(),
170                 Entry->Data
171                 );
172         LogF("\x1B[0m\r\n");    // Separate in case Entry->Data is too long
173         SHORTREL( &glLogOutput );
174 }
175
176 /**
177  * \brief KERNEL PANIC!!!!
178  */
179 void Log_KernelPanic(const char *Ident, const char *Message, ...)
180 {
181         va_list args;   
182         va_start(args, Message);
183         Log_AddEvent(Ident, LOG_LEVEL_KPANIC, Message, args);
184         va_end(args);
185         Panic("Log_KernelPanic - %s", Ident);
186 }
187
188 /**
189  * \brief Panic Message - Driver Unrecoverable error
190  */
191 void Log_Panic(const char *Ident, const char *Message, ...)
192 {
193         va_list args;   
194         va_start(args, Message);
195         Log_AddEvent(Ident, LOG_LEVEL_PANIC, Message, args);
196         va_end(args);
197 }
198
199 /**
200  * \brief Error Message - Recoverable Error
201  */
202 void Log_Error(const char *Ident, const char *Message, ...)
203 {
204         va_list args;   
205         va_start(args, Message);
206         Log_AddEvent(Ident, LOG_LEVEL_ERROR, Message, args);
207         va_end(args);
208 }
209
210 /**
211  * \brief Warning Message - Something the user should know
212  */
213 void Log_Warning(const char *Ident, const char *Message, ...)
214 {
215         va_list args;
216         
217         va_start(args, Message);
218         Log_AddEvent(Ident, LOG_LEVEL_WARNING, Message, args);
219         va_end(args);
220 }
221
222 /**
223  * \brief Notice Message - Something the user might like to know
224  */
225 void Log_Notice(const char *Ident, const char *Message, ...)
226 {
227         va_list args;   
228         va_start(args, Message);
229         Log_AddEvent(Ident, LOG_LEVEL_NOTICE, Message, args);
230         va_end(args);
231 }
232
233 /**
234  * \brief Log Message - Possibly useful information
235  */
236 void Log_Log(const char *Ident, const char *Message, ...)
237 {
238         va_list args;   
239         va_start(args, Message);
240         Log_AddEvent(Ident, LOG_LEVEL_LOG, Message, args);
241         va_end(args);
242 }
243
244 /**
245  * \brief Debug Message - Only a developer would want this info
246  */
247 void Log_Debug(const char *Ident, const char *Message, ...)
248 {
249         va_list args;   
250         va_start(args, Message);
251         Log_AddEvent(Ident, LOG_LEVEL_DEBUG, Message, args);
252         va_end(args);
253 }

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