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_mutex_init (&bank->lock);
110 vlc_cond_init (&bank->wait);
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_mutex_lock( &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_mutex_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_mutex_lock( &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_mutex_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_cond_destroy (&bank->wait);
187 vlc_mutex_destroy (&bank->lock);
190 struct msg_subscription_t
193 libvlc_int_t *instance;
195 msg_cb_data_t *opaque;
196 msg_item_t *items[VLC_MSG_QSIZE];
201 static void *msg_thread (void *data)
203 msg_subscription_t *sub = data;
204 msg_bank_t *bank = libvlc_bank (sub->instance);
206 vlc_mutex_lock (&bank->lock);
209 /* Wait for messages */
210 assert (sub->begin < VLC_MSG_QSIZE);
211 assert (sub->end < VLC_MSG_QSIZE);
212 while (sub->begin != sub->end)
214 msg_item_t *msg = sub->items[sub->begin];
215 unsigned overruns = sub->overruns;
217 if (++sub->begin == VLC_MSG_QSIZE)
220 vlc_mutex_unlock (&bank->lock);
222 sub->func (sub->opaque, msg, overruns);
225 vlc_mutex_lock (&bank->lock);
228 mutex_cleanup_push (&bank->lock);
229 vlc_cond_wait (&bank->wait, &bank->lock);
236 * Subscribe to the message queue.
237 * Whenever a message is emitted, a callback will be called.
238 * Callback invocation are serialized within a subscription.
240 * @param instance LibVLC instance to get messages from
241 * @param cb callback function
242 * @param opaque data for the callback function
243 * @return a subscription pointer, or NULL in case of failure
245 msg_subscription_t *msg_Subscribe (libvlc_int_t *instance, msg_callback_t cb,
246 msg_cb_data_t *opaque)
248 msg_subscription_t *sub = malloc (sizeof (*sub));
252 sub->instance = instance;
254 sub->opaque = opaque;
255 sub->begin = sub->end = sub->overruns = 0;
257 if (vlc_clone (&sub->thread, msg_thread, sub, VLC_THREAD_PRIORITY_LOW))
263 msg_bank_t *bank = libvlc_bank (instance);
264 vlc_mutex_lock (&bank->lock);
265 TAB_APPEND (bank->i_sub, bank->pp_sub, sub);
266 vlc_mutex_unlock (&bank->lock);
272 * Unsubscribe from the message queue.
273 * This function waits for the message callback to return if needed.
275 void msg_Unsubscribe (msg_subscription_t *sub)
277 msg_bank_t *bank = libvlc_bank (sub->instance);
279 /* TODO: flush support? */
280 vlc_cancel (sub->thread);
281 vlc_mutex_lock (&bank->lock);
282 TAB_REMOVE (bank->i_sub, bank->pp_sub, sub);
283 vlc_mutex_unlock (&bank->lock);
285 vlc_join (sub->thread, NULL);
287 /* Free dangling (not flushed) messages. */
288 /* NOTE: no locking, only this thread can refer to the subscription now. */
289 while (sub->begin != sub->end)
291 msg_Release (sub->items[sub->begin]);
292 if (++sub->begin == VLC_MSG_QSIZE)
298 /*****************************************************************************
299 * __msg_*: print a message
300 *****************************************************************************
301 * These functions queue a message for later printing.
302 *****************************************************************************/
303 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
304 const char *psz_format, ... )
308 va_start( args, psz_format );
309 QueueMsg( p_this, i_type, psz_module, psz_format, args );
313 void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
314 const char *psz_format, va_list args )
316 QueueMsg( p_this, i_type, psz_module, psz_format, args );
320 * Destroys a message.
322 static void msg_Free (gc_object_t *gc)
324 msg_item_t *msg = vlc_priv (gc, msg_item_t);
326 free (msg->psz_module);
328 free (msg->psz_header);
333 * Add a message to a queue
335 * This function provides basic functionnalities to other msg_* functions.
336 * It adds a message to a queue (after having printed all stored messages if it
337 * is full). If the message can't be converted to string in memory, it issues
340 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
341 const char *psz_format, va_list _args )
344 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
345 int i_header_size; /* Size of the additionnal header */
347 char * psz_str = NULL; /* formatted message string */
348 char * psz_header = NULL;
351 if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
352 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
356 /* Expand %m to strerror(errno) - only once */
357 char buf[strlen( psz_format ) + 2001], *ptr;
358 strcpy( buf, psz_format );
360 psz_format = (const char*) buf;
364 ptr = strchr( ptr, '%' );
374 strerror_r( errno, errbuf, 1001 );
376 int sockerr = WSAGetLastError( );
379 strncpy( errbuf, net_strerror( sockerr ), 1001 );
380 WSASetLastError( sockerr );
383 || (strcmp ("Unknown network stack error", errbuf) == 0))
384 strncpy( errbuf, strerror( errno ), 1001 );
388 /* Escape '%' from the error string */
389 for( char *percent = strchr( errbuf, '%' );
391 percent = strchr( percent + 2, '%' ) )
393 memmove( percent + 1, percent, strlen( percent ) + 1 );
396 errlen = strlen( errbuf );
397 memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
398 memcpy( ptr, errbuf, errlen );
399 break; /* Only once, so we don't overflow */
402 /* Looks for conversion specifier... */
405 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
407 ptr++; /* ...and skip it */
411 /* Convert message to string */
412 vlc_va_copy( args, _args );
413 if( vasprintf( &psz_str, psz_format, args ) == -1 )
417 if( psz_str == NULL )
419 int canc = vlc_savecancel (); /* Do not print half of a message... */
421 fprintf( stderr, "main warning: can't store message (%m): " );
425 /* we're not using GLIBC, so we are sure that the error description
426 * will be stored in the buffer we provide to strerror_r() */
427 strerror_r( errno, psz_err, 1001 );
429 strncpy( psz_err, strerror( errno ), 1001 );
431 psz_err[1000] = '\0';
432 fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
434 vlc_va_copy( args, _args );
435 /* We should use utf8_vfprintf - but it calls malloc()... */
436 vfprintf( stderr, psz_format, args );
438 fputs( "\n", stderr );
439 vlc_restorecancel (canc);
443 msg_item_t * p_item = malloc (sizeof (*p_item));
448 vlc_gc_init (p_item, msg_Free);
449 p_item->psz_module = p_item->psz_msg = p_item->psz_header = NULL;
455 while( p_obj != NULL )
457 char *psz_old = NULL;
458 if( p_obj->psz_header )
460 i_header_size += strlen( p_obj->psz_header ) + 4;
463 psz_old = strdup( psz_header );
464 psz_header = (char*)realloc( psz_header, i_header_size );
465 snprintf( psz_header, i_header_size , "[%s] %s",
466 p_obj->psz_header, psz_old );
470 psz_header = (char *)malloc( i_header_size );
471 snprintf( psz_header, i_header_size, "[%s]",
476 p_obj = p_obj->p_parent;
479 /* Fill message information fields */
480 p_item->i_type = i_type;
481 p_item->i_object_id = (uintptr_t)p_this;
482 p_item->psz_object_type = p_this->psz_object_type;
483 p_item->psz_module = strdup( psz_module );
484 p_item->psz_msg = psz_str;
485 p_item->psz_header = psz_header;
487 PrintMsg( p_this, p_item );
489 msg_bank_t *p_queue = &QUEUE;
490 vlc_mutex_lock( &p_queue->lock );
492 for (int i = 0; i < bank->i_sub; i++)
494 msg_subscription_t *sub = bank->pp_sub[i];
495 if ((sub->end + 1 - sub->begin) % VLC_MSG_QSIZE)
497 sub->items[sub->end++] = msg_Hold (p_item);
498 if (sub->end == VLC_MSG_QSIZE)
504 vlc_cond_broadcast (&bank->wait);
505 vlc_mutex_unlock (&bank->lock);
506 msg_Release (p_item);
509 /*****************************************************************************
510 * PrintMsg: output a standard message item to stderr
511 *****************************************************************************
512 * Print a message to stderr, with colour formatting if needed.
513 *****************************************************************************/
514 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
516 # define COL(x) "\033[" #x ";1m"
518 # define GREEN COL(32)
519 # define YELLOW COL(33)
520 # define WHITE COL(0)
521 # define GRAY "\033[0m"
526 static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
527 static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
528 const char *psz_object;
529 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
530 int i_type = p_item->i_type;
535 if( priv->i_verbose < 0 ) return;
538 if( priv->i_verbose < 0 ) return;
541 if( priv->i_verbose < 1 ) return;
544 if( priv->i_verbose < 2 ) return;
548 psz_object = p_item->psz_object_type;
549 void * val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
550 p_item->psz_module );
551 if( val == kObjectPrintingDisabled )
553 if( val == kObjectPrintingEnabled )
557 val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
559 if( val == kObjectPrintingDisabled )
561 if( val == kObjectPrintingEnabled )
563 else if( !priv->msg_all_objects_enabled )
568 # define CE_WRITE(str) WriteFile( QUEUE.logfile, \
569 str, strlen(str), &i_dummy, NULL );
570 CE_WRITE( p_item->psz_module );
572 CE_WRITE( psz_object );
573 CE_WRITE( ppsz_type[i_type] );
575 CE_WRITE( p_item->psz_msg );
577 FlushFileBuffers( QUEUE.logfile );
580 int canc = vlc_savecancel ();
581 /* Send the message to stderr */
582 utf8_fprintf( stderr, "[%s%p%s] %s%s%s %s%s: %s%s%s\n",
583 priv->b_color ? GREEN : "",
584 (void *)p_item->i_object_id,
585 priv->b_color ? GRAY : "",
586 p_item->psz_header ? p_item->psz_header : "",
587 p_item->psz_header ? " " : "",
588 p_item->psz_module, psz_object,
590 priv->b_color ? ppsz_color[i_type] : "",
592 priv->b_color ? GRAY : "" );
597 vlc_restorecancel (canc);
601 static msg_context_t* GetContext(void)
603 msg_context_t *p_ctx = vlc_threadvar_get( msg_context );
606 p_ctx = malloc( sizeof( msg_context_t ) );
609 p_ctx->psz_message = NULL;
610 vlc_threadvar_set( msg_context, p_ctx );
615 void msg_StackDestroy (void *data)
617 msg_context_t *p_ctx = data;
619 free (p_ctx->psz_message);
623 void msg_StackSet( int i_code, const char *psz_message, ... )
626 msg_context_t *p_ctx = GetContext();
630 free( p_ctx->psz_message );
632 va_start( ap, psz_message );
633 if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
634 p_ctx->psz_message = NULL;
637 p_ctx->i_code = i_code;
640 void msg_StackAdd( const char *psz_message, ... )
644 msg_context_t *p_ctx = GetContext();
649 va_start( ap, psz_message );
650 if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
654 if( !p_ctx->psz_message )
655 p_ctx->psz_message = psz_tmp;
659 if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
662 free( p_ctx->psz_message );
663 p_ctx->psz_message = psz_new;
668 const char* msg_StackMsg( void )
670 msg_context_t *p_ctx = GetContext();
672 return p_ctx->psz_message;