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 const char *msg_GetObjectTypeName(int i_object_type )
199 switch( i_object_type )
201 case VLC_OBJECT_GLOBAL: return "global";
202 case VLC_OBJECT_LIBVLC: return "libvlc";
203 case VLC_OBJECT_MODULE: return "module";
204 case VLC_OBJECT_INTF: return "interface";
205 case VLC_OBJECT_PLAYLIST: return "playlist";
206 case VLC_OBJECT_ITEM: return "item";
207 case VLC_OBJECT_INPUT: return "input";
208 case VLC_OBJECT_DECODER: return "decoder";
209 case VLC_OBJECT_PACKETIZER: return "packetizer";
210 case VLC_OBJECT_ENCODER: return "encoder";
211 case VLC_OBJECT_VOUT: return "video output";
212 case VLC_OBJECT_AOUT: return "audio output";
213 case VLC_OBJECT_SOUT: return "stream output";
214 case VLC_OBJECT_HTTPD: return "http server";
215 case VLC_OBJECT_HTTPD_HOST: return "http server";
216 case VLC_OBJECT_DIALOGS: return "dialogs provider";
217 case VLC_OBJECT_VLM: return "vlm";
218 case VLC_OBJECT_ANNOUNCE: return "announce handler";
219 case VLC_OBJECT_DEMUX: return "demuxer";
220 case VLC_OBJECT_ACCESS: return "access";
221 case VLC_OBJECT_META_ENGINE: return "meta engine";
222 default: return "private";
226 /*****************************************************************************
227 * __msg_*: print a message
228 *****************************************************************************
229 * These functions queue a message for later printing.
230 *****************************************************************************/
231 void __msg_Generic( vlc_object_t *p_this, int i_queue, int i_type,
232 const char *psz_module,
233 const char *psz_format, ... )
237 va_start( args, psz_format );
238 QueueMsg( p_this, i_queue, i_type, psz_module, psz_format, args );
242 void __msg_GenericVa( vlc_object_t *p_this, int i_queue,
243 int i_type, const char *psz_module,
244 const char *psz_format, va_list args )
246 QueueMsg( p_this, i_queue, i_type, psz_module, psz_format, args );
249 /* Generic functions used when variadic macros are not available. */
250 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
251 void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
254 va_start( args, psz_format ); \
255 QueueMsg( (vlc_object_t *)p_this,MSG_QUEUE_NORMAL, FN_TYPE, "unknown", \
256 psz_format, args ); \
261 * Output an informational message.
262 * \note Do not use this for debug messages
265 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
267 * Output an error message.
269 DECLARE_MSG_FN( __msg_Err, VLC_MSG_ERR );
271 * Output a waring message
273 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
275 * Output a debug message
277 DECLARE_MSG_FN( __msg_Dbg, VLC_MSG_DBG );
280 * Add a message to a queue
282 * This function provides basic functionnalities to other msg_* functions.
283 * It adds a message to a queue (after having printed all stored messages if it
284 * is full). If the message can't be converted to string in memory, it issues
287 static void QueueMsg( vlc_object_t *p_this, int i_queue, int i_type,
288 const char *psz_module,
289 const char *psz_format, va_list _args )
291 int i_header_size; /* Size of the additionnal header */
293 char * psz_str = NULL; /* formatted message string */
294 char * psz_header = NULL;
296 msg_item_t * p_item = NULL; /* pointer to message */
297 msg_item_t item; /* message in case of a full queue */
298 msg_queue_t *p_queue;
300 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
301 int i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
304 if( p_this == NULL || p_this->i_flags & OBJECT_FLAGS_QUIET ||
305 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
310 /* Convert message to string */
311 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
312 vlc_va_copy( args, _args );
313 vasprintf( &psz_str, psz_format, args );
316 psz_str = (char*) malloc( i_size * sizeof(char) );
319 if( psz_str == NULL )
321 fprintf( stderr, "main warning: can't store message (%s): ",
323 vlc_va_copy( args, _args );
324 /* We should use utf8_vfprintf - but it calls malloc()... */
325 vfprintf( stderr, psz_format, args );
327 fputs( "\n", stderr );
333 while( p_obj != NULL )
335 char *psz_old = NULL;
336 if( p_obj->psz_header )
338 i_header_size += strlen( p_obj->psz_header ) + 4;
341 psz_old = strdup( psz_header );
342 psz_header = (char*)realloc( psz_header, i_header_size );
343 snprintf( psz_header, i_header_size , "[%s] %s",
344 p_obj->psz_header, psz_old );
348 psz_header = (char *)malloc( i_header_size );
349 snprintf( psz_header, i_header_size, "[%s]",
353 if( psz_old ) free( psz_old );
354 p_obj = p_obj->p_parent;
357 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
358 vlc_va_copy( args, _args );
359 vsnprintf( psz_str, i_size, psz_format, args );
361 psz_str[ i_size - 1 ] = 0; /* Just in case */
364 assert( i_queue < NB_QUEUES );
366 p_queue = &QUEUE(i_queue) ;
367 vlc_mutex_lock( &p_queue->lock );
369 /* Check there is room in the queue for our message */
370 if( p_queue->b_overflow )
374 if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
376 /* Still in overflow mode, print from a dummy item */
381 /* Pheeew, at last, there is room in the queue! */
382 p_queue->b_overflow = VLC_FALSE;
385 else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
389 if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
391 p_queue->b_overflow = VLC_TRUE;
393 if( p_queue->i_id == MSG_QUEUE_NORMAL )
395 /* Put the overflow message in the queue */
396 p_item = p_queue->msg + p_queue->i_stop;
397 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
399 p_item->i_type = VLC_MSG_WARN;
400 p_item->i_object_id = p_this->i_object_id;
401 p_item->i_object_type = p_this->i_object_type;
402 p_item->psz_module = strdup( "message" );
403 p_item->psz_msg = strdup( "message queue overflowed" );
404 p_item->psz_header = NULL;
406 PrintMsg( p_this, p_item );
407 /* We print from a dummy item */
413 if( !p_queue->b_overflow )
415 /* Put the message in the queue */
416 p_item = p_queue->msg + p_queue->i_stop;
417 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
420 /* Fill message information fields */
421 p_item->i_type = i_type;
422 p_item->i_object_id = p_this->i_object_id;
423 p_item->i_object_type = p_this->i_object_type;
424 p_item->psz_module = strdup( psz_module );
425 p_item->psz_msg = psz_str;
426 p_item->psz_header = psz_header;
428 if( p_queue->i_id == MSG_QUEUE_NORMAL )
429 PrintMsg( p_this, p_item );
431 if( p_queue->b_overflow )
433 if( p_item->psz_module )
434 free( p_item->psz_module );
435 if( p_item->psz_msg )
436 free( p_item->psz_msg );
437 if( p_item->psz_header )
438 free( p_item->psz_header );
441 vlc_mutex_unlock ( &p_queue->lock );
445 /* following functions are local */
447 /*****************************************************************************
449 *****************************************************************************
450 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
451 * this function does not check the lock.
452 *****************************************************************************/
453 static void FlushMsg ( msg_queue_t *p_queue )
455 int i_index, i_start, i_stop;
457 /* Get the maximum message index that can be freed */
458 i_stop = p_queue->i_stop;
460 /* Check until which value we can free messages */
461 for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
463 i_start = p_queue->pp_sub[ i_index ]->i_start;
465 /* If this subscriber is late, we don't free messages before
466 * his i_start value, otherwise he'll miss messages */
467 if( ( i_start < i_stop
468 && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
469 || ( i_stop < i_start
470 && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
476 /* Free message data */
477 for( i_index = p_queue->i_start;
479 i_index = (i_index+1) % VLC_MSG_QSIZE )
481 if( p_queue->msg[i_index].psz_msg )
482 free( p_queue->msg[i_index].psz_msg );
483 if( p_queue->msg[i_index].psz_module )
484 free( p_queue->msg[i_index].psz_module );
485 if( p_queue->msg[i_index].psz_header )
486 free( p_queue->msg[i_index].psz_header );
489 /* Update the new start value */
490 p_queue->i_start = i_index;
493 /*****************************************************************************
494 * PrintMsg: output a standard message item to stderr
495 *****************************************************************************
496 * Print a message to stderr, with colour formatting if needed.
497 *****************************************************************************/
498 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
500 # define COL(x) "\033[" #x ";1m"
502 # define GREEN COL(32)
503 # define YELLOW COL(33)
504 # define WHITE COL(37)
505 # define GRAY "\033[0m"
510 static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
511 static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
512 const char *psz_object;
513 int i_type = p_item->i_type;
518 if( p_this->p_libvlc->i_verbose < 0 ) return;
521 if( p_this->p_libvlc->i_verbose < 0 ) return;
524 if( p_this->p_libvlc->i_verbose < 1 ) return;
527 if( p_this->p_libvlc->i_verbose < 2 ) return;
531 psz_object = msg_GetObjectTypeName(p_item->i_object_type);
534 # define CE_WRITE(str) WriteFile( QUEUE(MSG_QUEUE_NORMAL).logfile, \
535 str, strlen(str), &i_dummy, NULL );
536 CE_WRITE( p_item->psz_module );
538 CE_WRITE( psz_object );
539 CE_WRITE( ppsz_type[i_type] );
541 CE_WRITE( p_item->psz_msg );
543 FlushFileBuffers( QUEUE(MSG_QUEUE_NORMAL).logfile );
546 /* Send the message to stderr */
547 if( p_this->p_libvlc->b_color )
549 if( p_item->psz_header )
551 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s %s%s: %s%s" GRAY
553 p_item->i_object_id, p_item->psz_header,
554 p_item->psz_module, psz_object,
555 ppsz_type[i_type], ppsz_color[i_type],
560 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
561 p_item->i_object_id, p_item->psz_module, psz_object,
562 ppsz_type[i_type], ppsz_color[i_type],
568 if( p_item->psz_header )
570 utf8_fprintf( stderr, "[%.8i] %s %s %s%s: %s\n", p_item->i_object_id,
571 p_item->psz_header, p_item->psz_module,
572 psz_object, ppsz_type[i_type], p_item->psz_msg );
576 utf8_fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
577 p_item->psz_module, psz_object, ppsz_type[i_type],