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 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() */
40 # include <fcntl.h> /* O_CREAT, O_TRUNC, O_WRONLY, O_SYNC */
43 #include <errno.h> /* errno */
46 # include <unistd.h> /* close(), write() */
49 #include "vlc_interface.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.pp_queues[i]
65 /*****************************************************************************
67 *****************************************************************************/
68 static void QueueMsg ( vlc_object_t *, int, int , const char *,
69 const char *, va_list );
70 static void FlushMsg ( msg_queue_t * );
71 static void PrintMsg ( vlc_object_t *, msg_item_t * );
72 static void CreateMsgQueue( vlc_object_t *p_this, int i_queue );
75 * Initialize messages queues
76 * This function initializes all message queues
78 void __msg_Create( vlc_object_t *p_this )
80 vlc_mutex_init( p_this, &(p_this->p_libvlc->msg_bank.lock) );
82 CreateMsgQueue( p_this, MSG_QUEUE_NORMAL );
83 CreateMsgQueue( p_this, MSG_QUEUE_HTTPD_ACCESS );
86 QUEUE(MSG_QUEUE_NORMAL)->logfile =
87 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
88 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
89 CREATE_ALWAYS, 0, NULL );
90 SetFilePointer( QUEUE(MSG_QUEUE_NORMAL)->logfile, 0, NULL, FILE_END );
95 static void CreateMsgQueue( vlc_object_t *p_this, int i_queue )
97 msg_queue_t *p_queue = (msg_queue_t *)malloc( sizeof( msg_queue_t ) );
99 vlc_mutex_init( p_this, &p_queue->lock );
101 p_queue->b_overflow = VLC_FALSE;
102 p_queue->i_id = i_queue;
103 p_queue->i_start = 0;
107 p_queue->pp_sub = NULL;
109 INSERT_ELEM( p_this->p_libvlc->msg_bank.pp_queues,
110 p_this->p_libvlc->msg_bank.i_queues,
116 * Flush all message queues
118 void __msg_Flush( vlc_object_t *p_this )
122 for( i = 0 ; i < p_this->p_libvlc->msg_bank.i_queues; i++ )
124 vlc_mutex_lock( &QUEUE(i)->lock );
125 FlushMsg( QUEUE(i) );
126 vlc_mutex_unlock( &QUEUE(i)->lock );
131 * Free resources allocated by msg_Create
133 * This functions prints all messages remaining in the normal queue,
134 * then frees all the allocated ressources
135 * No other messages interface functions should be called after this one.
137 void __msg_Destroy( vlc_object_t *p_this )
140 for( i = p_this->p_libvlc->msg_bank.i_queues -1 ; i >= 0; i-- )
142 msg_queue_t *p_queue = QUEUE(i);
145 msg_Err( p_this, "stale interface subscribers" );
150 if( i == MSG_QUEUE_NORMAL )
151 CloseHandle( QUEUE(MSG_QUEUE_NORMAL)->logfile );
154 vlc_mutex_destroy( &p_queue->lock );
155 REMOVE_ELEM( p_this->p_libvlc->msg_bank.pp_queues,
156 p_this->p_libvlc->msg_bank.i_queues, i );
159 vlc_mutex_destroy( &(p_this->p_libvlc->msg_bank.lock) );
163 * Subscribe to the message queue.
165 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this, int i_queue )
167 msg_bank_t *p_bank = &p_this->p_libvlc->msg_bank;
168 msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
169 msg_queue_t *p_queue = NULL;
172 vlc_mutex_lock( &p_bank->lock );
174 for( i = 0 ; i <p_bank->i_queues ;i++ )
176 if( QUEUE(i)->i_id == i_queue )
178 p_queue = p_bank->pp_queues[i];
182 if( p_queue == NULL )
184 vlc_mutex_unlock( &p_bank->lock );
188 vlc_mutex_lock( &p_queue->lock );
190 /* Add subscription to the list */
191 INSERT_ELEM( p_bank->pp_queues[i_queue]->pp_sub,
192 p_bank->pp_queues[i_queue]->i_sub,
193 p_bank->pp_queues[i_queue]->i_sub,
196 p_sub->i_start = p_queue->i_start;
197 p_sub->pi_stop = &p_queue->i_stop;
199 p_sub->p_msg = p_queue->msg;
200 p_sub->p_lock = &p_queue->lock;
202 vlc_mutex_unlock( &p_queue->lock );
203 vlc_mutex_unlock( &p_bank->lock );
209 * Unsubscribe from the message queue.
211 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
213 msg_bank_t *p_bank = &p_this->p_libvlc->msg_bank;
216 vlc_mutex_lock( &p_bank->lock );
218 for( i = 0 ; i< p_bank->i_queues ; i++ )
220 vlc_mutex_lock( & p_bank->pp_queues[i]->lock );
221 for( j = 0 ; j< p_bank->pp_queues[i]->i_sub ; j++ )
223 if( p_bank->pp_queues[i]->pp_sub[j] == p_sub )
225 REMOVE_ELEM( p_bank->pp_queues[i]->pp_sub,
226 p_bank->pp_queues[i]->i_sub,
228 if( p_sub ) free( p_sub );
231 vlc_mutex_unlock( & p_bank->pp_queues[i]->lock );
234 vlc_mutex_unlock( &p_bank->lock );
237 /*****************************************************************************
238 * __msg_*: print a message
239 *****************************************************************************
240 * These functions queue a message for later printing.
241 *****************************************************************************/
242 void __msg_Generic( vlc_object_t *p_this, int i_queue_id, int i_type,
243 const char *psz_module,
244 const char *psz_format, ... )
248 va_start( args, psz_format );
249 QueueMsg( p_this, i_queue_id, i_type, psz_module, psz_format, args );
253 void __msg_GenericVa( vlc_object_t *p_this, int i_queue_id,
254 int i_type, const char *psz_module,
255 const char *psz_format, va_list args )
257 QueueMsg( p_this, i_queue_id, i_type, psz_module, psz_format, args );
260 /* Generic functions used when variadic macros are not available. */
261 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
262 void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
265 va_start( args, psz_format ); \
266 QueueMsg( (vlc_object_t *)p_this,MSG_QUEUE_NORMAL, FN_TYPE, "unknown", \
267 psz_format, args ); \
272 * Output an informational message.
273 * \note Do not use this for debug messages
276 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
278 * Output an error message.
280 DECLARE_MSG_FN( __msg_Err, VLC_MSG_ERR );
282 * Output a waring message
284 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
286 * Output a debug message
288 DECLARE_MSG_FN( __msg_Dbg, VLC_MSG_DBG );
291 * Add a message to a queue
293 * This function provides basic functionnalities to other msg_* functions.
294 * It adds a message to a queue (after having printed all stored messages if it
295 * is full). If the message can't be converted to string in memory, it issues
298 static void QueueMsg( vlc_object_t *p_this, int i_queue_id, int i_type,
299 const char *psz_module,
300 const char *psz_format, va_list _args )
302 int i_header_size; /* Size of the additionnal header */
304 msg_bank_t *p_bank; /* message bank */
305 msg_queue_t *p_queue = NULL;
306 char * psz_str = NULL; /* formatted message string */
307 char * psz_header = NULL;
309 msg_item_t * p_item = NULL; /* pointer to message */
310 msg_item_t item; /* message in case of a full queue */
312 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
313 int i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
317 if( p_this == NULL || p_this->i_flags & OBJECT_FLAGS_QUIET ||
318 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
323 p_bank = &p_this->p_libvlc->msg_bank;
326 * Convert message to string
328 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
329 vlc_va_copy( args, _args );
330 vasprintf( &psz_str, psz_format, args );
333 psz_str = (char*) malloc( i_size * sizeof(char) );
336 if( psz_str == NULL )
338 fprintf( stderr, "main warning: can't store message (%s): ",
340 vlc_va_copy( args, _args );
341 /* We should use utf8_vfprintf - but it calls malloc()... */
342 vfprintf( stderr, psz_format, args );
344 fputs( "\n", stderr );
350 while( p_obj != NULL )
352 char *psz_old = NULL;
353 if( p_obj == NULL ) break;
354 if( p_obj->psz_header )
356 i_header_size += strlen( p_obj->psz_header ) + 4;
359 psz_old = strdup( psz_header );
360 psz_header = (char*)realloc( psz_header, i_header_size );
361 snprintf( psz_header, i_header_size , "[%s] %s",
362 p_obj->psz_header, psz_old );
366 psz_header = (char *)malloc( i_header_size );
367 snprintf( psz_header, i_header_size, "[%s]",
371 if( psz_old ) free( psz_old );
372 p_obj = p_obj->p_parent;
375 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
376 vlc_va_copy( args, _args );
377 vsnprintf( psz_str, i_size, psz_format, args );
379 psz_str[ i_size - 1 ] = 0; /* Just in case */
382 /* Put message in queue */
383 vlc_mutex_lock( &p_bank->lock );
384 for( i = 0 ; i <p_bank->i_queues ;i++ )
386 if( p_bank->pp_queues[i]->i_id == i_queue_id )
388 p_queue = p_bank->pp_queues[i];
392 if( p_queue == NULL )
394 vlc_mutex_unlock( &p_bank->lock );
395 if( psz_str ) free( psz_str );
396 if( psz_header ) free( psz_header );
400 vlc_mutex_lock( &p_queue->lock );
402 /* Check there is room in the queue for our message */
403 if( p_queue->b_overflow )
407 if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
409 /* Still in overflow mode, print from a dummy item */
414 /* Pheeew, at last, there is room in the queue! */
415 p_queue->b_overflow = VLC_FALSE;
418 else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
422 if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
424 p_queue->b_overflow = VLC_TRUE;
426 if( p_queue->i_id == MSG_QUEUE_NORMAL )
428 /* Put the overflow message in the queue */
429 p_item = p_queue->msg + p_queue->i_stop;
430 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
432 p_item->i_type = VLC_MSG_WARN;
433 p_item->i_object_id = p_this->i_object_id;
434 p_item->i_object_type = p_this->i_object_type;
435 p_item->psz_module = strdup( "message" );
436 p_item->psz_msg = strdup( "message queue overflowed" );
437 p_item->psz_header = NULL;
439 PrintMsg( p_this, p_item );
440 /* We print from a dummy item */
446 if( !p_queue->b_overflow )
448 /* Put the message in the queue */
449 p_item = p_queue->msg + p_queue->i_stop;
450 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
453 /* Fill message information fields */
454 p_item->i_type = i_type;
455 p_item->i_object_id = p_this->i_object_id;
456 p_item->i_object_type = p_this->i_object_type;
457 p_item->psz_module = strdup( psz_module );
458 p_item->psz_msg = psz_str;
459 p_item->psz_header = psz_header;
461 if( p_queue->i_id == MSG_QUEUE_NORMAL )
462 PrintMsg( p_this, p_item );
464 if( p_queue->b_overflow )
466 if( p_item->psz_module )
467 free( p_item->psz_module );
468 if( p_item->psz_msg )
469 free( p_item->psz_msg );
470 if( p_item->psz_header )
471 free( p_item->psz_header );
474 vlc_mutex_unlock ( &p_queue->lock );
475 vlc_mutex_unlock( &p_bank->lock );
478 /* following functions are local */
480 /*****************************************************************************
482 *****************************************************************************
483 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
484 * this function does not check the lock.
485 *****************************************************************************/
486 static void FlushMsg ( msg_queue_t *p_queue )
488 int i_index, i_start, i_stop;
490 /* Get the maximum message index that can be freed */
491 i_stop = p_queue->i_stop;
493 /* Check until which value we can free messages */
494 for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
496 i_start = p_queue->pp_sub[ i_index ]->i_start;
498 /* If this subscriber is late, we don't free messages before
499 * his i_start value, otherwise he'll miss messages */
500 if( ( i_start < i_stop
501 && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
502 || ( i_stop < i_start
503 && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
509 /* Free message data */
510 for( i_index = p_queue->i_start;
512 i_index = (i_index+1) % VLC_MSG_QSIZE )
514 if( p_queue->msg[i_index].psz_msg )
515 free( p_queue->msg[i_index].psz_msg );
516 if( p_queue->msg[i_index].psz_module )
517 free( p_queue->msg[i_index].psz_module );
518 if( p_queue->msg[i_index].psz_header )
519 free( p_queue->msg[i_index].psz_header );
522 /* Update the new start value */
523 p_queue->i_start = i_index;
526 /*****************************************************************************
527 * PrintMsg: output a standard message item to stderr
528 *****************************************************************************
529 * Print a message to stderr, with colour formatting if needed.
530 *****************************************************************************/
531 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
533 # define COL(x) "\033[" #x ";1m"
535 # define GREEN COL(32)
536 # define YELLOW COL(33)
537 # define WHITE COL(37)
538 # define GRAY "\033[0m"
543 static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
544 static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
545 char *psz_object = "private";
546 int i_type = p_item->i_type;
551 if( p_this->p_libvlc->i_verbose < 0 ) return;
554 if( p_this->p_libvlc->i_verbose < 0 ) return;
557 if( p_this->p_libvlc->i_verbose < 1 ) return;
560 if( p_this->p_libvlc->i_verbose < 2 ) return;
564 switch( p_item->i_object_type )
566 case VLC_OBJECT_ROOT: psz_object = "root"; break;
567 case VLC_OBJECT_LIBVLC: psz_object = "libvlc"; break;
568 case VLC_OBJECT_MODULE: psz_object = "module"; break;
569 case VLC_OBJECT_INTF: psz_object = "interface"; break;
570 case VLC_OBJECT_PLAYLIST: psz_object = "playlist"; break;
571 case VLC_OBJECT_ITEM: psz_object = "item"; break;
572 case VLC_OBJECT_INPUT: psz_object = "input"; break;
573 case VLC_OBJECT_DECODER: psz_object = "decoder"; break;
574 case VLC_OBJECT_PACKETIZER: psz_object = "packetizer"; break;
575 case VLC_OBJECT_ENCODER: psz_object = "encoder"; break;
576 case VLC_OBJECT_VOUT: psz_object = "video output"; break;
577 case VLC_OBJECT_AOUT: psz_object = "audio output"; break;
578 case VLC_OBJECT_SOUT: psz_object = "stream output"; break;
579 case VLC_OBJECT_HTTPD: psz_object = "http server"; break;
580 case VLC_OBJECT_HTTPD_HOST: psz_object = "http server"; break;
581 case VLC_OBJECT_DIALOGS: psz_object = "dialogs provider"; break;
582 case VLC_OBJECT_VLM: psz_object = "vlm"; break;
583 case VLC_OBJECT_ANNOUNCE: psz_object = "announce handler"; break;
584 case VLC_OBJECT_DEMUX: psz_object = "demuxer"; break;
585 case VLC_OBJECT_ACCESS: psz_object = "access"; break;
589 # define CE_WRITE(str) WriteFile( QUEUE(MSG_QUEUE_NORMAL)->logfile, \
590 str, strlen(str), &i_dummy, NULL );
591 CE_WRITE( p_item->psz_module );
593 CE_WRITE( psz_object );
594 CE_WRITE( ppsz_type[i_type] );
596 CE_WRITE( p_item->psz_msg );
598 FlushFileBuffers( QUEUE(MSG_QUEUE_NORMAL)->logfile );
601 /* Send the message to stderr */
602 if( p_this->p_libvlc->b_color )
604 if( p_item->psz_header )
606 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s %s%s: %s%s" GRAY
608 p_item->i_object_id, p_item->psz_header,
609 p_item->psz_module, psz_object,
610 ppsz_type[i_type], ppsz_color[i_type],
615 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
616 p_item->i_object_id, p_item->psz_module, psz_object,
617 ppsz_type[i_type], ppsz_color[i_type],
623 if( p_item->psz_header )
625 utf8_fprintf( stderr, "[%.8i] %s %s %s%s: %s\n", p_item->i_object_id,
626 p_item->psz_header, p_item->psz_module,
627 psz_object, ppsz_type[i_type], p_item->psz_msg );
631 utf8_fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
632 p_item->psz_module, psz_object, ppsz_type[i_type],