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 priv->msg_bank.queue
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, 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 (libvlc_int_t *p_libvlc)
87 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
88 vlc_mutex_init( &priv->msg_bank.lock );
89 vlc_mutex_init( &QUEUE.lock );
90 QUEUE.b_overflow = false;
98 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
99 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
100 CREATE_ALWAYS, 0, NULL );
101 SetFilePointer( QUEUE.logfile, 0, NULL, FILE_END );
106 * Flush all message queues
108 void msg_Flush (libvlc_int_t *p_libvlc)
110 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
111 vlc_mutex_lock( &QUEUE.lock );
113 vlc_mutex_unlock( &QUEUE.lock );
117 * Destroy the message queues
119 * This functions prints all messages remaining in the queues,
120 * then frees all the allocated ressources
121 * No other messages interface functions should be called after this one.
123 void msg_Destroy (libvlc_int_t *p_libvlc)
125 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
128 msg_Err( p_libvlc, "stale interface subscribers" );
133 CloseHandle( QUEUE.logfile );
136 vlc_mutex_destroy( &QUEUE.lock );
137 vlc_mutex_destroy( &priv->msg_bank.lock);
141 * Subscribe to a message queue.
143 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this )
145 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
146 msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
152 vlc_mutex_lock( &QUEUE.lock );
154 TAB_APPEND( QUEUE.i_sub, QUEUE.pp_sub, p_sub );
156 p_sub->i_start = QUEUE.i_start;
157 p_sub->pi_stop = &QUEUE.i_stop;
158 p_sub->p_msg = QUEUE.msg;
159 p_sub->p_lock = &QUEUE.lock;
161 vlc_mutex_unlock( &QUEUE.lock );
168 * Unsubscribe from a message queue.
170 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
172 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
175 vlc_mutex_lock( &QUEUE.lock );
176 for( int j = 0 ; j< QUEUE.i_sub ; j++ )
178 if( QUEUE.pp_sub[j] == p_sub )
180 REMOVE_ELEM( QUEUE.pp_sub, QUEUE.i_sub, j );
184 vlc_mutex_unlock( &QUEUE.lock );
188 /*****************************************************************************
189 * __msg_*: print a message
190 *****************************************************************************
191 * These functions queue a message for later printing.
192 *****************************************************************************/
193 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
194 const char *psz_format, ... )
198 va_start( args, psz_format );
199 QueueMsg( p_this, i_type, psz_module, psz_format, args );
203 void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
204 const char *psz_format, va_list args )
206 QueueMsg( p_this, i_type, psz_module, psz_format, args );
209 /* Generic functions used when variadic macros are not available. */
210 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
211 void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
214 va_start( args, psz_format ); \
215 QueueMsg( p_this, FN_TYPE, "unknown", psz_format, args ); \
220 * Output an informational message.
221 * \note Do not use this for debug messages
224 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
226 * Output an error message.
228 DECLARE_MSG_FN( __msg_Err, VLC_MSG_ERR );
230 * Output a waring message
232 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
234 * Output a debug message
236 DECLARE_MSG_FN( __msg_Dbg, VLC_MSG_DBG );
239 * Add a message to a queue
241 * This function provides basic functionnalities to other msg_* functions.
242 * It adds a message to a queue (after having printed all stored messages if it
243 * is full). If the message can't be converted to string in memory, it issues
246 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
247 const char *psz_format, va_list _args )
250 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
251 int i_header_size; /* Size of the additionnal header */
253 char * psz_str = NULL; /* formatted message string */
254 char * psz_header = NULL;
256 msg_item_t * p_item = NULL; /* pointer to message */
257 msg_item_t item; /* message in case of a full queue */
258 msg_queue_t *p_queue;
260 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
261 int i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
264 if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
265 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
269 /* Expand %m to strerror(errno) - only once */
270 char buf[strlen( psz_format ) + 2001], *ptr;
271 strcpy( buf, psz_format );
273 psz_format = (const char*) buf;
277 ptr = strchr( ptr, '%' );
287 strerror_r( errno, errbuf, 1001 );
289 int sockerr = WSAGetLastError( );
292 strncpy( errbuf, net_strerror( sockerr ), 1001 );
293 WSASetLastError( sockerr );
296 || (strcmp ("Unknown network stack error", errbuf) == 0))
297 strncpy( errbuf, strerror( errno ), 1001 );
301 /* Escape '%' from the error string */
302 for( char *percent = strchr( errbuf, '%' );
304 percent = strchr( percent + 2, '%' ) )
306 memmove( percent + 1, percent, strlen( percent ) + 1 );
309 errlen = strlen( errbuf );
310 memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
311 memcpy( ptr, errbuf, errlen );
312 break; /* Only once, so we don't overflow */
315 /* Looks for conversion specifier... */
318 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
320 ptr++; /* ...and skip it */
324 /* Convert message to string */
325 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
326 vlc_va_copy( args, _args );
327 if( vasprintf( &psz_str, psz_format, args ) == -1 )
331 psz_str = (char*) malloc( i_size );
334 if( psz_str == NULL )
337 fprintf( stderr, "main warning: can't store message (%m): " );
341 /* we're not using GLIBC, so we are sure that the error description
342 * will be stored in the buffer we provide to strerror_r() */
343 strerror_r( errno, psz_err, 1001 );
345 strncpy( psz_err, strerror( errno ), 1001 );
347 psz_err[1000] = '\0';
348 fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
350 vlc_va_copy( args, _args );
351 /* We should use utf8_vfprintf - but it calls malloc()... */
352 vfprintf( stderr, psz_format, args );
354 fputs( "\n", stderr );
360 while( p_obj != NULL )
362 char *psz_old = NULL;
363 if( p_obj->psz_header )
365 i_header_size += strlen( p_obj->psz_header ) + 4;
368 psz_old = strdup( psz_header );
369 psz_header = (char*)realloc( psz_header, i_header_size );
370 snprintf( psz_header, i_header_size , "[%s] %s",
371 p_obj->psz_header, psz_old );
375 psz_header = (char *)malloc( i_header_size );
376 snprintf( psz_header, i_header_size, "[%s]",
381 p_obj = p_obj->p_parent;
384 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
385 vlc_va_copy( args, _args );
386 vsnprintf( psz_str, i_size, psz_format, args );
388 psz_str[ i_size - 1 ] = 0; /* Just in case */
393 vlc_mutex_lock( &p_queue->lock );
395 /* Check there is room in the queue for our message */
396 if( p_queue->b_overflow )
400 if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
402 /* Still in overflow mode, print from a dummy item */
407 /* Pheeew, at last, there is room in the queue! */
408 p_queue->b_overflow = false;
411 else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
415 if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
417 p_queue->b_overflow = true;
419 /* Put the overflow message in the queue */
420 p_item = p_queue->msg + p_queue->i_stop;
421 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
423 p_item->i_type = VLC_MSG_WARN;
424 p_item->i_object_id = p_this->i_object_id;
425 p_item->psz_object_type = p_this->psz_object_type;
426 p_item->psz_module = strdup( "message" );
427 p_item->psz_msg = strdup( "message queue overflowed" );
428 p_item->psz_header = NULL;
430 PrintMsg( p_this, p_item );
431 /* We print from a dummy item */
436 if( !p_queue->b_overflow )
438 /* Put the message in the queue */
439 p_item = p_queue->msg + p_queue->i_stop;
440 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
443 /* Fill message information fields */
444 p_item->i_type = i_type;
445 p_item->i_object_id = p_this->i_object_id;
446 p_item->psz_object_type = p_this->psz_object_type;
447 p_item->psz_module = strdup( psz_module );
448 p_item->psz_msg = psz_str;
449 p_item->psz_header = psz_header;
451 PrintMsg( p_this, p_item );
453 if( p_queue->b_overflow )
455 free( p_item->psz_module );
456 free( p_item->psz_msg );
457 free( p_item->psz_header );
460 vlc_mutex_unlock ( &p_queue->lock );
464 /* following functions are local */
466 /*****************************************************************************
468 *****************************************************************************
469 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
470 * this function does not check the lock.
471 *****************************************************************************/
472 static void FlushMsg ( msg_queue_t *p_queue )
474 int i_index, i_start, i_stop;
476 /* Get the maximum message index that can be freed */
477 i_stop = p_queue->i_stop;
479 /* Check until which value we can free messages */
480 for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
482 i_start = p_queue->pp_sub[ i_index ]->i_start;
484 /* If this subscriber is late, we don't free messages before
485 * his i_start value, otherwise he'll miss messages */
486 if( ( i_start < i_stop
487 && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
488 || ( i_stop < i_start
489 && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
495 /* Free message data */
496 for( i_index = p_queue->i_start;
498 i_index = (i_index+1) % VLC_MSG_QSIZE )
500 free( p_queue->msg[i_index].psz_msg );
501 free( p_queue->msg[i_index].psz_module );
502 free( p_queue->msg[i_index].psz_header );
505 /* Update the new start value */
506 p_queue->i_start = i_index;
509 /*****************************************************************************
510 * PrintMsg: output a standard message item to stderr
511 *****************************************************************************
512 * Print a message to stderr, with colour formatting if needed.
513 *****************************************************************************/
514 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
516 # define COL(x) "\033[" #x ";1m"
518 # define GREEN COL(32)
519 # define YELLOW COL(33)
520 # define WHITE COL(0)
521 # define GRAY "\033[0m"
526 static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
527 static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
528 const char *psz_object;
529 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
530 int i_type = p_item->i_type;
535 if( priv->i_verbose < 0 ) return;
538 if( priv->i_verbose < 0 ) return;
541 if( priv->i_verbose < 1 ) return;
544 if( priv->i_verbose < 2 ) return;
548 psz_object = p_item->psz_object_type;
551 # define CE_WRITE(str) WriteFile( QUEUE.logfile, \
552 str, strlen(str), &i_dummy, NULL );
553 CE_WRITE( p_item->psz_module );
555 CE_WRITE( psz_object );
556 CE_WRITE( ppsz_type[i_type] );
558 CE_WRITE( p_item->psz_msg );
560 FlushFileBuffers( QUEUE.logfile );
563 /* Send the message to stderr */
566 if( p_item->psz_header )
568 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s %s%s: %s%s" GRAY
570 p_item->i_object_id, p_item->psz_header,
571 p_item->psz_module, psz_object,
572 ppsz_type[i_type], ppsz_color[i_type],
577 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
578 p_item->i_object_id, p_item->psz_module, psz_object,
579 ppsz_type[i_type], ppsz_color[i_type],
585 if( p_item->psz_header )
587 utf8_fprintf( stderr, "[%.8i] %s %s %s%s: %s\n", p_item->i_object_id,
588 p_item->psz_header, p_item->psz_module,
589 psz_object, ppsz_type[i_type], p_item->psz_msg );
593 utf8_fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
594 p_item->psz_module, psz_object, ppsz_type[i_type],
605 static msg_context_t* GetContext(void)
607 msg_context_t *p_ctx = vlc_threadvar_get( &msg_context_global_key );
610 MALLOC_NULL( p_ctx, msg_context_t );
611 p_ctx->psz_message = NULL;
612 vlc_threadvar_set( &msg_context_global_key, p_ctx );
617 void msg_StackDestroy (void *data)
619 msg_context_t *p_ctx = data;
621 free (p_ctx->psz_message);
625 void msg_StackSet( int i_code, const char *psz_message, ... )
628 msg_context_t *p_ctx = GetContext();
632 free( p_ctx->psz_message );
634 va_start( ap, psz_message );
635 if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
636 p_ctx->psz_message = NULL;
639 p_ctx->i_code = i_code;
642 void msg_StackAdd( const char *psz_message, ... )
646 msg_context_t *p_ctx = GetContext();
651 va_start( ap, psz_message );
652 if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
656 if( !p_ctx->psz_message )
657 p_ctx->psz_message = psz_tmp;
661 if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
664 free( p_ctx->psz_message );
665 p_ctx->psz_message = psz_new;
670 const char* msg_StackMsg( void )
672 msg_context_t *p_ctx = GetContext();
674 return p_ctx->psz_message;