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.5 2002/07/23 00:30:22 sam 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 * );
54 static char *ConvertPrintfFormatString ( const char *psz_format );
57 /*****************************************************************************
58 * msg_Create: initialize messages interface
59 *****************************************************************************
60 * This functions has to be called before any call to other msg_* functions.
61 * It set up the locks and the message queue if it is used.
62 *****************************************************************************/
63 void __msg_Create( vlc_object_t *p_this )
65 /* Message queue initialization */
66 vlc_mutex_init( p_this, &p_this->p_vlc->msg_bank.lock );
68 p_this->p_vlc->msg_bank.i_start = 0;
69 p_this->p_vlc->msg_bank.i_stop = 0;
71 p_this->p_vlc->msg_bank.i_sub = 0;
72 p_this->p_vlc->msg_bank.pp_sub = NULL;
75 /*****************************************************************************
76 * msg_Flush: flush the message queue
77 *****************************************************************************/
78 void __msg_Flush( vlc_object_t *p_this )
82 vlc_mutex_lock( &p_this->p_vlc->msg_bank.lock );
84 for( i_index = p_this->p_vlc->msg_bank.i_start;
85 i_index != p_this->p_vlc->msg_bank.i_stop;
86 i_index = (i_index+1) % VLC_MSG_QSIZE )
88 PrintMsg( p_this, &p_this->p_vlc->msg_bank.msg[i_index] );
91 FlushMsg( &p_this->p_vlc->msg_bank );
93 vlc_mutex_unlock( &p_this->p_vlc->msg_bank.lock );
96 /*****************************************************************************
97 * msg_Destroy: free resources allocated by msg_Create
98 *****************************************************************************
99 * This functions prints all messages remaining in queue, then free all the
100 * resources allocated by msg_Create.
101 * No other messages interface functions should be called after this one.
102 *****************************************************************************/
103 void __msg_Destroy( vlc_object_t *p_this )
106 vlc_mutex_destroy( &p_this->p_vlc->msg_bank.lock );
108 if( p_this->p_vlc->msg_bank.i_sub )
110 fprintf( stderr, "main error: stale interface subscribers\n" );
113 /* Free remaining messages */
114 FlushMsg( &p_this->p_vlc->msg_bank );
117 /*****************************************************************************
118 * msg_Subscribe: subscribe to the message queue.
119 *****************************************************************************/
120 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this )
122 msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
124 vlc_mutex_lock( &p_this->p_vlc->msg_bank.lock );
126 /* Add subscription to the list */
127 p_this->p_vlc->msg_bank.i_sub++;
128 p_this->p_vlc->msg_bank.pp_sub = realloc( p_this->p_vlc->msg_bank.pp_sub,
129 p_this->p_vlc->msg_bank.i_sub * sizeof( msg_subscription_t* ) );
131 p_this->p_vlc->msg_bank.pp_sub[ p_this->p_vlc->msg_bank.i_sub - 1 ] = p_sub;
133 p_sub->i_start = p_this->p_vlc->msg_bank.i_start;
134 p_sub->pi_stop = &p_this->p_vlc->msg_bank.i_stop;
136 p_sub->p_msg = p_this->p_vlc->msg_bank.msg;
137 p_sub->p_lock = &p_this->p_vlc->msg_bank.lock;
139 vlc_mutex_unlock( &p_this->p_vlc->msg_bank.lock );
144 /*****************************************************************************
145 * msg_Unsubscribe: unsubscribe from the message queue.
146 *****************************************************************************/
147 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
151 vlc_mutex_lock( &p_this->p_vlc->msg_bank.lock );
154 if( !p_this->p_vlc->msg_bank.i_sub )
156 msg_Err( p_this, "no subscriber in the list" );
160 /* Look for the appropriate subscription */
161 for( i_index = 0; i_index < p_this->p_vlc->msg_bank.i_sub; i_index++ )
163 if( p_this->p_vlc->msg_bank.pp_sub[ i_index ] == p_sub )
169 if( p_this->p_vlc->msg_bank.pp_sub[ i_index ] != p_sub )
171 msg_Err( p_this, "subscriber not found" );
172 vlc_mutex_unlock( &p_this->p_vlc->msg_bank.lock );
176 /* Remove this subscription */
177 for( ; i_index < (p_this->p_vlc->msg_bank.i_sub - 1); i_index++ )
179 p_this->p_vlc->msg_bank.pp_sub[ i_index ] = p_this->p_vlc->msg_bank.pp_sub[ i_index+1 ];
182 p_this->p_vlc->msg_bank.i_sub--;
183 if( p_this->p_vlc->msg_bank.i_sub )
185 p_this->p_vlc->msg_bank.pp_sub = realloc( p_this->p_vlc->msg_bank.pp_sub,
186 p_this->p_vlc->msg_bank.i_sub * sizeof( msg_subscription_t* ) );
190 free( p_this->p_vlc->msg_bank.pp_sub );
191 p_this->p_vlc->msg_bank.pp_sub = NULL;
195 vlc_mutex_unlock( &p_this->p_vlc->msg_bank.lock );
198 /*****************************************************************************
199 * __msg_*: print a message
200 *****************************************************************************
201 * These functions queue a message for later printing.
202 *****************************************************************************/
203 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
204 const char *psz_format, ... )
208 va_start( args, psz_format );
209 QueueMsg( p_this, i_type, psz_module, psz_format, args );
213 /* Generic functions used when variadic macros are not available. */
214 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
215 void FN_NAME( void *p_this, const char *psz_format, ... ) \
218 va_start( args, psz_format ); \
219 QueueMsg( (vlc_object_t *)p_this, FN_TYPE, "unknown", \
220 psz_format, args ); \
224 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
225 DECLARE_MSG_FN( __msg_Err, VLC_MSG_ERR );
226 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
227 DECLARE_MSG_FN( __msg_Dbg, VLC_MSG_DBG );
229 /*****************************************************************************
230 * QueueMsg: add a message to a queue
231 *****************************************************************************
232 * This function provides basic functionnalities to other msg_* functions.
233 * It adds a message to a queue (after having printed all stored messages if it
234 * is full). If the message can't be converted to string in memory, it issues
236 *****************************************************************************/
237 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
238 const char *psz_format, va_list args )
240 char * psz_str = NULL; /* formatted message string */
241 msg_item_t * p_item; /* pointer to message */
242 msg_item_t item; /* message in case of a full queue */
248 * Convert message to string
250 #ifdef HAVE_VASPRINTF
251 vasprintf( &psz_str, psz_format, args );
253 psz_str = (char*) malloc( strlen(psz_format) + INTF_MAX_MSG_SIZE );
256 if( psz_str == NULL )
258 fprintf( stderr, "main warning: can't store message (%s): ",
260 vfprintf( stderr, psz_format, args );
261 fprintf( stderr, "\n" );
265 #ifndef HAVE_VASPRINTF
267 psz_temp = ConvertPrintfFormatString( psz_format );
270 fprintf( stderr, "main warning: couldn't print message\n" );
273 vsprintf( psz_str, psz_temp, args );
276 vsprintf( psz_str, psz_format, args );
280 /* Put message in queue */
281 vlc_mutex_lock( &p_this->p_vlc->msg_bank.lock );
283 /* Check there is room in the queue for our message */
284 if( ((p_this->p_vlc->msg_bank.i_stop - p_this->p_vlc->msg_bank.i_start + 1) % VLC_MSG_QSIZE) == 0 )
286 FlushMsg( &p_this->p_vlc->msg_bank );
288 if( ((p_this->p_vlc->msg_bank.i_stop - p_this->p_vlc->msg_bank.i_start + 1) % VLC_MSG_QSIZE) == 0 )
290 fprintf( stderr, "main warning: message queue overflow\n" );
296 /* Put the message in the queue */
297 p_item = p_this->p_vlc->msg_bank.msg + p_this->p_vlc->msg_bank.i_stop;
298 p_this->p_vlc->msg_bank.i_stop =
299 (p_this->p_vlc->msg_bank.i_stop + 1) % VLC_MSG_QSIZE;
301 /* Fill message information fields */
303 p_item->i_type = i_type;
304 p_item->i_object_id = p_this->i_object_id;
305 p_item->psz_module = strdup( psz_module );
306 p_item->psz_msg = psz_str;
308 PrintMsg( p_this, p_item );
310 vlc_mutex_unlock( &p_this->p_vlc->msg_bank.lock );
313 /* following functions are local */
315 /*****************************************************************************
317 *****************************************************************************
318 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
319 * this function does not check the lock.
320 *****************************************************************************/
321 static void FlushMsg ( msg_bank_t *p_bank )
323 int i_index, i_start, i_stop;
325 /* Get the maximum message index that can be freed */
326 i_stop = p_bank->i_stop;
328 /* Check until which value we can free messages */
329 for( i_index = 0; i_index < p_bank->i_sub; i_index++ )
331 i_start = p_bank->pp_sub[ i_index ]->i_start;
333 /* If this subscriber is late, we don't free messages before
334 * his i_start value, otherwise he'll miss messages */
335 if( ( i_start < i_stop
336 && (p_bank->i_stop <= i_start || i_stop <= p_bank->i_stop) )
337 || ( i_stop < i_start
338 && (i_stop <= p_bank->i_stop && p_bank->i_stop <= i_start) ) )
344 /* Free message data */
345 for( i_index = p_bank->i_start;
347 i_index = (i_index+1) % VLC_MSG_QSIZE )
349 free( p_bank->msg[i_index].psz_msg );
350 free( p_bank->msg[i_index].psz_module );
353 /* Update the new start value */
354 p_bank->i_start = i_index;
357 /*****************************************************************************
358 * PrintMsg: output a message item to stderr
359 *****************************************************************************
360 * Print a message to stderr, with colour formatting if needed.
361 *****************************************************************************/
362 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
364 # define COL(x) "\033[" #x ";1m"
366 # define GREEN COL(32)
367 # define YELLOW COL(33)
368 # define WHITE COL(37)
369 # define GRAY "\033[0m"
371 static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
372 static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
373 int i_type = p_item->i_type;
375 /* Send the message to stderr */
376 if( (i_type == VLC_MSG_ERR)
377 || ( (i_type == VLC_MSG_INFO) && !p_this->p_vlc->b_quiet )
378 || ( (i_type == VLC_MSG_WARN) && p_this->p_vlc->b_verbose
379 && !p_this->p_vlc->b_quiet )
380 || ( (i_type == VLC_MSG_DBG) && p_this->p_vlc->b_verbose
381 && !p_this->p_vlc->b_quiet ) )
383 if( p_this->p_vlc->b_color )
385 fprintf( stderr, "[" GREEN "%.2x" GRAY ":" GREEN "%.6x" GRAY "] "
386 "%s%s: %s%s" GRAY "\n", p_this->p_vlc->i_unique,
387 p_item->i_object_id, p_item->psz_module,
388 ppsz_type[i_type], ppsz_color[i_type],
393 fprintf( stderr, "[%.2x:%.6x] %s%s: %s\n",
394 p_this->p_vlc->i_unique, p_item->i_object_id,
395 p_item->psz_module, ppsz_type[i_type],
401 /*****************************************************************************
402 * ConvertPrintfFormatString: replace all occurrences of %ll with %I64 in the
403 * printf format string.
404 *****************************************************************************
405 * Win32 doesn't recognize the "%ll" format in a printf string, so we have
406 * to convert this string to something that win32 can handle.
407 * This is a REALLY UGLY HACK which won't even work in every situation,
408 * but hey I don't want to put an ifdef WIN32 each time I use printf with
409 * a "long long" type!!!
410 * By the way, if we don't do this we can sometimes end up with segfaults.
411 *****************************************************************************/
413 static char *ConvertPrintfFormatString( const char *psz_format )
415 int i, i_counter=0, i_pos=0;
418 /* We first need to check how many occurences of %ll there are in the
419 * psz_format string. Once we'll know that we'll be able to malloc the
420 * destination string */
422 if( strlen( psz_format ) <= 3 )
423 return strdup( psz_format );
425 for( i=0; i <= (strlen(psz_format) - 3); i++ )
427 if( !strncmp( (char *)(psz_format + i), "%ll", 3 ) )
433 /* malloc the destination string */
434 psz_dest = malloc( strlen(psz_format) + i_counter + 1 );
435 if( psz_dest == NULL )
437 fprintf( stderr, "main warning: ConvertPrintfFormatString failed\n" );
441 /* Now build the modified string */
443 for( i=0; i <= (strlen(psz_format) - 3); i++ )
445 if( !strncmp( (char *)(psz_format + i), "%ll", 3 ) )
447 memcpy( psz_dest+i_pos+i_counter, psz_format+i_pos, i-i_pos+1);
448 *(psz_dest+i+i_counter+1)='I';
449 *(psz_dest+i+i_counter+2)='6';
450 *(psz_dest+i+i_counter+3)='4';
455 strcpy( psz_dest+i_pos+i_counter, psz_format+i_pos );