Directory browser is now available in config panel, and works! Will only run files...
[pandora-libraries.git] / lib / pnd_logger.c
index c3bcc43..79b8558 100644 (file)
@@ -2,10 +2,13 @@
 #include <stdarg.h> // va-args
 #include <stdlib.h> // malloc/free
 #include <string.h> // strdup
+#include <time.h>
 #include "pnd_logger.h"
 
-char *log_pretext = NULL;
-unsigned char log_filterlevel = 0;
+static char *log_pretext = NULL;
+static unsigned char log_filterlevel = 0;
+static unsigned char log_flushafter = 0;
+static time_t log_first = 0;
 
 typedef enum {
   pndl_nil = 0,
@@ -43,6 +46,15 @@ unsigned char pnd_log_set_filter ( unsigned char newlevel ) {
   return ( foo );
 }
 
+unsigned char pnd_log_get_filter ( void ) {
+  return ( log_filterlevel );
+}
+
+void pnd_log_set_flush ( unsigned char x ) {
+  log_flushafter = x;
+  return;
+}
+
 void pnd_log_set_pretext ( char *pre ) {
 
   if ( log_pretext ) {
@@ -95,7 +107,7 @@ unsigned char pnd_log_max_targets ( void ) {
   return ( PND_LOG_MAX );
 }
 
-void pnd_log_emit ( char *message ) {
+static void pnd_log_emit ( unsigned char level, char *message ) {
   unsigned char i;
 
   // iterate across targets and attempt to emit
@@ -108,11 +120,21 @@ void pnd_log_emit ( char *message ) {
       break;
 
     case pndl_stream:
+      // pretext
       if ( log_pretext ) {
-       fprintf ( log_targets [ i ].stream, "%s\t", log_pretext );
+       fprintf ( log_targets [ i ].stream, "%s ", log_pretext );
       }
+      // log level
+      fprintf ( log_targets [ i ].stream, "%u %u\t", level, (unsigned int) (time ( NULL ) - log_first) );
+      // message
       if ( message ) {
-       fprintf ( log_targets [ i ].stream, "%s\n", message );
+       fprintf ( log_targets [ i ].stream, "%s", message );
+       if ( strchr ( message, '\n' ) == NULL ) {
+         fprintf ( log_targets [ i ].stream, "\n" );
+       }
+       if ( log_flushafter ) {
+         fflush ( log_targets [ i ].stream );
+       }
       }
       break;
 
@@ -133,7 +155,13 @@ void pnd_log_emit ( char *message ) {
 
 unsigned char pnd_log ( unsigned char level, char *fmt, ... ) {
 
-  if ( level < log_filterlevel ) {
+  if ( log_first == 0 ) {
+    log_first = time ( NULL );
+  }
+
+  if ( level == PND_LOG_FORCE ) {
+    // always proceed
+  } else if ( level < log_filterlevel ) {
     return ( 0 ); // too low level
   }
 
@@ -155,7 +183,7 @@ unsigned char pnd_log ( unsigned char level, char *fmt, ... ) {
 
     /* If that worked, return the string. */
     if ( n > -1 && n < size ) {
-      pnd_log_emit ( p );
+      pnd_log_emit ( level, p );
       break;
     }
 
@@ -179,3 +207,17 @@ unsigned char pnd_log ( unsigned char level, char *fmt, ... ) {
 
   return ( 1 );
 }
+
+
+static unsigned char _do_buried_logging = 0;
+void pnd_log_set_buried_logging ( unsigned char yesno ) {
+  _do_buried_logging = yesno;
+  return;
+}
+
+unsigned char pnd_log_do_buried_logging ( void ) {
+  if ( _do_buried_logging == 1 ) {
+    return ( 1 );
+  }
+  return ( 0 );
+}