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_interface.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 )
82 vlc_mutex_init( p_this, &(p_this->p_libvlc->msg_bank.lock) );
84 #define QUEUE_INIT(i) \
85 vlc_mutex_init( p_this, &QUEUE(i).lock ); \
86 QUEUE(i).b_overflow = VLC_FALSE; \
88 QUEUE(i).i_start = 0; \
89 QUEUE(i).i_stop = 0; \
99 QUEUE(MSG_QUEUE_NORMAL).logfile =
100 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
101 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
102 CREATE_ALWAYS, 0, NULL );
103 SetFilePointer( QUEUE(MSG_QUEUE_NORMAL).logfile, 0, NULL, FILE_END );
108 * Flush all message queues
110 void __msg_Flush( vlc_object_t *p_this )
113 for( i = 0 ; i < NB_QUEUES ; i++ )
115 vlc_mutex_lock( &QUEUE(i).lock );
116 FlushMsg( &QUEUE(i) );
117 vlc_mutex_unlock( &QUEUE(i).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( vlc_object_t *p_this )
131 for( i = NB_QUEUES -1 ; i >= 0; i-- )
134 msg_Err( p_this, "stale interface subscribers" );
136 FlushMsg( &QUEUE(i) );
139 if( i == MSG_QUEUE_NORMAL )
140 CloseHandle( QUEUE(MSG_QUEUE_NORMAL).logfile );
143 vlc_mutex_destroy( &QUEUE(i).lock );
145 vlc_mutex_destroy( &(p_this->p_libvlc->msg_bank.lock) );
149 * Subscribe to a message queue.
151 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this, int i )
153 msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
155 assert( i < NB_QUEUES );
158 vlc_mutex_lock( &QUEUE(i).lock );
160 TAB_APPEND( QUEUE(i).i_sub, QUEUE(i).pp_sub, p_sub );
162 p_sub->i_start = QUEUE(i).i_start;
163 p_sub->pi_stop = &QUEUE(i).i_stop;
164 p_sub->p_msg = QUEUE(i).msg;
165 p_sub->p_lock = &QUEUE(i).lock;
167 vlc_mutex_unlock( &QUEUE(i).lock );
174 * Unsubscribe from a message queue.
176 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
181 for( i = 0 ; i< NB_QUEUES ; i++ )
183 vlc_mutex_lock( &QUEUE(i).lock );
184 for( j = 0 ; j< QUEUE(i).i_sub ; j++ )
186 if( QUEUE(i).pp_sub[j] == p_sub )
188 REMOVE_ELEM( QUEUE(i).pp_sub, QUEUE(i).i_sub, j );
189 if( p_sub ) free( p_sub );
192 vlc_mutex_unlock( & QUEUE(i).lock );
197 /*****************************************************************************
198 * __msg_*: print a message
199 *****************************************************************************
200 * These functions queue a message for later printing.
201 *****************************************************************************/
202 void __msg_Generic( vlc_object_t *p_this, int i_queue, int i_type,
203 const char *psz_module,
204 const char *psz_format, ... )
208 va_start( args, psz_format );
209 QueueMsg( p_this, i_queue, i_type, psz_module, psz_format, args );
213 void __msg_GenericVa( vlc_object_t *p_this, int i_queue,
214 int i_type, const char *psz_module,
215 const char *psz_format, va_list args )
217 QueueMsg( p_this, i_queue, i_type, psz_module, psz_format, args );
220 /* Generic functions used when variadic macros are not available. */
221 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
222 void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
225 va_start( args, psz_format ); \
226 QueueMsg( (vlc_object_t *)p_this,MSG_QUEUE_NORMAL, FN_TYPE, "unknown", \
227 psz_format, args ); \
232 * Output an informational message.
233 * \note Do not use this for debug messages
236 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
238 * Output an error message.
240 DECLARE_MSG_FN( __msg_Err, VLC_MSG_ERR );
242 * Output a waring message
244 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
246 * Output a debug message
248 DECLARE_MSG_FN( __msg_Dbg, VLC_MSG_DBG );
251 * Add a message to a queue
253 * This function provides basic functionnalities to other msg_* functions.
254 * It adds a message to a queue (after having printed all stored messages if it
255 * is full). If the message can't be converted to string in memory, it issues
258 static void QueueMsg( vlc_object_t *p_this, int i_queue, int i_type,
259 const char *psz_module,
260 const char *psz_format, va_list _args )
262 int i_header_size; /* Size of the additionnal header */
264 char * psz_str = NULL; /* formatted message string */
265 char * psz_header = NULL;
267 msg_item_t * p_item = NULL; /* pointer to message */
268 msg_item_t item; /* message in case of a full queue */
269 msg_queue_t *p_queue;
271 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
272 int i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
275 if( p_this == NULL || p_this->i_flags & OBJECT_FLAGS_QUIET ||
276 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
281 /* Convert message to string */
282 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
283 vlc_va_copy( args, _args );
284 vasprintf( &psz_str, psz_format, args );
287 psz_str = (char*) malloc( i_size * sizeof(char) );
290 if( psz_str == NULL )
292 fprintf( stderr, "main warning: can't store message (%s): ",
294 vlc_va_copy( args, _args );
295 /* We should use utf8_vfprintf - but it calls malloc()... */
296 vfprintf( stderr, psz_format, args );
298 fputs( "\n", stderr );
304 while( p_obj != NULL )
306 char *psz_old = NULL;
307 if( p_obj == NULL ) break;
308 if( p_obj->psz_header )
310 i_header_size += strlen( p_obj->psz_header ) + 4;
313 psz_old = strdup( psz_header );
314 psz_header = (char*)realloc( psz_header, i_header_size );
315 snprintf( psz_header, i_header_size , "[%s] %s",
316 p_obj->psz_header, psz_old );
320 psz_header = (char *)malloc( i_header_size );
321 snprintf( psz_header, i_header_size, "[%s]",
325 if( psz_old ) free( psz_old );
326 p_obj = p_obj->p_parent;
329 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
330 vlc_va_copy( args, _args );
331 vsnprintf( psz_str, i_size, psz_format, args );
333 psz_str[ i_size - 1 ] = 0; /* Just in case */
336 assert( i_queue < NB_QUEUES );
338 p_queue = &QUEUE(i_queue) ;
339 vlc_mutex_lock( &p_queue->lock );
341 /* Check there is room in the queue for our message */
342 if( p_queue->b_overflow )
346 if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
348 /* Still in overflow mode, print from a dummy item */
353 /* Pheeew, at last, there is room in the queue! */
354 p_queue->b_overflow = VLC_FALSE;
357 else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
361 if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
363 p_queue->b_overflow = VLC_TRUE;
365 if( p_queue->i_id == MSG_QUEUE_NORMAL )
367 /* Put the overflow message in the queue */
368 p_item = p_queue->msg + p_queue->i_stop;
369 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
371 p_item->i_type = VLC_MSG_WARN;
372 p_item->i_object_id = p_this->i_object_id;
373 p_item->i_object_type = p_this->i_object_type;
374 p_item->psz_module = strdup( "message" );
375 p_item->psz_msg = strdup( "message queue overflowed" );
376 p_item->psz_header = NULL;
378 PrintMsg( p_this, p_item );
379 /* We print from a dummy item */
385 if( !p_queue->b_overflow )
387 /* Put the message in the queue */
388 p_item = p_queue->msg + p_queue->i_stop;
389 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
392 /* Fill message information fields */
393 p_item->i_type = i_type;
394 p_item->i_object_id = p_this->i_object_id;
395 p_item->i_object_type = p_this->i_object_type;
396 p_item->psz_module = strdup( psz_module );
397 p_item->psz_msg = psz_str;
398 p_item->psz_header = psz_header;
400 if( p_queue->i_id == MSG_QUEUE_NORMAL )
401 PrintMsg( p_this, p_item );
403 if( p_queue->b_overflow )
405 if( p_item->psz_module )
406 free( p_item->psz_module );
407 if( p_item->psz_msg )
408 free( p_item->psz_msg );
409 if( p_item->psz_header )
410 free( p_item->psz_header );
413 vlc_mutex_unlock ( &p_queue->lock );
417 /* following functions are local */
419 /*****************************************************************************
421 *****************************************************************************
422 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
423 * this function does not check the lock.
424 *****************************************************************************/
425 static void FlushMsg ( msg_queue_t *p_queue )
427 int i_index, i_start, i_stop;
429 /* Get the maximum message index that can be freed */
430 i_stop = p_queue->i_stop;
432 /* Check until which value we can free messages */
433 for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
435 i_start = p_queue->pp_sub[ i_index ]->i_start;
437 /* If this subscriber is late, we don't free messages before
438 * his i_start value, otherwise he'll miss messages */
439 if( ( i_start < i_stop
440 && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
441 || ( i_stop < i_start
442 && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
448 /* Free message data */
449 for( i_index = p_queue->i_start;
451 i_index = (i_index+1) % VLC_MSG_QSIZE )
453 if( p_queue->msg[i_index].psz_msg )
454 free( p_queue->msg[i_index].psz_msg );
455 if( p_queue->msg[i_index].psz_module )
456 free( p_queue->msg[i_index].psz_module );
457 if( p_queue->msg[i_index].psz_header )
458 free( p_queue->msg[i_index].psz_header );
461 /* Update the new start value */
462 p_queue->i_start = i_index;
465 /*****************************************************************************
466 * PrintMsg: output a standard message item to stderr
467 *****************************************************************************
468 * Print a message to stderr, with colour formatting if needed.
469 *****************************************************************************/
470 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
472 # define COL(x) "\033[" #x ";1m"
474 # define GREEN COL(32)
475 # define YELLOW COL(33)
476 # define WHITE COL(37)
477 # define GRAY "\033[0m"
482 static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
483 static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
484 const char *psz_object = "private";
485 int i_type = p_item->i_type;
490 if( p_this->p_libvlc->i_verbose < 0 ) return;
493 if( p_this->p_libvlc->i_verbose < 0 ) return;
496 if( p_this->p_libvlc->i_verbose < 1 ) return;
499 if( p_this->p_libvlc->i_verbose < 2 ) return;
503 switch( p_item->i_object_type )
505 case VLC_OBJECT_ROOT: psz_object = "root"; break;
506 case VLC_OBJECT_LIBVLC: psz_object = "libvlc"; break;
507 case VLC_OBJECT_MODULE: psz_object = "module"; break;
508 case VLC_OBJECT_INTF: psz_object = "interface"; break;
509 case VLC_OBJECT_PLAYLIST: psz_object = "playlist"; break;
510 case VLC_OBJECT_ITEM: psz_object = "item"; break;
511 case VLC_OBJECT_INPUT: psz_object = "input"; break;
512 case VLC_OBJECT_DECODER: psz_object = "decoder"; break;
513 case VLC_OBJECT_PACKETIZER: psz_object = "packetizer"; break;
514 case VLC_OBJECT_ENCODER: psz_object = "encoder"; break;
515 case VLC_OBJECT_VOUT: psz_object = "video output"; break;
516 case VLC_OBJECT_AOUT: psz_object = "audio output"; break;
517 case VLC_OBJECT_SOUT: psz_object = "stream output"; break;
518 case VLC_OBJECT_HTTPD: psz_object = "http server"; break;
519 case VLC_OBJECT_HTTPD_HOST: psz_object = "http server"; break;
520 case VLC_OBJECT_DIALOGS: psz_object = "dialogs provider"; break;
521 case VLC_OBJECT_VLM: psz_object = "vlm"; break;
522 case VLC_OBJECT_ANNOUNCE: psz_object = "announce handler"; break;
523 case VLC_OBJECT_DEMUX: psz_object = "demuxer"; break;
524 case VLC_OBJECT_ACCESS: psz_object = "access"; break;
525 case VLC_OBJECT_META_ENGINE: psz_object = "meta engine"; break;
529 # define CE_WRITE(str) WriteFile( QUEUE(MSG_QUEUE_NORMAL).logfile, \
530 str, strlen(str), &i_dummy, NULL );
531 CE_WRITE( p_item->psz_module );
533 CE_WRITE( psz_object );
534 CE_WRITE( ppsz_type[i_type] );
536 CE_WRITE( p_item->psz_msg );
538 FlushFileBuffers( QUEUE(MSG_QUEUE_NORMAL).logfile );
541 /* Send the message to stderr */
542 if( p_this->p_libvlc->b_color )
544 if( p_item->psz_header )
546 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s %s%s: %s%s" GRAY
548 p_item->i_object_id, p_item->psz_header,
549 p_item->psz_module, psz_object,
550 ppsz_type[i_type], ppsz_color[i_type],
555 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
556 p_item->i_object_id, p_item->psz_module, psz_object,
557 ppsz_type[i_type], ppsz_color[i_type],
563 if( p_item->psz_header )
565 utf8_fprintf( stderr, "[%.8i] %s %s %s%s: %s\n", p_item->i_object_id,
566 p_item->psz_header, p_item->psz_module,
567 psz_object, ppsz_type[i_type], p_item->psz_msg );
571 utf8_fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
572 p_item->psz_module, psz_object, ppsz_type[i_type],