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 )
82 vlc_mutex_init( p_this, &(p_this->p_libvlc->msg_bank.lock) );
84 for( i = 0; i < 2; i++ )
86 vlc_mutex_init( p_this, &QUEUE(i).lock );
87 QUEUE(i).b_overflow = VLC_FALSE;
96 QUEUE(MSG_QUEUE_NORMAL).logfile =
97 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
98 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
99 CREATE_ALWAYS, 0, NULL );
100 SetFilePointer( QUEUE(MSG_QUEUE_NORMAL).logfile, 0, NULL, FILE_END );
105 * Flush all message queues
107 void __msg_Flush( vlc_object_t *p_this )
110 for( i = 0 ; i < NB_QUEUES ; i++ )
112 vlc_mutex_lock( &QUEUE(i).lock );
113 FlushMsg( &QUEUE(i) );
114 vlc_mutex_unlock( &QUEUE(i).lock );
119 * Destroy the message queues
121 * This functions prints all messages remaining in the queues,
122 * then frees all the allocated ressources
123 * No other messages interface functions should be called after this one.
125 void __msg_Destroy( vlc_object_t *p_this )
128 for( i = NB_QUEUES -1 ; i >= 0; i-- )
131 msg_Err( p_this, "stale interface subscribers" );
133 FlushMsg( &QUEUE(i) );
136 if( i == MSG_QUEUE_NORMAL )
137 CloseHandle( QUEUE(MSG_QUEUE_NORMAL).logfile );
140 vlc_mutex_destroy( &QUEUE(i).lock );
142 vlc_mutex_destroy( &(p_this->p_libvlc->msg_bank.lock) );
146 * Subscribe to a message queue.
148 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this, int i )
150 msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
152 assert( i < NB_QUEUES );
155 vlc_mutex_lock( &QUEUE(i).lock );
157 TAB_APPEND( QUEUE(i).i_sub, QUEUE(i).pp_sub, p_sub );
159 p_sub->i_start = QUEUE(i).i_start;
160 p_sub->pi_stop = &QUEUE(i).i_stop;
161 p_sub->p_msg = QUEUE(i).msg;
162 p_sub->p_lock = &QUEUE(i).lock;
164 vlc_mutex_unlock( &QUEUE(i).lock );
171 * Unsubscribe from a message queue.
173 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
178 for( i = 0 ; i< NB_QUEUES ; i++ )
180 vlc_mutex_lock( &QUEUE(i).lock );
181 for( j = 0 ; j< QUEUE(i).i_sub ; j++ )
183 if( QUEUE(i).pp_sub[j] == p_sub )
185 REMOVE_ELEM( QUEUE(i).pp_sub, QUEUE(i).i_sub, j );
186 if( p_sub ) free( p_sub );
189 vlc_mutex_unlock( & QUEUE(i).lock );
194 const char *msg_GetObjectTypeName(int i_object_type )
196 switch( i_object_type )
198 case VLC_OBJECT_GLOBAL: return "global";
199 case VLC_OBJECT_LIBVLC: return "libvlc";
200 case VLC_OBJECT_MODULE: return "module";
201 case VLC_OBJECT_INTF: return "interface";
202 case VLC_OBJECT_PLAYLIST: return "playlist";
203 case VLC_OBJECT_ITEM: return "item";
204 case VLC_OBJECT_INPUT: return "input";
205 case VLC_OBJECT_DECODER: return "decoder";
206 case VLC_OBJECT_PACKETIZER: return "packetizer";
207 case VLC_OBJECT_ENCODER: return "encoder";
208 case VLC_OBJECT_VOUT: return "video output";
209 case VLC_OBJECT_AOUT: return "audio output";
210 case VLC_OBJECT_SOUT: return "stream output";
211 case VLC_OBJECT_HTTPD: return "http server";
212 case VLC_OBJECT_HTTPD_HOST: return "http server";
213 case VLC_OBJECT_DIALOGS: return "dialogs provider";
214 case VLC_OBJECT_VLM: return "vlm";
215 case VLC_OBJECT_ANNOUNCE: return "announce handler";
216 case VLC_OBJECT_DEMUX: return "demuxer";
217 case VLC_OBJECT_ACCESS: return "access";
218 case VLC_OBJECT_META_ENGINE: return "meta engine";
219 default: return "private";
223 /*****************************************************************************
224 * __msg_*: print a message
225 *****************************************************************************
226 * These functions queue a message for later printing.
227 *****************************************************************************/
228 void __msg_Generic( vlc_object_t *p_this, int i_queue, int i_type,
229 const char *psz_module,
230 const char *psz_format, ... )
234 va_start( args, psz_format );
235 QueueMsg( p_this, i_queue, i_type, psz_module, psz_format, args );
239 void __msg_GenericVa( vlc_object_t *p_this, int i_queue,
240 int i_type, const char *psz_module,
241 const char *psz_format, va_list args )
243 QueueMsg( p_this, i_queue, i_type, psz_module, psz_format, args );
246 /* Generic functions used when variadic macros are not available. */
247 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
248 void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
251 va_start( args, psz_format ); \
252 QueueMsg( (vlc_object_t *)p_this,MSG_QUEUE_NORMAL, FN_TYPE, "unknown", \
253 psz_format, args ); \
258 * Output an informational message.
259 * \note Do not use this for debug messages
262 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
264 * Output an error message.
266 DECLARE_MSG_FN( __msg_Err, VLC_MSG_ERR );
268 * Output a waring message
270 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
272 * Output a debug message
274 DECLARE_MSG_FN( __msg_Dbg, VLC_MSG_DBG );
277 * Add a message to a queue
279 * This function provides basic functionnalities to other msg_* functions.
280 * It adds a message to a queue (after having printed all stored messages if it
281 * is full). If the message can't be converted to string in memory, it issues
284 static void QueueMsg( vlc_object_t *p_this, int i_queue, int i_type,
285 const char *psz_module,
286 const char *psz_format, va_list _args )
288 int i_header_size; /* Size of the additionnal header */
290 char * psz_str = NULL; /* formatted message string */
291 char * psz_header = NULL;
293 msg_item_t * p_item = NULL; /* pointer to message */
294 msg_item_t item; /* message in case of a full queue */
295 msg_queue_t *p_queue;
297 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
298 int i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
301 if( p_this == NULL || p_this->i_flags & OBJECT_FLAGS_QUIET ||
302 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
307 /* Convert message to string */
308 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
309 vlc_va_copy( args, _args );
310 if( vasprintf( &psz_str, psz_format, args ) == -1 )
314 psz_str = (char*) malloc( i_size );
317 if( psz_str == NULL )
319 fprintf( stderr, "main warning: can't store message (%s): ",
321 vlc_va_copy( args, _args );
322 /* We should use utf8_vfprintf - but it calls malloc()... */
323 vfprintf( stderr, psz_format, args );
325 fputs( "\n", stderr );
331 while( p_obj != NULL )
333 char *psz_old = NULL;
334 if( p_obj->psz_header )
336 i_header_size += strlen( p_obj->psz_header ) + 4;
339 psz_old = strdup( psz_header );
340 psz_header = (char*)realloc( psz_header, i_header_size );
341 snprintf( psz_header, i_header_size , "[%s] %s",
342 p_obj->psz_header, psz_old );
346 psz_header = (char *)malloc( i_header_size );
347 snprintf( psz_header, i_header_size, "[%s]",
351 if( psz_old ) free( psz_old );
352 p_obj = p_obj->p_parent;
355 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
356 vlc_va_copy( args, _args );
357 vsnprintf( psz_str, i_size, psz_format, args );
359 psz_str[ i_size - 1 ] = 0; /* Just in case */
362 assert( i_queue < NB_QUEUES );
364 p_queue = &QUEUE(i_queue) ;
365 vlc_mutex_lock( &p_queue->lock );
367 /* Check there is room in the queue for our message */
368 if( p_queue->b_overflow )
372 if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
374 /* Still in overflow mode, print from a dummy item */
379 /* Pheeew, at last, there is room in the queue! */
380 p_queue->b_overflow = VLC_FALSE;
383 else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
387 if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
389 p_queue->b_overflow = VLC_TRUE;
391 if( p_queue->i_id == MSG_QUEUE_NORMAL )
393 /* Put the overflow message in the queue */
394 p_item = p_queue->msg + p_queue->i_stop;
395 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
397 p_item->i_type = VLC_MSG_WARN;
398 p_item->i_object_id = p_this->i_object_id;
399 p_item->i_object_type = p_this->i_object_type;
400 p_item->psz_module = strdup( "message" );
401 p_item->psz_msg = strdup( "message queue overflowed" );
402 p_item->psz_header = NULL;
404 PrintMsg( p_this, p_item );
405 /* We print from a dummy item */
411 if( !p_queue->b_overflow )
413 /* Put the message in the queue */
414 p_item = p_queue->msg + p_queue->i_stop;
415 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
418 /* Fill message information fields */
419 p_item->i_type = i_type;
420 p_item->i_object_id = p_this->i_object_id;
421 p_item->i_object_type = p_this->i_object_type;
422 p_item->psz_module = strdup( psz_module );
423 p_item->psz_msg = psz_str;
424 p_item->psz_header = psz_header;
426 if( p_queue->i_id == MSG_QUEUE_NORMAL )
427 PrintMsg( p_this, p_item );
429 if( p_queue->b_overflow )
431 if( p_item->psz_module )
432 free( p_item->psz_module );
433 if( p_item->psz_msg )
434 free( p_item->psz_msg );
435 if( p_item->psz_header )
436 free( p_item->psz_header );
439 vlc_mutex_unlock ( &p_queue->lock );
443 /* following functions are local */
445 /*****************************************************************************
447 *****************************************************************************
448 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
449 * this function does not check the lock.
450 *****************************************************************************/
451 static void FlushMsg ( msg_queue_t *p_queue )
453 int i_index, i_start, i_stop;
455 /* Get the maximum message index that can be freed */
456 i_stop = p_queue->i_stop;
458 /* Check until which value we can free messages */
459 for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
461 i_start = p_queue->pp_sub[ i_index ]->i_start;
463 /* If this subscriber is late, we don't free messages before
464 * his i_start value, otherwise he'll miss messages */
465 if( ( i_start < i_stop
466 && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
467 || ( i_stop < i_start
468 && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
474 /* Free message data */
475 for( i_index = p_queue->i_start;
477 i_index = (i_index+1) % VLC_MSG_QSIZE )
479 if( p_queue->msg[i_index].psz_msg )
480 free( p_queue->msg[i_index].psz_msg );
481 if( p_queue->msg[i_index].psz_module )
482 free( p_queue->msg[i_index].psz_module );
483 if( p_queue->msg[i_index].psz_header )
484 free( p_queue->msg[i_index].psz_header );
487 /* Update the new start value */
488 p_queue->i_start = i_index;
491 /*****************************************************************************
492 * PrintMsg: output a standard message item to stderr
493 *****************************************************************************
494 * Print a message to stderr, with colour formatting if needed.
495 *****************************************************************************/
496 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
498 # define COL(x) "\033[" #x ";1m"
500 # define GREEN COL(32)
501 # define YELLOW COL(33)
502 # define WHITE COL(37)
503 # define GRAY "\033[0m"
508 static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
509 static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
510 const char *psz_object;
511 int i_type = p_item->i_type;
516 if( p_this->p_libvlc->i_verbose < 0 ) return;
519 if( p_this->p_libvlc->i_verbose < 0 ) return;
522 if( p_this->p_libvlc->i_verbose < 1 ) return;
525 if( p_this->p_libvlc->i_verbose < 2 ) return;
529 psz_object = msg_GetObjectTypeName(p_item->i_object_type);
532 # define CE_WRITE(str) WriteFile( QUEUE(MSG_QUEUE_NORMAL).logfile, \
533 str, strlen(str), &i_dummy, NULL );
534 CE_WRITE( p_item->psz_module );
536 CE_WRITE( psz_object );
537 CE_WRITE( ppsz_type[i_type] );
539 CE_WRITE( p_item->psz_msg );
541 FlushFileBuffers( QUEUE(MSG_QUEUE_NORMAL).logfile );
544 /* Send the message to stderr */
545 if( p_this->p_libvlc->b_color )
547 if( p_item->psz_header )
549 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s %s%s: %s%s" GRAY
551 p_item->i_object_id, p_item->psz_header,
552 p_item->psz_module, psz_object,
553 ppsz_type[i_type], ppsz_color[i_type],
558 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
559 p_item->i_object_id, p_item->psz_module, psz_object,
560 ppsz_type[i_type], ppsz_color[i_type],
566 if( p_item->psz_header )
568 utf8_fprintf( stderr, "[%.8i] %s %s %s%s: %s\n", p_item->i_object_id,
569 p_item->psz_header, p_item->psz_module,
570 psz_object, ppsz_type[i_type], p_item->psz_msg );
574 utf8_fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
575 p_item->psz_module, psz_object, ppsz_type[i_type],
586 static msg_context_t* GetContext(void)
588 msg_context_t *p_ctx = vlc_threadvar_get( &msg_context_global_key );
591 MALLOC_NULL( p_ctx, msg_context_t );
592 p_ctx->psz_message = NULL;
593 vlc_threadvar_set( &msg_context_global_key, p_ctx );
598 void msg_StackSet( int i_code, const char *psz_message, ... )
601 msg_context_t *p_ctx = GetContext();
604 va_start( ap, psz_message );
605 free( p_ctx->psz_message );
607 if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
608 p_ctx->psz_message = NULL;
611 p_ctx->i_code = i_code;
614 void msg_StackAdd( const char *psz_message, ... )
618 msg_context_t *p_ctx = GetContext();
621 va_start( ap, psz_message );
622 if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
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;