AcessNative - Bugfixing
[tpg/acess2.git] / Kernel / logging.c
index 1895b5b..1650342 100644 (file)
@@ -7,6 +7,8 @@
 #include <acess.h>
 
 #define PRINT_ON_APPEND        1
+#define USE_RING_BUFFER        1
+#define RING_BUFFER_SIZE       4096
 
 // === CONSTANTS ===
 enum eLogLevels
@@ -64,10 +66,15 @@ EXPORT(Log_Log);
 EXPORT(Log_Debug);
 
 // === GLOBALS ===
-tSpinlock      glLog;
-tSpinlock      glLogOutput;
+tShortSpinlock glLogOutput;
+#if USE_RING_BUFFER
+Uint8  gaLog_RingBufferData[sizeof(tRingBuffer)+RING_BUFFER_SIZE];
+tRingBuffer    *gpLog_RingBuffer = (void*)gaLog_RingBufferData;
+#else
+tMutex glLog;
 tLogList       gLog;
 tLogList       gLog_Levels[NUM_LOG_LEVELS];
+#endif
 
 // === CODE ===
 /**
@@ -86,17 +93,33 @@ void Log_AddEvent(char *Ident, int Level, char *Format, va_list Args)
        
        //Log("len = %i", len);
        
+       #if USE_RING_BUFFER
+       {
+       char    buf[sizeof(tLogEntry)+len+1];
+       ent = (void*)buf;
+       #else
        ent = malloc(sizeof(tLogEntry)+len+1);
+       #endif
        ent->Time = now();
        strncpy(ent->Ident, Ident, 8);
+       ent->Ident[8] = '\0';
        ent->Level = Level;
        ent->Length = len;
-       vsnprintf( ent->Data, 256, Format, Args );
-       
-       //Log("ent->Ident = '%s'", ent->Ident);
-       //Log("ent->Data = '%s'", ent->Data);
+       vsnprintf( ent->Data, len+1, Format, Args );
        
-       LOCK( &glLog );
+       #if USE_RING_BUFFER
+       {
+               #define LOG_HDR_LEN     (14+1+2+8+2)
+               char    newData[ LOG_HDR_LEN + len + 2 + 1 ];
+               sprintf( newData, "%014lli%s [%+8s] ",
+                       ent->Time, csaLevelCodes[Level], Ident);
+               strcpy( newData + LOG_HDR_LEN, ent->Data );
+               strcpy( newData + LOG_HDR_LEN + len, "\r\n" );
+               gpLog_RingBuffer->Space = RING_BUFFER_SIZE;     // Needed to init the buffer
+               RingBuffer_Write( gpLog_RingBuffer, newData, LOG_HDR_LEN + len + 2 );
+       }
+       #else
+       Mutex_Acquire( &glLog );
        
        ent->Next = gLog.Tail;
        if(gLog.Head)
@@ -110,12 +133,16 @@ void Log_AddEvent(char *Ident, int Level, char *Format, va_list Args)
        else
                gLog_Levels[Level].Tail = gLog_Levels[Level].Head = ent;
        
-       RELEASE( &glLog );
+       Mutex_Release( &glLog );
+       #endif
        
        #if PRINT_ON_APPEND
        Log_Int_PrintMessage( ent );
        #endif
        
+       #if USE_RING_BUFFER
+       }
+       #endif
 }
 
 /**
@@ -123,7 +150,7 @@ void Log_AddEvent(char *Ident, int Level, char *Format, va_list Args)
  */
 void Log_Int_PrintMessage(tLogEntry *Entry)
 {
-       //LOCK( &glLogOutput );
+       SHORTLOCK( &glLogOutput );
        LogF("%s%014lli%s [%+8s] %s\x1B[0m\r\n",
                csaLevelColours[Entry->Level],
                Entry->Time,
@@ -131,7 +158,7 @@ void Log_Int_PrintMessage(tLogEntry *Entry)
                Entry->Ident,
                Entry->Data
                );
-       //RELEASE( &glLogOutput );
+       SHORTREL( &glLogOutput );
 }
 
 /**

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