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 <unistd.h> /* close(), write() */
51 #include <vlc_charset.h>
53 /*****************************************************************************
55 *****************************************************************************/
56 #if defined(HAVE_VA_COPY)
57 # define vlc_va_copy(dest,src) va_copy(dest,src)
58 #elif defined(HAVE___VA_COPY)
59 # define vlc_va_copy(dest,src) __va_copy(dest,src)
61 # define vlc_va_copy(dest,src) (dest)=(src)
64 #define QUEUE(i) p_this->p_libvlc->msg_bank.queues[i]
65 #define LOCK_BANK vlc_mutex_lock( &p_this->p_libvlc->msg_bank.lock );
66 #define UNLOCK_BANK vlc_mutex_unlock( &p_this->p_libvlc->msg_bank.lock );
68 /*****************************************************************************
70 *****************************************************************************/
71 static void QueueMsg ( vlc_object_t *, int, int , const char *,
72 const char *, va_list );
73 static void FlushMsg ( msg_queue_t * );
74 static void PrintMsg ( vlc_object_t *, msg_item_t * );
77 * Initialize messages queues
78 * This function initializes all message queues
80 void __msg_Create( vlc_object_t *p_this )
83 vlc_mutex_init( p_this, &(p_this->p_libvlc->msg_bank.lock) );
85 for( i = 0; i < 2; i++ )
87 vlc_mutex_init( p_this, &QUEUE(i).lock );
88 QUEUE(i).b_overflow = VLC_FALSE;
97 QUEUE(MSG_QUEUE_NORMAL).logfile =
98 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
99 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
100 CREATE_ALWAYS, 0, NULL );
101 SetFilePointer( QUEUE(MSG_QUEUE_NORMAL).logfile, 0, NULL, FILE_END );
106 * Flush all message queues
108 void __msg_Flush( vlc_object_t *p_this )
111 for( i = 0 ; i < NB_QUEUES ; i++ )
113 vlc_mutex_lock( &QUEUE(i).lock );
114 FlushMsg( &QUEUE(i) );
115 vlc_mutex_unlock( &QUEUE(i).lock );
120 * Destroy the message queues
122 * This functions prints all messages remaining in the queues,
123 * then frees all the allocated ressources
124 * No other messages interface functions should be called after this one.
126 void __msg_Destroy( vlc_object_t *p_this )
129 for( i = NB_QUEUES -1 ; i >= 0; i-- )
132 msg_Err( p_this, "stale interface subscribers" );
134 FlushMsg( &QUEUE(i) );
137 if( i == MSG_QUEUE_NORMAL )
138 CloseHandle( QUEUE(MSG_QUEUE_NORMAL).logfile );
141 vlc_mutex_destroy( &QUEUE(i).lock );
143 vlc_mutex_destroy( &(p_this->p_libvlc->msg_bank.lock) );
147 * Subscribe to a message queue.
149 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this, int i )
151 msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
153 assert( i < NB_QUEUES );
156 vlc_mutex_lock( &QUEUE(i).lock );
158 TAB_APPEND( QUEUE(i).i_sub, QUEUE(i).pp_sub, p_sub );
160 p_sub->i_start = QUEUE(i).i_start;
161 p_sub->pi_stop = &QUEUE(i).i_stop;
162 p_sub->p_msg = QUEUE(i).msg;
163 p_sub->p_lock = &QUEUE(i).lock;
165 vlc_mutex_unlock( &QUEUE(i).lock );
172 * Unsubscribe from a message queue.
174 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
179 for( i = 0 ; i< NB_QUEUES ; i++ )
181 vlc_mutex_lock( &QUEUE(i).lock );
182 for( j = 0 ; j< QUEUE(i).i_sub ; j++ )
184 if( QUEUE(i).pp_sub[j] == p_sub )
186 REMOVE_ELEM( QUEUE(i).pp_sub, QUEUE(i).i_sub, j );
187 if( p_sub ) free( p_sub );
190 vlc_mutex_unlock( & QUEUE(i).lock );
195 const char *msg_GetObjectTypeName(int i_object_type )
197 switch( i_object_type )
199 case VLC_OBJECT_GLOBAL: return "global";
200 case VLC_OBJECT_LIBVLC: return "libvlc";
201 case VLC_OBJECT_MODULE: return "module";
202 case VLC_OBJECT_INTF: return "interface";
203 case VLC_OBJECT_PLAYLIST: return "playlist";
204 case VLC_OBJECT_ITEM: return "item";
205 case VLC_OBJECT_INPUT: return "input";
206 case VLC_OBJECT_DECODER: return "decoder";
207 case VLC_OBJECT_PACKETIZER: return "packetizer";
208 case VLC_OBJECT_ENCODER: return "encoder";
209 case VLC_OBJECT_VOUT: return "video output";
210 case VLC_OBJECT_AOUT: return "audio output";
211 case VLC_OBJECT_SOUT: return "stream output";
212 case VLC_OBJECT_HTTPD: return "http server";
213 case VLC_OBJECT_HTTPD_HOST: return "http server";
214 case VLC_OBJECT_DIALOGS: return "dialogs provider";
215 case VLC_OBJECT_VLM: return "vlm";
216 case VLC_OBJECT_ANNOUNCE: return "announce handler";
217 case VLC_OBJECT_DEMUX: return "demuxer";
218 case VLC_OBJECT_ACCESS: return "access";
219 case VLC_OBJECT_META_ENGINE: return "meta engine";
220 default: return "private";
224 /*****************************************************************************
225 * __msg_*: print a message
226 *****************************************************************************
227 * These functions queue a message for later printing.
228 *****************************************************************************/
229 void __msg_Generic( vlc_object_t *p_this, int i_queue, int i_type,
230 const char *psz_module,
231 const char *psz_format, ... )
235 va_start( args, psz_format );
236 QueueMsg( p_this, i_queue, i_type, psz_module, psz_format, args );
240 void __msg_GenericVa( vlc_object_t *p_this, int i_queue,
241 int i_type, const char *psz_module,
242 const char *psz_format, va_list args )
244 QueueMsg( p_this, i_queue, i_type, psz_module, psz_format, args );
247 /* Generic functions used when variadic macros are not available. */
248 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
249 void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
252 va_start( args, psz_format ); \
253 QueueMsg( p_this,MSG_QUEUE_NORMAL, FN_TYPE, "unknown", \
254 psz_format, args ); \
259 * Output an informational message.
260 * \note Do not use this for debug messages
263 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
265 * Output an error message.
267 DECLARE_MSG_FN( __msg_Err, VLC_MSG_ERR );
269 * Output a waring message
271 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
273 * Output a debug message
275 DECLARE_MSG_FN( __msg_Dbg, VLC_MSG_DBG );
278 * Add a message to a queue
280 * This function provides basic functionnalities to other msg_* functions.
281 * It adds a message to a queue (after having printed all stored messages if it
282 * is full). If the message can't be converted to string in memory, it issues
285 static void QueueMsg( vlc_object_t *p_this, int i_queue, int i_type,
286 const char *psz_module,
287 const char *psz_format, va_list _args )
289 int i_header_size; /* Size of the additionnal header */
291 char * psz_str = NULL; /* formatted message string */
292 char * psz_header = NULL;
294 msg_item_t * p_item = NULL; /* pointer to message */
295 msg_item_t item; /* message in case of a full queue */
296 msg_queue_t *p_queue;
298 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
299 int i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
305 if( i_type == VLC_MSG_DBG )
308 utf8_vfprintf( stderr, psz_format, _args );
312 if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
313 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
317 /* Expand %m to strerror(errno) - only once */
318 char buf[strlen( psz_format ) + 2001], *ptr;
319 strcpy( buf, psz_format );
321 psz_format = (const char*) buf;
325 ptr = strchr( ptr, '%' );
335 strerror_r( errno, errbuf, 1001 );
337 int sockerr = WSAGetLastError( );
340 strncpy( errbuf, net_strerror( sockerr ), 1001 );
341 WSASetLastError( sockerr );
344 || (strcmp ("Unknown network stack error", errbuf) == 0))
345 strncpy( errbuf, strerror( errno ), 1001 );
349 /* Escape '%' from the error string */
350 for( char *percent = strchr( errbuf, '%' );
352 percent = strchr( percent + 2, '%' ) )
354 memmove( percent + 1, percent, strlen( percent ) + 1 );
357 errlen = strlen( errbuf );
358 memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
359 memcpy( ptr, errbuf, errlen );
360 break; /* Only once, so we don't overflow */
363 /* Looks for conversion specifier... */
366 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
368 ptr++; /* ...and skip it */
372 /* Convert message to string */
373 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
374 vlc_va_copy( args, _args );
375 if( vasprintf( &psz_str, psz_format, args ) == -1 )
379 psz_str = (char*) malloc( i_size );
382 if( psz_str == NULL )
385 fprintf( stderr, "main warning: can't store message (%m): " );
389 /* we're not using GLIBC, so we are sure that the error description
390 * will be stored in the buffer we provide to strerror_r() */
391 strerror_r( errno, psz_err, 1001 );
393 strncpy( psz_err, strerror( errno ), 1001 );
395 psz_err[1000] = '\0';
396 fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
398 vlc_va_copy( args, _args );
399 /* We should use utf8_vfprintf - but it calls malloc()... */
400 vfprintf( stderr, psz_format, args );
402 fputs( "\n", stderr );
408 while( p_obj != NULL )
410 char *psz_old = NULL;
411 if( p_obj->psz_header )
413 i_header_size += strlen( p_obj->psz_header ) + 4;
416 psz_old = strdup( psz_header );
417 psz_header = (char*)realloc( psz_header, i_header_size );
418 snprintf( psz_header, i_header_size , "[%s] %s",
419 p_obj->psz_header, psz_old );
423 psz_header = (char *)malloc( i_header_size );
424 snprintf( psz_header, i_header_size, "[%s]",
428 if( psz_old ) free( psz_old );
429 p_obj = p_obj->p_parent;
432 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
433 vlc_va_copy( args, _args );
434 vsnprintf( psz_str, i_size, psz_format, args );
436 psz_str[ i_size - 1 ] = 0; /* Just in case */
439 assert( i_queue < NB_QUEUES );
441 p_queue = &QUEUE(i_queue) ;
442 vlc_mutex_lock( &p_queue->lock );
444 /* Check there is room in the queue for our message */
445 if( p_queue->b_overflow )
449 if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
451 /* Still in overflow mode, print from a dummy item */
456 /* Pheeew, at last, there is room in the queue! */
457 p_queue->b_overflow = VLC_FALSE;
460 else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
464 if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
466 p_queue->b_overflow = VLC_TRUE;
468 if( p_queue->i_id == MSG_QUEUE_NORMAL )
470 /* Put the overflow message in the queue */
471 p_item = p_queue->msg + p_queue->i_stop;
472 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
474 p_item->i_type = VLC_MSG_WARN;
475 p_item->i_object_id = p_this->i_object_id;
476 p_item->i_object_type = p_this->i_object_type;
477 p_item->psz_module = strdup( "message" );
478 p_item->psz_msg = strdup( "message queue overflowed" );
479 p_item->psz_header = NULL;
481 PrintMsg( p_this, p_item );
482 /* We print from a dummy item */
488 if( !p_queue->b_overflow )
490 /* Put the message in the queue */
491 p_item = p_queue->msg + p_queue->i_stop;
492 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
495 /* Fill message information fields */
496 p_item->i_type = i_type;
497 p_item->i_object_id = p_this->i_object_id;
498 p_item->i_object_type = p_this->i_object_type;
499 p_item->psz_module = strdup( psz_module );
500 p_item->psz_msg = psz_str;
501 p_item->psz_header = psz_header;
503 if( p_queue->i_id == MSG_QUEUE_NORMAL )
504 PrintMsg( p_this, p_item );
506 if( p_queue->b_overflow )
508 if( p_item->psz_module )
509 free( p_item->psz_module );
510 if( p_item->psz_msg )
511 free( p_item->psz_msg );
512 if( p_item->psz_header )
513 free( p_item->psz_header );
516 vlc_mutex_unlock ( &p_queue->lock );
520 /* following functions are local */
522 /*****************************************************************************
524 *****************************************************************************
525 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
526 * this function does not check the lock.
527 *****************************************************************************/
528 static void FlushMsg ( msg_queue_t *p_queue )
530 int i_index, i_start, i_stop;
532 /* Get the maximum message index that can be freed */
533 i_stop = p_queue->i_stop;
535 /* Check until which value we can free messages */
536 for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
538 i_start = p_queue->pp_sub[ i_index ]->i_start;
540 /* If this subscriber is late, we don't free messages before
541 * his i_start value, otherwise he'll miss messages */
542 if( ( i_start < i_stop
543 && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
544 || ( i_stop < i_start
545 && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
551 /* Free message data */
552 for( i_index = p_queue->i_start;
554 i_index = (i_index+1) % VLC_MSG_QSIZE )
556 if( p_queue->msg[i_index].psz_msg )
557 free( p_queue->msg[i_index].psz_msg );
558 if( p_queue->msg[i_index].psz_module )
559 free( p_queue->msg[i_index].psz_module );
560 if( p_queue->msg[i_index].psz_header )
561 free( p_queue->msg[i_index].psz_header );
564 /* Update the new start value */
565 p_queue->i_start = i_index;
568 /*****************************************************************************
569 * PrintMsg: output a standard message item to stderr
570 *****************************************************************************
571 * Print a message to stderr, with colour formatting if needed.
572 *****************************************************************************/
573 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
575 # define COL(x) "\033[" #x ";1m"
577 # define GREEN COL(32)
578 # define YELLOW COL(33)
579 # define WHITE COL(0)
580 # define GRAY "\033[0m"
585 static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
586 static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
587 const char *psz_object;
588 int i_type = p_item->i_type;
593 if( p_this->p_libvlc->i_verbose < 0 ) return;
596 if( p_this->p_libvlc->i_verbose < 0 ) return;
599 if( p_this->p_libvlc->i_verbose < 1 ) return;
602 if( p_this->p_libvlc->i_verbose < 2 ) return;
606 psz_object = msg_GetObjectTypeName(p_item->i_object_type);
609 # define CE_WRITE(str) WriteFile( QUEUE(MSG_QUEUE_NORMAL).logfile, \
610 str, strlen(str), &i_dummy, NULL );
611 CE_WRITE( p_item->psz_module );
613 CE_WRITE( psz_object );
614 CE_WRITE( ppsz_type[i_type] );
616 CE_WRITE( p_item->psz_msg );
618 FlushFileBuffers( QUEUE(MSG_QUEUE_NORMAL).logfile );
621 /* Send the message to stderr */
622 if( p_this->p_libvlc->b_color )
624 if( p_item->psz_header )
626 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s %s%s: %s%s" GRAY
628 p_item->i_object_id, p_item->psz_header,
629 p_item->psz_module, psz_object,
630 ppsz_type[i_type], ppsz_color[i_type],
635 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
636 p_item->i_object_id, p_item->psz_module, psz_object,
637 ppsz_type[i_type], ppsz_color[i_type],
643 if( p_item->psz_header )
645 utf8_fprintf( stderr, "[%.8i] %s %s %s%s: %s\n", p_item->i_object_id,
646 p_item->psz_header, p_item->psz_module,
647 psz_object, ppsz_type[i_type], p_item->psz_msg );
651 utf8_fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
652 p_item->psz_module, psz_object, ppsz_type[i_type],
663 static msg_context_t* GetContext(void)
665 msg_context_t *p_ctx = vlc_threadvar_get( &msg_context_global_key );
668 MALLOC_NULL( p_ctx, msg_context_t );
669 p_ctx->psz_message = NULL;
670 vlc_threadvar_set( &msg_context_global_key, p_ctx );
675 void msg_StackSet( int i_code, const char *psz_message, ... )
678 msg_context_t *p_ctx = GetContext();
681 va_start( ap, psz_message );
682 free( p_ctx->psz_message );
684 if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
685 p_ctx->psz_message = NULL;
688 p_ctx->i_code = i_code;
691 void msg_StackAdd( const char *psz_message, ... )
695 msg_context_t *p_ctx = GetContext();
698 va_start( ap, psz_message );
699 if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
703 if( !p_ctx->psz_message )
704 p_ctx->psz_message = psz_tmp;
707 char *psz_old = malloc( strlen( p_ctx->psz_message ) + 1 );
708 memcpy( psz_old, p_ctx->psz_message, strlen( p_ctx->psz_message ) + 1 );
709 p_ctx->psz_message = realloc( p_ctx->psz_message,
710 strlen( p_ctx->psz_message ) +
712 strlen( psz_tmp ) + 3 );
713 sprintf( p_ctx->psz_message, "%s: %s", psz_tmp, psz_old );
714 free( psz_tmp ); free( psz_old );
718 const char* msg_StackMsg( void )
720 msg_context_t *p_ctx = GetContext();
722 return p_ctx->psz_message;