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"
49 /*****************************************************************************
51 *****************************************************************************/
52 #if defined(HAVE_VA_COPY)
53 # define vlc_va_copy(dest,src) va_copy(dest,src)
54 #elif defined(HAVE___VA_COPY)
55 # define vlc_va_copy(dest,src) __va_copy(dest,src)
57 # define vlc_va_copy(dest,src) (dest)=(src)
60 /*****************************************************************************
62 *****************************************************************************/
63 static void QueueMsg ( vlc_object_t *, int, int , const char *,
64 const char *, va_list );
65 static void FlushMsg ( msg_queue_t * );
66 static void PrintMsg ( vlc_object_t *, msg_item_t * );
67 static void CreateMsgQueue( vlc_object_t *p_this, int i_queue );
70 * Initialize messages queues
71 * This function initializes all message queues
73 void __msg_Create( vlc_object_t *p_this )
75 vlc_mutex_init( p_this, &(p_this->p_libvlc->msg_bank.lock) );
77 CreateMsgQueue( p_this, MSG_QUEUE_NORMAL );
78 CreateMsgQueue( p_this, MSG_QUEUE_HTTPD_ACCESS );
81 p_this->p_libvlc->msg_bank.pp_queues[MSG_QUEUE_NORMAL]->logfile =
82 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
83 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
84 CREATE_ALWAYS, 0, NULL );
85 SetFilePointer( p_this->p_libvlc->msg_bank.pp_queues[MSG_QUEUE_NORMAL]->
86 logfile, 0, NULL, FILE_END );
91 static void CreateMsgQueue( vlc_object_t *p_this, int i_queue )
93 msg_queue_t *p_queue = (msg_queue_t *)malloc( sizeof( msg_queue_t ) );
95 vlc_mutex_init( p_this, &p_queue->lock );
97 p_queue->b_overflow = VLC_FALSE;
98 p_queue->i_id = i_queue;
103 p_queue->pp_sub = NULL;
105 INSERT_ELEM( p_this->p_libvlc->msg_bank.pp_queues,
106 p_this->p_libvlc->msg_bank.i_queues,
112 * Flush all message queues
114 void __msg_Flush( vlc_object_t *p_this )
118 for( i = 0 ; i < p_this->p_libvlc->msg_bank.i_queues; i++ )
120 vlc_mutex_lock( &p_this->p_libvlc->msg_bank.pp_queues[i]->lock );
121 FlushMsg( p_this->p_libvlc->msg_bank.pp_queues[i] );
122 vlc_mutex_unlock( &p_this->p_libvlc->msg_bank.pp_queues[i]->lock );
127 * Free resources allocated by msg_Create
129 * This functions prints all messages remaining in the normal queue,
130 * then frees all the allocated ressources
131 * No other messages interface functions should be called after this one.
133 void __msg_Destroy( vlc_object_t *p_this )
136 for( i = 0 ; i < p_this->p_libvlc->msg_bank.i_queues; i++ )
138 msg_queue_t *p_queue = p_this->p_libvlc->msg_bank.pp_queues[i];
141 msg_Err( p_this, "stale interface subscribers" );
146 if( i == MSG_QUEUE_NORMAL )
147 CloseHandle( p_this->p_libvlc->msg_bank.pp_queues[MSG_QUEUE_NORMAL]->logfile );
150 vlc_mutex_destroy( &p_queue->lock );
152 vlc_mutex_destroy( &(p_this->p_libvlc->msg_bank.lock) );
156 * Subscribe to the message queue.
158 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this, int i_queue )
160 msg_bank_t *p_bank = &p_this->p_libvlc->msg_bank;
161 msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
162 msg_queue_t *p_queue = NULL;
165 vlc_mutex_lock( &p_bank->lock );
167 for( i = 0 ; i <p_bank->i_queues ;i++ )
169 if( p_bank->pp_queues[i]->i_id == i_queue )
171 p_queue = p_bank->pp_queues[i];
175 if( p_queue == NULL )
177 vlc_mutex_unlock( &p_bank->lock );
181 vlc_mutex_lock( &p_queue->lock );
183 /* Add subscription to the list */
184 INSERT_ELEM( p_bank->pp_queues[i_queue]->pp_sub,
185 p_bank->pp_queues[i_queue]->i_sub,
186 p_bank->pp_queues[i_queue]->i_sub,
189 p_sub->i_start = p_queue->i_start;
190 p_sub->pi_stop = &p_queue->i_stop;
192 p_sub->p_msg = p_queue->msg;
193 p_sub->p_lock = &p_queue->lock;
195 vlc_mutex_unlock( &p_queue->lock );
196 vlc_mutex_unlock( &p_bank->lock );
202 * Unsubscribe from the message queue.
204 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
206 msg_bank_t *p_bank = &p_this->p_libvlc->msg_bank;
209 vlc_mutex_lock( &p_bank->lock );
211 for( i = 0 ; i< p_bank->i_queues ; i++ )
213 vlc_mutex_lock( & p_bank->pp_queues[i]->lock );
214 for( j = 0 ; j< p_bank->pp_queues[i]->i_sub ; j++ )
216 if( p_bank->pp_queues[i]->pp_sub[j] == p_sub )
218 REMOVE_ELEM( p_bank->pp_queues[i]->pp_sub,
219 p_bank->pp_queues[i]->i_sub,
221 if( p_sub ) free( p_sub );
224 vlc_mutex_unlock( & p_bank->pp_queues[i]->lock );
227 vlc_mutex_unlock( &p_bank->lock );
230 /*****************************************************************************
231 * __msg_*: print a message
232 *****************************************************************************
233 * These functions queue a message for later printing.
234 *****************************************************************************/
235 void __msg_Generic( vlc_object_t *p_this, int i_queue_id, int i_type,
236 const char *psz_module,
237 const char *psz_format, ... )
241 va_start( args, psz_format );
242 QueueMsg( p_this, i_queue_id, i_type, psz_module, psz_format, args );
246 void __msg_GenericVa( vlc_object_t *p_this, int i_queue_id,
247 int i_type, const char *psz_module,
248 const char *psz_format, va_list args )
250 QueueMsg( p_this, i_queue_id, i_type, psz_module, psz_format, args );
253 /* Generic functions used when variadic macros are not available. */
254 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
255 void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
258 va_start( args, psz_format ); \
259 QueueMsg( (vlc_object_t *)p_this,MSG_QUEUE_NORMAL, FN_TYPE, "unknown", \
260 psz_format, args ); \
265 * Output an informational message.
266 * \note Do not use this for debug messages
269 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
271 * Output an error message.
273 DECLARE_MSG_FN( __msg_Err, VLC_MSG_ERR );
275 * Output a waring message
277 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
279 * Output a debug message
281 DECLARE_MSG_FN( __msg_Dbg, VLC_MSG_DBG );
284 * Add a message to a queue
286 * This function provides basic functionnalities to other msg_* functions.
287 * It adds a message to a queue (after having printed all stored messages if it
288 * is full). If the message can't be converted to string in memory, it issues
291 static void QueueMsg( vlc_object_t *p_this, int i_queue_id, int i_type,
292 const char *psz_module,
293 const char *psz_format, va_list _args )
295 int i_header_size; /* Size of the additionnal header */
297 msg_bank_t * p_bank = &p_this->p_libvlc->msg_bank; /* message bank */
298 msg_queue_t *p_queue = NULL;
299 char * psz_str = NULL; /* formatted message string */
300 char * psz_header = NULL;
302 msg_item_t * p_item = NULL; /* pointer to message */
303 msg_item_t item; /* message in case of a full queue */
305 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
306 int i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
311 * Convert message to string
313 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined( SYS_BEOS )
314 vlc_va_copy( args, _args );
315 vasprintf( &psz_str, psz_format, args );
318 psz_str = (char*) malloc( i_size * sizeof(char) );
321 if( psz_str == NULL )
323 fprintf( stderr, "main warning: can't store message (%s): ",
325 vlc_va_copy( args, _args );
326 vfprintf( stderr, psz_format, args );
328 fprintf( stderr, "\n" );
334 while( p_obj != NULL )
336 char *psz_old = NULL;
337 if( p_obj == NULL ) break;
338 if( p_obj->psz_header )
340 i_header_size += strlen( p_obj->psz_header ) + 4;
343 psz_old = strdup( psz_header );
344 psz_header = (char*)realloc( psz_header, i_header_size );
345 snprintf( psz_header, i_header_size , "[%s] %s",
346 p_obj->psz_header, psz_old );
350 psz_header = (char *)malloc( i_header_size );
351 snprintf( psz_header, i_header_size, "[%s]",
355 if( psz_old ) free( psz_old );
356 p_obj = p_obj->p_parent;
359 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
360 vlc_va_copy( args, _args );
361 vsnprintf( psz_str, i_size, psz_format, args );
363 psz_str[ i_size - 1 ] = 0; /* Just in case */
366 /* Put message in queue */
367 vlc_mutex_lock( &p_bank->lock );
368 for( i = 0 ; i <p_bank->i_queues ;i++ )
370 if( p_bank->pp_queues[i]->i_id == i_queue_id )
372 p_queue = p_bank->pp_queues[i];
376 if( p_queue == NULL )
378 vlc_mutex_unlock( &p_bank->lock );
382 vlc_mutex_lock( &p_queue->lock );
384 /* Check there is room in the queue for our message */
385 if( p_queue->b_overflow )
389 if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
391 /* Still in overflow mode, print from a dummy item */
396 /* Pheeew, at last, there is room in the queue! */
397 p_queue->b_overflow = VLC_FALSE;
400 else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
404 if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
406 p_queue->b_overflow = VLC_TRUE;
408 if( p_queue->i_id == MSG_QUEUE_NORMAL )
410 /* Put the overflow message in the queue */
411 p_item = p_queue->msg + p_queue->i_stop;
412 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
414 p_item->i_type = VLC_MSG_WARN;
415 p_item->i_object_id = p_this->i_object_id;
416 p_item->i_object_type = p_this->i_object_type;
417 p_item->psz_module = strdup( "message" );
418 p_item->psz_msg = strdup( "message queue overflowed" );
419 p_item->psz_header = NULL;
421 PrintMsg( p_this, p_item );
422 /* We print from a dummy item */
428 if( !p_queue->b_overflow )
430 /* Put the message in the queue */
431 p_item = p_queue->msg + p_queue->i_stop;
432 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
435 /* Fill message information fields */
436 p_item->i_type = i_type;
437 p_item->i_object_id = p_this->i_object_id;
438 p_item->i_object_type = p_this->i_object_type;
439 p_item->psz_module = strdup( psz_module );
440 p_item->psz_msg = psz_str;
441 p_item->psz_header = psz_header;
443 if( p_queue->i_id == MSG_QUEUE_NORMAL )
444 PrintMsg( p_this, p_item );
446 if( p_queue->b_overflow )
448 if( p_item->psz_module )
449 free( p_item->psz_module );
450 if( p_item->psz_msg )
451 free( p_item->psz_msg );
454 vlc_mutex_unlock ( &p_queue->lock );
455 vlc_mutex_unlock( &p_bank->lock );
458 /* following functions are local */
460 /*****************************************************************************
462 *****************************************************************************
463 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
464 * this function does not check the lock.
465 *****************************************************************************/
466 static void FlushMsg ( msg_queue_t *p_queue )
468 int i_index, i_start, i_stop;
470 /* Get the maximum message index that can be freed */
471 i_stop = p_queue->i_stop;
473 /* Check until which value we can free messages */
474 for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
476 i_start = p_queue->pp_sub[ i_index ]->i_start;
478 /* If this subscriber is late, we don't free messages before
479 * his i_start value, otherwise he'll miss messages */
480 if( ( i_start < i_stop
481 && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
482 || ( i_stop < i_start
483 && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
489 /* Free message data */
490 for( i_index = p_queue->i_start;
492 i_index = (i_index+1) % VLC_MSG_QSIZE )
494 if( p_queue->msg[i_index].psz_msg )
495 free( p_queue->msg[i_index].psz_msg );
496 if( p_queue->msg[i_index].psz_module )
497 free( p_queue->msg[i_index].psz_module );
498 if( p_queue->msg[i_index].psz_header )
499 free( p_queue->msg[i_index].psz_header );
502 /* Update the new start value */
503 p_queue->i_start = i_index;
506 /*****************************************************************************
507 * PrintMsg: output a standard message item to stderr
508 *****************************************************************************
509 * Print a message to stderr, with colour formatting if needed.
510 *****************************************************************************/
511 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
513 # define COL(x) "\033[" #x ";1m"
515 # define GREEN COL(32)
516 # define YELLOW COL(33)
517 # define WHITE COL(37)
518 # define GRAY "\033[0m"
523 static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
524 static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
525 char *psz_object = "private";
526 int i_type = p_item->i_type;
531 if( p_this->p_libvlc->i_verbose < 0 ) return;
534 if( p_this->p_libvlc->i_verbose < 0 ) return;
537 if( p_this->p_libvlc->i_verbose < 1 ) return;
540 if( p_this->p_libvlc->i_verbose < 2 ) return;
544 switch( p_item->i_object_type )
546 case VLC_OBJECT_ROOT: psz_object = "root"; break;
547 case VLC_OBJECT_VLC: psz_object = "vlc"; break;
548 case VLC_OBJECT_MODULE: psz_object = "module"; break;
549 case VLC_OBJECT_INTF: psz_object = "interface"; break;
550 case VLC_OBJECT_PLAYLIST: psz_object = "playlist"; break;
551 case VLC_OBJECT_ITEM: psz_object = "item"; break;
552 case VLC_OBJECT_INPUT: psz_object = "input"; break;
553 case VLC_OBJECT_DECODER: psz_object = "decoder"; break;
554 case VLC_OBJECT_PACKETIZER: psz_object = "packetizer"; break;
555 case VLC_OBJECT_ENCODER: psz_object = "encoder"; break;
556 case VLC_OBJECT_VOUT: psz_object = "video output"; break;
557 case VLC_OBJECT_AOUT: psz_object = "audio output"; break;
558 case VLC_OBJECT_SOUT: psz_object = "stream output"; break;
559 case VLC_OBJECT_HTTPD: psz_object = "http daemon"; break;
560 case VLC_OBJECT_DIALOGS: psz_object = "dialogs provider"; break;
561 case VLC_OBJECT_VLM: psz_object = "vlm"; break;
562 case VLC_OBJECT_ANNOUNCE: psz_object = "announce handler"; break;
563 case VLC_OBJECT_DEMUX: psz_object = "demuxer"; break;
564 case VLC_OBJECT_ACCESS: psz_object = "access"; break;
568 # define CE_WRITE(str) WriteFile( p_this->p_libvlc->msg_bank.pp_queues[MSG_QUEUE_NORMAL]->logfile, \
569 str, strlen(str), &i_dummy, NULL );
570 CE_WRITE( p_item->psz_module );
572 CE_WRITE( psz_object );
573 CE_WRITE( ppsz_type[i_type] );
575 CE_WRITE( p_item->psz_msg );
577 FlushFileBuffers( p_this->p_libvlc->msg_bank.pp_queues[MSG_QUEUE_NORMAL]->logfile );
580 /* Send the message to stderr */
581 if( p_this->p_libvlc->b_color )
583 if( p_item->psz_header )
585 fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s %s%s: %s%s" GRAY
587 p_item->i_object_id, p_item->psz_header,
588 p_item->psz_module, psz_object,
589 ppsz_type[i_type], ppsz_color[i_type],
594 fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
595 p_item->i_object_id, p_item->psz_module, psz_object,
596 ppsz_type[i_type], ppsz_color[i_type],
602 if( p_item->psz_header )
604 fprintf( stderr, "[%.8i] %s %s %s%s: %s\n", p_item->i_object_id,
605 p_item->psz_header, p_item->psz_module,
606 psz_object, ppsz_type[i_type], p_item->psz_msg );
610 fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
611 p_item->psz_module, psz_object, ppsz_type[i_type],