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 *****************************************************************************/
33 #include <stdio.h> /* required */
34 #include <stdarg.h> /* va_list for BSD */
35 #include <stdlib.h> /* malloc() */
36 #include <string.h> /* strerror() */
39 # include <fcntl.h> /* O_CREAT, O_TRUNC, O_WRONLY, O_SYNC */
42 #include <errno.h> /* errno */
45 # include <unistd.h> /* close(), write() */
50 #include <vlc_charset.h>
52 /*****************************************************************************
54 *****************************************************************************/
55 #if defined(HAVE_VA_COPY)
56 # define vlc_va_copy(dest,src) va_copy(dest,src)
57 #elif defined(HAVE___VA_COPY)
58 # define vlc_va_copy(dest,src) __va_copy(dest,src)
60 # define vlc_va_copy(dest,src) (dest)=(src)
63 #define QUEUE(i) p_this->p_libvlc->msg_bank.queues[i]
64 #define LOCK_BANK vlc_mutex_lock( &p_this->p_libvlc->msg_bank.lock );
65 #define UNLOCK_BANK vlc_mutex_unlock( &p_this->p_libvlc->msg_bank.lock );
67 /*****************************************************************************
69 *****************************************************************************/
70 static void QueueMsg ( vlc_object_t *, int, int , const char *,
71 const char *, va_list );
72 static void FlushMsg ( msg_queue_t * );
73 static void PrintMsg ( vlc_object_t *, msg_item_t * );
76 * Initialize messages queues
77 * This function initializes all message queues
79 void __msg_Create( vlc_object_t *p_this )
81 vlc_mutex_init( p_this, &(p_this->p_libvlc->msg_bank.lock) );
83 #define QUEUE_INIT(i) \
84 vlc_mutex_init( p_this, &QUEUE(i).lock ); \
85 QUEUE(i).b_overflow = VLC_FALSE; \
87 QUEUE(i).i_start = 0; \
88 QUEUE(i).i_stop = 0; \
98 QUEUE(MSG_QUEUE_NORMAL).logfile =
99 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
100 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
101 CREATE_ALWAYS, 0, NULL );
102 SetFilePointer( QUEUE(MSG_QUEUE_NORMAL).logfile, 0, NULL, FILE_END );
107 * Flush all message queues
109 void __msg_Flush( vlc_object_t *p_this )
112 for( i = 0 ; i < NB_QUEUES ; i++ )
114 vlc_mutex_lock( &QUEUE(i).lock );
115 FlushMsg( &QUEUE(i) );
116 vlc_mutex_unlock( &QUEUE(i).lock );
121 * Destroy the message queues
123 * This functions prints all messages remaining in the queues,
124 * then frees all the allocated ressources
125 * No other messages interface functions should be called after this one.
127 void __msg_Destroy( vlc_object_t *p_this )
130 for( i = NB_QUEUES -1 ; i >= 0; i-- )
133 msg_Err( p_this, "stale interface subscribers" );
135 FlushMsg( &QUEUE(i) );
138 if( i == MSG_QUEUE_NORMAL )
139 CloseHandle( QUEUE(MSG_QUEUE_NORMAL).logfile );
142 vlc_mutex_destroy( &QUEUE(i).lock );
144 vlc_mutex_destroy( &(p_this->p_libvlc->msg_bank.lock) );
148 * Subscribe to a message queue.
150 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this, int i )
152 msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
154 assert( i < NB_QUEUES );
157 vlc_mutex_lock( &QUEUE(i).lock );
159 TAB_APPEND( QUEUE(i).i_sub, QUEUE(i).pp_sub, p_sub );
161 p_sub->i_start = QUEUE(i).i_start;
162 p_sub->pi_stop = &QUEUE(i).i_stop;
163 p_sub->p_msg = QUEUE(i).msg;
164 p_sub->p_lock = &QUEUE(i).lock;
166 vlc_mutex_unlock( &QUEUE(i).lock );
173 * Unsubscribe from a message queue.
175 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
180 for( i = 0 ; i< NB_QUEUES ; i++ )
182 vlc_mutex_lock( &QUEUE(i).lock );
183 for( j = 0 ; j< QUEUE(i).i_sub ; j++ )
185 if( QUEUE(i).pp_sub[j] == p_sub )
187 REMOVE_ELEM( QUEUE(i).pp_sub, QUEUE(i).i_sub, j );
188 if( p_sub ) free( p_sub );
191 vlc_mutex_unlock( & QUEUE(i).lock );
196 const char *msg_GetObjectTypeName(int i_object_type )
198 switch( i_object_type )
200 case VLC_OBJECT_GLOBAL: return "global";
201 case VLC_OBJECT_LIBVLC: return "libvlc";
202 case VLC_OBJECT_MODULE: return "module";
203 case VLC_OBJECT_INTF: return "interface";
204 case VLC_OBJECT_PLAYLIST: return "playlist";
205 case VLC_OBJECT_ITEM: return "item";
206 case VLC_OBJECT_INPUT: return "input";
207 case VLC_OBJECT_DECODER: return "decoder";
208 case VLC_OBJECT_PACKETIZER: return "packetizer";
209 case VLC_OBJECT_ENCODER: return "encoder";
210 case VLC_OBJECT_VOUT: return "video output";
211 case VLC_OBJECT_AOUT: return "audio output";
212 case VLC_OBJECT_SOUT: return "stream output";
213 case VLC_OBJECT_HTTPD: return "http server";
214 case VLC_OBJECT_HTTPD_HOST: return "http server";
215 case VLC_OBJECT_DIALOGS: return "dialogs provider";
216 case VLC_OBJECT_VLM: return "vlm";
217 case VLC_OBJECT_ANNOUNCE: return "announce handler";
218 case VLC_OBJECT_DEMUX: return "demuxer";
219 case VLC_OBJECT_ACCESS: return "access";
220 case VLC_OBJECT_META_ENGINE: return "meta engine";
221 default: return "private";
225 /*****************************************************************************
226 * __msg_*: print a message
227 *****************************************************************************
228 * These functions queue a message for later printing.
229 *****************************************************************************/
230 void __msg_Generic( vlc_object_t *p_this, int i_queue, int i_type,
231 const char *psz_module,
232 const char *psz_format, ... )
236 va_start( args, psz_format );
237 QueueMsg( p_this, i_queue, i_type, psz_module, psz_format, args );
241 void __msg_GenericVa( vlc_object_t *p_this, int i_queue,
242 int i_type, const char *psz_module,
243 const char *psz_format, va_list args )
245 QueueMsg( p_this, i_queue, i_type, psz_module, psz_format, args );
248 /* Generic functions used when variadic macros are not available. */
249 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
250 void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
253 va_start( args, psz_format ); \
254 QueueMsg( (vlc_object_t *)p_this,MSG_QUEUE_NORMAL, FN_TYPE, "unknown", \
255 psz_format, args ); \
260 * Output an informational message.
261 * \note Do not use this for debug messages
264 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
266 * Output an error message.
268 DECLARE_MSG_FN( __msg_Err, VLC_MSG_ERR );
270 * Output a waring message
272 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
274 * Output a debug message
276 DECLARE_MSG_FN( __msg_Dbg, VLC_MSG_DBG );
279 * Add a message to a queue
281 * This function provides basic functionnalities to other msg_* functions.
282 * It adds a message to a queue (after having printed all stored messages if it
283 * is full). If the message can't be converted to string in memory, it issues
286 static void QueueMsg( vlc_object_t *p_this, int i_queue, int i_type,
287 const char *psz_module,
288 const char *psz_format, va_list _args )
290 int i_header_size; /* Size of the additionnal header */
292 char * psz_str = NULL; /* formatted message string */
293 char * psz_header = NULL;
295 msg_item_t * p_item = NULL; /* pointer to message */
296 msg_item_t item; /* message in case of a full queue */
297 msg_queue_t *p_queue;
299 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
300 int i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
303 if( p_this == NULL || p_this->i_flags & OBJECT_FLAGS_QUIET ||
304 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
309 /* Convert message to string */
310 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
311 vlc_va_copy( args, _args );
312 vasprintf( &psz_str, psz_format, args );
315 psz_str = (char*) malloc( i_size * sizeof(char) );
318 if( psz_str == NULL )
320 fprintf( stderr, "main warning: can't store message (%s): ",
322 vlc_va_copy( args, _args );
323 /* We should use utf8_vfprintf - but it calls malloc()... */
324 vfprintf( stderr, psz_format, args );
326 fputs( "\n", stderr );
332 while( p_obj != NULL )
334 char *psz_old = NULL;
335 if( p_obj->psz_header )
337 i_header_size += strlen( p_obj->psz_header ) + 4;
340 psz_old = strdup( psz_header );
341 psz_header = (char*)realloc( psz_header, i_header_size );
342 snprintf( psz_header, i_header_size , "[%s] %s",
343 p_obj->psz_header, psz_old );
347 psz_header = (char *)malloc( i_header_size );
348 snprintf( psz_header, i_header_size, "[%s]",
352 if( psz_old ) free( psz_old );
353 p_obj = p_obj->p_parent;
356 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
357 vlc_va_copy( args, _args );
358 vsnprintf( psz_str, i_size, psz_format, args );
360 psz_str[ i_size - 1 ] = 0; /* Just in case */
363 assert( i_queue < NB_QUEUES );
365 p_queue = &QUEUE(i_queue) ;
366 vlc_mutex_lock( &p_queue->lock );
368 /* Check there is room in the queue for our message */
369 if( p_queue->b_overflow )
373 if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
375 /* Still in overflow mode, print from a dummy item */
380 /* Pheeew, at last, there is room in the queue! */
381 p_queue->b_overflow = VLC_FALSE;
384 else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
388 if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
390 p_queue->b_overflow = VLC_TRUE;
392 if( p_queue->i_id == MSG_QUEUE_NORMAL )
394 /* Put the overflow message in the queue */
395 p_item = p_queue->msg + p_queue->i_stop;
396 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
398 p_item->i_type = VLC_MSG_WARN;
399 p_item->i_object_id = p_this->i_object_id;
400 p_item->i_object_type = p_this->i_object_type;
401 p_item->psz_module = strdup( "message" );
402 p_item->psz_msg = strdup( "message queue overflowed" );
403 p_item->psz_header = NULL;
405 PrintMsg( p_this, p_item );
406 /* We print from a dummy item */
412 if( !p_queue->b_overflow )
414 /* Put the message in the queue */
415 p_item = p_queue->msg + p_queue->i_stop;
416 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
419 /* Fill message information fields */
420 p_item->i_type = i_type;
421 p_item->i_object_id = p_this->i_object_id;
422 p_item->i_object_type = p_this->i_object_type;
423 p_item->psz_module = strdup( psz_module );
424 p_item->psz_msg = psz_str;
425 p_item->psz_header = psz_header;
427 if( p_queue->i_id == MSG_QUEUE_NORMAL )
428 PrintMsg( p_this, p_item );
430 if( p_queue->b_overflow )
432 if( p_item->psz_module )
433 free( p_item->psz_module );
434 if( p_item->psz_msg )
435 free( p_item->psz_msg );
436 if( p_item->psz_header )
437 free( p_item->psz_header );
440 vlc_mutex_unlock ( &p_queue->lock );
444 /* following functions are local */
446 /*****************************************************************************
448 *****************************************************************************
449 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
450 * this function does not check the lock.
451 *****************************************************************************/
452 static void FlushMsg ( msg_queue_t *p_queue )
454 int i_index, i_start, i_stop;
456 /* Get the maximum message index that can be freed */
457 i_stop = p_queue->i_stop;
459 /* Check until which value we can free messages */
460 for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
462 i_start = p_queue->pp_sub[ i_index ]->i_start;
464 /* If this subscriber is late, we don't free messages before
465 * his i_start value, otherwise he'll miss messages */
466 if( ( i_start < i_stop
467 && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
468 || ( i_stop < i_start
469 && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
475 /* Free message data */
476 for( i_index = p_queue->i_start;
478 i_index = (i_index+1) % VLC_MSG_QSIZE )
480 if( p_queue->msg[i_index].psz_msg )
481 free( p_queue->msg[i_index].psz_msg );
482 if( p_queue->msg[i_index].psz_module )
483 free( p_queue->msg[i_index].psz_module );
484 if( p_queue->msg[i_index].psz_header )
485 free( p_queue->msg[i_index].psz_header );
488 /* Update the new start value */
489 p_queue->i_start = i_index;
492 /*****************************************************************************
493 * PrintMsg: output a standard message item to stderr
494 *****************************************************************************
495 * Print a message to stderr, with colour formatting if needed.
496 *****************************************************************************/
497 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
499 # define COL(x) "\033[" #x ";1m"
501 # define GREEN COL(32)
502 # define YELLOW COL(33)
503 # define WHITE COL(37)
504 # define GRAY "\033[0m"
509 static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
510 static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
511 const char *psz_object;
512 int i_type = p_item->i_type;
517 if( p_this->p_libvlc->i_verbose < 0 ) return;
520 if( p_this->p_libvlc->i_verbose < 0 ) return;
523 if( p_this->p_libvlc->i_verbose < 1 ) return;
526 if( p_this->p_libvlc->i_verbose < 2 ) return;
530 psz_object = msg_GetObjectTypeName(p_item->i_object_type);
533 # define CE_WRITE(str) WriteFile( QUEUE(MSG_QUEUE_NORMAL).logfile, \
534 str, strlen(str), &i_dummy, NULL );
535 CE_WRITE( p_item->psz_module );
537 CE_WRITE( psz_object );
538 CE_WRITE( ppsz_type[i_type] );
540 CE_WRITE( p_item->psz_msg );
542 FlushFileBuffers( QUEUE(MSG_QUEUE_NORMAL).logfile );
545 /* Send the message to stderr */
546 if( p_this->p_libvlc->b_color )
548 if( p_item->psz_header )
550 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s %s%s: %s%s" GRAY
552 p_item->i_object_id, p_item->psz_header,
553 p_item->psz_module, psz_object,
554 ppsz_type[i_type], ppsz_color[i_type],
559 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
560 p_item->i_object_id, p_item->psz_module, psz_object,
561 ppsz_type[i_type], ppsz_color[i_type],
567 if( p_item->psz_header )
569 utf8_fprintf( stderr, "[%.8i] %s %s %s%s: %s\n", p_item->i_object_id,
570 p_item->psz_header, p_item->psz_module,
571 psz_object, ppsz_type[i_type], p_item->psz_msg );
575 utf8_fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
576 p_item->psz_module, psz_object, ppsz_type[i_type],
587 static msg_context_t* GetContext(void)
589 msg_context_t *p_ctx = vlc_threadvar_get( &msg_context_global_key );
592 MALLOC_NULL( p_ctx, msg_context_t );
593 p_ctx->psz_message = NULL;
594 vlc_threadvar_set( &msg_context_global_key, p_ctx );
599 void msg_StackSet( int i_code, const char *psz_message, ... )
602 msg_context_t *p_ctx = GetContext();
604 va_start( ap, psz_message );
605 if( p_ctx->psz_message != NULL )
607 free( p_ctx->psz_message );
610 vasprintf( &p_ctx->psz_message, psz_message, ap );
612 p_ctx->i_code = i_code;
615 void msg_StackAdd( const char *psz_message, ... )
619 msg_context_t *p_ctx = GetContext();
622 va_start( ap, psz_message );
623 vasprintf( &psz_tmp, psz_message, ap );
626 if( !p_ctx->psz_message )
627 p_ctx->psz_message = psz_tmp;
630 char *psz_old = malloc( strlen( p_ctx->psz_message ) + 1 );
631 memcpy( psz_old, p_ctx->psz_message, strlen( p_ctx->psz_message ) + 1 );
632 p_ctx->psz_message = realloc( p_ctx->psz_message,
633 strlen( p_ctx->psz_message ) +
635 strlen( psz_tmp ) + 3 );
636 sprintf( p_ctx->psz_message, "%s: %s", psz_tmp, psz_old );
637 free( psz_tmp ); free( psz_old );
641 const char* msg_StackMsg( void )
643 msg_context_t *p_ctx = GetContext();
645 return p_ctx->psz_message;