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_ROOT: return "root";
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 == NULL ) break;
337 if( p_obj->psz_header )
339 i_header_size += strlen( p_obj->psz_header ) + 4;
342 psz_old = strdup( psz_header );
343 psz_header = (char*)realloc( psz_header, i_header_size );
344 snprintf( psz_header, i_header_size , "[%s] %s",
345 p_obj->psz_header, psz_old );
349 psz_header = (char *)malloc( i_header_size );
350 snprintf( psz_header, i_header_size, "[%s]",
354 if( psz_old ) free( psz_old );
355 p_obj = p_obj->p_parent;
358 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
359 vlc_va_copy( args, _args );
360 vsnprintf( psz_str, i_size, psz_format, args );
362 psz_str[ i_size - 1 ] = 0; /* Just in case */
365 assert( i_queue < NB_QUEUES );
367 p_queue = &QUEUE(i_queue) ;
368 vlc_mutex_lock( &p_queue->lock );
370 /* Check there is room in the queue for our message */
371 if( p_queue->b_overflow )
375 if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
377 /* Still in overflow mode, print from a dummy item */
382 /* Pheeew, at last, there is room in the queue! */
383 p_queue->b_overflow = VLC_FALSE;
386 else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
390 if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
392 p_queue->b_overflow = VLC_TRUE;
394 if( p_queue->i_id == MSG_QUEUE_NORMAL )
396 /* Put the overflow message in the queue */
397 p_item = p_queue->msg + p_queue->i_stop;
398 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
400 p_item->i_type = VLC_MSG_WARN;
401 p_item->i_object_id = p_this->i_object_id;
402 p_item->i_object_type = p_this->i_object_type;
403 p_item->psz_module = strdup( "message" );
404 p_item->psz_msg = strdup( "message queue overflowed" );
405 p_item->psz_header = NULL;
407 PrintMsg( p_this, p_item );
408 /* We print from a dummy item */
414 if( !p_queue->b_overflow )
416 /* Put the message in the queue */
417 p_item = p_queue->msg + p_queue->i_stop;
418 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
421 /* Fill message information fields */
422 p_item->i_type = i_type;
423 p_item->i_object_id = p_this->i_object_id;
424 p_item->i_object_type = p_this->i_object_type;
425 p_item->psz_module = strdup( psz_module );
426 p_item->psz_msg = psz_str;
427 p_item->psz_header = psz_header;
429 if( p_queue->i_id == MSG_QUEUE_NORMAL )
430 PrintMsg( p_this, p_item );
432 if( p_queue->b_overflow )
434 if( p_item->psz_module )
435 free( p_item->psz_module );
436 if( p_item->psz_msg )
437 free( p_item->psz_msg );
438 if( p_item->psz_header )
439 free( p_item->psz_header );
442 vlc_mutex_unlock ( &p_queue->lock );
446 /* following functions are local */
448 /*****************************************************************************
450 *****************************************************************************
451 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
452 * this function does not check the lock.
453 *****************************************************************************/
454 static void FlushMsg ( msg_queue_t *p_queue )
456 int i_index, i_start, i_stop;
458 /* Get the maximum message index that can be freed */
459 i_stop = p_queue->i_stop;
461 /* Check until which value we can free messages */
462 for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
464 i_start = p_queue->pp_sub[ i_index ]->i_start;
466 /* If this subscriber is late, we don't free messages before
467 * his i_start value, otherwise he'll miss messages */
468 if( ( i_start < i_stop
469 && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
470 || ( i_stop < i_start
471 && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
477 /* Free message data */
478 for( i_index = p_queue->i_start;
480 i_index = (i_index+1) % VLC_MSG_QSIZE )
482 if( p_queue->msg[i_index].psz_msg )
483 free( p_queue->msg[i_index].psz_msg );
484 if( p_queue->msg[i_index].psz_module )
485 free( p_queue->msg[i_index].psz_module );
486 if( p_queue->msg[i_index].psz_header )
487 free( p_queue->msg[i_index].psz_header );
490 /* Update the new start value */
491 p_queue->i_start = i_index;
494 /*****************************************************************************
495 * PrintMsg: output a standard message item to stderr
496 *****************************************************************************
497 * Print a message to stderr, with colour formatting if needed.
498 *****************************************************************************/
499 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
501 # define COL(x) "\033[" #x ";1m"
503 # define GREEN COL(32)
504 # define YELLOW COL(33)
505 # define WHITE COL(37)
506 # define GRAY "\033[0m"
511 static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
512 static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
513 const char *psz_object;
514 int i_type = p_item->i_type;
519 if( p_this->p_libvlc->i_verbose < 0 ) return;
522 if( p_this->p_libvlc->i_verbose < 0 ) return;
525 if( p_this->p_libvlc->i_verbose < 1 ) return;
528 if( p_this->p_libvlc->i_verbose < 2 ) return;
532 psz_object = msg_GetObjectTypeName(p_item->i_object_type);
535 # define CE_WRITE(str) WriteFile( QUEUE(MSG_QUEUE_NORMAL).logfile, \
536 str, strlen(str), &i_dummy, NULL );
537 CE_WRITE( p_item->psz_module );
539 CE_WRITE( psz_object );
540 CE_WRITE( ppsz_type[i_type] );
542 CE_WRITE( p_item->psz_msg );
544 FlushFileBuffers( QUEUE(MSG_QUEUE_NORMAL).logfile );
547 /* Send the message to stderr */
548 if( p_this->p_libvlc->b_color )
550 if( p_item->psz_header )
552 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s %s%s: %s%s" GRAY
554 p_item->i_object_id, p_item->psz_header,
555 p_item->psz_module, psz_object,
556 ppsz_type[i_type], ppsz_color[i_type],
561 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
562 p_item->i_object_id, p_item->psz_module, psz_object,
563 ppsz_type[i_type], ppsz_color[i_type],
569 if( p_item->psz_header )
571 utf8_fprintf( stderr, "[%.8i] %s %s %s%s: %s\n", p_item->i_object_id,
572 p_item->psz_header, p_item->psz_module,
573 psz_object, ppsz_type[i_type], p_item->psz_msg );
577 utf8_fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
578 p_item->psz_module, psz_object, ppsz_type[i_type],