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>
36 #include <vlc_memory.h>
38 #include <stdarg.h> /* va_list for BSD */
41 # include <fcntl.h> /* O_CREAT, O_TRUNC, O_WRONLY, O_SYNC */
44 #include <errno.h> /* errno */
47 # include <vlc_network.h> /* 'net_strerror' and 'WSAGetLastError' */
51 # include <unistd.h> /* close(), write() */
56 #include <vlc_charset.h>
57 #include "../libvlc.h"
65 static void cleanup_msg_context (void *data)
67 msg_context_t *ctx = data;
68 free (ctx->psz_message);
72 static vlc_threadvar_t msg_context;
73 static uintptr_t banks = 0;
75 /*****************************************************************************
77 *****************************************************************************/
78 #if defined(HAVE_VA_COPY)
79 # define vlc_va_copy(dest,src) va_copy(dest,src)
80 #elif defined(HAVE___VA_COPY)
81 # define vlc_va_copy(dest,src) __va_copy(dest,src)
83 # define vlc_va_copy(dest,src) (dest)=(src)
86 #define QUEUE priv->msg_bank
87 static inline msg_bank_t *libvlc_bank (libvlc_int_t *inst)
89 return &(libvlc_priv (inst))->msg_bank;
92 /*****************************************************************************
94 *****************************************************************************/
95 static void QueueMsg ( vlc_object_t *, int, const char *,
96 const char *, va_list );
97 static void PrintMsg ( vlc_object_t *, msg_item_t * );
99 static vlc_mutex_t msg_stack_lock = VLC_STATIC_MUTEX;
102 * Initialize messages queues
103 * This function initializes all message queues
105 void msg_Create (libvlc_int_t *p_libvlc)
107 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
108 msg_bank_t *bank = libvlc_bank (p_libvlc);
110 vlc_rwlock_init (&bank->lock);
111 vlc_dictionary_init( &priv->msg_enabled_objects, 0 );
112 priv->msg_all_objects_enabled = true;
119 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
120 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
121 CREATE_ALWAYS, 0, NULL );
122 SetFilePointer( QUEUE.logfile, 0, NULL, FILE_END );
125 vlc_mutex_lock( &msg_stack_lock );
127 vlc_threadvar_create( &msg_context, cleanup_msg_context );
128 vlc_mutex_unlock( &msg_stack_lock );
132 * Object Printing selection
134 static void const * kObjectPrintingEnabled = &kObjectPrintingEnabled;
135 static void const * kObjectPrintingDisabled = &kObjectPrintingDisabled;
137 void __msg_EnableObjectPrinting (vlc_object_t *p_this, char * psz_object)
139 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
140 vlc_rwlock_wrlock( &QUEUE.lock );
141 if( !strcmp(psz_object, "all") )
142 priv->msg_all_objects_enabled = true;
144 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingEnabled );
145 vlc_rwlock_unlock( &QUEUE.lock );
148 void __msg_DisableObjectPrinting (vlc_object_t *p_this, char * psz_object)
150 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
151 vlc_rwlock_wrlock( &QUEUE.lock );
152 if( !strcmp(psz_object, "all") )
153 priv->msg_all_objects_enabled = false;
155 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingDisabled );
156 vlc_rwlock_unlock( &QUEUE.lock );
160 * Destroy the message queues
162 * This functions prints all messages remaining in the queues,
163 * then frees all the allocated resources
164 * No other messages interface functions should be called after this one.
166 void msg_Destroy (libvlc_int_t *p_libvlc)
168 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
169 msg_bank_t *bank = libvlc_bank (p_libvlc);
172 msg_Err( p_libvlc, "stale interface subscribers (VLC might crash)" );
174 vlc_mutex_lock( &msg_stack_lock );
177 vlc_threadvar_delete( &msg_context );
178 vlc_mutex_unlock( &msg_stack_lock );
181 CloseHandle( QUEUE.logfile );
184 vlc_dictionary_clear( &priv->msg_enabled_objects, NULL, NULL );
186 vlc_rwlock_destroy (&bank->lock);
189 struct msg_subscription_t
191 libvlc_int_t *instance;
193 msg_cb_data_t *opaque;
197 * Subscribe to the message queue.
198 * Whenever a message is emitted, a callback will be called.
199 * Callback invocation are serialized within a subscription.
201 * @param instance LibVLC instance to get messages from
202 * @param cb callback function
203 * @param opaque data for the callback function
204 * @return a subscription pointer, or NULL in case of failure
206 msg_subscription_t *msg_Subscribe (libvlc_int_t *instance, msg_callback_t cb,
207 msg_cb_data_t *opaque)
209 msg_subscription_t *sub = malloc (sizeof (*sub));
213 sub->instance = instance;
215 sub->opaque = opaque;
217 msg_bank_t *bank = libvlc_bank (instance);
218 vlc_rwlock_wrlock (&bank->lock);
219 TAB_APPEND (bank->i_sub, bank->pp_sub, sub);
220 vlc_rwlock_unlock (&bank->lock);
226 * Unsubscribe from the message queue.
227 * This function waits for the message callback to return if needed.
229 void msg_Unsubscribe (msg_subscription_t *sub)
231 msg_bank_t *bank = libvlc_bank (sub->instance);
233 vlc_rwlock_wrlock (&bank->lock);
234 TAB_REMOVE (bank->i_sub, bank->pp_sub, sub);
235 vlc_rwlock_unlock (&bank->lock);
239 /*****************************************************************************
240 * __msg_*: print a message
241 *****************************************************************************
242 * These functions queue a message for later printing.
243 *****************************************************************************/
244 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
245 const char *psz_format, ... )
249 va_start( args, psz_format );
250 QueueMsg( p_this, i_type, psz_module, psz_format, args );
254 void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
255 const char *psz_format, va_list args )
257 QueueMsg( p_this, i_type, psz_module, psz_format, args );
261 * Destroys a message.
263 static void msg_Free (gc_object_t *gc)
265 msg_item_t *msg = vlc_priv (gc, msg_item_t);
267 free (msg->psz_module);
269 free (msg->psz_header);
274 * Add a message to a queue
276 * This function provides basic functionnalities to other msg_* functions.
277 * It adds a message to a queue (after having printed all stored messages if it
278 * is full). If the message can't be converted to string in memory, it issues
281 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
282 const char *psz_format, va_list _args )
285 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
286 int i_header_size; /* Size of the additionnal header */
288 char * psz_str = NULL; /* formatted message string */
289 char * psz_header = NULL;
292 if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
293 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
297 /* Expand %m to strerror(errno) - only once */
298 char buf[strlen( psz_format ) + 2001], *ptr;
299 strcpy( buf, psz_format );
301 psz_format = (const char*) buf;
305 ptr = strchr( ptr, '%' );
315 strerror_r( errno, errbuf, 1001 );
317 int sockerr = WSAGetLastError( );
320 strncpy( errbuf, net_strerror( sockerr ), 1001 );
321 WSASetLastError( sockerr );
324 || (strcmp ("Unknown network stack error", errbuf) == 0))
325 strncpy( errbuf, strerror( errno ), 1001 );
329 /* Escape '%' from the error string */
330 for( char *percent = strchr( errbuf, '%' );
332 percent = strchr( percent + 2, '%' ) )
334 memmove( percent + 1, percent, strlen( percent ) + 1 );
337 errlen = strlen( errbuf );
338 memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
339 memcpy( ptr, errbuf, errlen );
340 break; /* Only once, so we don't overflow */
343 /* Looks for conversion specifier... */
346 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
348 ptr++; /* ...and skip it */
352 /* Convert message to string */
353 vlc_va_copy( args, _args );
354 if( vasprintf( &psz_str, psz_format, args ) == -1 )
358 if( psz_str == NULL )
360 int canc = vlc_savecancel (); /* Do not print half of a message... */
362 fprintf( stderr, "main warning: can't store message (%m): " );
366 /* we're not using GLIBC, so we are sure that the error description
367 * will be stored in the buffer we provide to strerror_r() */
368 strerror_r( errno, psz_err, 1001 );
370 strncpy( psz_err, strerror( errno ), 1001 );
372 psz_err[1000] = '\0';
373 fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
375 vlc_va_copy( args, _args );
376 /* We should use utf8_vfprintf - but it calls malloc()... */
377 vfprintf( stderr, psz_format, args );
379 fputs( "\n", stderr );
380 vlc_restorecancel (canc);
384 msg_item_t * p_item = malloc (sizeof (*p_item));
388 vlc_gc_init (p_item, msg_Free);
389 p_item->psz_module = p_item->psz_msg = p_item->psz_header = NULL;
395 while( p_obj != NULL )
397 char *psz_old = NULL;
398 if( p_obj->psz_header )
400 i_header_size += strlen( p_obj->psz_header ) + 4;
403 psz_old = strdup( psz_header );
404 psz_header = realloc_or_free( psz_header, i_header_size );
405 assert( psz_header );
406 snprintf( psz_header, i_header_size , "[%s] %s",
407 p_obj->psz_header, psz_old );
411 psz_header = malloc( i_header_size );
412 assert( psz_header );
413 snprintf( psz_header, i_header_size, "[%s]",
418 p_obj = p_obj->p_parent;
421 /* Fill message information fields */
422 p_item->i_type = i_type;
423 p_item->i_object_id = (uintptr_t)p_this;
424 p_item->psz_object_type = p_this->psz_object_type;
425 p_item->psz_module = strdup( psz_module );
426 p_item->psz_msg = psz_str;
427 p_item->psz_header = psz_header;
429 PrintMsg( p_this, p_item );
431 msg_bank_t *bank = &QUEUE;
432 vlc_rwlock_rdlock (&bank->lock);
433 for (int i = 0; i < bank->i_sub; i++)
435 msg_subscription_t *sub = bank->pp_sub[i];
436 sub->func (sub->opaque, p_item, 0);
438 vlc_rwlock_unlock (&bank->lock);
439 msg_Release (p_item);
442 /*****************************************************************************
443 * PrintMsg: output a standard message item to stderr
444 *****************************************************************************
445 * Print a message to stderr, with colour formatting if needed.
446 *****************************************************************************/
447 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
449 # define COL(x) "\033[" #x ";1m"
451 # define GREEN COL(32)
452 # define YELLOW COL(33)
453 # define WHITE COL(0)
454 # define GRAY "\033[0m"
459 static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
460 static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
461 const char *psz_object;
462 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
463 int i_type = p_item->i_type;
468 if( priv->i_verbose < 0 ) return;
471 if( priv->i_verbose < 0 ) return;
474 if( priv->i_verbose < 1 ) return;
477 if( priv->i_verbose < 2 ) return;
481 psz_object = p_item->psz_object_type;
482 void * val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
483 p_item->psz_module );
484 if( val == kObjectPrintingDisabled )
486 if( val == kObjectPrintingEnabled )
490 val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
492 if( val == kObjectPrintingDisabled )
494 if( val == kObjectPrintingEnabled )
496 else if( !priv->msg_all_objects_enabled )
501 # define CE_WRITE(str) WriteFile( QUEUE.logfile, \
502 str, strlen(str), &i_dummy, NULL );
503 CE_WRITE( p_item->psz_module );
505 CE_WRITE( psz_object );
506 CE_WRITE( ppsz_type[i_type] );
508 CE_WRITE( p_item->psz_msg );
510 FlushFileBuffers( QUEUE.logfile );
513 int canc = vlc_savecancel ();
514 /* Send the message to stderr */
515 utf8_fprintf( stderr, "[%s%p%s] %s%s%s %s%s: %s%s%s\n",
516 priv->b_color ? GREEN : "",
517 (void *)p_item->i_object_id,
518 priv->b_color ? GRAY : "",
519 p_item->psz_header ? p_item->psz_header : "",
520 p_item->psz_header ? " " : "",
521 p_item->psz_module, psz_object,
523 priv->b_color ? ppsz_color[i_type] : "",
525 priv->b_color ? GRAY : "" );
530 vlc_restorecancel (canc);
534 static msg_context_t* GetContext(void)
536 msg_context_t *p_ctx = vlc_threadvar_get( msg_context );
539 p_ctx = malloc( sizeof( msg_context_t ) );
542 p_ctx->psz_message = NULL;
543 vlc_threadvar_set( msg_context, p_ctx );
548 void msg_StackDestroy (void *data)
550 msg_context_t *p_ctx = data;
552 free (p_ctx->psz_message);
556 void msg_StackSet( int i_code, const char *psz_message, ... )
559 msg_context_t *p_ctx = GetContext();
563 free( p_ctx->psz_message );
565 va_start( ap, psz_message );
566 if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
567 p_ctx->psz_message = NULL;
570 p_ctx->i_code = i_code;
573 void msg_StackAdd( const char *psz_message, ... )
577 msg_context_t *p_ctx = GetContext();
582 va_start( ap, psz_message );
583 if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
587 if( !p_ctx->psz_message )
588 p_ctx->psz_message = psz_tmp;
592 if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
595 free( p_ctx->psz_message );
596 p_ctx->psz_message = psz_new;
601 const char* msg_StackMsg( void )
603 msg_context_t *p_ctx = GetContext();
605 return p_ctx->psz_message;