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 vlc_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 *****************************************************************************/
35 #include <vlc_common.h>
37 #include <stdarg.h> /* va_list for BSD */
40 # include <fcntl.h> /* O_CREAT, O_TRUNC, O_WRONLY, O_SYNC */
43 #include <errno.h> /* errno */
46 # include <vlc_network.h> /* 'net_strerror' and 'WSAGetLastError' */
50 # include <unistd.h> /* close(), write() */
55 #include <vlc_charset.h>
56 #include "../libvlc.h"
64 static void cleanup_msg_context (void *data)
66 msg_context_t *ctx = data;
67 free (ctx->psz_message);
71 static vlc_threadvar_t msg_context;
72 static uintptr_t banks = 0;
74 /*****************************************************************************
76 *****************************************************************************/
77 #if defined(HAVE_VA_COPY)
78 # define vlc_va_copy(dest,src) va_copy(dest,src)
79 #elif defined(HAVE___VA_COPY)
80 # define vlc_va_copy(dest,src) __va_copy(dest,src)
82 # define vlc_va_copy(dest,src) (dest)=(src)
85 #define QUEUE priv->msg_bank
86 static inline msg_bank_t *libvlc_bank (libvlc_int_t *inst)
88 return &(libvlc_priv (inst))->msg_bank;
91 /*****************************************************************************
93 *****************************************************************************/
94 static void QueueMsg ( vlc_object_t *, int, const char *,
95 const char *, va_list );
96 static void PrintMsg ( vlc_object_t *, msg_item_t * );
98 static vlc_mutex_t msg_stack_lock = VLC_STATIC_MUTEX;
101 * Initialize messages queues
102 * This function initializes all message queues
104 void msg_Create (libvlc_int_t *p_libvlc)
106 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
107 msg_bank_t *bank = libvlc_bank (p_libvlc);
109 vlc_rwlock_init (&bank->lock);
110 vlc_dictionary_init( &priv->msg_enabled_objects, 0 );
111 priv->msg_all_objects_enabled = true;
118 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
119 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
120 CREATE_ALWAYS, 0, NULL );
121 SetFilePointer( QUEUE.logfile, 0, NULL, FILE_END );
124 vlc_mutex_lock( &msg_stack_lock );
126 vlc_threadvar_create( &msg_context, cleanup_msg_context );
127 vlc_mutex_unlock( &msg_stack_lock );
131 * Object Printing selection
133 static void const * kObjectPrintingEnabled = &kObjectPrintingEnabled;
134 static void const * kObjectPrintingDisabled = &kObjectPrintingDisabled;
136 void __msg_EnableObjectPrinting (vlc_object_t *p_this, const char * psz_object)
138 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
139 vlc_rwlock_wrlock( &QUEUE.lock );
140 if( !strcmp(psz_object, "all") )
141 priv->msg_all_objects_enabled = true;
143 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingEnabled );
144 vlc_rwlock_unlock( &QUEUE.lock );
147 void __msg_DisableObjectPrinting (vlc_object_t *p_this, const char * psz_object)
149 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
150 vlc_rwlock_wrlock( &QUEUE.lock );
151 if( !strcmp(psz_object, "all") )
152 priv->msg_all_objects_enabled = false;
154 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingDisabled );
155 vlc_rwlock_unlock( &QUEUE.lock );
159 * Destroy the message queues
161 * This functions prints all messages remaining in the queues,
162 * then frees all the allocated resources
163 * No other messages interface functions should be called after this one.
165 void msg_Destroy (libvlc_int_t *p_libvlc)
167 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
168 msg_bank_t *bank = libvlc_bank (p_libvlc);
171 msg_Err( p_libvlc, "stale interface subscribers (VLC might crash)" );
173 vlc_mutex_lock( &msg_stack_lock );
176 vlc_threadvar_delete( &msg_context );
177 vlc_mutex_unlock( &msg_stack_lock );
180 CloseHandle( QUEUE.logfile );
183 vlc_dictionary_clear( &priv->msg_enabled_objects, NULL, NULL );
185 vlc_rwlock_destroy (&bank->lock);
188 struct msg_subscription_t
190 libvlc_int_t *instance;
192 msg_cb_data_t *opaque;
196 * Subscribe to the message queue.
197 * Whenever a message is emitted, a callback will be called.
198 * Callback invocation are serialized within a subscription.
200 * @param instance LibVLC instance to get messages from
201 * @param cb callback function
202 * @param opaque data for the callback function
203 * @return a subscription pointer, or NULL in case of failure
205 msg_subscription_t *msg_Subscribe (libvlc_int_t *instance, msg_callback_t cb,
206 msg_cb_data_t *opaque)
208 msg_subscription_t *sub = malloc (sizeof (*sub));
212 sub->instance = instance;
214 sub->opaque = opaque;
216 msg_bank_t *bank = libvlc_bank (instance);
217 vlc_rwlock_wrlock (&bank->lock);
218 TAB_APPEND (bank->i_sub, bank->pp_sub, sub);
219 vlc_rwlock_unlock (&bank->lock);
225 * Unsubscribe from the message queue.
226 * This function waits for the message callback to return if needed.
228 void msg_Unsubscribe (msg_subscription_t *sub)
230 msg_bank_t *bank = libvlc_bank (sub->instance);
232 vlc_rwlock_wrlock (&bank->lock);
233 TAB_REMOVE (bank->i_sub, bank->pp_sub, sub);
234 vlc_rwlock_unlock (&bank->lock);
238 /*****************************************************************************
239 * __msg_*: print a message
240 *****************************************************************************
241 * These functions queue a message for later printing.
242 *****************************************************************************/
243 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
244 const char *psz_format, ... )
248 va_start( args, psz_format );
249 QueueMsg( p_this, i_type, psz_module, psz_format, args );
253 void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
254 const char *psz_format, va_list args )
256 QueueMsg( p_this, i_type, psz_module, psz_format, args );
260 * Destroys a message.
262 static void msg_Free (gc_object_t *gc)
264 msg_item_t *msg = vlc_priv (gc, msg_item_t);
266 free (msg->psz_module);
268 free (msg->psz_header);
273 * Add a message to a queue
275 * This function provides basic functionnalities to other msg_* functions.
276 * It adds a message to a queue (after having printed all stored messages if it
277 * is full). If the message can't be converted to string in memory, it issues
280 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
281 const char *psz_format, va_list _args )
284 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
285 int i_header_size; /* Size of the additionnal header */
287 char * psz_str = NULL; /* formatted message string */
288 char * psz_header = NULL;
291 if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
292 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
296 /* Expand %m to strerror(errno) - only once */
297 char buf[strlen( psz_format ) + 2001], *ptr;
298 strcpy( buf, psz_format );
300 psz_format = (const char*) buf;
304 ptr = strchr( ptr, '%' );
314 strerror_r( errno, errbuf, 1001 );
316 int sockerr = WSAGetLastError( );
319 strncpy( errbuf, net_strerror( sockerr ), 1001 );
320 WSASetLastError( sockerr );
323 || (strcmp ("Unknown network stack error", errbuf) == 0))
324 strncpy( errbuf, strerror( errno ), 1001 );
328 /* Escape '%' from the error string */
329 for( char *percent = strchr( errbuf, '%' );
331 percent = strchr( percent + 2, '%' ) )
333 memmove( percent + 1, percent, strlen( percent ) + 1 );
336 errlen = strlen( errbuf );
337 memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
338 memcpy( ptr, errbuf, errlen );
339 break; /* Only once, so we don't overflow */
342 /* Looks for conversion specifier... */
345 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
347 ptr++; /* ...and skip it */
351 /* Convert message to string */
352 vlc_va_copy( args, _args );
353 if( vasprintf( &psz_str, psz_format, args ) == -1 )
357 if( psz_str == NULL )
359 int canc = vlc_savecancel (); /* Do not print half of a message... */
361 fprintf( stderr, "main warning: can't store message (%m): " );
365 /* we're not using GLIBC, so we are sure that the error description
366 * will be stored in the buffer we provide to strerror_r() */
367 strerror_r( errno, psz_err, 1001 );
369 strncpy( psz_err, strerror( errno ), 1001 );
371 psz_err[1000] = '\0';
372 fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
374 vlc_va_copy( args, _args );
375 /* We should use utf8_vfprintf - but it calls malloc()... */
376 vfprintf( stderr, psz_format, args );
378 fputs( "\n", stderr );
379 vlc_restorecancel (canc);
383 msg_item_t * p_item = malloc (sizeof (*p_item));
387 vlc_gc_init (p_item, msg_Free);
388 p_item->psz_module = p_item->psz_msg = p_item->psz_header = NULL;
394 while( p_obj != NULL )
396 char *psz_old = NULL;
397 if( p_obj->psz_header )
399 i_header_size += strlen( p_obj->psz_header ) + 4;
402 psz_old = strdup( psz_header );
403 psz_header = xrealloc( psz_header, i_header_size );
404 snprintf( psz_header, i_header_size , "[%s] %s",
405 p_obj->psz_header, psz_old );
409 psz_header = xmalloc( i_header_size );
410 snprintf( psz_header, i_header_size, "[%s]",
415 p_obj = p_obj->p_parent;
418 /* Fill message information fields */
419 p_item->i_type = i_type;
420 p_item->i_object_id = (uintptr_t)p_this;
421 p_item->psz_object_type = p_this->psz_object_type;
422 p_item->psz_module = strdup( psz_module );
423 p_item->psz_msg = psz_str;
424 p_item->psz_header = psz_header;
426 PrintMsg( p_this, p_item );
428 msg_bank_t *bank = &QUEUE;
429 vlc_rwlock_rdlock (&bank->lock);
430 for (int i = 0; i < bank->i_sub; i++)
432 msg_subscription_t *sub = bank->pp_sub[i];
433 sub->func (sub->opaque, p_item, 0);
435 vlc_rwlock_unlock (&bank->lock);
436 msg_Release (p_item);
439 /*****************************************************************************
440 * PrintMsg: output a standard message item to stderr
441 *****************************************************************************
442 * Print a message to stderr, with colour formatting if needed.
443 *****************************************************************************/
444 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
446 # define COL(x) "\033[" #x ";1m"
448 # define GREEN COL(32)
449 # define YELLOW COL(33)
450 # define WHITE COL(0)
451 # define GRAY "\033[0m"
456 static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
457 static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
458 const char *psz_object;
459 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
460 int i_type = p_item->i_type;
465 if( priv->i_verbose < 0 ) return;
468 if( priv->i_verbose < 0 ) return;
471 if( priv->i_verbose < 1 ) return;
474 if( priv->i_verbose < 2 ) return;
478 psz_object = p_item->psz_object_type;
479 void * val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
480 p_item->psz_module );
481 if( val == kObjectPrintingDisabled )
483 if( val == kObjectPrintingEnabled )
487 val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
489 if( val == kObjectPrintingDisabled )
491 if( val == kObjectPrintingEnabled )
493 else if( !priv->msg_all_objects_enabled )
498 # define CE_WRITE(str) WriteFile( QUEUE.logfile, \
499 str, strlen(str), &i_dummy, NULL );
500 CE_WRITE( p_item->psz_module );
502 CE_WRITE( psz_object );
503 CE_WRITE( ppsz_type[i_type] );
505 CE_WRITE( p_item->psz_msg );
507 FlushFileBuffers( QUEUE.logfile );
510 int canc = vlc_savecancel ();
511 /* Send the message to stderr */
512 utf8_fprintf( stderr, "[%s%p%s] %s%s%s %s%s: %s%s%s\n",
513 priv->b_color ? GREEN : "",
514 (void *)p_item->i_object_id,
515 priv->b_color ? GRAY : "",
516 p_item->psz_header ? p_item->psz_header : "",
517 p_item->psz_header ? " " : "",
518 p_item->psz_module, psz_object,
520 priv->b_color ? ppsz_color[i_type] : "",
522 priv->b_color ? GRAY : "" );
527 vlc_restorecancel (canc);
531 static msg_context_t* GetContext(void)
533 msg_context_t *p_ctx = vlc_threadvar_get( msg_context );
536 p_ctx = malloc( sizeof( msg_context_t ) );
539 p_ctx->psz_message = NULL;
540 vlc_threadvar_set( msg_context, p_ctx );
545 void msg_StackDestroy (void *data)
547 msg_context_t *p_ctx = data;
549 free (p_ctx->psz_message);
553 void msg_StackSet( int i_code, const char *psz_message, ... )
556 msg_context_t *p_ctx = GetContext();
560 free( p_ctx->psz_message );
562 va_start( ap, psz_message );
563 if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
564 p_ctx->psz_message = NULL;
567 p_ctx->i_code = i_code;
570 void msg_StackAdd( const char *psz_message, ... )
574 msg_context_t *p_ctx = GetContext();
579 va_start( ap, psz_message );
580 if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
584 if( !p_ctx->psz_message )
585 p_ctx->psz_message = psz_tmp;
589 if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
592 free( p_ctx->psz_message );
593 p_ctx->psz_message = psz_new;
598 const char* msg_StackMsg( void )
600 msg_context_t *p_ctx = GetContext();
602 return p_ctx->psz_message;