]> git.sesse.net Git - vlc/blobdiff - src/misc/messages.c
* ./src/libvlc.c, ./include/main.h: the root of all objects is now
[vlc] / src / misc / messages.c
index d6741e8c0377f079a663ed63e3350cc9be8a53d7..3ae6ad868ca8a61a278cb2e42bd6371f58b6abc0 100644 (file)
@@ -4,7 +4,7 @@
  * modules, especially intf modules. See config.h for output configuration.
  *****************************************************************************
  * Copyright (C) 1998-2002 VideoLAN
- * $Id: messages.c,v 1.2 2002/06/01 18:04:49 sam Exp $
+ * $Id: messages.c,v 1.11 2002/10/03 13:21:55 sam Exp $
  *
  * Authors: Vincent Seguin <seguin@via.ecp.fr>
  *          Samuel Hocevar <sam@zoy.org>
@@ -48,6 +48,7 @@
 static void QueueMsg ( vlc_object_t *, int , const char *,
                        const char *, va_list );
 static void FlushMsg ( msg_bank_t * );
+static void PrintMsg ( vlc_object_t *, msg_item_t * );
 
 #if defined( WIN32 )
 static char *ConvertPrintfFormatString ( const char *psz_format );
@@ -62,12 +63,39 @@ static char *ConvertPrintfFormatString ( const char *psz_format );
 void __msg_Create( vlc_object_t *p_this )
 {
     /* Message queue initialization */
-    vlc_mutex_init( p_this, &p_this->p_vlc->msg_bank.lock );
-    p_this->p_vlc->msg_bank.i_start = 0;
-    p_this->p_vlc->msg_bank.i_stop = 0;
+    vlc_mutex_init( p_this, &p_this->p_libvlc->msg_bank.lock );
 
-    p_this->p_vlc->msg_bank.i_sub = 0;
-    p_this->p_vlc->msg_bank.pp_sub = NULL;
+    p_this->p_libvlc->msg_bank.b_configured = VLC_FALSE;
+    p_this->p_libvlc->msg_bank.b_overflow = VLC_FALSE;
+
+    p_this->p_libvlc->msg_bank.i_start = 0;
+    p_this->p_libvlc->msg_bank.i_stop = 0;
+
+    p_this->p_libvlc->msg_bank.i_sub = 0;
+    p_this->p_libvlc->msg_bank.pp_sub = NULL;
+}
+
+/*****************************************************************************
+ * msg_Flush: flush the message queue
+ *****************************************************************************/
+void __msg_Flush( vlc_object_t *p_this )
+{
+    int i_index;
+
+    vlc_mutex_lock( &p_this->p_libvlc->msg_bank.lock );
+
+    p_this->p_libvlc->msg_bank.b_configured = VLC_TRUE;
+
+    for( i_index = p_this->p_libvlc->msg_bank.i_start;
+         i_index != p_this->p_libvlc->msg_bank.i_stop;
+         i_index = (i_index+1) % VLC_MSG_QSIZE )
+    {
+        PrintMsg( p_this, &p_this->p_libvlc->msg_bank.msg[i_index] );
+    }
+
+    FlushMsg( &p_this->p_libvlc->msg_bank );
+
+    vlc_mutex_unlock( &p_this->p_libvlc->msg_bank.lock );
 }
 
 /*****************************************************************************
@@ -79,16 +107,23 @@ void __msg_Create( vlc_object_t *p_this )
  *****************************************************************************/
 void __msg_Destroy( vlc_object_t *p_this )
 {
-    /* Destroy lock */
-    vlc_mutex_destroy( &p_this->p_vlc->msg_bank.lock );
+    if( p_this->p_libvlc->msg_bank.i_sub )
+    {
+        msg_Err( p_this, "stale interface subscribers" );
+    }
 
-    if( p_this->p_vlc->msg_bank.i_sub )
+    /* Flush the queue */
+    if( !p_this->p_libvlc->msg_bank.b_configured )
+    {
+        msg_Flush( p_this );
+    }
+    else
     {
-        fprintf( stderr, "main error: stale interface subscribers\n" );
+        FlushMsg( &p_this->p_libvlc->msg_bank );
     }
 
-    /* Free remaining messages */
-    FlushMsg( &p_this->p_vlc->msg_bank );
+    /* Destroy lock */
+    vlc_mutex_destroy( &p_this->p_libvlc->msg_bank.lock );
 }
 
 /*****************************************************************************
@@ -96,24 +131,25 @@ void __msg_Destroy( vlc_object_t *p_this )
  *****************************************************************************/
 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this )
 {
+    msg_bank_t *p_bank = &p_this->p_libvlc->msg_bank;
     msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
 
-    vlc_mutex_lock( &p_this->p_vlc->msg_bank.lock );
+    vlc_mutex_lock( &p_bank->lock );
 
     /* Add subscription to the list */
-    p_this->p_vlc->msg_bank.i_sub++;
-    p_this->p_vlc->msg_bank.pp_sub = realloc( p_this->p_vlc->msg_bank.pp_sub,
-        p_this->p_vlc->msg_bank.i_sub * sizeof( msg_subscription_t* ) );
+    p_bank->i_sub++;
+    p_bank->pp_sub = realloc( p_bank->pp_sub,
+                              p_bank->i_sub * sizeof( msg_subscription_t* ) );
 
-    p_this->p_vlc->msg_bank.pp_sub[ p_this->p_vlc->msg_bank.i_sub - 1 ] = p_sub;
+    p_bank->pp_sub[ p_bank->i_sub - 1 ] = p_sub;
 
-    p_sub->i_start = p_this->p_vlc->msg_bank.i_start;
-    p_sub->pi_stop = &p_this->p_vlc->msg_bank.i_stop;
+    p_sub->i_start = p_bank->i_start;
+    p_sub->pi_stop = &p_bank->i_stop;
 
-    p_sub->p_msg   = p_this->p_vlc->msg_bank.msg;
-    p_sub->p_lock  = &p_this->p_vlc->msg_bank.lock;
+    p_sub->p_msg   = p_bank->msg;
+    p_sub->p_lock  = &p_bank->lock;
 
-    vlc_mutex_unlock( &p_this->p_vlc->msg_bank.lock );
+    vlc_mutex_unlock( &p_bank->lock );
 
     return p_sub;
 }
@@ -123,53 +159,54 @@ msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this )
  *****************************************************************************/
 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
 {
+    msg_bank_t *p_bank = &p_this->p_libvlc->msg_bank;
     int i_index;
 
-    vlc_mutex_lock( &p_this->p_vlc->msg_bank.lock );
+    vlc_mutex_lock( &p_bank->lock );
 
     /* Sanity check */
-    if( !p_this->p_vlc->msg_bank.i_sub )
+    if( !p_bank->i_sub )
     {
         msg_Err( p_this, "no subscriber in the list" );
         return;
     }
 
     /* Look for the appropriate subscription */
-    for( i_index = 0; i_index < p_this->p_vlc->msg_bank.i_sub; i_index++ )
+    for( i_index = 0; i_index < p_bank->i_sub; i_index++ )
     {
-        if( p_this->p_vlc->msg_bank.pp_sub[ i_index ] == p_sub )
+        if( p_bank->pp_sub[ i_index ] == p_sub )
         {
             break;
         }
     }
 
-    if( p_this->p_vlc->msg_bank.pp_sub[ i_index ] != p_sub )
+    if( p_bank->pp_sub[ i_index ] != p_sub )
     {
         msg_Err( p_this, "subscriber not found" );
-        vlc_mutex_unlock( &p_this->p_vlc->msg_bank.lock );
+        vlc_mutex_unlock( &p_bank->lock );
         return;
     }
 
     /* Remove this subscription */
-    for( ; i_index < (p_this->p_vlc->msg_bank.i_sub - 1); i_index++ )
+    for( ; i_index < (p_bank->i_sub - 1); i_index++ )
     {
-        p_this->p_vlc->msg_bank.pp_sub[ i_index ] = p_this->p_vlc->msg_bank.pp_sub[ i_index+1 ];
+        p_bank->pp_sub[ i_index ] = p_bank->pp_sub[ i_index+1 ];
     }
 
-    p_this->p_vlc->msg_bank.i_sub--;
-    if( p_this->p_vlc->msg_bank.i_sub )
+    p_bank->i_sub--;
+    if( p_bank->i_sub )
     {
-        p_this->p_vlc->msg_bank.pp_sub = realloc( p_this->p_vlc->msg_bank.pp_sub,
-            p_this->p_vlc->msg_bank.i_sub * sizeof( msg_subscription_t* ) );
+        p_bank->pp_sub = realloc( p_bank->pp_sub, p_bank->i_sub
+                                   * sizeof( msg_subscription_t* ) );
     }
     else
     {
-        free( p_this->p_vlc->msg_bank.pp_sub );
-        p_this->p_vlc->msg_bank.pp_sub = NULL;
+        free( p_bank->pp_sub );
+        p_bank->pp_sub = NULL;
     }
 
 
-    vlc_mutex_unlock( &p_this->p_vlc->msg_bank.lock );
+    vlc_mutex_unlock( &p_bank->lock );
 }
 
 /*****************************************************************************
@@ -187,79 +224,22 @@ void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
     va_end( args );
 }
 
-void __msg_Info( void *p_this, const char *psz_format, ... )
-{
-    va_list args;
-    va_start( args, psz_format );
-    QueueMsg( (vlc_object_t *)p_this, VLC_MSG_INFO, "unknown",
-              psz_format, args );
-    va_end( args );
-}
-
-void __msg_Err( void *p_this, const char *psz_format, ... )
-{
-    va_list args;
-    va_start( args, psz_format );
-    QueueMsg( (vlc_object_t *)p_this, VLC_MSG_ERR, "unknown",
-              psz_format, args );
-    va_end( args );
-}
-
-void __msg_Warn( void *p_this, const char *psz_format, ... )
-{
-    va_list args;
-    va_start( args, psz_format );
-    QueueMsg( (vlc_object_t *)p_this, VLC_MSG_WARN, "unknown",
-              psz_format, args );
-    va_end( args );
-}
-
-void __msg_Dbg( void *p_this, const char *psz_format, ... )
-{
-    va_list args;
-    va_start( args, psz_format );
-    QueueMsg( (vlc_object_t *)p_this, VLC_MSG_DBG, "unknown",
-              psz_format, args );
-    va_end( args );
-}
-
-#if 0
-/*****************************************************************************
- * intf_WarnHexDump : print a hexadecimal dump of a memory area
- *****************************************************************************
- * This is convenient for debugging purposes.
- *****************************************************************************/
-void intf_WarnHexDump( int i_level, void *p_data, int i_size )
-{
-    int   i_index = 0;
-    int   i_subindex;
-    char  p_string[75];
-    u8   *p_area = (u8 *)p_data;
-
-    msg_Dbg( "dumping %i bytes at address %p", i_size, p_data );
-
-    while( i_index < i_size )
-    {
-        i_subindex = 0;
-
-        while( ( i_subindex < 24 ) && ( i_index + i_subindex < i_size ) )
-        {
-            sprintf( p_string + 3 * i_subindex, "%.2x ",
-                     p_area[ i_index + i_subindex ] );
-
-            i_subindex++;
-        }
-
-        /* -1 here is safe because we know we printed at least one */
-        p_string[ 3 * i_subindex - 1 ] = '\0';
-        msg_Dbg( "0x%.4x: %s", i_index, p_string );
-
-        i_index += 24;
-    }
-
-    msg_Dbg( "hexdump: %i bytes dumped", i_size );
-}
-#endif
+/* Generic functions used when variadic macros are not available. */
+#define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
+    void FN_NAME( void *p_this, const char *psz_format, ... ) \
+    { \
+        va_list args; \
+        va_start( args, psz_format ); \
+        QueueMsg( (vlc_object_t *)p_this, FN_TYPE, "unknown", \
+                  psz_format, args ); \
+        va_end( args ); \
+    } \
+    struct _
+
+DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
+DECLARE_MSG_FN( __msg_Err,  VLC_MSG_ERR );
+DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
+DECLARE_MSG_FN( __msg_Dbg,  VLC_MSG_DBG );
 
 /*****************************************************************************
  * QueueMsg: add a message to a queue
@@ -272,11 +252,15 @@ void intf_WarnHexDump( int i_level, void *p_data, int i_size )
 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
                       const char *psz_format, va_list args )
 {
-    static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
-    char *              psz_str;                 /* formatted message string */
-    msg_item_t *        p_item;                        /* pointer to message */
+    msg_bank_t * p_bank = &p_this->p_libvlc->msg_bank;       /* message bank */
+    char *       psz_str = NULL;                 /* formatted message string */
+    msg_item_t * p_item = NULL;                        /* pointer to message */
+    msg_item_t   item;                    /* message in case of a full queue */
 #ifdef WIN32
-    char *              psz_temp;
+    char *       psz_temp;
+#endif
+#ifndef HAVE_VASPRINTF
+    int          i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
 #endif
 
     /*
@@ -285,7 +269,7 @@ static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
 #ifdef HAVE_VASPRINTF
     vasprintf( &psz_str, psz_format, args );
 #else
-    psz_str = (char*) malloc( strlen(psz_format) + INTF_MAX_MSG_SIZE );
+    psz_str = (char*) malloc( i_size * sizeof(char) );
 #endif
 
     if( psz_str == NULL )
@@ -305,67 +289,81 @@ static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
         fprintf( stderr, "main warning: couldn't print message\n" );
         return;
     }
-    vsprintf( psz_str, psz_temp, args );
+    vsnprintf( psz_str, i_size, psz_temp, args );
     free( psz_temp );
 #   else
-    vsprintf( psz_str, psz_format, args );
+    vsnprintf( psz_str, i_size, psz_format, args );
 #   endif
+    psz_str[ i_size - 1 ] = 0; /* Just in case */
 #endif
 
     /* Put message in queue */
-    vlc_mutex_lock( &p_this->p_vlc->msg_bank.lock );
+    vlc_mutex_lock( &p_bank->lock );
 
-    /* Send the message to stderr */
-    if( (i_type == VLC_MSG_ERR)
-         || ( (i_type == VLC_MSG_INFO) && !p_this->p_vlc->b_quiet )
-         || ( (i_type == VLC_MSG_WARN) && p_this->p_vlc->b_verbose
-                                       && !p_this->p_vlc->b_quiet )
-         || ( (i_type == VLC_MSG_DBG) && p_this->p_vlc->b_verbose
-                                       && !p_this->p_vlc->b_quiet ) )
+    /* Check there is room in the queue for our message */
+    if( p_bank->b_overflow )
     {
-        if( p_this->p_vlc->b_color )
+        FlushMsg( p_bank );
+
+        if( ((p_bank->i_stop - p_bank->i_start + 1) % VLC_MSG_QSIZE) == 0 )
         {
-#           define COL(x)  "\033[" #x ";1m"
-#           define RED     COL(31)
-#           define GREEN   COL(32)
-#           define YELLOW  COL(33)
-#           define WHITE   COL(37)
-#           define GRAY    "\033[0m"
-            static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
-
-            fprintf( stderr, "[" GREEN "%.8x" GRAY "] " "%s%s"
-                     ": %s%s" GRAY "\n", p_this->i_object_id, psz_module,
-                     ppsz_type[i_type], ppsz_color[i_type], psz_str );
+            /* Still in overflow mode, print from a dummy item */
+            p_item = &item;
         }
         else
         {
-            fprintf( stderr, "[%.8x] %s%s: %s\n", p_this->i_object_id,
-                             psz_module, ppsz_type[i_type], psz_str );
+            /* Pheeew, at last, there is room in the queue! */
+            p_bank->b_overflow = VLC_FALSE;
         }
     }
-
-    /* Put the message in the queue if there is room for it */
-    if( ((p_this->p_vlc->msg_bank.i_stop - p_this->p_vlc->msg_bank.i_start + 1) % VLC_MSG_QSIZE) == 0 )
+    else if( ((p_bank->i_stop - p_bank->i_start + 2) % VLC_MSG_QSIZE) == 0 )
     {
-        FlushMsg( &p_this->p_vlc->msg_bank );
+        FlushMsg( p_bank );
 
-        if( ((p_this->p_vlc->msg_bank.i_stop - p_this->p_vlc->msg_bank.i_start + 1) % VLC_MSG_QSIZE) == 0 )
+        if( ((p_bank->i_stop - p_bank->i_start + 2) % VLC_MSG_QSIZE) == 0 )
         {
-            fprintf( stderr, "main warning: message queue overflow\n" );
-            vlc_mutex_unlock( &p_this->p_vlc->msg_bank.lock );
-            return;
+            p_bank->b_overflow = VLC_TRUE;
+
+            /* Put the overflow message in the queue */
+            p_item = p_bank->msg + p_bank->i_stop;
+            p_bank->i_stop = (p_bank->i_stop + 1) % VLC_MSG_QSIZE;
+
+            p_item->i_type =        VLC_MSG_ERR;
+            p_item->i_object_id =   p_this->i_object_id;
+            p_item->i_object_type = p_this->i_object_type;
+            p_item->psz_module =    strdup( "message" );
+            p_item->psz_msg =       strdup( "message queue overflowed" );
+
+            PrintMsg( p_this, p_item );
+
+            /* We print from a dummy item */
+            p_item = &item;
         }
     }
 
-    p_item = p_this->p_vlc->msg_bank.msg + p_this->p_vlc->msg_bank.i_stop;
-    p_this->p_vlc->msg_bank.i_stop = (p_this->p_vlc->msg_bank.i_stop + 1) % VLC_MSG_QSIZE;
+    if( !p_bank->b_overflow )
+    {
+        /* Put the message in the queue */
+        p_item = p_bank->msg + p_bank->i_stop;
+        p_bank->i_stop = (p_bank->i_stop + 1) % VLC_MSG_QSIZE;
+    }
 
     /* Fill message information fields */
-    p_item->i_type =     i_type;
-    p_item->psz_module = strdup( psz_module );
-    p_item->psz_msg =    psz_str;
+    p_item->i_type =        i_type;
+    p_item->i_object_id =   p_this->i_object_id;
+    p_item->i_object_type = p_this->i_object_type;
+    p_item->psz_module =    strdup( psz_module );
+    p_item->psz_msg =       psz_str;
 
-    vlc_mutex_unlock( &p_this->p_vlc->msg_bank.lock );
+    PrintMsg( p_this, p_item );
+
+    if( p_bank->b_overflow )
+    {
+        free( p_item->psz_module );
+        free( p_item->psz_msg );
+    }
+
+    vlc_mutex_unlock( &p_bank->lock );
 }
 
 /* following functions are local */
@@ -380,6 +378,12 @@ static void FlushMsg ( msg_bank_t *p_bank )
 {
     int i_index, i_start, i_stop;
 
+    /* Only flush the queue if it has been properly configured */
+    if( !p_bank->b_configured )
+    {
+        return;
+    }
+
     /* Get the maximum message index that can be freed */
     i_stop = p_bank->i_stop;
 
@@ -412,6 +416,67 @@ static void FlushMsg ( msg_bank_t *p_bank )
     p_bank->i_start = i_index;
 }
 
+/*****************************************************************************
+ * PrintMsg: output a message item to stderr
+ *****************************************************************************
+ * Print a message to stderr, with colour formatting if needed.
+ *****************************************************************************/
+static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
+{
+#   define COL(x)  "\033[" #x ";1m"
+#   define RED     COL(31)
+#   define GREEN   COL(32)
+#   define YELLOW  COL(33)
+#   define WHITE   COL(37)
+#   define GRAY    "\033[0m"
+
+    static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
+    static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
+    char *psz_object = "private";
+    int i_type = p_item->i_type;
+
+    if( /*p_this->p_vlc->b_quiet ||*/ !p_this->p_libvlc->msg_bank.b_configured )
+    {
+        return;
+    }
+
+    if( /*!p_this->p_vlc->b_verbose && */
+         ( (i_type == VLC_MSG_WARN) || (i_type == VLC_MSG_DBG) ) )
+    {
+        return;
+    }
+
+    switch( p_item->i_object_type )
+    {
+        case VLC_OBJECT_ROOT: psz_object = "root"; break;
+        case VLC_OBJECT_VLC: psz_object = "vlc"; break;
+        case VLC_OBJECT_MODULE: psz_object = "module"; break;
+        case VLC_OBJECT_INTF: psz_object = "interface"; break;
+        case VLC_OBJECT_PLAYLIST: psz_object = "playlist"; break;
+        case VLC_OBJECT_ITEM: psz_object = "item"; break;
+        case VLC_OBJECT_INPUT: psz_object = "input"; break;
+        case VLC_OBJECT_DECODER: psz_object = "decoder"; break;
+        case VLC_OBJECT_VOUT: psz_object = "video output"; break;
+        case VLC_OBJECT_AOUT: psz_object = "audio output"; break;
+        case VLC_OBJECT_SOUT: psz_object = "stream output"; break;
+    }
+
+    /* Send the message to stderr */
+    if( /*p_this->p_vlc->b_color*/1 )
+    {
+        fprintf( stderr, "[" GREEN "%.6x" GRAY "] %s %s%s: %s%s" GRAY "\n",
+                         p_item->i_object_id, p_item->psz_module, psz_object,
+                         ppsz_type[i_type], ppsz_color[i_type],
+                         p_item->psz_msg );
+    }
+    else
+    {
+        fprintf( stderr, "[%.6x] %s %s%s: %s\n", p_item->i_object_id,
+                         p_item->psz_module, psz_object, ppsz_type[i_type],
+                         p_item->psz_msg );
+    }
+}
+
 /*****************************************************************************
  * ConvertPrintfFormatString: replace all occurrences of %ll with %I64 in the
  *                            printf format string.