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 return strdup( p_this->psz_object_type );
87 * Initialize messages queues
88 * This function initializes all message queues
90 void msg_Create (libvlc_int_t *p_libvlc)
92 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
93 vlc_mutex_init( &priv->msg_bank.lock );
94 vlc_mutex_init( &QUEUE.lock );
95 QUEUE.b_overflow = false;
103 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
104 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
105 CREATE_ALWAYS, 0, NULL );
106 SetFilePointer( QUEUE.logfile, 0, NULL, FILE_END );
111 * Flush all message queues
113 void msg_Flush (libvlc_int_t *p_libvlc)
115 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
116 vlc_mutex_lock( &QUEUE.lock );
118 vlc_mutex_unlock( &QUEUE.lock );
122 * Destroy the message queues
124 * This functions prints all messages remaining in the queues,
125 * then frees all the allocated ressources
126 * No other messages interface functions should be called after this one.
128 void msg_Destroy (libvlc_int_t *p_libvlc)
130 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
133 msg_Err( p_libvlc, "stale interface subscribers" );
138 CloseHandle( QUEUE.logfile );
141 vlc_mutex_destroy( &QUEUE.lock );
142 vlc_mutex_destroy( &priv->msg_bank.lock);
146 * Subscribe to a message queue.
148 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this )
150 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
151 msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
157 vlc_mutex_lock( &QUEUE.lock );
159 TAB_APPEND( QUEUE.i_sub, QUEUE.pp_sub, p_sub );
161 p_sub->i_start = QUEUE.i_start;
162 p_sub->pi_stop = &QUEUE.i_stop;
163 p_sub->p_msg = QUEUE.msg;
164 p_sub->p_lock = &QUEUE.lock;
166 vlc_mutex_unlock( &QUEUE.lock );
173 * Unsubscribe from a message queue.
175 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
177 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
180 vlc_mutex_lock( &QUEUE.lock );
181 for( int j = 0 ; j< QUEUE.i_sub ; j++ )
183 if( QUEUE.pp_sub[j] == p_sub )
185 REMOVE_ELEM( QUEUE.pp_sub, QUEUE.i_sub, j );
189 vlc_mutex_unlock( &QUEUE.lock );
193 /*****************************************************************************
194 * __msg_*: print a message
195 *****************************************************************************
196 * These functions queue a message for later printing.
197 *****************************************************************************/
198 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
199 const char *psz_format, ... )
203 va_start( args, psz_format );
204 QueueMsg( p_this, i_type, psz_module, psz_format, args );
208 void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
209 const char *psz_format, va_list args )
211 QueueMsg( p_this, i_type, psz_module, psz_format, args );
214 /* Generic functions used when variadic macros are not available. */
215 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
216 void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
219 va_start( args, psz_format ); \
220 QueueMsg( p_this, FN_TYPE, "unknown", psz_format, args ); \
225 * Output an informational message.
226 * \note Do not use this for debug messages
229 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
231 * Output an error message.
233 DECLARE_MSG_FN( __msg_Err, VLC_MSG_ERR );
235 * Output a waring message
237 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
239 * Output a debug message
241 DECLARE_MSG_FN( __msg_Dbg, VLC_MSG_DBG );
244 * Add a message to a queue
246 * This function provides basic functionnalities to other msg_* functions.
247 * It adds a message to a queue (after having printed all stored messages if it
248 * is full). If the message can't be converted to string in memory, it issues
251 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
252 const char *psz_format, va_list _args )
255 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
256 int i_header_size; /* Size of the additionnal header */
258 char * psz_str = NULL; /* formatted message string */
259 char * psz_header = NULL;
261 msg_item_t * p_item = NULL; /* pointer to message */
262 msg_item_t item; /* message in case of a full queue */
263 msg_queue_t *p_queue;
265 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
266 int i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
269 if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
270 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
274 /* Expand %m to strerror(errno) - only once */
275 char buf[strlen( psz_format ) + 2001], *ptr;
276 strcpy( buf, psz_format );
278 psz_format = (const char*) buf;
282 ptr = strchr( ptr, '%' );
292 strerror_r( errno, errbuf, 1001 );
294 int sockerr = WSAGetLastError( );
297 strncpy( errbuf, net_strerror( sockerr ), 1001 );
298 WSASetLastError( sockerr );
301 || (strcmp ("Unknown network stack error", errbuf) == 0))
302 strncpy( errbuf, strerror( errno ), 1001 );
306 /* Escape '%' from the error string */
307 for( char *percent = strchr( errbuf, '%' );
309 percent = strchr( percent + 2, '%' ) )
311 memmove( percent + 1, percent, strlen( percent ) + 1 );
314 errlen = strlen( errbuf );
315 memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
316 memcpy( ptr, errbuf, errlen );
317 break; /* Only once, so we don't overflow */
320 /* Looks for conversion specifier... */
323 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
325 ptr++; /* ...and skip it */
329 /* Convert message to string */
330 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
331 vlc_va_copy( args, _args );
332 if( vasprintf( &psz_str, psz_format, args ) == -1 )
336 psz_str = (char*) malloc( i_size );
339 if( psz_str == NULL )
342 fprintf( stderr, "main warning: can't store message (%m): " );
346 /* we're not using GLIBC, so we are sure that the error description
347 * will be stored in the buffer we provide to strerror_r() */
348 strerror_r( errno, psz_err, 1001 );
350 strncpy( psz_err, strerror( errno ), 1001 );
352 psz_err[1000] = '\0';
353 fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
355 vlc_va_copy( args, _args );
356 /* We should use utf8_vfprintf - but it calls malloc()... */
357 vfprintf( stderr, psz_format, args );
359 fputs( "\n", stderr );
365 while( p_obj != NULL )
367 char *psz_old = NULL;
368 if( p_obj->psz_header )
370 i_header_size += strlen( p_obj->psz_header ) + 4;
373 psz_old = strdup( psz_header );
374 psz_header = (char*)realloc( psz_header, i_header_size );
375 snprintf( psz_header, i_header_size , "[%s] %s",
376 p_obj->psz_header, psz_old );
380 psz_header = (char *)malloc( i_header_size );
381 snprintf( psz_header, i_header_size, "[%s]",
386 p_obj = p_obj->p_parent;
389 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
390 vlc_va_copy( args, _args );
391 vsnprintf( psz_str, i_size, psz_format, args );
393 psz_str[ i_size - 1 ] = 0; /* Just in case */
398 vlc_mutex_lock( &p_queue->lock );
400 /* Check there is room in the queue for our message */
401 if( p_queue->b_overflow )
405 if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
407 /* Still in overflow mode, print from a dummy item */
412 /* Pheeew, at last, there is room in the queue! */
413 p_queue->b_overflow = false;
416 else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
420 if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
422 p_queue->b_overflow = true;
424 /* Put the overflow message in the queue */
425 p_item = p_queue->msg + p_queue->i_stop;
426 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
428 p_item->i_type = VLC_MSG_WARN;
429 p_item->i_object_id = p_this->i_object_id;
430 p_item->psz_object = object_description( p_this );
431 p_item->psz_module = strdup( "message" );
432 p_item->psz_msg = strdup( "message queue overflowed" );
433 p_item->psz_header = NULL;
435 PrintMsg( p_this, p_item );
436 /* We print from a dummy item */
441 if( !p_queue->b_overflow )
443 /* Put the message in the queue */
444 p_item = p_queue->msg + p_queue->i_stop;
445 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
448 /* Fill message information fields */
449 p_item->i_type = i_type;
450 p_item->i_object_id = p_this->i_object_id;
451 p_item->psz_object = object_description( p_this );
452 p_item->psz_module = strdup( psz_module );
453 p_item->psz_msg = psz_str;
454 p_item->psz_header = psz_header;
456 PrintMsg( p_this, p_item );
458 if( p_queue->b_overflow )
460 free( p_item->psz_module );
461 free( p_item->psz_msg );
462 free( p_item->psz_header );
463 free( p_item->psz_object );
466 vlc_mutex_unlock ( &p_queue->lock );
470 /* following functions are local */
472 /*****************************************************************************
474 *****************************************************************************
475 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
476 * this function does not check the lock.
477 *****************************************************************************/
478 static void FlushMsg ( msg_queue_t *p_queue )
480 int i_index, i_start, i_stop;
482 /* Get the maximum message index that can be freed */
483 i_stop = p_queue->i_stop;
485 /* Check until which value we can free messages */
486 for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
488 i_start = p_queue->pp_sub[ i_index ]->i_start;
490 /* If this subscriber is late, we don't free messages before
491 * his i_start value, otherwise he'll miss messages */
492 if( ( i_start < i_stop
493 && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
494 || ( i_stop < i_start
495 && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
501 /* Free message data */
502 for( i_index = p_queue->i_start;
504 i_index = (i_index+1) % VLC_MSG_QSIZE )
506 free( p_queue->msg[i_index].psz_msg );
507 free( p_queue->msg[i_index].psz_module );
508 free( p_queue->msg[i_index].psz_header );
509 free( p_queue->msg[i_index].psz_object );
512 /* Update the new start value */
513 p_queue->i_start = i_index;
516 /*****************************************************************************
517 * PrintMsg: output a standard message item to stderr
518 *****************************************************************************
519 * Print a message to stderr, with colour formatting if needed.
520 *****************************************************************************/
521 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
523 # define COL(x) "\033[" #x ";1m"
525 # define GREEN COL(32)
526 # define YELLOW COL(33)
527 # define WHITE COL(0)
528 # define GRAY "\033[0m"
533 static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
534 static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
535 const char *psz_object;
536 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
537 int i_type = p_item->i_type;
542 if( priv->i_verbose < 0 ) return;
545 if( priv->i_verbose < 0 ) return;
548 if( priv->i_verbose < 1 ) return;
551 if( priv->i_verbose < 2 ) return;
555 psz_object = p_item->psz_object;
558 # define CE_WRITE(str) WriteFile( QUEUE.logfile, \
559 str, strlen(str), &i_dummy, NULL );
560 CE_WRITE( p_item->psz_module );
562 CE_WRITE( psz_object );
563 CE_WRITE( ppsz_type[i_type] );
565 CE_WRITE( p_item->psz_msg );
567 FlushFileBuffers( QUEUE.logfile );
570 /* Send the message to stderr */
573 if( p_item->psz_header )
575 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s %s%s: %s%s" GRAY
577 p_item->i_object_id, p_item->psz_header,
578 p_item->psz_module, psz_object,
579 ppsz_type[i_type], ppsz_color[i_type],
584 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
585 p_item->i_object_id, p_item->psz_module, psz_object,
586 ppsz_type[i_type], ppsz_color[i_type],
592 if( p_item->psz_header )
594 utf8_fprintf( stderr, "[%.8i] %s %s %s%s: %s\n", p_item->i_object_id,
595 p_item->psz_header, p_item->psz_module,
596 psz_object, ppsz_type[i_type], p_item->psz_msg );
600 utf8_fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
601 p_item->psz_module, psz_object, ppsz_type[i_type],
612 static msg_context_t* GetContext(void)
614 msg_context_t *p_ctx = vlc_threadvar_get( &msg_context_global_key );
617 MALLOC_NULL( p_ctx, msg_context_t );
618 p_ctx->psz_message = NULL;
619 vlc_threadvar_set( &msg_context_global_key, p_ctx );
624 void msg_StackDestroy (void *data)
626 msg_context_t *p_ctx = data;
628 free (p_ctx->psz_message);
632 void msg_StackSet( int i_code, const char *psz_message, ... )
635 msg_context_t *p_ctx = GetContext();
639 free( p_ctx->psz_message );
641 va_start( ap, psz_message );
642 if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
643 p_ctx->psz_message = NULL;
646 p_ctx->i_code = i_code;
649 void msg_StackAdd( const char *psz_message, ... )
653 msg_context_t *p_ctx = GetContext();
658 va_start( ap, psz_message );
659 if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
663 if( !p_ctx->psz_message )
664 p_ctx->psz_message = psz_tmp;
668 if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
671 free( p_ctx->psz_message );
672 p_ctx->psz_message = psz_new;
677 const char* msg_StackMsg( void )
679 msg_context_t *p_ctx = GetContext();
681 return p_ctx->psz_message;