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 *****************************************************************************/
35 #include <vlc_common.h>
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 * );
81 static inline char * object_description( vlc_object_t * p_this )
83 if( p_this->i_object_type == VLC_OBJECT_GENERIC
84 && p_this->psz_object_name )
85 return strdup( p_this->psz_object_name );
86 return strdup( p_this->psz_object_type );
90 * Initialize messages queues
91 * This function initializes all message queues
93 void msg_Create (libvlc_int_t *p_libvlc)
95 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
96 vlc_mutex_init( &priv->msg_bank.lock );
97 vlc_mutex_init( &QUEUE.lock );
98 QUEUE.b_overflow = false;
106 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
107 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
108 CREATE_ALWAYS, 0, NULL );
109 SetFilePointer( QUEUE.logfile, 0, NULL, FILE_END );
114 * Flush all message queues
116 void msg_Flush (libvlc_int_t *p_libvlc)
118 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
119 vlc_mutex_lock( &QUEUE.lock );
121 vlc_mutex_unlock( &QUEUE.lock );
125 * Destroy the message queues
127 * This functions prints all messages remaining in the queues,
128 * then frees all the allocated ressources
129 * No other messages interface functions should be called after this one.
131 void msg_Destroy (libvlc_int_t *p_libvlc)
133 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
136 msg_Err( p_libvlc, "stale interface subscribers" );
141 CloseHandle( QUEUE.logfile );
144 vlc_mutex_destroy( &QUEUE.lock );
145 vlc_mutex_destroy( &priv->msg_bank.lock);
149 * Subscribe to a message queue.
151 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this )
153 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
154 msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
160 vlc_mutex_lock( &QUEUE.lock );
162 TAB_APPEND( QUEUE.i_sub, QUEUE.pp_sub, p_sub );
164 p_sub->i_start = QUEUE.i_start;
165 p_sub->pi_stop = &QUEUE.i_stop;
166 p_sub->p_msg = QUEUE.msg;
167 p_sub->p_lock = &QUEUE.lock;
169 vlc_mutex_unlock( &QUEUE.lock );
176 * Unsubscribe from a message queue.
178 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
180 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
183 vlc_mutex_lock( &QUEUE.lock );
184 for( int j = 0 ; j< QUEUE.i_sub ; j++ )
186 if( QUEUE.pp_sub[j] == p_sub )
188 REMOVE_ELEM( QUEUE.pp_sub, QUEUE.i_sub, j );
192 vlc_mutex_unlock( &QUEUE.lock );
196 /*****************************************************************************
197 * __msg_*: print a message
198 *****************************************************************************
199 * These functions queue a message for later printing.
200 *****************************************************************************/
201 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
202 const char *psz_format, ... )
206 va_start( args, psz_format );
207 QueueMsg( p_this, i_type, psz_module, psz_format, args );
211 void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
212 const char *psz_format, va_list args )
214 QueueMsg( p_this, i_type, psz_module, psz_format, args );
217 /* Generic functions used when variadic macros are not available. */
218 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
219 void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
222 va_start( args, psz_format ); \
223 QueueMsg( p_this, FN_TYPE, "unknown", psz_format, args ); \
228 * Output an informational message.
229 * \note Do not use this for debug messages
232 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
234 * Output an error message.
236 DECLARE_MSG_FN( __msg_Err, VLC_MSG_ERR );
238 * Output a waring message
240 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
242 * Output a debug message
244 DECLARE_MSG_FN( __msg_Dbg, VLC_MSG_DBG );
247 * Add a message to a queue
249 * This function provides basic functionnalities to other msg_* functions.
250 * It adds a message to a queue (after having printed all stored messages if it
251 * is full). If the message can't be converted to string in memory, it issues
254 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
255 const char *psz_format, va_list _args )
258 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
259 int i_header_size; /* Size of the additionnal header */
261 char * psz_str = NULL; /* formatted message string */
262 char * psz_header = NULL;
264 msg_item_t * p_item = NULL; /* pointer to message */
265 msg_item_t item; /* message in case of a full queue */
266 msg_queue_t *p_queue;
268 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
269 int i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
272 if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
273 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
277 /* Expand %m to strerror(errno) - only once */
278 char buf[strlen( psz_format ) + 2001], *ptr;
279 strcpy( buf, psz_format );
281 psz_format = (const char*) buf;
285 ptr = strchr( ptr, '%' );
295 strerror_r( errno, errbuf, 1001 );
297 int sockerr = WSAGetLastError( );
300 strncpy( errbuf, net_strerror( sockerr ), 1001 );
301 WSASetLastError( sockerr );
304 || (strcmp ("Unknown network stack error", errbuf) == 0))
305 strncpy( errbuf, strerror( errno ), 1001 );
309 /* Escape '%' from the error string */
310 for( char *percent = strchr( errbuf, '%' );
312 percent = strchr( percent + 2, '%' ) )
314 memmove( percent + 1, percent, strlen( percent ) + 1 );
317 errlen = strlen( errbuf );
318 memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
319 memcpy( ptr, errbuf, errlen );
320 break; /* Only once, so we don't overflow */
323 /* Looks for conversion specifier... */
326 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
328 ptr++; /* ...and skip it */
332 /* Convert message to string */
333 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
334 vlc_va_copy( args, _args );
335 if( vasprintf( &psz_str, psz_format, args ) == -1 )
339 psz_str = (char*) malloc( i_size );
342 if( psz_str == NULL )
345 fprintf( stderr, "main warning: can't store message (%m): " );
349 /* we're not using GLIBC, so we are sure that the error description
350 * will be stored in the buffer we provide to strerror_r() */
351 strerror_r( errno, psz_err, 1001 );
353 strncpy( psz_err, strerror( errno ), 1001 );
355 psz_err[1000] = '\0';
356 fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
358 vlc_va_copy( args, _args );
359 /* We should use utf8_vfprintf - but it calls malloc()... */
360 vfprintf( stderr, psz_format, args );
362 fputs( "\n", stderr );
368 while( p_obj != NULL )
370 char *psz_old = NULL;
371 if( p_obj->psz_header )
373 i_header_size += strlen( p_obj->psz_header ) + 4;
376 psz_old = strdup( psz_header );
377 psz_header = (char*)realloc( psz_header, i_header_size );
378 snprintf( psz_header, i_header_size , "[%s] %s",
379 p_obj->psz_header, psz_old );
383 psz_header = (char *)malloc( i_header_size );
384 snprintf( psz_header, i_header_size, "[%s]",
389 p_obj = p_obj->p_parent;
392 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
393 vlc_va_copy( args, _args );
394 vsnprintf( psz_str, i_size, psz_format, args );
396 psz_str[ i_size - 1 ] = 0; /* Just in case */
401 vlc_mutex_lock( &p_queue->lock );
403 /* Check there is room in the queue for our message */
404 if( p_queue->b_overflow )
408 if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
410 /* Still in overflow mode, print from a dummy item */
415 /* Pheeew, at last, there is room in the queue! */
416 p_queue->b_overflow = false;
419 else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
423 if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
425 p_queue->b_overflow = true;
427 /* Put the overflow message in the queue */
428 p_item = p_queue->msg + p_queue->i_stop;
429 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
431 p_item->i_type = VLC_MSG_WARN;
432 p_item->i_object_id = p_this->i_object_id;
433 p_item->psz_object = object_description( p_this );
434 p_item->psz_module = strdup( "message" );
435 p_item->psz_msg = strdup( "message queue overflowed" );
436 p_item->psz_header = NULL;
438 PrintMsg( p_this, p_item );
439 /* We print from a dummy item */
444 if( !p_queue->b_overflow )
446 /* Put the message in the queue */
447 p_item = p_queue->msg + p_queue->i_stop;
448 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
451 /* Fill message information fields */
452 p_item->i_type = i_type;
453 p_item->i_object_id = p_this->i_object_id;
454 p_item->psz_object = object_description( p_this );
455 p_item->psz_module = strdup( psz_module );
456 p_item->psz_msg = psz_str;
457 p_item->psz_header = psz_header;
459 PrintMsg( p_this, p_item );
461 if( p_queue->b_overflow )
463 free( p_item->psz_module );
464 free( p_item->psz_msg );
465 free( p_item->psz_header );
466 free( p_item->psz_object );
469 vlc_mutex_unlock ( &p_queue->lock );
473 /* following functions are local */
475 /*****************************************************************************
477 *****************************************************************************
478 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
479 * this function does not check the lock.
480 *****************************************************************************/
481 static void FlushMsg ( msg_queue_t *p_queue )
483 int i_index, i_start, i_stop;
485 /* Get the maximum message index that can be freed */
486 i_stop = p_queue->i_stop;
488 /* Check until which value we can free messages */
489 for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
491 i_start = p_queue->pp_sub[ i_index ]->i_start;
493 /* If this subscriber is late, we don't free messages before
494 * his i_start value, otherwise he'll miss messages */
495 if( ( i_start < i_stop
496 && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
497 || ( i_stop < i_start
498 && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
504 /* Free message data */
505 for( i_index = p_queue->i_start;
507 i_index = (i_index+1) % VLC_MSG_QSIZE )
509 free( p_queue->msg[i_index].psz_msg );
510 free( p_queue->msg[i_index].psz_module );
511 free( p_queue->msg[i_index].psz_header );
512 free( p_queue->msg[i_index].psz_object );
515 /* Update the new start value */
516 p_queue->i_start = i_index;
519 /*****************************************************************************
520 * PrintMsg: output a standard message item to stderr
521 *****************************************************************************
522 * Print a message to stderr, with colour formatting if needed.
523 *****************************************************************************/
524 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
526 # define COL(x) "\033[" #x ";1m"
528 # define GREEN COL(32)
529 # define YELLOW COL(33)
530 # define WHITE COL(0)
531 # define GRAY "\033[0m"
536 static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
537 static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
538 const char *psz_object;
539 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
540 int i_type = p_item->i_type;
545 if( priv->i_verbose < 0 ) return;
548 if( priv->i_verbose < 0 ) return;
551 if( priv->i_verbose < 1 ) return;
554 if( priv->i_verbose < 2 ) return;
558 psz_object = p_item->psz_object;
561 # define CE_WRITE(str) WriteFile( QUEUE.logfile, \
562 str, strlen(str), &i_dummy, NULL );
563 CE_WRITE( p_item->psz_module );
565 CE_WRITE( psz_object );
566 CE_WRITE( ppsz_type[i_type] );
568 CE_WRITE( p_item->psz_msg );
570 FlushFileBuffers( QUEUE.logfile );
573 /* Send the message to stderr */
576 if( p_item->psz_header )
578 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s %s%s: %s%s" GRAY
580 p_item->i_object_id, p_item->psz_header,
581 p_item->psz_module, psz_object,
582 ppsz_type[i_type], ppsz_color[i_type],
587 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
588 p_item->i_object_id, p_item->psz_module, psz_object,
589 ppsz_type[i_type], ppsz_color[i_type],
595 if( p_item->psz_header )
597 utf8_fprintf( stderr, "[%.8i] %s %s %s%s: %s\n", p_item->i_object_id,
598 p_item->psz_header, p_item->psz_module,
599 psz_object, ppsz_type[i_type], p_item->psz_msg );
603 utf8_fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
604 p_item->psz_module, psz_object, ppsz_type[i_type],
615 static msg_context_t* GetContext(void)
617 msg_context_t *p_ctx = vlc_threadvar_get( &msg_context_global_key );
620 MALLOC_NULL( p_ctx, msg_context_t );
621 p_ctx->psz_message = NULL;
622 vlc_threadvar_set( &msg_context_global_key, p_ctx );
627 void msg_StackDestroy (void *data)
629 msg_context_t *p_ctx = data;
631 free (p_ctx->psz_message);
635 void msg_StackSet( int i_code, const char *psz_message, ... )
638 msg_context_t *p_ctx = GetContext();
642 free( p_ctx->psz_message );
644 va_start( ap, psz_message );
645 if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
646 p_ctx->psz_message = NULL;
649 p_ctx->i_code = i_code;
652 void msg_StackAdd( const char *psz_message, ... )
656 msg_context_t *p_ctx = GetContext();
661 va_start( ap, psz_message );
662 if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
666 if( !p_ctx->psz_message )
667 p_ctx->psz_message = psz_tmp;
671 if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
674 free( p_ctx->psz_message );
675 p_ctx->psz_message = psz_new;
680 const char* msg_StackMsg( void )
682 msg_context_t *p_ctx = GetContext();
684 return p_ctx->psz_message;