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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 , const char *,
64 const char *, va_list );
65 static void FlushMsg ( msg_bank_t * );
66 static void PrintMsg ( vlc_object_t *, msg_item_t * );
69 * Initialize messages interface
71 * This functions has to be called before any call to other msg_* functions.
72 * It set up the locks and the message queue if it is used.
74 void __msg_Create( vlc_object_t *p_this )
76 /* Message queue initialization */
77 vlc_mutex_init( p_this, &p_this->p_libvlc->msg_bank.lock );
79 p_this->p_libvlc->msg_bank.b_configured = VLC_FALSE;
80 p_this->p_libvlc->msg_bank.b_overflow = VLC_FALSE;
82 p_this->p_libvlc->msg_bank.i_start = 0;
83 p_this->p_libvlc->msg_bank.i_stop = 0;
85 p_this->p_libvlc->msg_bank.i_sub = 0;
86 p_this->p_libvlc->msg_bank.pp_sub = NULL;
89 p_this->p_libvlc->msg_bank.logfile =
90 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
91 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
92 CREATE_ALWAYS, 0, NULL );
93 SetFilePointer( p_this->p_libvlc->msg_bank.logfile, 0, NULL, FILE_END );
98 * Flush the message queue
100 void __msg_Flush( vlc_object_t *p_this )
102 vlc_mutex_lock( &p_this->p_libvlc->msg_bank.lock );
104 p_this->p_libvlc->msg_bank.b_configured = VLC_TRUE;
107 /* Some messages remain in the queue, dont rewrite them */
108 for( i_index = p_this->p_libvlc->msg_bank.i_start;
109 i_index != p_this->p_libvlc->msg_bank.i_stop;
110 i_index = (i_index+1) % VLC_MSG_QSIZE )
112 PrintMsg( p_this, &p_this->p_libvlc->msg_bank.msg[i_index] );
116 FlushMsg( &p_this->p_libvlc->msg_bank );
118 vlc_mutex_unlock( &p_this->p_libvlc->msg_bank.lock );
122 * Free resources allocated by msg_Create
124 * This functions prints all messages remaining in queue, then free all the
125 * resources allocated by msg_Create.
126 * No other messages interface functions should be called after this one.
128 void __msg_Destroy( vlc_object_t *p_this )
130 if( p_this->p_libvlc->msg_bank.i_sub )
132 msg_Err( p_this, "stale interface subscribers" );
135 /* Flush the queue */
136 if( !p_this->p_libvlc->msg_bank.b_configured )
142 FlushMsg( &p_this->p_libvlc->msg_bank );
146 CloseHandle( p_this->p_libvlc->msg_bank.logfile );
150 vlc_mutex_destroy( &p_this->p_libvlc->msg_bank.lock );
154 * Subscribe to the message queue.
156 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this )
158 msg_bank_t *p_bank = &p_this->p_libvlc->msg_bank;
159 msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
161 vlc_mutex_lock( &p_bank->lock );
163 /* Add subscription to the list */
164 INSERT_ELEM( p_bank->pp_sub, p_bank->i_sub, p_bank->i_sub, p_sub );
166 p_sub->i_start = p_bank->i_start;
167 p_sub->pi_stop = &p_bank->i_stop;
169 p_sub->p_msg = p_bank->msg;
170 p_sub->p_lock = &p_bank->lock;
172 vlc_mutex_unlock( &p_bank->lock );
178 * Unsubscribe from the message queue.
180 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
182 msg_bank_t *p_bank = &p_this->p_libvlc->msg_bank;
185 vlc_mutex_lock( &p_bank->lock );
190 msg_Err( p_this, "no subscriber in the list" );
194 /* Look for the appropriate subscription */
195 for( i_index = 0; i_index < p_bank->i_sub; i_index++ )
197 if( p_bank->pp_sub[ i_index ] == p_sub )
203 if( p_bank->pp_sub[ i_index ] != p_sub )
205 msg_Err( p_this, "subscriber not found" );
206 vlc_mutex_unlock( &p_bank->lock );
210 /* Remove this subscription */
211 REMOVE_ELEM( p_bank->pp_sub, p_bank->i_sub, i_index );
213 if( p_sub ) free( p_sub );
214 vlc_mutex_unlock( &p_bank->lock );
217 /*****************************************************************************
218 * __msg_*: print a message
219 *****************************************************************************
220 * These functions queue a message for later printing.
221 *****************************************************************************/
222 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
223 const char *psz_format, ... )
227 va_start( args, psz_format );
228 QueueMsg( p_this, i_type, psz_module, psz_format, args );
232 void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
233 const char *psz_format, va_list args )
235 QueueMsg( p_this, i_type, psz_module, psz_format, args );
238 /* Generic functions used when variadic macros are not available. */
239 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
240 void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
243 va_start( args, psz_format ); \
244 QueueMsg( (vlc_object_t *)p_this, FN_TYPE, "unknown", \
245 psz_format, args ); \
250 * Output an informational message.
251 * \note Do not use this for debug messages
254 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
256 * Output an error message.
258 DECLARE_MSG_FN( __msg_Err, VLC_MSG_ERR );
260 * Output a waring message
262 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
264 * Output a debug message
266 DECLARE_MSG_FN( __msg_Dbg, VLC_MSG_DBG );
269 * Add a message to a queue
271 * This function provides basic functionnalities to other msg_* functions.
272 * It adds a message to a queue (after having printed all stored messages if it
273 * is full). If the message can't be converted to string in memory, it issues
276 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
277 const char *psz_format, va_list _args )
279 msg_bank_t * p_bank = &p_this->p_libvlc->msg_bank; /* message bank */
280 char * psz_str = NULL; /* formatted message string */
282 msg_item_t * p_item = NULL; /* pointer to message */
283 msg_item_t item; /* message in case of a full queue */
285 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
286 int i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
290 * Convert message to string
292 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined( SYS_BEOS )
293 vlc_va_copy( args, _args );
294 vasprintf( &psz_str, psz_format, args );
297 psz_str = (char*) malloc( i_size * sizeof(char) );
300 if( psz_str == NULL )
302 fprintf( stderr, "main warning: can't store message (%s): ",
304 vlc_va_copy( args, _args );
305 vfprintf( stderr, psz_format, args );
307 fprintf( stderr, "\n" );
311 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
312 vlc_va_copy( args, _args );
313 vsnprintf( psz_str, i_size, psz_format, args );
315 psz_str[ i_size - 1 ] = 0; /* Just in case */
318 /* Put message in queue */
319 vlc_mutex_lock( &p_bank->lock );
321 /* Check there is room in the queue for our message */
322 if( p_bank->b_overflow )
326 if( ((p_bank->i_stop - p_bank->i_start + 1) % VLC_MSG_QSIZE) == 0 )
328 /* Still in overflow mode, print from a dummy item */
333 /* Pheeew, at last, there is room in the queue! */
334 p_bank->b_overflow = VLC_FALSE;
337 else if( ((p_bank->i_stop - p_bank->i_start + 2) % VLC_MSG_QSIZE) == 0 )
341 if( ((p_bank->i_stop - p_bank->i_start + 2) % VLC_MSG_QSIZE) == 0 )
343 p_bank->b_overflow = VLC_TRUE;
345 /* Put the overflow message in the queue */
346 p_item = p_bank->msg + p_bank->i_stop;
347 p_bank->i_stop = (p_bank->i_stop + 1) % VLC_MSG_QSIZE;
349 p_item->i_type = VLC_MSG_WARN;
350 p_item->i_object_id = p_this->i_object_id;
351 p_item->i_object_type = p_this->i_object_type;
352 p_item->psz_module = strdup( "message" );
353 p_item->psz_msg = strdup( "message queue overflowed" );
355 PrintMsg( p_this, p_item );
357 /* We print from a dummy item */
362 if( !p_bank->b_overflow )
364 /* Put the message in the queue */
365 p_item = p_bank->msg + p_bank->i_stop;
366 p_bank->i_stop = (p_bank->i_stop + 1) % VLC_MSG_QSIZE;
369 /* Fill message information fields */
370 p_item->i_type = i_type;
371 p_item->i_object_id = p_this->i_object_id;
372 p_item->i_object_type = p_this->i_object_type;
373 p_item->psz_module = strdup( psz_module );
374 p_item->psz_msg = psz_str;
376 PrintMsg( p_this, p_item );
378 if( p_bank->b_overflow )
380 if( p_item->psz_module )
381 free( p_item->psz_module );
382 if( p_item->psz_msg )
383 free( p_item->psz_msg );
386 vlc_mutex_unlock( &p_bank->lock );
389 /* following functions are local */
391 /*****************************************************************************
393 *****************************************************************************
394 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
395 * this function does not check the lock.
396 *****************************************************************************/
397 static void FlushMsg ( msg_bank_t *p_bank )
399 int i_index, i_start, i_stop;
401 /* Only flush the queue if it has been properly configured */
402 if( !p_bank->b_configured )
407 /* Get the maximum message index that can be freed */
408 i_stop = p_bank->i_stop;
410 /* Check until which value we can free messages */
411 for( i_index = 0; i_index < p_bank->i_sub; i_index++ )
413 i_start = p_bank->pp_sub[ i_index ]->i_start;
415 /* If this subscriber is late, we don't free messages before
416 * his i_start value, otherwise he'll miss messages */
417 if( ( i_start < i_stop
418 && (p_bank->i_stop <= i_start || i_stop <= p_bank->i_stop) )
419 || ( i_stop < i_start
420 && (i_stop <= p_bank->i_stop && p_bank->i_stop <= i_start) ) )
426 /* Free message data */
427 for( i_index = p_bank->i_start;
429 i_index = (i_index+1) % VLC_MSG_QSIZE )
431 if( p_bank->msg[i_index].psz_msg )
432 free( p_bank->msg[i_index].psz_msg );
433 if( p_bank->msg[i_index].psz_module )
434 free( p_bank->msg[i_index].psz_module );
437 /* Update the new start value */
438 p_bank->i_start = i_index;
441 /*****************************************************************************
442 * PrintMsg: output a message item to stderr
443 *****************************************************************************
444 * Print a message to stderr, with colour formatting if needed.
445 *****************************************************************************/
446 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
448 # define COL(x) "\033[" #x ";1m"
450 # define GREEN COL(32)
451 # define YELLOW COL(33)
452 # define WHITE COL(37)
453 # define GRAY "\033[0m"
458 static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
459 static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
460 char *psz_object = "private";
461 int i_type = p_item->i_type;
466 if( p_this->p_libvlc->i_verbose < 0 ) return;
469 if( p_this->p_libvlc->i_verbose < 0 ) return;
472 if( p_this->p_libvlc->i_verbose < 1 ) return;
475 if( p_this->p_libvlc->i_verbose < 2 ) return;
479 switch( p_item->i_object_type )
481 case VLC_OBJECT_ROOT: psz_object = "root"; break;
482 case VLC_OBJECT_VLC: psz_object = "vlc"; break;
483 case VLC_OBJECT_MODULE: psz_object = "module"; break;
484 case VLC_OBJECT_INTF: psz_object = "interface"; break;
485 case VLC_OBJECT_PLAYLIST: psz_object = "playlist"; break;
486 case VLC_OBJECT_ITEM: psz_object = "item"; break;
487 case VLC_OBJECT_INPUT: psz_object = "input"; break;
488 case VLC_OBJECT_DECODER: psz_object = "decoder"; break;
489 case VLC_OBJECT_PACKETIZER: psz_object = "packetizer"; break;
490 case VLC_OBJECT_ENCODER: psz_object = "encoder"; break;
491 case VLC_OBJECT_VOUT: psz_object = "video output"; break;
492 case VLC_OBJECT_AOUT: psz_object = "audio output"; break;
493 case VLC_OBJECT_SOUT: psz_object = "stream output"; break;
494 case VLC_OBJECT_HTTPD: psz_object = "http daemon"; break;
495 case VLC_OBJECT_DIALOGS: psz_object = "dialogs provider"; break;
496 case VLC_OBJECT_VLM: psz_object = "vlm"; break;
497 case VLC_OBJECT_ANNOUNCE: psz_object = "announce handler"; break;
498 case VLC_OBJECT_DEMUX: psz_object = "demuxer"; break;
499 case VLC_OBJECT_ACCESS: psz_object = "access"; break;
503 # define CE_WRITE(str) WriteFile( p_this->p_libvlc->msg_bank.logfile, \
504 str, strlen(str), &i_dummy, NULL );
505 CE_WRITE( p_item->psz_module );
507 CE_WRITE( psz_object );
508 CE_WRITE( ppsz_type[i_type] );
510 CE_WRITE( p_item->psz_msg );
512 FlushFileBuffers( p_this->p_libvlc->msg_bank.logfile );
515 /* Send the message to stderr */
516 if( p_this->p_libvlc->b_color )
518 fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
519 p_item->i_object_id, p_item->psz_module, psz_object,
520 ppsz_type[i_type], ppsz_color[i_type],
525 fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
526 p_item->psz_module, psz_object, ppsz_type[i_type],