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