]> git.sesse.net Git - vlc/blob - src/misc/messages.c
change warning color in console
[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 #ifdef __APPLE__
39 # include <xlocale.h>
40 #elif defined(HAVE_LOCALE_H)
41 # include <locale.h>
42 #endif
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 static inline msg_bank_t *libvlc_bank (libvlc_int_t *inst)
86 {
87     return (libvlc_priv (inst))->msg_bank;
88 }
89
90 /*****************************************************************************
91  * Local prototypes
92  *****************************************************************************/
93 static void PrintMsg ( vlc_object_t *, msg_item_t * );
94
95 static vlc_mutex_t msg_stack_lock = VLC_STATIC_MUTEX;
96
97 /**
98  * Store all data required by messages interfaces.
99  */
100 struct msg_bank_t
101 {
102     /** Message queue lock */
103     vlc_rwlock_t lock;
104
105     /* Subscribers */
106     int i_sub;
107     msg_subscription_t **pp_sub;
108
109     locale_t locale; /**< C locale for error messages */
110     vlc_dictionary_t enabled_objects; ///< Enabled objects
111     bool all_objects_enabled; ///< Should we print all objects?
112 };
113
114 /**
115  * Initialize messages queues
116  * This function initializes all message queues
117  */
118 msg_bank_t *msg_Create (void)
119 {
120     msg_bank_t *bank = malloc (sizeof (*bank));
121
122     vlc_rwlock_init (&bank->lock);
123     vlc_dictionary_init (&bank->enabled_objects, 0);
124     bank->all_objects_enabled = true;
125
126     bank->i_sub = 0;
127     bank->pp_sub = NULL;
128
129     /* C locale to get error messages in English in the logs */
130     bank->locale = newlocale (LC_MESSAGES_MASK, "C", (locale_t)0);
131
132     vlc_mutex_lock( &msg_stack_lock );
133     if( banks++ == 0 )
134         vlc_threadvar_create( &msg_context, cleanup_msg_context );
135     vlc_mutex_unlock( &msg_stack_lock );
136     return bank;
137 }
138
139 /**
140  * Object Printing selection
141  */
142 static void const * kObjectPrintingEnabled = &kObjectPrintingEnabled;
143 static void const * kObjectPrintingDisabled = &kObjectPrintingDisabled;
144
145 #undef msg_EnableObjectPrinting
146 void msg_EnableObjectPrinting (vlc_object_t *obj, const char * psz_object)
147 {
148     msg_bank_t *bank = libvlc_bank (obj->p_libvlc);
149
150     vlc_rwlock_wrlock (&bank->lock);
151     if( !strcmp(psz_object, "all") )
152         bank->all_objects_enabled = true;
153     else
154         vlc_dictionary_insert (&bank->enabled_objects, psz_object,
155                                (void *)kObjectPrintingEnabled);
156     vlc_rwlock_unlock (&bank->lock);
157 }
158
159 #undef msg_DisableObjectPrinting
160 void msg_DisableObjectPrinting (vlc_object_t *obj, const char * psz_object)
161 {
162     msg_bank_t *bank = libvlc_bank (obj->p_libvlc);
163
164     vlc_rwlock_wrlock (&bank->lock);
165     if( !strcmp(psz_object, "all") )
166         bank->all_objects_enabled = false;
167     else
168         vlc_dictionary_insert (&bank->enabled_objects, psz_object,
169                                (void *)kObjectPrintingDisabled);
170     vlc_rwlock_unlock (&bank->lock);
171 }
172
173 /**
174  * Destroy the message queues
175  *
176  * This functions prints all messages remaining in the queues,
177  * then frees all the allocated resources
178  * No other messages interface functions should be called after this one.
179  */
180 void msg_Destroy (msg_bank_t *bank)
181 {
182     if (unlikely(bank->i_sub != 0))
183         fputs ("stale interface subscribers (LibVLC might crash)\n", stderr);
184
185     vlc_mutex_lock( &msg_stack_lock );
186     assert(banks > 0);
187     if( --banks == 0 )
188         vlc_threadvar_delete( &msg_context );
189     vlc_mutex_unlock( &msg_stack_lock );
190
191     if (bank->locale != (locale_t)0)
192        freelocale (bank->locale);
193
194     vlc_dictionary_clear (&bank->enabled_objects, NULL, NULL);
195
196     vlc_rwlock_destroy (&bank->lock);
197     free (bank);
198 }
199
200 struct msg_subscription_t
201 {
202     libvlc_int_t   *instance;
203     msg_callback_t  func;
204     msg_cb_data_t  *opaque;
205 };
206
207 /**
208  * Subscribe to the message queue.
209  * Whenever a message is emitted, a callback will be called.
210  * Callback invocation are serialized within a subscription.
211  *
212  * @param instance LibVLC instance to get messages from
213  * @param cb callback function
214  * @param opaque data for the callback function
215  * @return a subscription pointer, or NULL in case of failure
216  */
217 msg_subscription_t *msg_Subscribe (libvlc_int_t *instance, msg_callback_t cb,
218                                    msg_cb_data_t *opaque)
219 {
220     msg_subscription_t *sub = malloc (sizeof (*sub));
221     if (sub == NULL)
222         return NULL;
223
224     sub->instance = instance;
225     sub->func = cb;
226     sub->opaque = opaque;
227
228     msg_bank_t *bank = libvlc_bank (instance);
229     vlc_rwlock_wrlock (&bank->lock);
230     TAB_APPEND (bank->i_sub, bank->pp_sub, sub);
231     vlc_rwlock_unlock (&bank->lock);
232
233     return sub;
234 }
235
236 /**
237  * Unsubscribe from the message queue.
238  * This function waits for the message callback to return if needed.
239  */
240 void msg_Unsubscribe (msg_subscription_t *sub)
241 {
242     msg_bank_t *bank = libvlc_bank (sub->instance);
243
244     vlc_rwlock_wrlock (&bank->lock);
245     TAB_REMOVE (bank->i_sub, bank->pp_sub, sub);
246     vlc_rwlock_unlock (&bank->lock);
247     free (sub);
248 }
249
250 /*****************************************************************************
251  * msg_*: print a message
252  *****************************************************************************
253  * These functions queue a message for later printing.
254  *****************************************************************************/
255 void msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
256                     const char *psz_format, ... )
257 {
258     va_list args;
259
260     va_start( args, psz_format );
261     msg_GenericVa (p_this, i_type, psz_module, psz_format, args);
262     va_end( args );
263 }
264
265 /**
266  * Destroys a message.
267  */
268 static void msg_Free (gc_object_t *gc)
269 {
270     msg_item_t *msg = vlc_priv (gc, msg_item_t);
271
272     free (msg->psz_module);
273     free (msg->psz_msg);
274     free (msg->psz_header);
275     free (msg);
276 }
277
278 #undef msg_GenericVa
279 /**
280  * Add a message to a queue
281  *
282  * This function provides basic functionnalities to other msg_* functions.
283  * It adds a message to a queue (after having printed all stored messages if it
284  * is full). If the message can't be converted to string in memory, it issues
285  * a warning.
286  */
287 void msg_GenericVa (vlc_object_t *p_this, int i_type,
288                            const char *psz_module,
289                            const char *psz_format, va_list _args)
290 {
291     size_t      i_header_size;             /* Size of the additionnal header */
292     vlc_object_t *p_obj;
293     char *       psz_str = NULL;                 /* formatted message string */
294     char *       psz_header = NULL;
295     va_list      args;
296
297     assert (p_this);
298
299     if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
300         (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
301         return;
302
303     msg_bank_t *bank = libvlc_bank (p_this->p_libvlc);
304     locale_t locale = uselocale (bank->locale);
305
306 #ifndef __GLIBC__
307     /* Expand %m to strerror(errno) - only once */
308     char buf[strlen( psz_format ) + 2001], *ptr;
309     strcpy( buf, psz_format );
310     ptr = (char*)buf;
311     psz_format = (const char*) buf;
312
313     for( ;; )
314     {
315         ptr = strchr( ptr, '%' );
316         if( ptr == NULL )
317             break;
318
319         if( ptr[1] == 'm' )
320         {
321             char errbuf[2001];
322             size_t errlen;
323
324 #ifndef WIN32
325             strerror_r( errno, errbuf, 1001 );
326 #else
327             int sockerr = WSAGetLastError( );
328             if( sockerr )
329             {
330                 strncpy( errbuf, net_strerror( sockerr ), 1001 );
331                 WSASetLastError( sockerr );
332             }
333             if ((sockerr == 0)
334              || (strcmp ("Unknown network stack error", errbuf) == 0))
335                 strncpy( errbuf, strerror( errno ), 1001 );
336 #endif
337             errbuf[1000] = 0;
338
339             /* Escape '%' from the error string */
340             for( char *percent = strchr( errbuf, '%' );
341                  percent != NULL;
342                  percent = strchr( percent + 2, '%' ) )
343             {
344                 memmove( percent + 1, percent, strlen( percent ) + 1 );
345             }
346
347             errlen = strlen( errbuf );
348             memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
349             memcpy( ptr, errbuf, errlen );
350             break; /* Only once, so we don't overflow */
351         }
352
353         /* Looks for conversion specifier... */
354         do
355             ptr++;
356         while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
357         if( *ptr )
358             ptr++; /* ...and skip it */
359     }
360 #endif
361
362     /* Convert message to string  */
363     vlc_va_copy( args, _args );
364     if( vasprintf( &psz_str, psz_format, args ) == -1 )
365         psz_str = NULL;
366     va_end( args );
367
368     if( psz_str == NULL )
369     {
370         int canc = vlc_savecancel (); /* Do not print half of a message... */
371 #ifdef __GLIBC__
372         fprintf( stderr, "main warning: can't store message (%m): " );
373 #else
374         char psz_err[1001];
375 #ifndef WIN32
376         /* we're not using GLIBC, so we are sure that the error description
377          * will be stored in the buffer we provide to strerror_r() */
378         strerror_r( errno, psz_err, 1001 );
379 #else
380         strncpy( psz_err, strerror( errno ), 1001 );
381 #endif
382         psz_err[1000] = '\0';
383         fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
384 #endif
385         vlc_va_copy( args, _args );
386         /* We should use utf8_vfprintf - but it calls malloc()... */
387         vfprintf( stderr, psz_format, args );
388         va_end( args );
389         fputs( "\n", stderr );
390         vlc_restorecancel (canc);
391         uselocale (locale);
392         return;
393     }
394     uselocale (locale);
395
396     msg_item_t * p_item = malloc (sizeof (*p_item));
397     if (p_item == NULL)
398         return; /* Uho! */
399
400     vlc_gc_init (p_item, msg_Free);
401     p_item->psz_module = p_item->psz_msg = p_item->psz_header = NULL;
402
403
404
405     i_header_size = 0;
406     p_obj = p_this;
407     while( p_obj != NULL )
408     {
409         char *psz_old = NULL;
410         if( p_obj->psz_header )
411         {
412             i_header_size += strlen( p_obj->psz_header ) + 4;
413             if( psz_header )
414             {
415                 psz_old = strdup( psz_header );
416                 psz_header = xrealloc( psz_header, i_header_size );
417                 snprintf( psz_header, i_header_size , "[%s] %s",
418                           p_obj->psz_header, psz_old );
419             }
420             else
421             {
422                 psz_header = xmalloc( i_header_size );
423                 snprintf( psz_header, i_header_size, "[%s]",
424                           p_obj->psz_header );
425             }
426         }
427         free( psz_old );
428         p_obj = p_obj->p_parent;
429     }
430
431     /* Fill message information fields */
432     p_item->i_type =        i_type;
433     p_item->i_object_id =   (uintptr_t)p_this;
434     p_item->psz_object_type = p_this->psz_object_type;
435     p_item->psz_module =    strdup( psz_module );
436     p_item->psz_msg =       psz_str;
437     p_item->psz_header =    psz_header;
438
439     PrintMsg( p_this, p_item );
440
441     vlc_rwlock_rdlock (&bank->lock);
442     for (int i = 0; i < bank->i_sub; i++)
443     {
444         msg_subscription_t *sub = bank->pp_sub[i];
445         sub->func (sub->opaque, p_item, 0);
446     }
447     vlc_rwlock_unlock (&bank->lock);
448     msg_Release (p_item);
449 }
450
451 /*****************************************************************************
452  * PrintMsg: output a standard message item to stderr
453  *****************************************************************************
454  * Print a message to stderr, with colour formatting if needed.
455  *****************************************************************************/
456 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
457 {
458 #   define COL(x,y)  "\033[" #x ";" #y "m"
459 #   define RED     COL(31,1)
460 #   define GREEN   COL(32,1)
461 #   define YELLOW  COL(0,33)
462 #   define WHITE   COL(0,1)
463 #   define GRAY    "\033[0m"
464
465     static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
466     static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
467     const char *psz_object;
468     libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
469     msg_bank_t *bank = priv->msg_bank;
470     int i_type = p_item->i_type;
471
472     switch( i_type )
473     {
474         case VLC_MSG_ERR:
475             if( priv->i_verbose < 0 ) return;
476             break;
477         case VLC_MSG_INFO:
478             if( priv->i_verbose < 0 ) return;
479             break;
480         case VLC_MSG_WARN:
481             if( priv->i_verbose < 1 ) return;
482             break;
483         case VLC_MSG_DBG:
484             if( priv->i_verbose < 2 ) return;
485             break;
486     }
487
488     psz_object = p_item->psz_object_type;
489     void * val = vlc_dictionary_value_for_key (&bank->enabled_objects,
490                                                p_item->psz_module);
491     if( val == kObjectPrintingDisabled )
492         return;
493     if( val == kObjectPrintingEnabled )
494         /* Allowed */;
495     else
496     {
497         val = vlc_dictionary_value_for_key (&bank->enabled_objects,
498                                             psz_object);
499         if( val == kObjectPrintingDisabled )
500             return;
501         if( val == kObjectPrintingEnabled )
502             /* Allowed */;
503         else if( !bank->all_objects_enabled )
504             return;
505     }
506
507     int canc = vlc_savecancel ();
508     /* Send the message to stderr */
509     utf8_fprintf( stderr, "[%s%p%s] %s%s%s %s%s: %s%s%s\n",
510                   priv->b_color ? GREEN : "",
511                   (void *)p_item->i_object_id,
512                   priv->b_color ? GRAY : "",
513                   p_item->psz_header ? p_item->psz_header : "",
514                   p_item->psz_header ? " " : "",
515                   p_item->psz_module, psz_object,
516                   ppsz_type[i_type],
517                   priv->b_color ? ppsz_color[i_type] : "",
518                   p_item->psz_msg,
519                   priv->b_color ? GRAY : "" );
520
521 #ifdef WIN32
522     fflush( stderr );
523 #endif
524     vlc_restorecancel (canc);
525 }
526
527 static msg_context_t* GetContext(void)
528 {
529     msg_context_t *p_ctx = vlc_threadvar_get( msg_context );
530     if( p_ctx == NULL )
531     {
532         p_ctx = malloc( sizeof( msg_context_t ) );
533         if( !p_ctx )
534             return NULL;
535         p_ctx->psz_message = NULL;
536         vlc_threadvar_set( msg_context, p_ctx );
537     }
538     return p_ctx;
539 }
540
541 void msg_StackSet( int i_code, const char *psz_message, ... )
542 {
543     va_list ap;
544     msg_context_t *p_ctx = GetContext();
545
546     if( p_ctx == NULL )
547         return;
548     free( p_ctx->psz_message );
549
550     va_start( ap, psz_message );
551     if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
552         p_ctx->psz_message = NULL;
553     va_end( ap );
554
555     p_ctx->i_code = i_code;
556 }
557
558 void msg_StackAdd( const char *psz_message, ... )
559 {
560     char *psz_tmp;
561     va_list ap;
562     msg_context_t *p_ctx = GetContext();
563
564     if( p_ctx == NULL )
565         return;
566
567     va_start( ap, psz_message );
568     if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
569         psz_tmp = NULL;
570     va_end( ap );
571
572     if( !p_ctx->psz_message )
573         p_ctx->psz_message = psz_tmp;
574     else
575     {
576         char *psz_new;
577         if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
578             psz_new = NULL;
579
580         free( p_ctx->psz_message );
581         p_ctx->psz_message = psz_new;
582         free( psz_tmp );
583     }
584 }
585
586 const char* msg_StackMsg( void )
587 {
588     msg_context_t *p_ctx = GetContext();
589     assert( p_ctx );
590     return p_ctx->psz_message;
591 }