]> git.sesse.net Git - vlc/blob - src/misc/messages.c
Add facilities to report and enrich error messages.
[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 #include <vlc/vlc.h>
32
33 #include <stdio.h>                                               /* required */
34 #include <stdarg.h>                                       /* va_list for BSD */
35 #include <stdlib.h>                                              /* malloc() */
36 #include <string.h>                                            /* strerror() */
37
38 #ifdef HAVE_FCNTL_H
39 #   include <fcntl.h>                  /* O_CREAT, O_TRUNC, O_WRONLY, O_SYNC */
40 #endif
41
42 #include <errno.h>                                                  /* errno */
43
44 #ifdef HAVE_UNISTD_H
45 #   include <unistd.h>                                   /* close(), write() */
46 #endif
47
48 #include <assert.h>
49
50 #include <vlc_charset.h>
51
52 /*****************************************************************************
53  * Local macros
54  *****************************************************************************/
55 #if defined(HAVE_VA_COPY)
56 #   define vlc_va_copy(dest,src) va_copy(dest,src)
57 #elif defined(HAVE___VA_COPY)
58 #   define vlc_va_copy(dest,src) __va_copy(dest,src)
59 #else
60 #   define vlc_va_copy(dest,src) (dest)=(src)
61 #endif
62
63 #define QUEUE(i) p_this->p_libvlc->msg_bank.queues[i]
64 #define LOCK_BANK vlc_mutex_lock( &p_this->p_libvlc->msg_bank.lock );
65 #define UNLOCK_BANK vlc_mutex_unlock( &p_this->p_libvlc->msg_bank.lock );
66
67 /*****************************************************************************
68  * Local prototypes
69  *****************************************************************************/
70 static void QueueMsg ( vlc_object_t *, int, int , const char *,
71                        const char *, va_list );
72 static void FlushMsg ( msg_queue_t * );
73 static void PrintMsg ( vlc_object_t *, msg_item_t * );
74
75 /**
76  * Initialize messages queues
77  * This function initializes all message queues
78  */
79 void __msg_Create( vlc_object_t *p_this )
80 {
81     vlc_mutex_init( p_this, &(p_this->p_libvlc->msg_bank.lock) );
82
83 #define QUEUE_INIT(i) \
84     vlc_mutex_init( p_this, &QUEUE(i).lock ); \
85     QUEUE(i).b_overflow = VLC_FALSE; \
86     QUEUE(i).i_id = i; \
87     QUEUE(i).i_start = 0; \
88     QUEUE(i).i_stop = 0; \
89     QUEUE(i).i_sub = 0; \
90     QUEUE(i).pp_sub = 0;
91
92     QUEUE_INIT( 0 );
93     QUEUE_INIT( 1 );
94
95 #undef QUEUE_INIT
96
97 #ifdef UNDER_CE
98     QUEUE(MSG_QUEUE_NORMAL).logfile =
99         CreateFile( L"vlc-log.txt", GENERIC_WRITE,
100                     FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
101                     CREATE_ALWAYS, 0, NULL );
102     SetFilePointer( QUEUE(MSG_QUEUE_NORMAL).logfile, 0, NULL, FILE_END );
103 #endif
104 }
105
106 /**
107  * Flush all message queues
108  */
109 void __msg_Flush( vlc_object_t *p_this )
110 {
111     int i;
112     for( i = 0 ; i < NB_QUEUES ; i++ )
113     {
114         vlc_mutex_lock( &QUEUE(i).lock );
115         FlushMsg( &QUEUE(i) );
116         vlc_mutex_unlock( &QUEUE(i).lock );
117     }
118 }
119
120 /**
121  * Destroy the message queues
122  *
123  * This functions prints all messages remaining in the queues,
124  * then frees all the allocated ressources
125  * No other messages interface functions should be called after this one.
126  */
127 void __msg_Destroy( vlc_object_t *p_this )
128 {
129     int i;
130     for( i = NB_QUEUES -1 ; i >= 0;  i-- )
131     {
132         if( QUEUE(i).i_sub )
133             msg_Err( p_this, "stale interface subscribers" );
134
135         FlushMsg( &QUEUE(i) );
136
137 #ifdef UNDER_CE
138         if( i == MSG_QUEUE_NORMAL )
139             CloseHandle( QUEUE(MSG_QUEUE_NORMAL).logfile );
140 #endif
141         /* Destroy lock */
142         vlc_mutex_destroy( &QUEUE(i).lock );
143     }
144     vlc_mutex_destroy( &(p_this->p_libvlc->msg_bank.lock) );
145 }
146
147 /**
148  * Subscribe to a message queue.
149  */
150 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this, int i )
151 {
152     msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
153
154     assert( i < NB_QUEUES );
155
156     LOCK_BANK;
157     vlc_mutex_lock( &QUEUE(i).lock );
158
159     TAB_APPEND( QUEUE(i).i_sub, QUEUE(i).pp_sub, p_sub );
160
161     p_sub->i_start = QUEUE(i).i_start;
162     p_sub->pi_stop = &QUEUE(i).i_stop;
163     p_sub->p_msg   = QUEUE(i).msg;
164     p_sub->p_lock  = &QUEUE(i).lock;
165
166     vlc_mutex_unlock( &QUEUE(i).lock );
167     UNLOCK_BANK;
168
169     return p_sub;
170 }
171
172 /**
173  * Unsubscribe from a message queue.
174  */
175 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
176 {
177     int i,j;
178
179     LOCK_BANK;
180     for( i = 0 ; i< NB_QUEUES ; i++ )
181     {
182         vlc_mutex_lock( &QUEUE(i).lock );
183         for( j = 0 ; j< QUEUE(i).i_sub ; j++ )
184         {
185             if( QUEUE(i).pp_sub[j] == p_sub )
186             {
187                 REMOVE_ELEM( QUEUE(i).pp_sub, QUEUE(i).i_sub, j );
188                 if( p_sub ) free( p_sub );
189             }
190         }
191         vlc_mutex_unlock( & QUEUE(i).lock );
192     }
193     UNLOCK_BANK;
194 }
195
196 const char *msg_GetObjectTypeName(int i_object_type )
197 {
198     switch( i_object_type )
199     {
200         case VLC_OBJECT_GLOBAL: return "global";
201         case VLC_OBJECT_LIBVLC: return "libvlc";
202         case VLC_OBJECT_MODULE: return "module";
203         case VLC_OBJECT_INTF: return "interface";
204         case VLC_OBJECT_PLAYLIST: return "playlist";
205         case VLC_OBJECT_ITEM: return "item";
206         case VLC_OBJECT_INPUT: return "input";
207         case VLC_OBJECT_DECODER: return "decoder";
208         case VLC_OBJECT_PACKETIZER: return "packetizer";
209         case VLC_OBJECT_ENCODER: return "encoder";
210         case VLC_OBJECT_VOUT: return "video output";
211         case VLC_OBJECT_AOUT: return "audio output";
212         case VLC_OBJECT_SOUT: return "stream output";
213         case VLC_OBJECT_HTTPD: return "http server";
214         case VLC_OBJECT_HTTPD_HOST: return "http server";
215         case VLC_OBJECT_DIALOGS: return "dialogs provider";
216         case VLC_OBJECT_VLM: return "vlm";
217         case VLC_OBJECT_ANNOUNCE: return "announce handler";
218         case VLC_OBJECT_DEMUX: return "demuxer";
219         case VLC_OBJECT_ACCESS: return "access";
220         case VLC_OBJECT_META_ENGINE: return "meta engine";
221         default: return "private";
222     }
223 }
224
225 /*****************************************************************************
226  * __msg_*: print a message
227  *****************************************************************************
228  * These functions queue a message for later printing.
229  *****************************************************************************/
230 void __msg_Generic( vlc_object_t *p_this, int i_queue, int i_type,
231                     const char *psz_module,
232                     const char *psz_format, ... )
233 {
234     va_list args;
235
236     va_start( args, psz_format );
237     QueueMsg( p_this, i_queue, i_type, psz_module, psz_format, args );
238     va_end( args );
239 }
240
241 void __msg_GenericVa( vlc_object_t *p_this, int i_queue,
242                       int i_type, const char *psz_module,
243                       const char *psz_format, va_list args )
244 {
245     QueueMsg( p_this, i_queue, i_type, psz_module, psz_format, args );
246 }
247
248 /* Generic functions used when variadic macros are not available. */
249 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
250     void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
251     { \
252         va_list args; \
253         va_start( args, psz_format ); \
254         QueueMsg( (vlc_object_t *)p_this,MSG_QUEUE_NORMAL, FN_TYPE, "unknown", \
255                   psz_format, args ); \
256         va_end( args ); \
257     } \
258     struct _
259 /**
260  * Output an informational message.
261  * \note Do not use this for debug messages
262  * \see input_AddInfo
263  */
264 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
265 /**
266  * Output an error message.
267  */
268 DECLARE_MSG_FN( __msg_Err,  VLC_MSG_ERR );
269 /**
270  * Output a waring message
271  */
272 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
273 /**
274  * Output a debug message
275  */
276 DECLARE_MSG_FN( __msg_Dbg,  VLC_MSG_DBG );
277
278 /**
279  * Add a message to a queue
280  *
281  * This function provides basic functionnalities to other msg_* functions.
282  * It adds a message to a queue (after having printed all stored messages if it
283  * is full). If the message can't be converted to string in memory, it issues
284  * a warning.
285  */
286 static void QueueMsg( vlc_object_t *p_this, int i_queue, int i_type,
287                       const char *psz_module,
288                       const char *psz_format, va_list _args )
289 {
290     int         i_header_size;             /* Size of the additionnal header */
291     vlc_object_t *p_obj;
292     char *       psz_str = NULL;                 /* formatted message string */
293     char *       psz_header = NULL;
294     va_list      args;
295     msg_item_t * p_item = NULL;                        /* pointer to message */
296     msg_item_t   item;                    /* message in case of a full queue */
297     msg_queue_t *p_queue;
298
299 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
300     int          i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
301 #endif
302
303     if( p_this == NULL || p_this->i_flags & OBJECT_FLAGS_QUIET ||
304         (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
305     {
306         return;
307     }
308
309     /* Convert message to string  */
310 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
311     vlc_va_copy( args, _args );
312     vasprintf( &psz_str, psz_format, args );
313     va_end( args );
314 #else
315     psz_str = (char*) malloc( i_size * sizeof(char) );
316 #endif
317
318     if( psz_str == NULL )
319     {
320         fprintf( stderr, "main warning: can't store message (%s): ",
321                  strerror(errno) );
322         vlc_va_copy( args, _args );
323         /* We should use utf8_vfprintf - but it calls malloc()... */
324         vfprintf( stderr, psz_format, args );
325         va_end( args );
326         fputs( "\n", stderr );
327         return;
328     }
329
330     i_header_size = 0;
331     p_obj = p_this;
332     while( p_obj != NULL )
333     {
334         char *psz_old = NULL;
335         if( p_obj->psz_header )
336         {
337             i_header_size += strlen( p_obj->psz_header ) + 4;
338             if( psz_header )
339             {
340                 psz_old = strdup( psz_header );
341                 psz_header = (char*)realloc( psz_header, i_header_size );
342                 snprintf( psz_header, i_header_size , "[%s] %s",
343                           p_obj->psz_header, psz_old );
344             }
345             else
346             {
347                 psz_header = (char *)malloc( i_header_size );
348                 snprintf( psz_header, i_header_size, "[%s]",
349                           p_obj->psz_header );
350             }
351         }
352         if( psz_old ) free( psz_old );
353         p_obj = p_obj->p_parent;
354     }
355
356 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
357     vlc_va_copy( args, _args );
358     vsnprintf( psz_str, i_size, psz_format, args );
359     va_end( args );
360     psz_str[ i_size - 1 ] = 0; /* Just in case */
361 #endif
362
363     assert( i_queue < NB_QUEUES );
364     LOCK_BANK;
365     p_queue = &QUEUE(i_queue) ;
366     vlc_mutex_lock( &p_queue->lock );
367
368     /* Check there is room in the queue for our message */
369     if( p_queue->b_overflow )
370     {
371         FlushMsg( p_queue );
372
373         if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
374         {
375             /* Still in overflow mode, print from a dummy item */
376             p_item = &item;
377         }
378         else
379         {
380             /* Pheeew, at last, there is room in the queue! */
381             p_queue->b_overflow = VLC_FALSE;
382         }
383     }
384     else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
385     {
386         FlushMsg( p_queue );
387
388         if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
389         {
390             p_queue->b_overflow = VLC_TRUE;
391
392             if( p_queue->i_id == MSG_QUEUE_NORMAL )
393             {
394                /* Put the overflow message in the queue */
395                 p_item = p_queue->msg + p_queue->i_stop;
396                 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
397
398                 p_item->i_type =        VLC_MSG_WARN;
399                 p_item->i_object_id =   p_this->i_object_id;
400                 p_item->i_object_type = p_this->i_object_type;
401                 p_item->psz_module =    strdup( "message" );
402                 p_item->psz_msg =       strdup( "message queue overflowed" );
403                 p_item->psz_header =    NULL;
404
405                PrintMsg( p_this, p_item );
406                /* We print from a dummy item */
407                p_item = &item;
408             }
409         }
410     }
411
412     if( !p_queue->b_overflow )
413     {
414         /* Put the message in the queue */
415         p_item = p_queue->msg + p_queue->i_stop;
416         p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
417     }
418
419     /* Fill message information fields */
420     p_item->i_type =        i_type;
421     p_item->i_object_id =   p_this->i_object_id;
422     p_item->i_object_type = p_this->i_object_type;
423     p_item->psz_module =    strdup( psz_module );
424     p_item->psz_msg =       psz_str;
425     p_item->psz_header =    psz_header;
426
427     if( p_queue->i_id == MSG_QUEUE_NORMAL )
428         PrintMsg( p_this, p_item );
429
430     if( p_queue->b_overflow )
431     {
432         if( p_item->psz_module )
433             free( p_item->psz_module );
434         if( p_item->psz_msg )
435             free( p_item->psz_msg );
436         if( p_item->psz_header )
437             free( p_item->psz_header );
438     }
439
440     vlc_mutex_unlock ( &p_queue->lock );
441     UNLOCK_BANK;
442 }
443
444 /* following functions are local */
445
446 /*****************************************************************************
447  * FlushMsg
448  *****************************************************************************
449  * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
450  * this function does not check the lock.
451  *****************************************************************************/
452 static void FlushMsg ( msg_queue_t *p_queue )
453 {
454     int i_index, i_start, i_stop;
455
456     /* Get the maximum message index that can be freed */
457     i_stop = p_queue->i_stop;
458
459     /* Check until which value we can free messages */
460     for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
461     {
462         i_start = p_queue->pp_sub[ i_index ]->i_start;
463
464         /* If this subscriber is late, we don't free messages before
465          * his i_start value, otherwise he'll miss messages */
466         if(   ( i_start < i_stop
467                && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
468            || ( i_stop < i_start
469                && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
470         {
471             i_stop = i_start;
472         }
473     }
474
475     /* Free message data */
476     for( i_index = p_queue->i_start;
477          i_index != i_stop;
478          i_index = (i_index+1) % VLC_MSG_QSIZE )
479     {
480         if( p_queue->msg[i_index].psz_msg )
481             free( p_queue->msg[i_index].psz_msg );
482         if( p_queue->msg[i_index].psz_module )
483             free( p_queue->msg[i_index].psz_module );
484         if( p_queue->msg[i_index].psz_header )
485             free( p_queue->msg[i_index].psz_header );
486     }
487
488     /* Update the new start value */
489     p_queue->i_start = i_index;
490 }
491
492 /*****************************************************************************
493  * PrintMsg: output a standard message item to stderr
494  *****************************************************************************
495  * Print a message to stderr, with colour formatting if needed.
496  *****************************************************************************/
497 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
498 {
499 #   define COL(x)  "\033[" #x ";1m"
500 #   define RED     COL(31)
501 #   define GREEN   COL(32)
502 #   define YELLOW  COL(33)
503 #   define WHITE   COL(37)
504 #   define GRAY    "\033[0m"
505
506 #ifdef UNDER_CE
507     int i_dummy;
508 #endif
509     static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
510     static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
511     const char *psz_object;
512     int i_type = p_item->i_type;
513
514     switch( i_type )
515     {
516         case VLC_MSG_ERR:
517             if( p_this->p_libvlc->i_verbose < 0 ) return;
518             break;
519         case VLC_MSG_INFO:
520             if( p_this->p_libvlc->i_verbose < 0 ) return;
521             break;
522         case VLC_MSG_WARN:
523             if( p_this->p_libvlc->i_verbose < 1 ) return;
524             break;
525         case VLC_MSG_DBG:
526             if( p_this->p_libvlc->i_verbose < 2 ) return;
527             break;
528     }
529
530     psz_object = msg_GetObjectTypeName(p_item->i_object_type);
531
532 #ifdef UNDER_CE
533 #   define CE_WRITE(str) WriteFile( QUEUE(MSG_QUEUE_NORMAL).logfile, \
534                                     str, strlen(str), &i_dummy, NULL );
535     CE_WRITE( p_item->psz_module );
536     CE_WRITE( " " );
537     CE_WRITE( psz_object );
538     CE_WRITE( ppsz_type[i_type] );
539     CE_WRITE( ": " );
540     CE_WRITE( p_item->psz_msg );
541     CE_WRITE( "\r\n" );
542     FlushFileBuffers( QUEUE(MSG_QUEUE_NORMAL).logfile );
543
544 #else
545     /* Send the message to stderr */
546     if( p_this->p_libvlc->b_color )
547     {
548         if( p_item->psz_header )
549         {
550             utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s %s%s: %s%s" GRAY
551                               "\n",
552                          p_item->i_object_id, p_item->psz_header,
553                          p_item->psz_module, psz_object,
554                          ppsz_type[i_type], ppsz_color[i_type],
555                          p_item->psz_msg );
556         }
557         else
558         {
559              utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
560                          p_item->i_object_id, p_item->psz_module, psz_object,
561                          ppsz_type[i_type], ppsz_color[i_type],
562                          p_item->psz_msg );
563         }
564     }
565     else
566     {
567         if( p_item->psz_header )
568         {
569             utf8_fprintf( stderr, "[%.8i] %s %s %s%s: %s\n", p_item->i_object_id,
570                          p_item->psz_header, p_item->psz_module,
571                          psz_object, ppsz_type[i_type], p_item->psz_msg );
572         }
573         else
574         {
575             utf8_fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
576                          p_item->psz_module, psz_object, ppsz_type[i_type],
577                          p_item->psz_msg );
578         }
579     }
580
581 #   if defined(WIN32)
582     fflush( stderr );
583 #   endif
584 #endif
585 }
586
587 static msg_context_t* GetContext(void)
588 {
589     msg_context_t *p_ctx = vlc_threadvar_get( &msg_context_global_key );
590     if( p_ctx == NULL )
591     {
592         MALLOC_NULL( p_ctx, msg_context_t );
593         p_ctx->psz_message = NULL;
594         vlc_threadvar_set( &msg_context_global_key, p_ctx );
595     }
596     return p_ctx;
597 }
598
599 void msg_StackSet( int i_code, const char *psz_message, ... )
600 {
601     va_list ap;
602     msg_context_t *p_ctx = GetContext();
603     assert( p_ctx );
604     va_start( ap, psz_message );
605     if( p_ctx->psz_message != NULL )
606     {
607         free( p_ctx->psz_message );
608     }
609
610     vasprintf( &p_ctx->psz_message, psz_message, ap );
611     va_end( ap );
612     p_ctx->i_code = i_code;
613 }
614
615 void msg_StackAdd( const char *psz_message, ... )
616 {
617     char *psz_tmp;
618     va_list ap;
619     msg_context_t *p_ctx = GetContext();
620     assert( p_ctx );
621
622     va_start( ap, psz_message );
623     vasprintf( &psz_tmp, psz_message, ap );
624     va_end( ap );
625
626     if( !p_ctx->psz_message )
627         p_ctx->psz_message = psz_tmp;
628     else
629     {
630         char *psz_old = malloc( strlen( p_ctx->psz_message ) + 1 );
631         memcpy( psz_old, p_ctx->psz_message, strlen( p_ctx->psz_message ) + 1 );
632         p_ctx->psz_message = realloc( p_ctx->psz_message,
633                                       strlen( p_ctx->psz_message ) +
634                                       /* ':', ' ', '0' */
635                                       strlen( psz_tmp ) + 3 );
636         sprintf( p_ctx->psz_message, "%s: %s", psz_tmp, psz_old );
637         free( psz_tmp ); free( psz_old );
638     }
639 }
640
641 const char* msg_StackMsg( void )
642 {
643     msg_context_t *p_ctx = GetContext();
644     assert( p_ctx );
645     return p_ctx->psz_message;
646 }