]> git.sesse.net Git - vlc/blob - src/interface/intf_msg.c
* Ported Glide and MGA plugins to the new module API. MGA never worked,
[vlc] / src / interface / intf_msg.c
1 /*****************************************************************************
2  * intf_msg.c: messages interface
3  * This library provides basic functions for threads to interact with user
4  * interface, such as message output. See config.h for output configuration.
5  *****************************************************************************
6  * Copyright (C) 1998, 1999, 2000 VideoLAN
7  *
8  * Authors: Vincent Seguin <seguin@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "defs.h"
29
30 #include <errno.h>                                                  /* errno */
31 #include <fcntl.h>                     /* O_CREAT, O_TRUNC, O_WRONLY, O_SYNC */
32 #include <stdio.h>                                               /* required */
33 #include <stdarg.h>                                       /* va_list for BSD */
34 #include <stdlib.h>                                              /* malloc() */
35 #include <string.h>                                            /* strerror() */
36 #include <unistd.h>                                      /* close(), write() */
37
38 #include "config.h"
39 #include "common.h"
40 #include "threads.h"
41 #include "mtime.h"
42
43 #include "intf_msg.h"
44 #include "interface.h"
45 #include "intf_console.h"
46
47 #include "main.h"
48
49 /*****************************************************************************
50  * intf_msg_item_t
51  *****************************************************************************
52  * Store a single message. Messages have a maximal size of INTF_MSG_MSGSIZE.
53  * If DEBUG is defined, messages have a date field and debug messages are
54  * printed with a date to allow more precise profiling.
55  *****************************************************************************/
56 typedef struct
57 {
58     int     i_type;                               /* message type, see below */
59     char *  psz_msg;                                   /* the message itself */
60
61 #ifdef DEBUG
62     /* Debugging informations - in DEBUG mode, debug messages have calling
63      * location informations printed */
64     mtime_t date;                                     /* date of the message */
65     char *  psz_file;               /* file in which the function was called */
66     char *  psz_function;     /* function from which the function was called */
67     int     i_line;                 /* line at which the function was called */
68 #endif
69 } intf_msg_item_t;
70
71 /* Message types */
72 #define INTF_MSG_STD    0                                /* standard message */
73 #define INTF_MSG_ERR    1                                   /* error message */
74 #define INTF_MSG_INTF   2                               /* interface message */
75 #define INTF_MSG_DBG    3                                   /* debug message */
76 #define INTF_MSG_WARN   4                                  /* warning message*/
77
78
79 /*****************************************************************************
80  * intf_msg_t
81  *****************************************************************************
82  * Store all data requiered by messages interfaces. It has a single reference
83  * int p_main.
84  *****************************************************************************/
85 typedef struct intf_msg_s
86 {
87 #ifdef INTF_MSG_QUEUE
88     /* Message queue */
89     vlc_mutex_t             lock;                      /* message queue lock */
90     int                     i_count;            /* number of messages stored */
91     intf_msg_item_t         msg[INTF_MSG_QSIZE];            /* message queue */
92 #endif
93
94 #ifdef DEBUG_LOG
95     /* Log file */
96     FILE *                  p_log_file;                          /* log file */
97 #endif
98
99 #if !defined(INTF_MSG_QUEUE) && !defined(DEBUG_LOG)
100     /* If neither messages queue, neither log file is used, then the structure
101      * is empty. However, empty structures are not allowed in C. Therefore, a
102      * dummy integer is used to fill it. */
103     int                     i_dummy;                        /* unused filler */
104 #endif
105 } intf_msg_t;
106
107 /*****************************************************************************
108  * Local prototypes
109  *****************************************************************************/
110
111 static void QueueMsg        ( intf_msg_t *p_msg, int i_type,
112                               char *psz_format, va_list ap );
113 static void PrintMsg        ( intf_msg_item_t *p_msg );
114 #ifdef DEBUG
115 static void QueueDbgMsg     ( intf_msg_t *p_msg, char *psz_file,
116                               char *psz_function, int i_line,
117                               char *psz_format, va_list ap );
118 #endif
119 #ifdef INTF_MSG_QUEUE
120 static void FlushLockedMsg  ( intf_msg_t *p_msg );
121 #endif
122
123
124 /*****************************************************************************
125  * intf_MsgCreate: initialize messages interface                         (ok ?)
126  *****************************************************************************
127  * This functions has to be called before any call to other intf_*Msg functions.
128  * It set up the locks and the message queue if it is used.
129  *****************************************************************************/
130 p_intf_msg_t intf_MsgCreate( void )
131 {
132     p_intf_msg_t p_msg;
133
134     /* Allocate structure */
135     p_msg = malloc( sizeof( intf_msg_t ) );
136     if( p_msg == NULL )
137     {
138         errno = ENOMEM;
139     }
140     else
141     {
142 #ifdef INTF_MSG_QUEUE
143     /* Message queue initialization */
144     vlc_mutex_init( &p_msg->lock );                        /* intialize lock */
145     p_msg->i_count = 0;                                    /* queue is empty */
146 #endif
147
148     
149 #ifdef DEBUG_LOG
150         /* Log file initialization - on failure, file pointer will be null,
151          * and no log will be issued, but this is not considered as an
152          * error */
153         p_msg->p_log_file = fopen( DEBUG_LOG, "w" );
154 #endif
155     }
156     return( p_msg );
157 }
158
159 /*****************************************************************************
160  * intf_MsgDestroy: free resources allocated by intf_InitMsg            (ok ?)
161  *****************************************************************************
162  * This functions prints all messages remaining in queue, then free all the
163  * resources allocated by intf_InitMsg.
164  * No other messages interface functions should be called after this one.
165  *****************************************************************************/
166 void intf_MsgDestroy( void )
167 {
168     intf_FlushMsg();                         /* print all remaining messages */
169
170 #ifdef DEBUG_LOG
171     /* Close log file if any */
172     if( p_main->p_msg->p_log_file != NULL )
173     {
174         fclose( p_main->p_msg->p_log_file );
175     }
176 #endif
177
178 #ifdef INTF_MSG_QUEUE
179     /* destroy lock */
180     vlc_mutex_destroy( &p_main->p_msg->lock );
181 #endif
182     
183     /* Free structure */
184     free( p_main->p_msg );
185 }
186
187 /*****************************************************************************
188  * intf_Msg: print a message                                             (ok ?)
189  *****************************************************************************
190  * This function queue a message for later printing, or print it immediately
191  * if the queue isn't used.
192  *****************************************************************************/
193 void intf_Msg( char *psz_format, ... )
194 {
195     va_list ap;
196
197     va_start( ap, psz_format );
198     QueueMsg( p_main->p_msg, INTF_MSG_STD, psz_format, ap );
199     va_end( ap );
200 }
201
202 /*****************************************************************************
203  * intf_ErrMsg : print an error message                                  (ok ?)
204  *****************************************************************************
205  * This function is the same as intf_Msg, except that it prints its messages
206  * on stderr.
207  *****************************************************************************/
208 void intf_ErrMsg( char *psz_format, ... )
209 {
210     va_list ap;
211
212     va_start( ap, psz_format );
213     QueueMsg( p_main->p_msg, INTF_MSG_ERR, psz_format, ap );
214     va_end( ap );
215 }
216
217 /*****************************************************************************
218  * intf_WarnMsg : print a warning message
219  *****************************************************************************
220  * This function is the same as intf_Msg, except that it concerns warning
221  * messages for testing purpose.
222  *****************************************************************************/
223 void intf_WarnMsg( int i_level, char *psz_format, ... )
224 {
225     va_list ap;
226     
227     if( i_level >= p_main->i_warning_level )
228     {
229         va_start( ap, psz_format );
230         QueueMsg( p_main->p_msg, INTF_MSG_WARN, psz_format, ap );
231         va_end( ap );
232     }
233 }
234
235
236 /*****************************************************************************
237  * intf_IntfMsg : print an interface message                             (ok ?)
238  *****************************************************************************
239  * In opposition to all other intf_*Msg function, this function does not print
240  * it's message on default terminal (stdout or stderr), but send it to
241  * interface (in fact to the X11 console). This means that the interface MUST
242  * be initialized and a X11 console openned before this function is used, and
243  * that once the console is closed, this call is vorbidden.
244  * Practically, only the interface thread itself should call this function, and
245  * flush all messages before intf_CloseX11Console() is called.
246  *****************************************************************************/
247 void intf_IntfMsg(char *psz_format, ...)
248 {
249     va_list ap;
250
251     va_start( ap, psz_format );
252     QueueMsg( p_main->p_msg, INTF_MSG_INTF, psz_format, ap );
253     va_end( ap );
254 }
255
256 /*****************************************************************************
257  * _intf_DbgMsg: print a debugging message                               (ok ?)
258  *****************************************************************************
259  * This function prints a debugging message. Compared to other intf_*Msg
260  * functions, it is only defined if DEBUG is defined and require a file name,
261  * a function name and a line number as additionnal debugging informations. It
262  * also prints a debugging header for each received line.
263  *****************************************************************************/
264 #ifdef DEBUG
265 void _intf_DbgMsg( char *psz_file, char *psz_function, int i_line,
266                    char *psz_format, ...)
267 {
268     va_list ap;
269
270     va_start( ap, psz_format );
271     QueueDbgMsg( p_main->p_msg, psz_file, psz_function, i_line,
272                  psz_format, ap );
273     va_end( ap );
274 }
275 #endif
276
277 /*****************************************************************************
278  * intf_MsgImm: print a message                                          (ok ?)
279  *****************************************************************************
280  * This function prints a message immediately. If the queue is used, all
281  * waiting messages are also printed.
282  *****************************************************************************/
283 void intf_MsgImm( char *psz_format, ... )
284 {
285     va_list ap;
286
287     va_start( ap, psz_format );
288     QueueMsg( p_main->p_msg, INTF_MSG_STD, psz_format, ap );
289     va_end( ap );
290     intf_FlushMsg();
291 }
292
293 /*****************************************************************************
294  * intf_ErrMsgImm: print an error message immediately                    (ok ?)
295  *****************************************************************************
296  * This function is the same as intf_MsgImm, except that it prints its message
297  * on stderr.
298  *****************************************************************************/
299 void intf_ErrMsgImm(char *psz_format, ...)
300 {
301     va_list ap;
302
303     va_start( ap, psz_format );
304     QueueMsg( p_main->p_msg, INTF_MSG_ERR, psz_format, ap );
305     va_end( ap );
306     intf_FlushMsg();
307 }
308
309 /*****************************************************************************
310  * intf_WarnMsgImm : print a warning message
311  *****************************************************************************
312  * This function is the same as intf_MsgImm, except that it concerns warning
313  * messages for testing purpose.
314  *****************************************************************************/
315 void intf_WarnMsgImm( int i_level, char *psz_format, ... )
316 {
317     va_list ap;
318
319     if( i_level >= p_main->i_warning_level )
320     {
321         va_start( ap, psz_format );
322         QueueMsg( p_main->p_msg, INTF_MSG_WARN, psz_format, ap );
323         va_end( ap );
324     }
325     intf_FlushMsg();
326 }
327
328
329
330 /*****************************************************************************
331  * _intf_DbgMsgImm: print a debugging message immediately                (ok ?)
332  *****************************************************************************
333  * This function is the same as intf_DbgMsgImm, except that it prints its
334  * message immediately. It should only be called through the macro
335  * intf_DbgMsgImm().
336  *****************************************************************************/
337 #ifdef DEBUG
338 void _intf_DbgMsgImm( char *psz_file, char *psz_function, int i_line,
339                       char *psz_format, ...)
340 {
341     va_list ap;
342
343     va_start( ap, psz_format );
344     QueueDbgMsg( p_main->p_msg, psz_file, psz_function, i_line,
345                  psz_format, ap );
346     va_end( ap );
347     intf_FlushMsg();
348 }
349 #endif
350
351 /*****************************************************************************
352  * intf_FlushMsg                                                        (ok ?)
353  *****************************************************************************
354  * Print all messages remaining in queue: get lock and call FlushLockedMsg.
355  * This function does nothing if the message queue isn't used.
356  * This function is only implemented if message queue is used. If not, it is
357  * an empty macro.
358  *****************************************************************************/
359 #ifdef INTF_MSG_QUEUE
360 void intf_FlushMsg( void )
361 {
362     vlc_mutex_lock( &p_main->p_msg->lock );                      /* get lock */
363     FlushLockedMsg( p_main->p_msg );                       /* flush messages */
364     vlc_mutex_unlock( &p_main->p_msg->lock );              /* give lock back */
365 }
366 #endif
367
368 /* following functions are local */
369
370 /*****************************************************************************
371  * QueueMsg: add a message to a queue
372  *****************************************************************************
373  * This function provide basic functionnalities to other intf_*Msg functions.
374  * It add a message to a queue (after having printed all stored messages if it
375  * is full. If the message can't be converted to string in memory, it exit the
376  * program. If the queue is not used, it prints the message immediately.
377  *****************************************************************************/
378 static void QueueMsg( intf_msg_t *p_msg, int i_type, char *psz_format, va_list ap )
379 {
380     char *                  psz_str;             /* formatted message string */
381     intf_msg_item_t *       p_msg_item;                /* pointer to message */
382
383 #ifndef INTF_MSG_QUEUE /*................................... instant mode ...*/
384     intf_msg_item_t         msg_item;                             /* message */
385     p_msg_item =           &msg_item;
386 #endif /*....................................................................*/
387
388     /*
389      * Convert message to string
390      */
391 #ifdef HAVE_VASPRINTF
392     vasprintf( &psz_str, psz_format, ap );
393 #else
394     psz_str = (char*) malloc( strlen(psz_format) + INTF_MAX_MSG_SIZE );
395 #endif
396     if( psz_str == NULL )
397     {
398         fprintf(stderr, "warning: can't store following message (%s): ",
399                 strerror(errno) );
400         vfprintf(stderr, psz_format, ap );
401         fprintf(stderr, "\n" );
402         exit( errno );
403     }
404 #ifdef HAVE_VASPRINTF
405 #else
406     vsprintf( psz_str, psz_format, ap );
407 #endif
408
409 #ifdef INTF_MSG_QUEUE /*...................................... queue mode ...*/
410     vlc_mutex_lock( &p_msg->lock );                              /* get lock */
411     if( p_msg->i_count == INTF_MSG_QSIZE )          /* flush queue if needed */
412     {
413 #ifdef DEBUG               /* in debug mode, queue overflow causes a warning */
414         fprintf(stderr, "warning: message queue overflow\n" );
415 #endif
416         FlushLockedMsg( p_msg );
417     }
418     p_msg_item = p_msg->msg + p_msg->i_count++;            /* select message */
419 #endif /*.............................................. end of queue mode ...*/
420
421     /*
422      * Fill message information fields
423      */
424     p_msg_item->i_type =     i_type;
425     p_msg_item->psz_msg =    psz_str;
426 #ifdef DEBUG    
427     p_msg_item->date =       mdate();
428 #endif
429
430 #ifdef INTF_MSG_QUEUE /*......................................... queue mode */
431     vlc_mutex_unlock( &p_msg->lock );                      /* give lock back */
432 #else /*....................................................... instant mode */
433     PrintMsg( p_msg_item );                                 /* print message */
434     free( psz_str );                                    /* free message data */
435 #endif /*....................................................................*/
436 }
437
438 /*****************************************************************************
439  * QueueDbgMsg: add a message to a queue with debugging informations
440  *****************************************************************************
441  * This function is the same as QueueMsg, except that it is only defined when
442  * DEBUG is define, and require additionnal debugging informations.
443  *****************************************************************************/
444 #ifdef DEBUG
445 static void QueueDbgMsg(intf_msg_t *p_msg, char *psz_file, char *psz_function,
446                         int i_line, char *psz_format, va_list ap)
447 {
448     char *                  psz_str;             /* formatted message string */
449     intf_msg_item_t *       p_msg_item;                /* pointer to message */
450
451 #ifndef INTF_MSG_QUEUE /*................................... instant mode ...*/
452     intf_msg_item_t         msg_item;                             /* message */
453     p_msg_item =           &msg_item;
454 #endif /*....................................................................*/
455
456     /*
457      * Convert message to string
458      */
459 #ifdef HAVE_VASPRINTF
460     vasprintf( &psz_str, psz_format, ap );
461 #else
462     psz_str = (char*) malloc( INTF_MAX_MSG_SIZE );
463     vsprintf( psz_str, psz_format, ap );
464 #endif
465     if( psz_str == NULL )
466     {
467         fprintf(stderr, "warning: can't store following message (%s): ",
468                 strerror(errno) );
469         fprintf(stderr, INTF_MSG_DBG_FORMAT, psz_file, psz_function, i_line );
470         vfprintf(stderr, psz_format, ap );
471         fprintf(stderr, "\n" );
472         exit( errno );
473     }
474
475 #ifdef INTF_MSG_QUEUE /*...................................... queue mode ...*/
476     vlc_mutex_lock( &p_msg->lock );                              /* get lock */
477     if( p_msg->i_count == INTF_MSG_QSIZE )          /* flush queue if needed */
478     {
479         fprintf(stderr, "warning: message queue overflow\n" );
480         FlushLockedMsg( p_msg );
481     }
482     p_msg_item = p_msg->msg + p_msg->i_count++;            /* select message */
483 #endif /*.............................................. end of queue mode ...*/
484
485     /*
486      * Fill message information fields
487      */
488     p_msg_item->i_type =       INTF_MSG_DBG;
489     p_msg_item->psz_msg =      psz_str;
490     p_msg_item->psz_file =     psz_file;
491     p_msg_item->psz_function = psz_function;
492     p_msg_item->i_line =       i_line;
493     p_msg_item->date =         mdate();
494
495 #ifdef INTF_MSG_QUEUE /*......................................... queue mode */
496     vlc_mutex_unlock( &p_msg->lock );                      /* give lock back */
497 #else /*....................................................... instant mode */
498     PrintMsg( p_msg_item );                                 /* print message */
499     free( psz_str );                                    /* free message data */
500 #endif /*....................................................................*/
501 }
502 #endif
503
504 /*****************************************************************************
505  * FlushLockedMsg                                                       (ok ?)
506  *****************************************************************************
507  * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
508  * this function does not check the lock. This function is only defined if
509  * INTF_MSG_QUEUE is defined.
510  *****************************************************************************/
511 #ifdef INTF_MSG_QUEUE
512 static void FlushLockedMsg ( intf_msg_t *p_msg )
513 {
514     int i_index;
515
516     for( i_index = 0; i_index < p_msg->i_count; i_index++ )
517     {
518         /* Print message and free message data */
519         PrintMsg( &p_msg->msg[i_index] );
520         free( p_msg->msg[i_index].psz_msg );
521     }
522
523     p_msg->i_count = 0;
524 }
525 #endif
526
527 /*****************************************************************************
528  * PrintMsg: print a message                                             (ok ?)
529  *****************************************************************************
530  * Print a single message. The message data is not freed. This function exists
531  * in two version. The DEBUG version prints a date with each message, and is
532  * able to log messages (if DEBUG_LOG is defined).
533  * The normal one just prints messages to the screen.
534  *****************************************************************************/
535 #ifdef DEBUG
536
537 static void PrintMsg( intf_msg_item_t *p_msg )
538 {
539     char    psz_date[MSTRTIME_MAX_SIZE];            /* formatted time buffer */
540     char *  psz_msg;                                       /* message buffer */
541
542     /* Format message - the message is formatted here because in case the log
543      * file is used, it avoids another format string parsing */
544     switch( p_msg->i_type )
545     {
546     case INTF_MSG_STD:                                   /* regular messages */
547     case INTF_MSG_ERR:
548         asprintf( &psz_msg, "%s", p_msg->psz_msg );
549         break;
550
551     case INTF_MSG_WARN:                                   /* Warning message */
552         mstrtime( psz_date, p_msg->date );
553         asprintf( &psz_msg, "(%s) %s",
554                   psz_date, p_msg->psz_msg );
555
556         break;
557         
558     case INTF_MSG_INTF:                                /* interface messages */
559         asprintf( &psz_msg, "%s", p_msg->psz_msg );
560         break;
561
562     case INTF_MSG_DBG:                                     /* debug messages */
563         mstrtime( psz_date, p_msg->date );
564         asprintf( &psz_msg, "(%s) " INTF_MSG_DBG_FORMAT "%s",
565                   psz_date, p_msg->psz_file, p_msg->psz_function, p_msg->i_line,
566                   p_msg->psz_msg );
567         break;
568     }
569
570     /* Check if formatting function succeeded */
571     if( psz_msg == NULL )
572     {
573         fprintf( stderr, "error: can not format message (%s): %s\n",
574                  strerror( errno ), p_msg->psz_msg );
575         return;
576     }
577
578     /*
579      * Print messages
580      */
581     switch( p_msg->i_type )
582     {
583     case INTF_MSG_STD:                                  /* standard messages */
584         fprintf( stdout, "%s\n", psz_msg );
585         break;
586     case INTF_MSG_ERR:                                     /* error messages */
587     case INTF_MSG_WARN:
588 #ifndef DEBUG_LOG_ONLY
589     case INTF_MSG_DBG:                                 /* debugging messages */
590 #endif
591         fprintf( stderr, "%s\n", psz_msg );
592         break;
593     case INTF_MSG_INTF:                                /* interface messages */
594         intf_ConsolePrint( p_main->p_intf->p_console, psz_msg );
595         break;
596     }
597
598 #ifdef DEBUG_LOG
599     /* Append all messages to log file */
600     if( p_main->p_msg->p_log_file != NULL )
601     {
602         fwrite( psz_msg, strlen( psz_msg ), 1, p_main->p_msg->p_log_file );
603         fwrite( "\n", 1, 1, p_main->p_msg->p_log_file );
604     }
605 #endif
606
607     /* Free formatted message */
608     free( psz_msg );
609 }
610
611 #else
612
613 static void PrintMsg( intf_msg_item_t *p_msg )
614 {
615     /*
616      * Print messages on screen
617      */
618     switch( p_msg->i_type )
619     {
620     case INTF_MSG_STD:                                  /* standard messages */
621     case INTF_MSG_DBG:                                     /* debug messages */
622         fprintf( stdout, "%s\n", p_msg->psz_msg );
623         break;
624     case INTF_MSG_ERR:                                     /* error messages */
625     case INTF_MSG_WARN:
626         fprintf( stderr, "%s\n", p_msg->psz_msg );        /* warning message */
627         break;
628     case INTF_MSG_INTF:                                /* interface messages */
629         intf_ConsolePrint( p_main->p_intf->p_console, p_msg->psz_msg );
630         break;
631     }
632 }
633
634 #endif
635