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[] = {
38 typedef struct sLogEntry
40 struct sLogEntry *Next;
41 struct sLogEntry *LevelNext;
48 typedef struct sLogList
55 void Log_AddEvent(const char *Ident, int Level, const char *Format, va_list Args);
56 static void Log_Int_PrintMessage(tLogEntry *Entry);
57 //void Log_KernelPanic(const char *Ident, const char *Message, ...);
58 //void Log_Panic(const char *Ident, const char *Message, ...);
59 //void Log_Error(const char *Ident, const char *Message, ...);
60 //void Log_Warning(const char *Ident, const char *Message, ...);
61 //void Log_Notice(const char *Ident, const char *Message, ...);
62 //void Log_Log(const char *Ident, const char *Message, ...);
63 //void Log_Debug(const char *Ident, const char *Message, ...);
74 tShortSpinlock glLogOutput;
77 Uint8 gaLog_RingBufferData[sizeof(tRingBuffer)+RING_BUFFER_SIZE];
78 tRingBuffer *gpLog_RingBuffer = (void*)gaLog_RingBufferData;
82 tLogList gLog_Levels[NUM_LOG_LEVELS];
83 # endif // USE_RING_BUFFER
84 #endif // CACHE_MESSAGES
88 * \brief Adds an event to the log
90 void Log_AddEvent(const char *Ident, int Level, const char *Format, va_list Args)
96 if( Level >= NUM_LOG_LEVELS ) return;
98 va_copy(args_tmp, Args);
99 len = vsnprintf(NULL, 0, Format, args_tmp);
101 #if USE_RING_BUFFER || !CACHE_MESSAGES
103 char buf[sizeof(tLogEntry)+len+1];
106 ent = malloc(sizeof(tLogEntry)+len+1);
109 strncpy(ent->Ident, Ident, 8);
110 ent->Ident[8] = '\0';
113 vsnprintf( ent->Data, len+1, Format, Args );
118 #define LOG_HDR_LEN (14+1+2+8+2)
119 char newData[ LOG_HDR_LEN + len + 2 + 1 ];
120 sprintf( newData, "%014lli%s [%-8.8s] ",
121 ent->Time, csaLevelCodes[Level], Ident);
122 strcpy( newData + LOG_HDR_LEN, ent->Data );
123 strcpy( newData + LOG_HDR_LEN + len, "\r\n" );
124 gpLog_RingBuffer->Space = RING_BUFFER_SIZE; // Needed to init the buffer
125 RingBuffer_Write( gpLog_RingBuffer, newData, LOG_HDR_LEN + len + 2 );
128 Mutex_Acquire( &glLog );
130 ent->Next = gLog.Tail;
131 tLogEntry **pnp = (gLog.Tail ? &gLog.Tail->Next : &gLog.Head);
135 ent->LevelNext = gLog_Levels[Level].Tail;
136 pnp = (gLog_Levels[Level].Tail ? &gLog_Levels[Level].Tail->LevelNext : &gLog_Levels[Level].Head);
138 gLog_Levels[Level].Tail = ent;
140 Mutex_Release( &glLog );
144 #if PRINT_ON_APPEND || !CACHE_MESSAGES
145 Log_Int_PrintMessage( ent );
148 #if USE_RING_BUFFER || !CACHE_MESSAGES
154 * \brief Prints a log message to the debug console
156 void Log_Int_PrintMessage(tLogEntry *Entry)
158 if( CPU_HAS_LOCK(&glLogOutput) )
159 return ; // TODO: Error?
160 SHORTLOCK( &glLogOutput );
162 csaLevelColours[Entry->Level],
165 LogF("%s [%-8s] %i - %.*s",
166 csaLevelCodes[Entry->Level],
172 LogF("\x1B[0m\r\n"); // Separate in case Entry->Data is too long
173 SHORTREL( &glLogOutput );
177 * \brief KERNEL PANIC!!!!
179 void Log_KernelPanic(const char *Ident, const char *Message, ...)
182 va_start(args, Message);
183 Log_AddEvent(Ident, LOG_LEVEL_KPANIC, Message, args);
185 Panic("Log_KernelPanic - %s", Ident);
189 * \brief Panic Message - Driver Unrecoverable error
191 void Log_Panic(const char *Ident, const char *Message, ...)
194 va_start(args, Message);
195 Log_AddEvent(Ident, LOG_LEVEL_PANIC, Message, args);
200 * \brief Error Message - Recoverable Error
202 void Log_Error(const char *Ident, const char *Message, ...)
205 va_start(args, Message);
206 Log_AddEvent(Ident, LOG_LEVEL_ERROR, Message, args);
211 * \brief Warning Message - Something the user should know
213 void Log_Warning(const char *Ident, const char *Message, ...)
217 va_start(args, Message);
218 Log_AddEvent(Ident, LOG_LEVEL_WARNING, Message, args);
223 * \brief Notice Message - Something the user might like to know
225 void Log_Notice(const char *Ident, const char *Message, ...)
228 va_start(args, Message);
229 Log_AddEvent(Ident, LOG_LEVEL_NOTICE, Message, args);
234 * \brief Log Message - Possibly useful information
236 void Log_Log(const char *Ident, const char *Message, ...)
239 va_start(args, Message);
240 Log_AddEvent(Ident, LOG_LEVEL_LOG, Message, args);
245 * \brief Debug Message - Only a developer would want this info
247 void Log_Debug(const char *Ident, const char *Message, ...)
250 va_start(args, Message);
251 Log_AddEvent(Ident, LOG_LEVEL_DEBUG, Message, args);