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 vlc_threadvar_t msg_context;
65 static uintptr_t banks = 0;
67 /*****************************************************************************
69 *****************************************************************************/
70 #if defined(HAVE_VA_COPY)
71 # define vlc_va_copy(dest,src) va_copy(dest,src)
72 #elif defined(HAVE___VA_COPY)
73 # define vlc_va_copy(dest,src) __va_copy(dest,src)
75 # define vlc_va_copy(dest,src) (dest)=(src)
78 #define QUEUE priv->msg_bank
79 static inline msg_bank_t *libvlc_bank (libvlc_int_t *inst)
81 return &(libvlc_priv (inst))->msg_bank;
84 /*****************************************************************************
86 *****************************************************************************/
87 static void QueueMsg ( vlc_object_t *, int, const char *,
88 const char *, va_list );
89 static void PrintMsg ( vlc_object_t *, msg_item_t * );
91 static vlc_mutex_t msg_stack_lock = VLC_STATIC_MUTEX;
94 * Initialize messages queues
95 * This function initializes all message queues
97 void msg_Create (libvlc_int_t *p_libvlc)
99 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
100 msg_bank_t *bank = libvlc_bank (p_libvlc);
102 vlc_mutex_init (&bank->lock);
103 vlc_cond_init (&bank->wait);
104 vlc_dictionary_init( &priv->msg_enabled_objects, 0 );
105 priv->msg_all_objects_enabled = true;
112 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
113 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
114 CREATE_ALWAYS, 0, NULL );
115 SetFilePointer( QUEUE.logfile, 0, NULL, FILE_END );
118 vlc_mutex_lock( &msg_stack_lock );
120 vlc_threadvar_create( &msg_context, NULL );
121 vlc_mutex_unlock( &msg_stack_lock );
125 * Object Printing selection
127 static void const * kObjectPrintingEnabled = &kObjectPrintingEnabled;
128 static void const * kObjectPrintingDisabled = &kObjectPrintingDisabled;
130 void __msg_EnableObjectPrinting (vlc_object_t *p_this, char * psz_object)
132 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
133 vlc_mutex_lock( &QUEUE.lock );
134 if( !strcmp(psz_object, "all") )
135 priv->msg_all_objects_enabled = true;
137 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingEnabled );
138 vlc_mutex_unlock( &QUEUE.lock );
141 void __msg_DisableObjectPrinting (vlc_object_t *p_this, char * psz_object)
143 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
144 vlc_mutex_lock( &QUEUE.lock );
145 if( !strcmp(psz_object, "all") )
146 priv->msg_all_objects_enabled = false;
148 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingDisabled );
149 vlc_mutex_unlock( &QUEUE.lock );
153 * Destroy the message queues
155 * This functions prints all messages remaining in the queues,
156 * then frees all the allocated ressources
157 * No other messages interface functions should be called after this one.
159 void msg_Destroy (libvlc_int_t *p_libvlc)
161 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
162 msg_bank_t *bank = libvlc_bank (p_libvlc);
165 msg_Err( p_libvlc, "stale interface subscribers (VLC might crash)" );
167 vlc_mutex_lock( &msg_stack_lock );
169 vlc_threadvar_delete( &msg_context );
170 vlc_mutex_unlock( &msg_stack_lock );
173 CloseHandle( QUEUE.logfile );
176 vlc_dictionary_clear( &priv->msg_enabled_objects, NULL, NULL );
178 vlc_cond_destroy (&bank->wait);
179 vlc_mutex_destroy (&bank->lock);
182 struct msg_subscription_t
185 libvlc_int_t *instance;
187 msg_cb_data_t *opaque;
188 msg_item_t *items[VLC_MSG_QSIZE];
193 static void *msg_thread (void *data)
195 msg_subscription_t *sub = data;
196 msg_bank_t *bank = libvlc_bank (sub->instance);
198 vlc_mutex_lock (&bank->lock);
201 /* Wait for messages */
202 assert (sub->begin < VLC_MSG_QSIZE);
203 assert (sub->end < VLC_MSG_QSIZE);
204 while (sub->begin != sub->end)
206 msg_item_t *msg = sub->items[sub->begin];
207 unsigned overruns = sub->overruns;
209 if (++sub->begin == VLC_MSG_QSIZE)
212 vlc_mutex_unlock (&bank->lock);
214 sub->func (sub->opaque, msg, overruns);
217 vlc_mutex_lock (&bank->lock);
220 mutex_cleanup_push (&bank->lock);
221 vlc_cond_wait (&bank->wait, &bank->lock);
228 * Subscribe to the message queue.
229 * Whenever a message is emitted, a callback will be called.
230 * Callback invocation are serialized within a subscription.
232 * @param instance LibVLC instance to get messages from
233 * @param cb callback function
234 * @param opaque data for the callback function
235 * @return a subscription pointer, or NULL in case of failure
237 msg_subscription_t *msg_Subscribe (libvlc_int_t *instance, msg_callback_t cb,
238 msg_cb_data_t *opaque)
240 msg_subscription_t *sub = malloc (sizeof (*sub));
244 sub->instance = instance;
246 sub->opaque = opaque;
247 sub->begin = sub->end = sub->overruns = 0;
249 if (vlc_clone (&sub->thread, msg_thread, sub, VLC_THREAD_PRIORITY_LOW))
255 msg_bank_t *bank = libvlc_bank (instance);
256 vlc_mutex_lock (&bank->lock);
257 TAB_APPEND (bank->i_sub, bank->pp_sub, sub);
258 vlc_mutex_unlock (&bank->lock);
264 * Unsubscribe from the message queue.
265 * This function waits for the message callback to return if needed.
267 void msg_Unsubscribe (msg_subscription_t *sub)
269 msg_bank_t *bank = libvlc_bank (sub->instance);
271 /* TODO: flush support? */
272 vlc_cancel (sub->thread);
273 vlc_mutex_lock (&bank->lock);
274 TAB_REMOVE (bank->i_sub, bank->pp_sub, sub);
275 vlc_mutex_unlock (&bank->lock);
277 vlc_join (sub->thread, NULL);
279 /* Free dangling (not flushed) messages. */
280 /* NOTE: no locking, only this thread can refer to the subscription now. */
281 while (sub->begin != sub->end)
283 msg_Release (sub->items[sub->begin]);
284 if (++sub->begin == VLC_MSG_QSIZE)
290 /*****************************************************************************
291 * __msg_*: print a message
292 *****************************************************************************
293 * These functions queue a message for later printing.
294 *****************************************************************************/
295 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
296 const char *psz_format, ... )
300 va_start( args, psz_format );
301 QueueMsg( p_this, i_type, psz_module, psz_format, args );
305 void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
306 const char *psz_format, va_list args )
308 QueueMsg( p_this, i_type, psz_module, psz_format, args );
312 * Destroys a message.
314 static void msg_Free (gc_object_t *gc)
316 msg_item_t *msg = vlc_priv (gc, msg_item_t);
318 free (msg->psz_module);
320 free (msg->psz_header);
325 * Add a message to a queue
327 * This function provides basic functionnalities to other msg_* functions.
328 * It adds a message to a queue (after having printed all stored messages if it
329 * is full). If the message can't be converted to string in memory, it issues
332 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
333 const char *psz_format, va_list _args )
336 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
337 int i_header_size; /* Size of the additionnal header */
339 char * psz_str = NULL; /* formatted message string */
340 char * psz_header = NULL;
343 if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
344 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
348 /* Expand %m to strerror(errno) - only once */
349 char buf[strlen( psz_format ) + 2001], *ptr;
350 strcpy( buf, psz_format );
352 psz_format = (const char*) buf;
356 ptr = strchr( ptr, '%' );
366 strerror_r( errno, errbuf, 1001 );
368 int sockerr = WSAGetLastError( );
371 strncpy( errbuf, net_strerror( sockerr ), 1001 );
372 WSASetLastError( sockerr );
375 || (strcmp ("Unknown network stack error", errbuf) == 0))
376 strncpy( errbuf, strerror( errno ), 1001 );
380 /* Escape '%' from the error string */
381 for( char *percent = strchr( errbuf, '%' );
383 percent = strchr( percent + 2, '%' ) )
385 memmove( percent + 1, percent, strlen( percent ) + 1 );
388 errlen = strlen( errbuf );
389 memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
390 memcpy( ptr, errbuf, errlen );
391 break; /* Only once, so we don't overflow */
394 /* Looks for conversion specifier... */
397 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
399 ptr++; /* ...and skip it */
403 /* Convert message to string */
404 vlc_va_copy( args, _args );
405 if( vasprintf( &psz_str, psz_format, args ) == -1 )
409 if( psz_str == NULL )
411 int canc = vlc_savecancel (); /* Do not print half of a message... */
413 fprintf( stderr, "main warning: can't store message (%m): " );
417 /* we're not using GLIBC, so we are sure that the error description
418 * will be stored in the buffer we provide to strerror_r() */
419 strerror_r( errno, psz_err, 1001 );
421 strncpy( psz_err, strerror( errno ), 1001 );
423 psz_err[1000] = '\0';
424 fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
426 vlc_va_copy( args, _args );
427 /* We should use utf8_vfprintf - but it calls malloc()... */
428 vfprintf( stderr, psz_format, args );
430 fputs( "\n", stderr );
431 vlc_restorecancel (canc);
435 msg_item_t * p_item = malloc (sizeof (*p_item));
440 vlc_gc_init (p_item, msg_Free);
441 p_item->psz_module = p_item->psz_msg = p_item->psz_header = NULL;
447 while( p_obj != NULL )
449 char *psz_old = NULL;
450 if( p_obj->psz_header )
452 i_header_size += strlen( p_obj->psz_header ) + 4;
455 psz_old = strdup( psz_header );
456 psz_header = (char*)realloc( psz_header, i_header_size );
457 snprintf( psz_header, i_header_size , "[%s] %s",
458 p_obj->psz_header, psz_old );
462 psz_header = (char *)malloc( i_header_size );
463 snprintf( psz_header, i_header_size, "[%s]",
468 p_obj = p_obj->p_parent;
471 /* Fill message information fields */
472 p_item->i_type = i_type;
473 p_item->i_object_id = (uintptr_t)p_this;
474 p_item->psz_object_type = p_this->psz_object_type;
475 p_item->psz_module = strdup( psz_module );
476 p_item->psz_msg = psz_str;
477 p_item->psz_header = psz_header;
479 PrintMsg( p_this, p_item );
481 msg_bank_t *p_queue = &QUEUE;
482 vlc_mutex_lock( &p_queue->lock );
484 for (int i = 0; i < bank->i_sub; i++)
486 msg_subscription_t *sub = bank->pp_sub[i];
487 if ((sub->end + 1 - sub->begin) % VLC_MSG_QSIZE)
489 sub->items[sub->end++] = msg_Hold (p_item);
490 if (sub->end == VLC_MSG_QSIZE)
496 vlc_cond_broadcast (&bank->wait);
497 vlc_mutex_unlock (&bank->lock);
498 msg_Release (p_item);
501 /*****************************************************************************
502 * PrintMsg: output a standard message item to stderr
503 *****************************************************************************
504 * Print a message to stderr, with colour formatting if needed.
505 *****************************************************************************/
506 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
508 # define COL(x) "\033[" #x ";1m"
510 # define GREEN COL(32)
511 # define YELLOW COL(33)
512 # define WHITE COL(0)
513 # define GRAY "\033[0m"
518 static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
519 static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
520 const char *psz_object;
521 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
522 int i_type = p_item->i_type;
527 if( priv->i_verbose < 0 ) return;
530 if( priv->i_verbose < 0 ) return;
533 if( priv->i_verbose < 1 ) return;
536 if( priv->i_verbose < 2 ) return;
540 psz_object = p_item->psz_object_type;
541 void * val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
542 p_item->psz_module );
543 if( val == kObjectPrintingDisabled )
545 if( val == kObjectPrintingEnabled )
549 val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
551 if( val == kObjectPrintingDisabled )
553 if( val == kObjectPrintingEnabled )
555 else if( !priv->msg_all_objects_enabled )
559 int canc = vlc_savecancel ();
561 # define CE_WRITE(str) WriteFile( QUEUE.logfile, \
562 str, strlen(str), &i_dummy, NULL );
563 CE_WRITE( p_item->psz_module );
565 CE_WRITE( psz_object );
566 CE_WRITE( ppsz_type[i_type] );
568 CE_WRITE( p_item->psz_msg );
570 FlushFileBuffers( QUEUE.logfile );
573 /* Send the message to stderr */
574 utf8_fprintf( stderr, "[%s%p%s] %s%s%s %s%s: %s%s%s\n",
575 priv->b_color ? GREEN : "",
576 (void *)p_item->i_object_id,
577 priv->b_color ? GRAY : "",
578 p_item->psz_header ? p_item->psz_header : "",
579 p_item->psz_header ? " " : "",
580 p_item->psz_module, psz_object,
582 priv->b_color ? ppsz_color[i_type] : "",
584 priv->b_color ? GRAY : "" );
589 vlc_restorecancel (canc);
593 static msg_context_t* GetContext(void)
595 msg_context_t *p_ctx = vlc_threadvar_get( &msg_context );
598 p_ctx = malloc( sizeof( msg_context_t ) );
601 p_ctx->psz_message = NULL;
602 vlc_threadvar_set( &msg_context, p_ctx );
607 void msg_StackDestroy (void *data)
609 msg_context_t *p_ctx = data;
611 free (p_ctx->psz_message);
615 void msg_StackSet( int i_code, const char *psz_message, ... )
618 msg_context_t *p_ctx = GetContext();
622 free( p_ctx->psz_message );
624 va_start( ap, psz_message );
625 if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
626 p_ctx->psz_message = NULL;
629 p_ctx->i_code = i_code;
632 void msg_StackAdd( const char *psz_message, ... )
636 msg_context_t *p_ctx = GetContext();
641 va_start( ap, psz_message );
642 if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
646 if( !p_ctx->psz_message )
647 p_ctx->psz_message = psz_tmp;
651 if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
654 free( p_ctx->psz_message );
655 p_ctx->psz_message = psz_new;
660 const char* msg_StackMsg( void )
662 msg_context_t *p_ctx = GetContext();
664 return p_ctx->psz_message;