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-2002 VideoLAN
7 * $Id: messages.c,v 1.20 2002/11/08 10:26:53 gbazin Exp $
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 <errno.h> /* errno */
31 #include <fcntl.h> /* O_CREAT, O_TRUNC, O_WRONLY, O_SYNC */
32 #include <stdio.h> /* required */
33 #include <stdarg.h> /* va_list for BSD */
34 #include <stdlib.h> /* malloc() */
35 #include <string.h> /* strerror() */
40 #include <unistd.h> /* close(), write() */
43 #include "interface.h"
45 /*****************************************************************************
47 *****************************************************************************/
48 static void QueueMsg ( vlc_object_t *, int , const char *,
49 const char *, va_list );
50 static void FlushMsg ( msg_bank_t * );
51 static void PrintMsg ( vlc_object_t *, msg_item_t * );
53 /*****************************************************************************
54 * msg_Create: initialize messages interface
55 *****************************************************************************
56 * This functions has to be called before any call to other msg_* functions.
57 * It set up the locks and the message queue if it is used.
58 *****************************************************************************/
59 void __msg_Create( vlc_object_t *p_this )
61 /* Message queue initialization */
62 vlc_mutex_init( p_this, &p_this->p_libvlc->msg_bank.lock );
64 p_this->p_libvlc->msg_bank.b_configured = VLC_FALSE;
65 p_this->p_libvlc->msg_bank.b_overflow = VLC_FALSE;
67 p_this->p_libvlc->msg_bank.i_start = 0;
68 p_this->p_libvlc->msg_bank.i_stop = 0;
70 p_this->p_libvlc->msg_bank.i_sub = 0;
71 p_this->p_libvlc->msg_bank.pp_sub = NULL;
74 /*****************************************************************************
75 * msg_Flush: flush the message queue
76 *****************************************************************************/
77 void __msg_Flush( vlc_object_t *p_this )
81 vlc_mutex_lock( &p_this->p_libvlc->msg_bank.lock );
83 p_this->p_libvlc->msg_bank.b_configured = VLC_TRUE;
85 for( i_index = p_this->p_libvlc->msg_bank.i_start;
86 i_index != p_this->p_libvlc->msg_bank.i_stop;
87 i_index = (i_index+1) % VLC_MSG_QSIZE )
89 PrintMsg( p_this, &p_this->p_libvlc->msg_bank.msg[i_index] );
92 FlushMsg( &p_this->p_libvlc->msg_bank );
94 vlc_mutex_unlock( &p_this->p_libvlc->msg_bank.lock );
97 /*****************************************************************************
98 * msg_Destroy: free resources allocated by msg_Create
99 *****************************************************************************
100 * This functions prints all messages remaining in queue, then free all the
101 * resources allocated by msg_Create.
102 * No other messages interface functions should be called after this one.
103 *****************************************************************************/
104 void __msg_Destroy( vlc_object_t *p_this )
106 if( p_this->p_libvlc->msg_bank.i_sub )
108 msg_Err( p_this, "stale interface subscribers" );
111 /* Flush the queue */
112 if( !p_this->p_libvlc->msg_bank.b_configured )
118 FlushMsg( &p_this->p_libvlc->msg_bank );
122 vlc_mutex_destroy( &p_this->p_libvlc->msg_bank.lock );
125 /*****************************************************************************
126 * msg_Subscribe: subscribe to the message queue.
127 *****************************************************************************/
128 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this )
130 msg_bank_t *p_bank = &p_this->p_libvlc->msg_bank;
131 msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
133 vlc_mutex_lock( &p_bank->lock );
135 /* Add subscription to the list */
136 INSERT_ELEM( p_bank->pp_sub, p_bank->i_sub, p_bank->i_sub, p_sub );
138 p_sub->i_start = p_bank->i_start;
139 p_sub->pi_stop = &p_bank->i_stop;
141 p_sub->p_msg = p_bank->msg;
142 p_sub->p_lock = &p_bank->lock;
144 vlc_mutex_unlock( &p_bank->lock );
149 /*****************************************************************************
150 * msg_Unsubscribe: unsubscribe from the message queue.
151 *****************************************************************************/
152 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
154 msg_bank_t *p_bank = &p_this->p_libvlc->msg_bank;
157 vlc_mutex_lock( &p_bank->lock );
162 msg_Err( p_this, "no subscriber in the list" );
166 /* Look for the appropriate subscription */
167 for( i_index = 0; i_index < p_bank->i_sub; i_index++ )
169 if( p_bank->pp_sub[ i_index ] == p_sub )
175 if( p_bank->pp_sub[ i_index ] != p_sub )
177 msg_Err( p_this, "subscriber not found" );
178 vlc_mutex_unlock( &p_bank->lock );
182 /* Remove this subscription */
183 REMOVE_ELEM( p_bank->pp_sub, p_bank->i_sub, i_index );
185 vlc_mutex_unlock( &p_bank->lock );
188 /*****************************************************************************
189 * __msg_*: print a message
190 *****************************************************************************
191 * These functions queue a message for later printing.
192 *****************************************************************************/
193 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
194 const char *psz_format, ... )
198 va_start( args, psz_format );
199 QueueMsg( p_this, i_type, psz_module, psz_format, args );
203 /* Generic functions used when variadic macros are not available. */
204 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
205 void FN_NAME( void *p_this, const char *psz_format, ... ) \
208 va_start( args, psz_format ); \
209 QueueMsg( (vlc_object_t *)p_this, FN_TYPE, "unknown", \
210 psz_format, args ); \
215 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
216 DECLARE_MSG_FN( __msg_Err, VLC_MSG_ERR );
217 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
218 DECLARE_MSG_FN( __msg_Dbg, VLC_MSG_DBG );
220 /*****************************************************************************
221 * QueueMsg: add a message to a queue
222 *****************************************************************************
223 * This function provides basic functionnalities to other msg_* functions.
224 * It adds a message to a queue (after having printed all stored messages if it
225 * is full). If the message can't be converted to string in memory, it issues
227 *****************************************************************************/
228 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
229 const char *psz_format, va_list args )
231 msg_bank_t * p_bank = &p_this->p_libvlc->msg_bank; /* message bank */
232 char * psz_str = NULL; /* formatted message string */
233 msg_item_t * p_item = NULL; /* pointer to message */
234 msg_item_t item; /* message in case of a full queue */
236 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN)
237 int i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
241 * Convert message to string
243 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN)
244 vasprintf( &psz_str, psz_format, args );
246 psz_str = (char*) malloc( i_size * sizeof(char) );
249 if( psz_str == NULL )
251 fprintf( stderr, "main warning: can't store message (%s): ",
253 vfprintf( stderr, psz_format, args );
254 fprintf( stderr, "\n" );
258 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN)
259 vsnprintf( psz_str, i_size, psz_format, args );
260 psz_str[ i_size - 1 ] = 0; /* Just in case */
263 /* Put message in queue */
264 vlc_mutex_lock( &p_bank->lock );
266 /* Check there is room in the queue for our message */
267 if( p_bank->b_overflow )
271 if( ((p_bank->i_stop - p_bank->i_start + 1) % VLC_MSG_QSIZE) == 0 )
273 /* Still in overflow mode, print from a dummy item */
278 /* Pheeew, at last, there is room in the queue! */
279 p_bank->b_overflow = VLC_FALSE;
282 else if( ((p_bank->i_stop - p_bank->i_start + 2) % VLC_MSG_QSIZE) == 0 )
286 if( ((p_bank->i_stop - p_bank->i_start + 2) % VLC_MSG_QSIZE) == 0 )
288 p_bank->b_overflow = VLC_TRUE;
290 /* Put the overflow message in the queue */
291 p_item = p_bank->msg + p_bank->i_stop;
292 p_bank->i_stop = (p_bank->i_stop + 1) % VLC_MSG_QSIZE;
294 p_item->i_type = VLC_MSG_ERR;
295 p_item->i_object_id = p_this->i_object_id;
296 p_item->i_object_type = p_this->i_object_type;
297 p_item->psz_module = strdup( "message" );
298 p_item->psz_msg = strdup( "message queue overflowed" );
300 PrintMsg( p_this, p_item );
302 /* We print from a dummy item */
307 if( !p_bank->b_overflow )
309 /* Put the message in the queue */
310 p_item = p_bank->msg + p_bank->i_stop;
311 p_bank->i_stop = (p_bank->i_stop + 1) % VLC_MSG_QSIZE;
314 /* Fill message information fields */
315 p_item->i_type = i_type;
316 p_item->i_object_id = p_this->i_object_id;
317 p_item->i_object_type = p_this->i_object_type;
318 p_item->psz_module = strdup( psz_module );
319 p_item->psz_msg = psz_str;
321 PrintMsg( p_this, p_item );
323 if( p_bank->b_overflow )
325 free( p_item->psz_module );
326 free( p_item->psz_msg );
329 vlc_mutex_unlock( &p_bank->lock );
332 /* following functions are local */
334 /*****************************************************************************
336 *****************************************************************************
337 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
338 * this function does not check the lock.
339 *****************************************************************************/
340 static void FlushMsg ( msg_bank_t *p_bank )
342 int i_index, i_start, i_stop;
344 /* Only flush the queue if it has been properly configured */
345 if( !p_bank->b_configured )
350 /* Get the maximum message index that can be freed */
351 i_stop = p_bank->i_stop;
353 /* Check until which value we can free messages */
354 for( i_index = 0; i_index < p_bank->i_sub; i_index++ )
356 i_start = p_bank->pp_sub[ i_index ]->i_start;
358 /* If this subscriber is late, we don't free messages before
359 * his i_start value, otherwise he'll miss messages */
360 if( ( i_start < i_stop
361 && (p_bank->i_stop <= i_start || i_stop <= p_bank->i_stop) )
362 || ( i_stop < i_start
363 && (i_stop <= p_bank->i_stop && p_bank->i_stop <= i_start) ) )
369 /* Free message data */
370 for( i_index = p_bank->i_start;
372 i_index = (i_index+1) % VLC_MSG_QSIZE )
374 free( p_bank->msg[i_index].psz_msg );
375 free( p_bank->msg[i_index].psz_module );
378 /* Update the new start value */
379 p_bank->i_start = i_index;
382 /*****************************************************************************
383 * PrintMsg: output a message item to stderr
384 *****************************************************************************
385 * Print a message to stderr, with colour formatting if needed.
386 *****************************************************************************/
387 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
389 # define COL(x) "\033[" #x ";1m"
391 # define GREEN COL(32)
392 # define YELLOW COL(33)
393 # define WHITE COL(37)
394 # define GRAY "\033[0m"
396 static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
397 static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
398 char *psz_object = "private";
399 int i_type = p_item->i_type;
404 if( p_this->p_libvlc->i_verbose < 0 ) return;
407 if( p_this->p_libvlc->i_verbose < 0 ) return;
410 if( p_this->p_libvlc->i_verbose < 1 ) return;
413 if( p_this->p_libvlc->i_verbose < 2 ) return;
417 switch( p_item->i_object_type )
419 case VLC_OBJECT_ROOT: psz_object = "root"; break;
420 case VLC_OBJECT_VLC: psz_object = "vlc"; break;
421 case VLC_OBJECT_MODULE: psz_object = "module"; break;
422 case VLC_OBJECT_INTF: psz_object = "interface"; break;
423 case VLC_OBJECT_PLAYLIST: psz_object = "playlist"; break;
424 case VLC_OBJECT_ITEM: psz_object = "item"; break;
425 case VLC_OBJECT_INPUT: psz_object = "input"; break;
426 case VLC_OBJECT_DECODER: psz_object = "decoder"; break;
427 case VLC_OBJECT_VOUT: psz_object = "video output"; break;
428 case VLC_OBJECT_AOUT: psz_object = "audio output"; break;
429 case VLC_OBJECT_SOUT: psz_object = "stream output"; break;
432 /* Send the message to stderr */
433 if( p_this->p_libvlc->b_color )
435 fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
436 p_item->i_object_id, p_item->psz_module, psz_object,
437 ppsz_type[i_type], ppsz_color[i_type],
442 fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
443 p_item->psz_module, psz_object, ppsz_type[i_type],