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 *****************************************************************************/
30 #include <stdio.h> /* required */
31 #include <stdarg.h> /* va_list for BSD */
32 #include <stdlib.h> /* malloc() */
33 #include <string.h> /* strerror() */
38 # include <fcntl.h> /* O_CREAT, O_TRUNC, O_WRONLY, O_SYNC */
41 #include <errno.h> /* errno */
44 # include <unistd.h> /* close(), write() */
47 #include "vlc_interface.h"
50 /*****************************************************************************
52 *****************************************************************************/
53 #if defined(HAVE_VA_COPY)
54 # define vlc_va_copy(dest,src) va_copy(dest,src)
55 #elif defined(HAVE___VA_COPY)
56 # define vlc_va_copy(dest,src) __va_copy(dest,src)
58 # define vlc_va_copy(dest,src) (dest)=(src)
61 /*****************************************************************************
63 *****************************************************************************/
64 static void QueueMsg ( vlc_object_t *, int, int , const char *,
65 const char *, va_list );
66 static void FlushMsg ( msg_queue_t * );
67 static void PrintMsg ( vlc_object_t *, msg_item_t * );
68 static void CreateMsgQueue( vlc_object_t *p_this, int i_queue );
71 * Initialize messages queues
72 * This function initializes all message queues
74 void __msg_Create( vlc_object_t *p_this )
76 vlc_mutex_init( p_this, &(p_this->p_libvlc->msg_bank.lock) );
78 CreateMsgQueue( p_this, MSG_QUEUE_NORMAL );
79 CreateMsgQueue( p_this, MSG_QUEUE_HTTPD_ACCESS );
82 p_this->p_libvlc->msg_bank.pp_queues[MSG_QUEUE_NORMAL]->logfile =
83 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
84 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
85 CREATE_ALWAYS, 0, NULL );
86 SetFilePointer( p_this->p_libvlc->msg_bank.pp_queues[MSG_QUEUE_NORMAL]->
87 logfile, 0, NULL, FILE_END );
92 static void CreateMsgQueue( vlc_object_t *p_this, int i_queue )
94 msg_queue_t *p_queue = (msg_queue_t *)malloc( sizeof( msg_queue_t ) );
96 vlc_mutex_init( p_this, &p_queue->lock );
98 p_queue->b_overflow = VLC_FALSE;
99 p_queue->i_id = i_queue;
100 p_queue->i_start = 0;
104 p_queue->pp_sub = NULL;
106 INSERT_ELEM( p_this->p_libvlc->msg_bank.pp_queues,
107 p_this->p_libvlc->msg_bank.i_queues,
113 * Flush all message queues
115 void __msg_Flush( vlc_object_t *p_this )
119 for( i = 0 ; i < p_this->p_libvlc->msg_bank.i_queues; i++ )
121 vlc_mutex_lock( &p_this->p_libvlc->msg_bank.pp_queues[i]->lock );
122 FlushMsg( p_this->p_libvlc->msg_bank.pp_queues[i] );
123 vlc_mutex_unlock( &p_this->p_libvlc->msg_bank.pp_queues[i]->lock );
128 * Free resources allocated by msg_Create
130 * This functions prints all messages remaining in the normal queue,
131 * then frees all the allocated ressources
132 * No other messages interface functions should be called after this one.
134 void __msg_Destroy( vlc_object_t *p_this )
137 for( i = p_this->p_libvlc->msg_bank.i_queues -1 ; i >= 0; i-- )
139 msg_queue_t *p_queue = p_this->p_libvlc->msg_bank.pp_queues[i];
142 msg_Err( p_this, "stale interface subscribers" );
147 if( i == MSG_QUEUE_NORMAL )
148 CloseHandle( p_this->p_libvlc->msg_bank.pp_queues[MSG_QUEUE_NORMAL]->logfile );
151 vlc_mutex_destroy( &p_queue->lock );
152 REMOVE_ELEM( p_this->p_libvlc->msg_bank.pp_queues,
153 p_this->p_libvlc->msg_bank.i_queues, i );
156 vlc_mutex_destroy( &(p_this->p_libvlc->msg_bank.lock) );
160 * Subscribe to the message queue.
162 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this, int i_queue )
164 msg_bank_t *p_bank = &p_this->p_libvlc->msg_bank;
165 msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
166 msg_queue_t *p_queue = NULL;
169 vlc_mutex_lock( &p_bank->lock );
171 for( i = 0 ; i <p_bank->i_queues ;i++ )
173 if( p_bank->pp_queues[i]->i_id == i_queue )
175 p_queue = p_bank->pp_queues[i];
179 if( p_queue == NULL )
181 vlc_mutex_unlock( &p_bank->lock );
185 vlc_mutex_lock( &p_queue->lock );
187 /* Add subscription to the list */
188 INSERT_ELEM( p_bank->pp_queues[i_queue]->pp_sub,
189 p_bank->pp_queues[i_queue]->i_sub,
190 p_bank->pp_queues[i_queue]->i_sub,
193 p_sub->i_start = p_queue->i_start;
194 p_sub->pi_stop = &p_queue->i_stop;
196 p_sub->p_msg = p_queue->msg;
197 p_sub->p_lock = &p_queue->lock;
199 vlc_mutex_unlock( &p_queue->lock );
200 vlc_mutex_unlock( &p_bank->lock );
206 * Unsubscribe from the message queue.
208 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
210 msg_bank_t *p_bank = &p_this->p_libvlc->msg_bank;
213 vlc_mutex_lock( &p_bank->lock );
215 for( i = 0 ; i< p_bank->i_queues ; i++ )
217 vlc_mutex_lock( & p_bank->pp_queues[i]->lock );
218 for( j = 0 ; j< p_bank->pp_queues[i]->i_sub ; j++ )
220 if( p_bank->pp_queues[i]->pp_sub[j] == p_sub )
222 REMOVE_ELEM( p_bank->pp_queues[i]->pp_sub,
223 p_bank->pp_queues[i]->i_sub,
225 if( p_sub ) free( p_sub );
228 vlc_mutex_unlock( & p_bank->pp_queues[i]->lock );
231 vlc_mutex_unlock( &p_bank->lock );
234 /*****************************************************************************
235 * __msg_*: print a message
236 *****************************************************************************
237 * These functions queue a message for later printing.
238 *****************************************************************************/
239 void __msg_Generic( vlc_object_t *p_this, int i_queue_id, int i_type,
240 const char *psz_module,
241 const char *psz_format, ... )
245 va_start( args, psz_format );
246 QueueMsg( p_this, i_queue_id, i_type, psz_module, psz_format, args );
250 void __msg_GenericVa( vlc_object_t *p_this, int i_queue_id,
251 int i_type, const char *psz_module,
252 const char *psz_format, va_list args )
254 QueueMsg( p_this, i_queue_id, i_type, psz_module, psz_format, args );
257 /* Generic functions used when variadic macros are not available. */
258 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
259 void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
262 va_start( args, psz_format ); \
263 QueueMsg( (vlc_object_t *)p_this,MSG_QUEUE_NORMAL, FN_TYPE, "unknown", \
264 psz_format, args ); \
269 * Output an informational message.
270 * \note Do not use this for debug messages
273 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
275 * Output an error message.
277 DECLARE_MSG_FN( __msg_Err, VLC_MSG_ERR );
279 * Output a waring message
281 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
283 * Output a debug message
285 DECLARE_MSG_FN( __msg_Dbg, VLC_MSG_DBG );
288 * Add a message to a queue
290 * This function provides basic functionnalities to other msg_* functions.
291 * It adds a message to a queue (after having printed all stored messages if it
292 * is full). If the message can't be converted to string in memory, it issues
295 static void QueueMsg( vlc_object_t *p_this, int i_queue_id, int i_type,
296 const char *psz_module,
297 const char *psz_format, va_list _args )
299 int i_header_size; /* Size of the additionnal header */
301 msg_bank_t * p_bank = &p_this->p_libvlc->msg_bank; /* message bank */
302 msg_queue_t *p_queue = NULL;
303 char * psz_str = NULL; /* formatted message string */
304 char * psz_header = NULL;
306 msg_item_t * p_item = NULL; /* pointer to message */
307 msg_item_t item; /* message in case of a full queue */
309 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
310 int i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
314 if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
315 (p_this->i_flags & OBJECT_FLAGS_NODBG &&
316 i_type == VLC_MSG_DBG ) )
322 * Convert message to string
324 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
325 vlc_va_copy( args, _args );
326 vasprintf( &psz_str, psz_format, args );
329 psz_str = (char*) malloc( i_size * sizeof(char) );
332 if( psz_str == NULL )
334 fprintf( stderr, "main warning: can't store message (%s): ",
336 vlc_va_copy( args, _args );
337 /* We should use utf8_vfprintf - but it calls malloc()... */
338 vfprintf( stderr, psz_format, args );
340 fputs( "\n", stderr );
346 while( p_obj != NULL )
348 char *psz_old = NULL;
349 if( p_obj == NULL ) break;
350 if( p_obj->psz_header )
352 i_header_size += strlen( p_obj->psz_header ) + 4;
355 psz_old = strdup( psz_header );
356 psz_header = (char*)realloc( psz_header, i_header_size );
357 snprintf( psz_header, i_header_size , "[%s] %s",
358 p_obj->psz_header, psz_old );
362 psz_header = (char *)malloc( i_header_size );
363 snprintf( psz_header, i_header_size, "[%s]",
367 if( psz_old ) free( psz_old );
368 p_obj = p_obj->p_parent;
371 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
372 vlc_va_copy( args, _args );
373 vsnprintf( psz_str, i_size, psz_format, args );
375 psz_str[ i_size - 1 ] = 0; /* Just in case */
378 /* Put message in queue */
379 vlc_mutex_lock( &p_bank->lock );
380 for( i = 0 ; i <p_bank->i_queues ;i++ )
382 if( p_bank->pp_queues[i]->i_id == i_queue_id )
384 p_queue = p_bank->pp_queues[i];
388 if( p_queue == NULL )
390 vlc_mutex_unlock( &p_bank->lock );
391 if( psz_str ) free( psz_str );
392 if( psz_header ) free( psz_header );
396 vlc_mutex_lock( &p_queue->lock );
398 /* Check there is room in the queue for our message */
399 if( p_queue->b_overflow )
403 if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
405 /* Still in overflow mode, print from a dummy item */
410 /* Pheeew, at last, there is room in the queue! */
411 p_queue->b_overflow = VLC_FALSE;
414 else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
418 if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
420 p_queue->b_overflow = VLC_TRUE;
422 if( p_queue->i_id == MSG_QUEUE_NORMAL )
424 /* Put the overflow message in the queue */
425 p_item = p_queue->msg + p_queue->i_stop;
426 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
428 p_item->i_type = VLC_MSG_WARN;
429 p_item->i_object_id = p_this->i_object_id;
430 p_item->i_object_type = p_this->i_object_type;
431 p_item->psz_module = strdup( "message" );
432 p_item->psz_msg = strdup( "message queue overflowed" );
433 p_item->psz_header = NULL;
435 PrintMsg( p_this, p_item );
436 /* We print from a dummy item */
442 if( !p_queue->b_overflow )
444 /* Put the message in the queue */
445 p_item = p_queue->msg + p_queue->i_stop;
446 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
449 /* Fill message information fields */
450 p_item->i_type = i_type;
451 p_item->i_object_id = p_this->i_object_id;
452 p_item->i_object_type = p_this->i_object_type;
453 p_item->psz_module = strdup( psz_module );
454 p_item->psz_msg = psz_str;
455 p_item->psz_header = psz_header;
457 if( p_queue->i_id == MSG_QUEUE_NORMAL )
458 PrintMsg( p_this, p_item );
460 if( p_queue->b_overflow )
462 if( p_item->psz_module )
463 free( p_item->psz_module );
464 if( p_item->psz_msg )
465 free( p_item->psz_msg );
466 if( p_item->psz_header )
467 free( p_item->psz_header );
470 vlc_mutex_unlock ( &p_queue->lock );
471 vlc_mutex_unlock( &p_bank->lock );
474 /* following functions are local */
476 /*****************************************************************************
478 *****************************************************************************
479 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
480 * this function does not check the lock.
481 *****************************************************************************/
482 static void FlushMsg ( msg_queue_t *p_queue )
484 int i_index, i_start, i_stop;
486 /* Get the maximum message index that can be freed */
487 i_stop = p_queue->i_stop;
489 /* Check until which value we can free messages */
490 for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
492 i_start = p_queue->pp_sub[ i_index ]->i_start;
494 /* If this subscriber is late, we don't free messages before
495 * his i_start value, otherwise he'll miss messages */
496 if( ( i_start < i_stop
497 && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
498 || ( i_stop < i_start
499 && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
505 /* Free message data */
506 for( i_index = p_queue->i_start;
508 i_index = (i_index+1) % VLC_MSG_QSIZE )
510 if( p_queue->msg[i_index].psz_msg )
511 free( p_queue->msg[i_index].psz_msg );
512 if( p_queue->msg[i_index].psz_module )
513 free( p_queue->msg[i_index].psz_module );
514 if( p_queue->msg[i_index].psz_header )
515 free( p_queue->msg[i_index].psz_header );
518 /* Update the new start value */
519 p_queue->i_start = i_index;
522 /*****************************************************************************
523 * PrintMsg: output a standard message item to stderr
524 *****************************************************************************
525 * Print a message to stderr, with colour formatting if needed.
526 *****************************************************************************/
527 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
529 # define COL(x) "\033[" #x ";1m"
531 # define GREEN COL(32)
532 # define YELLOW COL(33)
533 # define WHITE COL(37)
534 # define GRAY "\033[0m"
539 static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
540 static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
541 char *psz_object = "private";
542 int i_type = p_item->i_type;
547 if( p_this->p_libvlc->i_verbose < 0 ) return;
550 if( p_this->p_libvlc->i_verbose < 0 ) return;
553 if( p_this->p_libvlc->i_verbose < 1 ) return;
556 if( p_this->p_libvlc->i_verbose < 2 ) return;
560 switch( p_item->i_object_type )
562 case VLC_OBJECT_ROOT: psz_object = "root"; break;
563 case VLC_OBJECT_VLC: psz_object = "vlc"; break;
564 case VLC_OBJECT_MODULE: psz_object = "module"; break;
565 case VLC_OBJECT_INTF: psz_object = "interface"; break;
566 case VLC_OBJECT_PLAYLIST: psz_object = "playlist"; break;
567 case VLC_OBJECT_ITEM: psz_object = "item"; break;
568 case VLC_OBJECT_INPUT: psz_object = "input"; break;
569 case VLC_OBJECT_DECODER: psz_object = "decoder"; break;
570 case VLC_OBJECT_PACKETIZER: psz_object = "packetizer"; break;
571 case VLC_OBJECT_ENCODER: psz_object = "encoder"; break;
572 case VLC_OBJECT_VOUT: psz_object = "video output"; break;
573 case VLC_OBJECT_AOUT: psz_object = "audio output"; break;
574 case VLC_OBJECT_SOUT: psz_object = "stream output"; break;
575 case VLC_OBJECT_HTTPD: psz_object = "http daemon"; break;
576 case VLC_OBJECT_DIALOGS: psz_object = "dialogs provider"; break;
577 case VLC_OBJECT_VLM: psz_object = "vlm"; break;
578 case VLC_OBJECT_ANNOUNCE: psz_object = "announce handler"; break;
579 case VLC_OBJECT_DEMUX: psz_object = "demuxer"; break;
580 case VLC_OBJECT_ACCESS: psz_object = "access"; break;
584 # define CE_WRITE(str) WriteFile( p_this->p_libvlc->msg_bank.pp_queues[MSG_QUEUE_NORMAL]->logfile, \
585 str, strlen(str), &i_dummy, NULL );
586 CE_WRITE( p_item->psz_module );
588 CE_WRITE( psz_object );
589 CE_WRITE( ppsz_type[i_type] );
591 CE_WRITE( p_item->psz_msg );
593 FlushFileBuffers( p_this->p_libvlc->msg_bank.pp_queues[MSG_QUEUE_NORMAL]->logfile );
596 /* Send the message to stderr */
597 if( p_this->p_libvlc->b_color )
599 if( p_item->psz_header )
601 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s %s%s: %s%s" GRAY
603 p_item->i_object_id, p_item->psz_header,
604 p_item->psz_module, psz_object,
605 ppsz_type[i_type], ppsz_color[i_type],
610 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
611 p_item->i_object_id, p_item->psz_module, psz_object,
612 ppsz_type[i_type], ppsz_color[i_type],
618 if( p_item->psz_header )
620 utf8_fprintf( stderr, "[%.8i] %s %s %s%s: %s\n", p_item->i_object_id,
621 p_item->psz_header, p_item->psz_module,
622 psz_object, ppsz_type[i_type], p_item->psz_msg );
626 utf8_fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
627 p_item->psz_module, psz_object, ppsz_type[i_type],