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