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