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