1 /*****************************************************************************
2 * messages.c: messages interface
3 * This library provides an interface to the message queue to be used by other
4 * modules, especially intf modules. See vlc_config.h for output configuration.
5 *****************************************************************************
6 * Copyright (C) 1998-2005 the VideoLAN team
9 * Authors: Vincent Seguin <seguin@via.ecp.fr>
10 * Samuel Hocevar <sam@zoy.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
37 #include <stdarg.h> /* va_list for BSD */
40 # include <fcntl.h> /* O_CREAT, O_TRUNC, O_WRONLY, O_SYNC */
43 #include <errno.h> /* errno */
46 # include <vlc_network.h> /* 'net_strerror' and 'WSAGetLastError' */
50 # include <unistd.h> /* close(), write() */
55 #include <vlc_charset.h>
56 #include "../libvlc.h"
58 /*****************************************************************************
60 *****************************************************************************/
61 #if defined(HAVE_VA_COPY)
62 # define vlc_va_copy(dest,src) va_copy(dest,src)
63 #elif defined(HAVE___VA_COPY)
64 # define vlc_va_copy(dest,src) __va_copy(dest,src)
66 # define vlc_va_copy(dest,src) (dest)=(src)
69 #define QUEUE(i) priv->msg_bank.queues[i]
70 #define LOCK_BANK vlc_mutex_lock( &priv->msg_bank.lock );
71 #define UNLOCK_BANK vlc_mutex_unlock( &priv->msg_bank.lock );
73 /*****************************************************************************
75 *****************************************************************************/
76 static void QueueMsg ( vlc_object_t *, int, int , const char *,
77 const char *, va_list );
78 static void FlushMsg ( msg_queue_t * );
79 static void PrintMsg ( vlc_object_t *, msg_item_t * );
82 * Initialize messages queues
83 * This function initializes all message queues
85 void __msg_Create( vlc_object_t *p_this )
87 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
88 vlc_mutex_init( &priv->msg_bank.lock );
90 for( int i = 0; i < 2; i++ )
92 vlc_mutex_init( &QUEUE(i).lock );
93 QUEUE(i).b_overflow = false;
102 QUEUE(MSG_QUEUE_NORMAL).logfile =
103 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
104 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
105 CREATE_ALWAYS, 0, NULL );
106 SetFilePointer( QUEUE(MSG_QUEUE_NORMAL).logfile, 0, NULL, FILE_END );
111 * Flush all message queues
113 void __msg_Flush( vlc_object_t *p_this )
115 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
117 for( int i = 0 ; i < NB_QUEUES ; i++ )
119 vlc_mutex_lock( &QUEUE(i).lock );
120 FlushMsg( &QUEUE(i) );
121 vlc_mutex_unlock( &QUEUE(i).lock );
126 * Destroy the message queues
128 * This functions prints all messages remaining in the queues,
129 * then frees all the allocated ressources
130 * No other messages interface functions should be called after this one.
132 void __msg_Destroy( vlc_object_t *p_this )
134 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
136 for( int i = NB_QUEUES -1 ; i >= 0; i-- )
139 msg_Err( p_this, "stale interface subscribers" );
141 FlushMsg( &QUEUE(i) );
144 if( i == MSG_QUEUE_NORMAL )
145 CloseHandle( QUEUE(MSG_QUEUE_NORMAL).logfile );
148 vlc_mutex_destroy( &QUEUE(i).lock );
150 vlc_mutex_destroy( &priv->msg_bank.lock);
154 * Subscribe to a message queue.
156 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this, int i )
158 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
159 msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
164 assert( i < NB_QUEUES );
167 vlc_mutex_lock( &QUEUE(i).lock );
169 TAB_APPEND( QUEUE(i).i_sub, QUEUE(i).pp_sub, p_sub );
171 p_sub->i_start = QUEUE(i).i_start;
172 p_sub->pi_stop = &QUEUE(i).i_stop;
173 p_sub->p_msg = QUEUE(i).msg;
174 p_sub->p_lock = &QUEUE(i).lock;
176 vlc_mutex_unlock( &QUEUE(i).lock );
183 * Unsubscribe from a message queue.
185 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
187 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
190 for( int i = 0 ; i< NB_QUEUES ; i++ )
192 vlc_mutex_lock( &QUEUE(i).lock );
193 for( int j = 0 ; j< QUEUE(i).i_sub ; j++ )
195 if( QUEUE(i).pp_sub[j] == p_sub )
197 REMOVE_ELEM( QUEUE(i).pp_sub, QUEUE(i).i_sub, j );
201 vlc_mutex_unlock( & QUEUE(i).lock );
206 /*****************************************************************************
207 * __msg_*: print a message
208 *****************************************************************************
209 * These functions queue a message for later printing.
210 *****************************************************************************/
211 void __msg_Generic( vlc_object_t *p_this, int i_queue, int i_type,
212 const char *psz_module,
213 const char *psz_format, ... )
217 va_start( args, psz_format );
218 QueueMsg( p_this, i_queue, i_type, psz_module, psz_format, args );
222 void __msg_GenericVa( vlc_object_t *p_this, int i_queue,
223 int i_type, const char *psz_module,
224 const char *psz_format, va_list args )
226 QueueMsg( p_this, i_queue, i_type, psz_module, psz_format, args );
229 /* Generic functions used when variadic macros are not available. */
230 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
231 void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
234 va_start( args, psz_format ); \
235 QueueMsg( p_this,MSG_QUEUE_NORMAL, FN_TYPE, "unknown", \
236 psz_format, args ); \
241 * Output an informational message.
242 * \note Do not use this for debug messages
245 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
247 * Output an error message.
249 DECLARE_MSG_FN( __msg_Err, VLC_MSG_ERR );
251 * Output a waring message
253 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
255 * Output a debug message
257 DECLARE_MSG_FN( __msg_Dbg, VLC_MSG_DBG );
260 * Add a message to a queue
262 * This function provides basic functionnalities to other msg_* functions.
263 * It adds a message to a queue (after having printed all stored messages if it
264 * is full). If the message can't be converted to string in memory, it issues
267 static void QueueMsg( vlc_object_t *p_this, int i_queue, int i_type,
268 const char *psz_module,
269 const char *psz_format, va_list _args )
271 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
272 int i_header_size; /* Size of the additionnal header */
274 char * psz_str = NULL; /* formatted message string */
275 char * psz_header = NULL;
277 msg_item_t * p_item = NULL; /* pointer to message */
278 msg_item_t item; /* message in case of a full queue */
279 msg_queue_t *p_queue;
281 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
282 int i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
288 if( i_type == VLC_MSG_DBG )
291 utf8_vfprintf( stderr, psz_format, _args );
292 fputc ('\n', stderr);
296 if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
297 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
301 /* Expand %m to strerror(errno) - only once */
302 char buf[strlen( psz_format ) + 2001], *ptr;
303 strcpy( buf, psz_format );
305 psz_format = (const char*) buf;
309 ptr = strchr( ptr, '%' );
319 strerror_r( errno, errbuf, 1001 );
321 int sockerr = WSAGetLastError( );
324 strncpy( errbuf, net_strerror( sockerr ), 1001 );
325 WSASetLastError( sockerr );
328 || (strcmp ("Unknown network stack error", errbuf) == 0))
329 strncpy( errbuf, strerror( errno ), 1001 );
333 /* Escape '%' from the error string */
334 for( char *percent = strchr( errbuf, '%' );
336 percent = strchr( percent + 2, '%' ) )
338 memmove( percent + 1, percent, strlen( percent ) + 1 );
341 errlen = strlen( errbuf );
342 memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
343 memcpy( ptr, errbuf, errlen );
344 break; /* Only once, so we don't overflow */
347 /* Looks for conversion specifier... */
350 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
352 ptr++; /* ...and skip it */
356 /* Convert message to string */
357 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
358 vlc_va_copy( args, _args );
359 if( vasprintf( &psz_str, psz_format, args ) == -1 )
363 psz_str = (char*) malloc( i_size );
366 if( psz_str == NULL )
369 fprintf( stderr, "main warning: can't store message (%m): " );
373 /* we're not using GLIBC, so we are sure that the error description
374 * will be stored in the buffer we provide to strerror_r() */
375 strerror_r( errno, psz_err, 1001 );
377 strncpy( psz_err, strerror( errno ), 1001 );
379 psz_err[1000] = '\0';
380 fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
382 vlc_va_copy( args, _args );
383 /* We should use utf8_vfprintf - but it calls malloc()... */
384 vfprintf( stderr, psz_format, args );
386 fputs( "\n", stderr );
392 while( p_obj != NULL )
394 char *psz_old = NULL;
395 if( p_obj->psz_header )
397 i_header_size += strlen( p_obj->psz_header ) + 4;
400 psz_old = strdup( psz_header );
401 psz_header = (char*)realloc( psz_header, i_header_size );
402 snprintf( psz_header, i_header_size , "[%s] %s",
403 p_obj->psz_header, psz_old );
407 psz_header = (char *)malloc( i_header_size );
408 snprintf( psz_header, i_header_size, "[%s]",
413 p_obj = p_obj->p_parent;
416 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
417 vlc_va_copy( args, _args );
418 vsnprintf( psz_str, i_size, psz_format, args );
420 psz_str[ i_size - 1 ] = 0; /* Just in case */
423 assert( i_queue < NB_QUEUES );
425 p_queue = &QUEUE(i_queue) ;
426 vlc_mutex_lock( &p_queue->lock );
428 /* Check there is room in the queue for our message */
429 if( p_queue->b_overflow )
433 if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
435 /* Still in overflow mode, print from a dummy item */
440 /* Pheeew, at last, there is room in the queue! */
441 p_queue->b_overflow = false;
444 else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
448 if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
450 p_queue->b_overflow = true;
452 if( p_queue->i_id == MSG_QUEUE_NORMAL )
454 /* Put the overflow message in the queue */
455 p_item = p_queue->msg + p_queue->i_stop;
456 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
458 p_item->i_type = VLC_MSG_WARN;
459 p_item->i_object_id = p_this->i_object_id;
460 p_item->psz_object_type = p_this->psz_object_type;
461 p_item->psz_module = strdup( "message" );
462 p_item->psz_msg = strdup( "message queue overflowed" );
463 p_item->psz_header = NULL;
465 PrintMsg( p_this, p_item );
466 /* We print from a dummy item */
472 if( !p_queue->b_overflow )
474 /* Put the message in the queue */
475 p_item = p_queue->msg + p_queue->i_stop;
476 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
479 /* Fill message information fields */
480 p_item->i_type = i_type;
481 p_item->i_object_id = p_this->i_object_id;
482 p_item->psz_object_type = p_this->psz_object_type;
483 p_item->psz_module = strdup( psz_module );
484 p_item->psz_msg = psz_str;
485 p_item->psz_header = psz_header;
487 if( p_queue->i_id == MSG_QUEUE_NORMAL )
488 PrintMsg( p_this, p_item );
490 if( p_queue->b_overflow )
492 free( p_item->psz_module );
493 free( p_item->psz_msg );
494 free( p_item->psz_header );
497 vlc_mutex_unlock ( &p_queue->lock );
501 /* following functions are local */
503 /*****************************************************************************
505 *****************************************************************************
506 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
507 * this function does not check the lock.
508 *****************************************************************************/
509 static void FlushMsg ( msg_queue_t *p_queue )
511 int i_index, i_start, i_stop;
513 /* Get the maximum message index that can be freed */
514 i_stop = p_queue->i_stop;
516 /* Check until which value we can free messages */
517 for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
519 i_start = p_queue->pp_sub[ i_index ]->i_start;
521 /* If this subscriber is late, we don't free messages before
522 * his i_start value, otherwise he'll miss messages */
523 if( ( i_start < i_stop
524 && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
525 || ( i_stop < i_start
526 && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
532 /* Free message data */
533 for( i_index = p_queue->i_start;
535 i_index = (i_index+1) % VLC_MSG_QSIZE )
537 free( p_queue->msg[i_index].psz_msg );
538 free( p_queue->msg[i_index].psz_module );
539 free( p_queue->msg[i_index].psz_header );
542 /* Update the new start value */
543 p_queue->i_start = i_index;
546 /*****************************************************************************
547 * PrintMsg: output a standard message item to stderr
548 *****************************************************************************
549 * Print a message to stderr, with colour formatting if needed.
550 *****************************************************************************/
551 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
553 # define COL(x) "\033[" #x ";1m"
555 # define GREEN COL(32)
556 # define YELLOW COL(33)
557 # define WHITE COL(0)
558 # define GRAY "\033[0m"
563 static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
564 static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
565 const char *psz_object;
566 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
567 int i_type = p_item->i_type;
572 if( priv->i_verbose < 0 ) return;
575 if( priv->i_verbose < 0 ) return;
578 if( priv->i_verbose < 1 ) return;
581 if( priv->i_verbose < 2 ) return;
585 psz_object = p_item->psz_object_type;
588 # define CE_WRITE(str) WriteFile( QUEUE(MSG_QUEUE_NORMAL).logfile, \
589 str, strlen(str), &i_dummy, NULL );
590 CE_WRITE( p_item->psz_module );
592 CE_WRITE( psz_object );
593 CE_WRITE( ppsz_type[i_type] );
595 CE_WRITE( p_item->psz_msg );
597 FlushFileBuffers( QUEUE(MSG_QUEUE_NORMAL).logfile );
600 /* Send the message to stderr */
603 if( p_item->psz_header )
605 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s %s%s: %s%s" GRAY
607 p_item->i_object_id, p_item->psz_header,
608 p_item->psz_module, psz_object,
609 ppsz_type[i_type], ppsz_color[i_type],
614 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
615 p_item->i_object_id, p_item->psz_module, psz_object,
616 ppsz_type[i_type], ppsz_color[i_type],
622 if( p_item->psz_header )
624 utf8_fprintf( stderr, "[%.8i] %s %s %s%s: %s\n", p_item->i_object_id,
625 p_item->psz_header, p_item->psz_module,
626 psz_object, ppsz_type[i_type], p_item->psz_msg );
630 utf8_fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
631 p_item->psz_module, psz_object, ppsz_type[i_type],
642 static msg_context_t* GetContext(void)
644 msg_context_t *p_ctx = vlc_threadvar_get( &msg_context_global_key );
647 MALLOC_NULL( p_ctx, msg_context_t );
648 p_ctx->psz_message = NULL;
649 vlc_threadvar_set( &msg_context_global_key, p_ctx );
654 void msg_StackSet( int i_code, const char *psz_message, ... )
657 msg_context_t *p_ctx = GetContext();
660 va_start( ap, psz_message );
661 free( p_ctx->psz_message );
663 if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
664 p_ctx->psz_message = NULL;
667 p_ctx->i_code = i_code;
670 void msg_StackAdd( const char *psz_message, ... )
674 msg_context_t *p_ctx = GetContext();
677 va_start( ap, psz_message );
678 if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
682 if( !p_ctx->psz_message )
683 p_ctx->psz_message = psz_tmp;
686 char *psz_old = malloc( strlen( p_ctx->psz_message ) + 1 );
687 memcpy( psz_old, p_ctx->psz_message, strlen( p_ctx->psz_message ) + 1 );
688 p_ctx->psz_message = realloc( p_ctx->psz_message,
689 strlen( p_ctx->psz_message ) +
691 strlen( psz_tmp ) + 3 );
692 sprintf( p_ctx->psz_message, "%s: %s", psz_tmp, psz_old );
693 free( psz_tmp ); free( psz_old );
697 const char* msg_StackMsg( void )
699 msg_context_t *p_ctx = GetContext();
701 return p_ctx->psz_message;