3 * - By John Hodge (thePowersGang)
5 * logging.c - Kernel Logging Service
10 #define CACHE_MESSAGES 0
11 #define PRINT_ON_APPEND 1
12 #define USE_RING_BUFFER 1
13 #define RING_BUFFER_SIZE 4096
28 const char *csaLevelColours[] = {
29 "\x1B[35m", "\x1B[34m", "\x1B[36m", "\x1B[31m",
30 "\x1B[33m", "\x1B[32m", "\x1B[0m", "\x1B[0m"
32 const char *csaLevelCodes[] = {"k","p","f","e","w","n","l","d"};
35 typedef struct sLogEntry
37 struct sLogEntry *Next;
38 struct sLogEntry *LevelNext;
45 typedef struct sLogList
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, ...);
71 tShortSpinlock glLogOutput;
73 Uint8 gaLog_RingBufferData[sizeof(tRingBuffer)+RING_BUFFER_SIZE];
74 tRingBuffer *gpLog_RingBuffer = (void*)gaLog_RingBufferData;
78 tLogList gLog_Levels[NUM_LOG_LEVELS];
83 * \brief Adds an event to the log
85 void Log_AddEvent(const char *Ident, int Level, const char *Format, va_list Args)
91 if( Level >= NUM_LOG_LEVELS ) return;
93 va_copy(args_tmp, Args);
94 len = vsnprintf(NULL, 256, Format, args_tmp);
96 //Log("len = %i", len);
98 #if USE_RING_BUFFER || !CACHE_MESSAGES
100 char buf[sizeof(tLogEntry)+len+1];
103 ent = malloc(sizeof(tLogEntry)+len+1);
106 strncpy(ent->Ident, Ident, 8);
107 ent->Ident[8] = '\0';
110 vsnprintf( ent->Data, len+1, Format, Args );
115 #define LOG_HDR_LEN (14+1+2+8+2)
116 char newData[ LOG_HDR_LEN + len + 2 + 1 ];
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 );
127 Mutex_Acquire( &glLog );
129 ent->Next = gLog.Tail;
133 gLog.Tail = gLog.Head = ent;
135 ent->LevelNext = gLog_Levels[Level].Tail;
136 if(gLog_Levels[Level].Head)
137 gLog_Levels[Level].Tail = ent;
139 gLog_Levels[Level].Tail = gLog_Levels[Level].Head = ent;
141 Mutex_Release( &glLog );
145 #if PRINT_ON_APPEND || !CACHE_MESSAGES
146 Log_Int_PrintMessage( ent );
149 #if USE_RING_BUFFER || !CACHE_MESSAGES
155 * \brief Prints a log message to the debug console
157 void Log_Int_PrintMessage(tLogEntry *Entry)
159 SHORTLOCK( &glLogOutput );
160 LogF("%s%014lli%s [%-8s] %s",
161 csaLevelColours[Entry->Level],
163 csaLevelCodes[Entry->Level],
167 LogF("\x1B[0m\r\n"); // Separate in case Entry->Data is too long
168 SHORTREL( &glLogOutput );
172 * \brief KERNEL PANIC!!!!
174 void Log_KernelPanic(const char *Ident, const char *Message, ...)
177 va_start(args, Message);
178 Log_AddEvent(Ident, LOG_LEVEL_KPANIC, Message, args);
180 Panic("Log_KernelPanic");
184 * \brief Panic Message - Driver Unrecoverable error
186 void Log_Panic(const char *Ident, const char *Message, ...)
189 va_start(args, Message);
190 Log_AddEvent(Ident, LOG_LEVEL_PANIC, Message, args);
195 * \brief Error Message - Recoverable Error
197 void Log_Error(const char *Ident, const char *Message, ...)
200 va_start(args, Message);
201 Log_AddEvent(Ident, LOG_LEVEL_ERROR, Message, args);
206 * \brief Warning Message - Something the user should know
208 void Log_Warning(const char *Ident, const char *Message, ...)
212 va_start(args, Message);
213 Log_AddEvent(Ident, LOG_LEVEL_WARNING, Message, args);
218 * \brief Notice Message - Something the user might like to know
220 void Log_Notice(const char *Ident, const char *Message, ...)
223 va_start(args, Message);
224 Log_AddEvent(Ident, LOG_LEVEL_NOTICE, Message, args);
229 * \brief Log Message - Possibly useful information
231 void Log_Log(const char *Ident, const char *Message, ...)
234 va_start(args, Message);
235 Log_AddEvent(Ident, LOG_LEVEL_LOG, Message, args);
240 * \brief Debug Message - Only a developer would want this info
242 void Log_Debug(const char *Ident, const char *Message, ...)
245 va_start(args, Message);
246 Log_AddEvent(Ident, LOG_LEVEL_DEBUG, Message, args);