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 */
39 #include <errno.h> /* errno */
42 # include <vlc_network.h> /* 'net_strerror' and 'WSAGetLastError' */
46 # include <unistd.h> /* close(), write() */
51 #include <vlc_charset.h>
52 #include "../libvlc.h"
60 static void cleanup_msg_context (void *data)
62 msg_context_t *ctx = data;
63 free (ctx->psz_message);
67 static vlc_threadvar_t msg_context;
68 static uintptr_t banks = 0;
70 /*****************************************************************************
72 *****************************************************************************/
73 #if defined(HAVE_VA_COPY)
74 # define vlc_va_copy(dest,src) va_copy(dest,src)
75 #elif defined(HAVE___VA_COPY)
76 # define vlc_va_copy(dest,src) __va_copy(dest,src)
78 # define vlc_va_copy(dest,src) (dest)=(src)
81 #define QUEUE priv->msg_bank
82 static inline msg_bank_t *libvlc_bank (libvlc_int_t *inst)
84 return &(libvlc_priv (inst))->msg_bank;
87 /*****************************************************************************
89 *****************************************************************************/
90 static void QueueMsg ( vlc_object_t *, int, const char *,
91 const char *, va_list );
92 static void PrintMsg ( vlc_object_t *, msg_item_t * );
94 static vlc_mutex_t msg_stack_lock = VLC_STATIC_MUTEX;
97 * Initialize messages queues
98 * This function initializes all message queues
100 void msg_Create (libvlc_int_t *p_libvlc)
102 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
103 msg_bank_t *bank = libvlc_bank (p_libvlc);
105 vlc_rwlock_init (&bank->lock);
106 vlc_dictionary_init( &priv->msg_enabled_objects, 0 );
107 priv->msg_all_objects_enabled = true;
114 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
115 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
116 CREATE_ALWAYS, 0, NULL );
117 SetFilePointer( QUEUE.logfile, 0, NULL, FILE_END );
120 vlc_mutex_lock( &msg_stack_lock );
122 vlc_threadvar_create( &msg_context, cleanup_msg_context );
123 vlc_mutex_unlock( &msg_stack_lock );
127 * Object Printing selection
129 static void const * kObjectPrintingEnabled = &kObjectPrintingEnabled;
130 static void const * kObjectPrintingDisabled = &kObjectPrintingDisabled;
132 void __msg_EnableObjectPrinting (vlc_object_t *p_this, const char * psz_object)
134 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
135 vlc_rwlock_wrlock( &QUEUE.lock );
136 if( !strcmp(psz_object, "all") )
137 priv->msg_all_objects_enabled = true;
139 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingEnabled );
140 vlc_rwlock_unlock( &QUEUE.lock );
143 void __msg_DisableObjectPrinting (vlc_object_t *p_this, const char * psz_object)
145 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
146 vlc_rwlock_wrlock( &QUEUE.lock );
147 if( !strcmp(psz_object, "all") )
148 priv->msg_all_objects_enabled = false;
150 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingDisabled );
151 vlc_rwlock_unlock( &QUEUE.lock );
155 * Destroy the message queues
157 * This functions prints all messages remaining in the queues,
158 * then frees all the allocated resources
159 * No other messages interface functions should be called after this one.
161 void msg_Destroy (libvlc_int_t *p_libvlc)
163 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
164 msg_bank_t *bank = libvlc_bank (p_libvlc);
167 msg_Err( p_libvlc, "stale interface subscribers (VLC might crash)" );
169 vlc_mutex_lock( &msg_stack_lock );
172 vlc_threadvar_delete( &msg_context );
173 vlc_mutex_unlock( &msg_stack_lock );
176 CloseHandle( QUEUE.logfile );
179 vlc_dictionary_clear( &priv->msg_enabled_objects, NULL, NULL );
181 vlc_rwlock_destroy (&bank->lock);
184 struct msg_subscription_t
186 libvlc_int_t *instance;
188 msg_cb_data_t *opaque;
192 * Subscribe to the message queue.
193 * Whenever a message is emitted, a callback will be called.
194 * Callback invocation are serialized within a subscription.
196 * @param instance LibVLC instance to get messages from
197 * @param cb callback function
198 * @param opaque data for the callback function
199 * @return a subscription pointer, or NULL in case of failure
201 msg_subscription_t *msg_Subscribe (libvlc_int_t *instance, msg_callback_t cb,
202 msg_cb_data_t *opaque)
204 msg_subscription_t *sub = malloc (sizeof (*sub));
208 sub->instance = instance;
210 sub->opaque = opaque;
212 msg_bank_t *bank = libvlc_bank (instance);
213 vlc_rwlock_wrlock (&bank->lock);
214 TAB_APPEND (bank->i_sub, bank->pp_sub, sub);
215 vlc_rwlock_unlock (&bank->lock);
221 * Unsubscribe from the message queue.
222 * This function waits for the message callback to return if needed.
224 void msg_Unsubscribe (msg_subscription_t *sub)
226 msg_bank_t *bank = libvlc_bank (sub->instance);
228 vlc_rwlock_wrlock (&bank->lock);
229 TAB_REMOVE (bank->i_sub, bank->pp_sub, sub);
230 vlc_rwlock_unlock (&bank->lock);
234 /*****************************************************************************
235 * __msg_*: print a message
236 *****************************************************************************
237 * These functions queue a message for later printing.
238 *****************************************************************************/
239 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
240 const char *psz_format, ... )
244 va_start( args, psz_format );
245 QueueMsg( p_this, i_type, psz_module, psz_format, args );
249 void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
250 const char *psz_format, va_list args )
252 QueueMsg( p_this, i_type, psz_module, psz_format, args );
256 * Destroys a message.
258 static void msg_Free (gc_object_t *gc)
260 msg_item_t *msg = vlc_priv (gc, msg_item_t);
262 free (msg->psz_module);
264 free (msg->psz_header);
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 )
280 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
281 int i_header_size; /* Size of the additionnal header */
283 char * psz_str = NULL; /* formatted message string */
284 char * psz_header = NULL;
287 if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
288 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
292 /* Expand %m to strerror(errno) - only once */
293 char buf[strlen( psz_format ) + 2001], *ptr;
294 strcpy( buf, psz_format );
296 psz_format = (const char*) buf;
300 ptr = strchr( ptr, '%' );
310 strerror_r( errno, errbuf, 1001 );
312 int sockerr = WSAGetLastError( );
315 strncpy( errbuf, net_strerror( sockerr ), 1001 );
316 WSASetLastError( sockerr );
319 || (strcmp ("Unknown network stack error", errbuf) == 0))
320 strncpy( errbuf, strerror( errno ), 1001 );
324 /* Escape '%' from the error string */
325 for( char *percent = strchr( errbuf, '%' );
327 percent = strchr( percent + 2, '%' ) )
329 memmove( percent + 1, percent, strlen( percent ) + 1 );
332 errlen = strlen( errbuf );
333 memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
334 memcpy( ptr, errbuf, errlen );
335 break; /* Only once, so we don't overflow */
338 /* Looks for conversion specifier... */
341 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
343 ptr++; /* ...and skip it */
347 /* Convert message to string */
348 vlc_va_copy( args, _args );
349 if( vasprintf( &psz_str, psz_format, args ) == -1 )
353 if( psz_str == NULL )
355 int canc = vlc_savecancel (); /* Do not print half of a message... */
357 fprintf( stderr, "main warning: can't store message (%m): " );
361 /* we're not using GLIBC, so we are sure that the error description
362 * will be stored in the buffer we provide to strerror_r() */
363 strerror_r( errno, psz_err, 1001 );
365 strncpy( psz_err, strerror( errno ), 1001 );
367 psz_err[1000] = '\0';
368 fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
370 vlc_va_copy( args, _args );
371 /* We should use utf8_vfprintf - but it calls malloc()... */
372 vfprintf( stderr, psz_format, args );
374 fputs( "\n", stderr );
375 vlc_restorecancel (canc);
379 msg_item_t * p_item = malloc (sizeof (*p_item));
383 vlc_gc_init (p_item, msg_Free);
384 p_item->psz_module = p_item->psz_msg = p_item->psz_header = NULL;
390 while( p_obj != NULL )
392 char *psz_old = NULL;
393 if( p_obj->psz_header )
395 i_header_size += strlen( p_obj->psz_header ) + 4;
398 psz_old = strdup( psz_header );
399 psz_header = xrealloc( psz_header, i_header_size );
400 snprintf( psz_header, i_header_size , "[%s] %s",
401 p_obj->psz_header, psz_old );
405 psz_header = xmalloc( i_header_size );
406 snprintf( psz_header, i_header_size, "[%s]",
411 p_obj = p_obj->p_parent;
414 /* Fill message information fields */
415 p_item->i_type = i_type;
416 p_item->i_object_id = (uintptr_t)p_this;
417 p_item->psz_object_type = p_this->psz_object_type;
418 p_item->psz_module = strdup( psz_module );
419 p_item->psz_msg = psz_str;
420 p_item->psz_header = psz_header;
422 PrintMsg( p_this, p_item );
424 msg_bank_t *bank = &QUEUE;
425 vlc_rwlock_rdlock (&bank->lock);
426 for (int i = 0; i < bank->i_sub; i++)
428 msg_subscription_t *sub = bank->pp_sub[i];
429 sub->func (sub->opaque, p_item, 0);
431 vlc_rwlock_unlock (&bank->lock);
432 msg_Release (p_item);
435 /*****************************************************************************
436 * PrintMsg: output a standard message item to stderr
437 *****************************************************************************
438 * Print a message to stderr, with colour formatting if needed.
439 *****************************************************************************/
440 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
442 # define COL(x) "\033[" #x ";1m"
444 # define GREEN COL(32)
445 # define YELLOW COL(33)
446 # define WHITE COL(0)
447 # define GRAY "\033[0m"
452 static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
453 static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
454 const char *psz_object;
455 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
456 int i_type = p_item->i_type;
461 if( priv->i_verbose < 0 ) return;
464 if( priv->i_verbose < 0 ) return;
467 if( priv->i_verbose < 1 ) return;
470 if( priv->i_verbose < 2 ) return;
474 psz_object = p_item->psz_object_type;
475 void * val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
476 p_item->psz_module );
477 if( val == kObjectPrintingDisabled )
479 if( val == kObjectPrintingEnabled )
483 val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
485 if( val == kObjectPrintingDisabled )
487 if( val == kObjectPrintingEnabled )
489 else if( !priv->msg_all_objects_enabled )
494 # define CE_WRITE(str) WriteFile( QUEUE.logfile, \
495 str, strlen(str), &i_dummy, NULL );
496 CE_WRITE( p_item->psz_module );
498 CE_WRITE( psz_object );
499 CE_WRITE( ppsz_type[i_type] );
501 CE_WRITE( p_item->psz_msg );
503 FlushFileBuffers( QUEUE.logfile );
506 int canc = vlc_savecancel ();
507 /* Send the message to stderr */
508 utf8_fprintf( stderr, "[%s%p%s] %s%s%s %s%s: %s%s%s\n",
509 priv->b_color ? GREEN : "",
510 (void *)p_item->i_object_id,
511 priv->b_color ? GRAY : "",
512 p_item->psz_header ? p_item->psz_header : "",
513 p_item->psz_header ? " " : "",
514 p_item->psz_module, psz_object,
516 priv->b_color ? ppsz_color[i_type] : "",
518 priv->b_color ? GRAY : "" );
523 vlc_restorecancel (canc);
527 static msg_context_t* GetContext(void)
529 msg_context_t *p_ctx = vlc_threadvar_get( msg_context );
532 p_ctx = malloc( sizeof( msg_context_t ) );
535 p_ctx->psz_message = NULL;
536 vlc_threadvar_set( msg_context, p_ctx );
541 void msg_StackDestroy (void *data)
543 msg_context_t *p_ctx = data;
545 free (p_ctx->psz_message);
549 void msg_StackSet( int i_code, const char *psz_message, ... )
552 msg_context_t *p_ctx = GetContext();
556 free( p_ctx->psz_message );
558 va_start( ap, psz_message );
559 if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
560 p_ctx->psz_message = NULL;
563 p_ctx->i_code = i_code;
566 void msg_StackAdd( const char *psz_message, ... )
570 msg_context_t *p_ctx = GetContext();
575 va_start( ap, psz_message );
576 if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
580 if( !p_ctx->psz_message )
581 p_ctx->psz_message = psz_tmp;
585 if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
588 free( p_ctx->psz_message );
589 p_ctx->psz_message = psz_new;
594 const char* msg_StackMsg( void )
596 msg_context_t *p_ctx = GetContext();
598 return p_ctx->psz_message;