3 * - By John Hodge (thePowersGang)
5 * logging.c - Kernel Logging Service
9 #define PRINT_ON_APPEND 1
10 #define USE_RING_BUFFER 1
11 #define RING_BUFFER_SIZE 4096
26 const char *csaLevelColours[] = {
27 "\x1B[35m", "\x1B[34m", "\x1B[36m", "\x1B[31m",
28 "\x1B[33m", "\x1B[32m", "\x1B[0m", "\x1B[0m"
30 const char *csaLevelCodes[] = {"k","p","f","e","w","n","l","d"};
33 typedef struct sLogEntry
35 struct sLogEntry *Next;
36 struct sLogEntry *LevelNext;
43 typedef struct sLogList
50 void Log_AddEvent(const char *Ident, int Level, const char *Format, va_list Args);
51 static void Log_Int_PrintMessage(tLogEntry *Entry);
52 //void Log_KernelPanic(const char *Ident, const char *Message, ...);
53 //void Log_Panic(const char *Ident, const char *Message, ...);
54 //void Log_Error(const char *Ident, const char *Message, ...);
55 //void Log_Warning(const char *Ident, const char *Message, ...);
56 //void Log_Notice(const char *Ident, const char *Message, ...);
57 //void Log_Log(const char *Ident, const char *Message, ...);
58 //void Log_Debug(const char *Ident, const char *Message, ...);
69 tShortSpinlock glLogOutput;
71 Uint8 gaLog_RingBufferData[sizeof(tRingBuffer)+RING_BUFFER_SIZE];
72 tRingBuffer *gpLog_RingBuffer = (void*)gaLog_RingBufferData;
76 tLogList gLog_Levels[NUM_LOG_LEVELS];
81 * \brief Adds an event to the log
83 void Log_AddEvent(const char *Ident, int Level, const char *Format, va_list Args)
89 if( Level >= NUM_LOG_LEVELS ) return;
91 va_copy(args_tmp, Args);
92 len = vsnprintf(NULL, 256, Format, args_tmp);
94 //Log("len = %i", len);
98 char buf[sizeof(tLogEntry)+len+1];
101 ent = malloc(sizeof(tLogEntry)+len+1);
104 strncpy(ent->Ident, Ident, 8);
105 ent->Ident[8] = '\0';
108 vsnprintf( ent->Data, len+1, Format, Args );
112 #define LOG_HDR_LEN (14+1+2+8+2)
113 char newData[ LOG_HDR_LEN + len + 2 + 1 ];
115 strncpy(_ident, Ident, 9);
116 sprintf( newData, "%014lli%s [%-8s] ",
117 ent->Time, csaLevelCodes[Level], Ident);
118 strcpy( newData + LOG_HDR_LEN, ent->Data );
119 strcpy( newData + LOG_HDR_LEN + len, "\r\n" );
120 gpLog_RingBuffer->Space = RING_BUFFER_SIZE; // Needed to init the buffer
121 RingBuffer_Write( gpLog_RingBuffer, newData, LOG_HDR_LEN + len + 2 );
124 Mutex_Acquire( &glLog );
126 ent->Next = gLog.Tail;
130 gLog.Tail = gLog.Head = ent;
132 ent->LevelNext = gLog_Levels[Level].Tail;
133 if(gLog_Levels[Level].Head)
134 gLog_Levels[Level].Tail = ent;
136 gLog_Levels[Level].Tail = gLog_Levels[Level].Head = ent;
138 Mutex_Release( &glLog );
142 Log_Int_PrintMessage( ent );
151 * \brief Prints a log message to the debug console
153 void Log_Int_PrintMessage(tLogEntry *Entry)
155 SHORTLOCK( &glLogOutput );
156 LogF("%s%014lli%s [%-8s] %s",
157 csaLevelColours[Entry->Level],
159 csaLevelCodes[Entry->Level],
163 LogF("\x1B[0m\r\n"); // Separate in case Entry->Data is too long
164 SHORTREL( &glLogOutput );
168 * \brief KERNEL PANIC!!!!
170 void Log_KernelPanic(const char *Ident, const char *Message, ...)
173 va_start(args, Message);
174 Log_AddEvent(Ident, LOG_LEVEL_KPANIC, Message, args);
176 Panic("Log_KernelPanic");
180 * \brief Panic Message - Driver Unrecoverable error
182 void Log_Panic(const char *Ident, const char *Message, ...)
185 va_start(args, Message);
186 Log_AddEvent(Ident, LOG_LEVEL_PANIC, Message, args);
191 * \brief Error Message - Recoverable Error
193 void Log_Error(const char *Ident, const char *Message, ...)
196 va_start(args, Message);
197 Log_AddEvent(Ident, LOG_LEVEL_ERROR, Message, args);
202 * \brief Warning Message - Something the user should know
204 void Log_Warning(const char *Ident, const char *Message, ...)
208 va_start(args, Message);
209 Log_AddEvent(Ident, LOG_LEVEL_WARNING, Message, args);
214 * \brief Notice Message - Something the user might like to know
216 void Log_Notice(const char *Ident, const char *Message, ...)
219 va_start(args, Message);
220 Log_AddEvent(Ident, LOG_LEVEL_NOTICE, Message, args);
225 * \brief Log Message - Possibly useful information
227 void Log_Log(const char *Ident, const char *Message, ...)
230 va_start(args, Message);
231 Log_AddEvent(Ident, LOG_LEVEL_LOG, Message, args);
236 * \brief Debug Message - Only a developer would want this info
238 void Log_Debug(const char *Ident, const char *Message, ...)
241 va_start(args, Message);
242 Log_AddEvent(Ident, LOG_LEVEL_DEBUG, Message, args);