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