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