]> git.sesse.net Git - vlc/blob - src/misc/messages.c
Messages: missing const, fix Qt4 warning
[vlc] / src / misc / messages.c
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
7  * $Id$
8  *
9  * Authors: Vincent Seguin <seguin@via.ecp.fr>
10  *          Samuel Hocevar <sam@zoy.org>
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36
37 #include <stdarg.h>                                       /* va_list for BSD */
38
39 #ifdef HAVE_FCNTL_H
40 #   include <fcntl.h>                  /* O_CREAT, O_TRUNC, O_WRONLY, O_SYNC */
41 #endif
42
43 #include <errno.h>                                                  /* errno */
44
45 #ifdef WIN32
46 #   include <vlc_network.h>          /* 'net_strerror' and 'WSAGetLastError' */
47 #endif
48
49 #ifdef HAVE_UNISTD_H
50 #   include <unistd.h>                                   /* close(), write() */
51 #endif
52
53 #include <assert.h>
54
55 #include <vlc_charset.h>
56 #include "../libvlc.h"
57
58 typedef struct
59 {
60     int i_code;
61     char * psz_message;
62 } msg_context_t;
63
64 static void cleanup_msg_context (void *data)
65 {
66     msg_context_t *ctx = data;
67     free (ctx->psz_message);
68     free (ctx);
69 }
70
71 static vlc_threadvar_t msg_context;
72 static uintptr_t banks = 0;
73
74 /*****************************************************************************
75  * Local macros
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)
81 #else
82 #   define vlc_va_copy(dest,src) (dest)=(src)
83 #endif
84
85 #define QUEUE priv->msg_bank
86 static inline msg_bank_t *libvlc_bank (libvlc_int_t *inst)
87 {
88     return &(libvlc_priv (inst))->msg_bank;
89 }
90
91 /*****************************************************************************
92  * Local prototypes
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 * );
97
98 static vlc_mutex_t msg_stack_lock = VLC_STATIC_MUTEX;
99
100 /**
101  * Initialize messages queues
102  * This function initializes all message queues
103  */
104 void msg_Create (libvlc_int_t *p_libvlc)
105 {
106     libvlc_priv_t *priv = libvlc_priv (p_libvlc);
107     msg_bank_t *bank = libvlc_bank (p_libvlc);
108
109     vlc_rwlock_init (&bank->lock);
110     vlc_dictionary_init( &priv->msg_enabled_objects, 0 );
111     priv->msg_all_objects_enabled = true;
112
113     QUEUE.i_sub = 0;
114     QUEUE.pp_sub = NULL;
115
116 #ifdef UNDER_CE
117     QUEUE.logfile =
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 );
122 #endif
123
124     vlc_mutex_lock( &msg_stack_lock );
125     if( banks++ == 0 )
126         vlc_threadvar_create( &msg_context, cleanup_msg_context );
127     vlc_mutex_unlock( &msg_stack_lock );
128 }
129
130 /**
131  * Object Printing selection
132  */
133 static void const * kObjectPrintingEnabled = &kObjectPrintingEnabled;
134 static void const * kObjectPrintingDisabled = &kObjectPrintingDisabled;
135
136 void __msg_EnableObjectPrinting (vlc_object_t *p_this, const char * psz_object)
137 {
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;
142     else
143         vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingEnabled );
144     vlc_rwlock_unlock( &QUEUE.lock );
145 }
146
147 void __msg_DisableObjectPrinting (vlc_object_t *p_this, const char * psz_object)
148 {
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;
153     else
154         vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingDisabled );
155     vlc_rwlock_unlock( &QUEUE.lock );
156 }
157
158 /**
159  * Destroy the message queues
160  *
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.
164  */
165 void msg_Destroy (libvlc_int_t *p_libvlc)
166 {
167     libvlc_priv_t *priv = libvlc_priv (p_libvlc);
168     msg_bank_t *bank = libvlc_bank (p_libvlc);
169
170     if( QUEUE.i_sub )
171         msg_Err( p_libvlc, "stale interface subscribers (VLC might crash)" );
172
173     vlc_mutex_lock( &msg_stack_lock );
174     assert(banks > 0);
175     if( --banks == 0 )
176         vlc_threadvar_delete( &msg_context );
177     vlc_mutex_unlock( &msg_stack_lock );
178
179 #ifdef UNDER_CE
180     CloseHandle( QUEUE.logfile );
181 #endif
182
183     vlc_dictionary_clear( &priv->msg_enabled_objects, NULL, NULL );
184
185     vlc_rwlock_destroy (&bank->lock);
186 }
187
188 struct msg_subscription_t
189 {
190     libvlc_int_t   *instance;
191     msg_callback_t  func;
192     msg_cb_data_t  *opaque;
193 };
194
195 /**
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.
199  *
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
204  */
205 msg_subscription_t *msg_Subscribe (libvlc_int_t *instance, msg_callback_t cb,
206                                    msg_cb_data_t *opaque)
207 {
208     msg_subscription_t *sub = malloc (sizeof (*sub));
209     if (sub == NULL)
210         return NULL;
211
212     sub->instance = instance;
213     sub->func = cb;
214     sub->opaque = opaque;
215
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);
220
221     return sub;
222 }
223
224 /**
225  * Unsubscribe from the message queue.
226  * This function waits for the message callback to return if needed.
227  */
228 void msg_Unsubscribe (msg_subscription_t *sub)
229 {
230     msg_bank_t *bank = libvlc_bank (sub->instance);
231
232     vlc_rwlock_wrlock (&bank->lock);
233     TAB_REMOVE (bank->i_sub, bank->pp_sub, sub);
234     vlc_rwlock_unlock (&bank->lock);
235     free (sub);
236 }
237
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, ... )
245 {
246     va_list args;
247
248     va_start( args, psz_format );
249     QueueMsg( p_this, i_type, psz_module, psz_format, args );
250     va_end( args );
251 }
252
253 void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
254                       const char *psz_format, va_list args )
255 {
256     QueueMsg( p_this, i_type, psz_module, psz_format, args );
257 }
258
259 /**
260  * Destroys a message.
261  */
262 static void msg_Free (gc_object_t *gc)
263 {
264     msg_item_t *msg = vlc_priv (gc, msg_item_t);
265
266     free (msg->psz_module);
267     free (msg->psz_msg);
268     free (msg->psz_header);
269     free (msg);
270 }
271
272 /**
273  * Add a message to a queue
274  *
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
278  * a warning.
279  */
280 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
281                       const char *psz_format, va_list _args )
282 {
283     assert (p_this);
284     libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
285     int         i_header_size;             /* Size of the additionnal header */
286     vlc_object_t *p_obj;
287     char *       psz_str = NULL;                 /* formatted message string */
288     char *       psz_header = NULL;
289     va_list      args;
290
291     if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
292         (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
293         return;
294
295 #ifndef __GLIBC__
296     /* Expand %m to strerror(errno) - only once */
297     char buf[strlen( psz_format ) + 2001], *ptr;
298     strcpy( buf, psz_format );
299     ptr = (char*)buf;
300     psz_format = (const char*) buf;
301
302     for( ;; )
303     {
304         ptr = strchr( ptr, '%' );
305         if( ptr == NULL )
306             break;
307
308         if( ptr[1] == 'm' )
309         {
310             char errbuf[2001];
311             size_t errlen;
312
313 #ifndef WIN32
314             strerror_r( errno, errbuf, 1001 );
315 #else
316             int sockerr = WSAGetLastError( );
317             if( sockerr )
318             {
319                 strncpy( errbuf, net_strerror( sockerr ), 1001 );
320                 WSASetLastError( sockerr );
321             }
322             if ((sockerr == 0)
323              || (strcmp ("Unknown network stack error", errbuf) == 0))
324                 strncpy( errbuf, strerror( errno ), 1001 );
325 #endif
326             errbuf[1000] = 0;
327
328             /* Escape '%' from the error string */
329             for( char *percent = strchr( errbuf, '%' );
330                  percent != NULL;
331                  percent = strchr( percent + 2, '%' ) )
332             {
333                 memmove( percent + 1, percent, strlen( percent ) + 1 );
334             }
335
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 */
340         }
341
342         /* Looks for conversion specifier... */
343         do
344             ptr++;
345         while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
346         if( *ptr )
347             ptr++; /* ...and skip it */
348     }
349 #endif
350
351     /* Convert message to string  */
352     vlc_va_copy( args, _args );
353     if( vasprintf( &psz_str, psz_format, args ) == -1 )
354         psz_str = NULL;
355     va_end( args );
356
357     if( psz_str == NULL )
358     {
359         int canc = vlc_savecancel (); /* Do not print half of a message... */
360 #ifdef __GLIBC__
361         fprintf( stderr, "main warning: can't store message (%m): " );
362 #else
363         char psz_err[1001];
364 #ifndef WIN32
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 );
368 #else
369         strncpy( psz_err, strerror( errno ), 1001 );
370 #endif
371         psz_err[1000] = '\0';
372         fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
373 #endif
374         vlc_va_copy( args, _args );
375         /* We should use utf8_vfprintf - but it calls malloc()... */
376         vfprintf( stderr, psz_format, args );
377         va_end( args );
378         fputs( "\n", stderr );
379         vlc_restorecancel (canc);
380         return;
381     }
382
383     msg_item_t * p_item = malloc (sizeof (*p_item));
384     if (p_item == NULL)
385         return; /* Uho! */
386
387     vlc_gc_init (p_item, msg_Free);
388     p_item->psz_module = p_item->psz_msg = p_item->psz_header = NULL;
389
390
391
392     i_header_size = 0;
393     p_obj = p_this;
394     while( p_obj != NULL )
395     {
396         char *psz_old = NULL;
397         if( p_obj->psz_header )
398         {
399             i_header_size += strlen( p_obj->psz_header ) + 4;
400             if( psz_header )
401             {
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 );
406             }
407             else
408             {
409                 psz_header = xmalloc( i_header_size );
410                 snprintf( psz_header, i_header_size, "[%s]",
411                           p_obj->psz_header );
412             }
413         }
414         free( psz_old );
415         p_obj = p_obj->p_parent;
416     }
417
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;
425
426     PrintMsg( p_this, p_item );
427
428     msg_bank_t *bank = &QUEUE;
429     vlc_rwlock_rdlock (&bank->lock);
430     for (int i = 0; i < bank->i_sub; i++)
431     {
432         msg_subscription_t *sub = bank->pp_sub[i];
433         sub->func (sub->opaque, p_item, 0);
434     }
435     vlc_rwlock_unlock (&bank->lock);
436     msg_Release (p_item);
437 }
438
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 )
445 {
446 #   define COL(x)  "\033[" #x ";1m"
447 #   define RED     COL(31)
448 #   define GREEN   COL(32)
449 #   define YELLOW  COL(33)
450 #   define WHITE   COL(0)
451 #   define GRAY    "\033[0m"
452
453 #ifdef UNDER_CE
454     int i_dummy;
455 #endif
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;
461
462     switch( i_type )
463     {
464         case VLC_MSG_ERR:
465             if( priv->i_verbose < 0 ) return;
466             break;
467         case VLC_MSG_INFO:
468             if( priv->i_verbose < 0 ) return;
469             break;
470         case VLC_MSG_WARN:
471             if( priv->i_verbose < 1 ) return;
472             break;
473         case VLC_MSG_DBG:
474             if( priv->i_verbose < 2 ) return;
475             break;
476     }
477
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 )
482         return;
483     if( val == kObjectPrintingEnabled )
484         /* Allowed */;
485     else
486     {
487         val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
488                                             psz_object );
489         if( val == kObjectPrintingDisabled )
490             return;
491         if( val == kObjectPrintingEnabled )
492             /* Allowed */;
493         else if( !priv->msg_all_objects_enabled )
494             return;
495     }
496
497 #ifdef UNDER_CE
498 #   define CE_WRITE(str) WriteFile( QUEUE.logfile, \
499                                     str, strlen(str), &i_dummy, NULL );
500     CE_WRITE( p_item->psz_module );
501     CE_WRITE( " " );
502     CE_WRITE( psz_object );
503     CE_WRITE( ppsz_type[i_type] );
504     CE_WRITE( ": " );
505     CE_WRITE( p_item->psz_msg );
506     CE_WRITE( "\r\n" );
507     FlushFileBuffers( QUEUE.logfile );
508
509 #else
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,
519                   ppsz_type[i_type],
520                   priv->b_color ? ppsz_color[i_type] : "",
521                   p_item->psz_msg,
522                   priv->b_color ? GRAY : "" );
523
524 #   if defined(WIN32)
525     fflush( stderr );
526 #   endif
527     vlc_restorecancel (canc);
528 #endif
529 }
530
531 static msg_context_t* GetContext(void)
532 {
533     msg_context_t *p_ctx = vlc_threadvar_get( msg_context );
534     if( p_ctx == NULL )
535     {
536         p_ctx = malloc( sizeof( msg_context_t ) );
537         if( !p_ctx )
538             return NULL;
539         p_ctx->psz_message = NULL;
540         vlc_threadvar_set( msg_context, p_ctx );
541     }
542     return p_ctx;
543 }
544
545 void msg_StackDestroy (void *data)
546 {
547     msg_context_t *p_ctx = data;
548
549     free (p_ctx->psz_message);
550     free (p_ctx);
551 }
552
553 void msg_StackSet( int i_code, const char *psz_message, ... )
554 {
555     va_list ap;
556     msg_context_t *p_ctx = GetContext();
557
558     if( p_ctx == NULL )
559         return;
560     free( p_ctx->psz_message );
561
562     va_start( ap, psz_message );
563     if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
564         p_ctx->psz_message = NULL;
565     va_end( ap );
566
567     p_ctx->i_code = i_code;
568 }
569
570 void msg_StackAdd( const char *psz_message, ... )
571 {
572     char *psz_tmp;
573     va_list ap;
574     msg_context_t *p_ctx = GetContext();
575
576     if( p_ctx == NULL )
577         return;
578
579     va_start( ap, psz_message );
580     if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
581         psz_tmp = NULL;
582     va_end( ap );
583
584     if( !p_ctx->psz_message )
585         p_ctx->psz_message = psz_tmp;
586     else
587     {
588         char *psz_new;
589         if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
590             psz_new = NULL;
591
592         free( p_ctx->psz_message );
593         p_ctx->psz_message = psz_new;
594         free( psz_tmp );
595     }
596 }
597
598 const char* msg_StackMsg( void )
599 {
600     msg_context_t *p_ctx = GetContext();
601     assert( p_ctx );
602     return p_ctx->psz_message;
603 }