]> git.sesse.net Git - vlc/blob - src/interface/intf_msg.c
. should compile & run on Solaris with ./configure --disable-dsp
[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:
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 #include "plugins.h"
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     /* Free structure */
179     free( p_main->p_msg );
180 }
181
182 /*****************************************************************************
183  * intf_Msg: print a message                                             (ok ?)
184  *****************************************************************************
185  * This function queue a message for later printing, or print it immediately
186  * if the queue isn't used.
187  *****************************************************************************/
188 void intf_Msg( char *psz_format, ... )
189 {
190     va_list ap;
191
192     va_start( ap, psz_format );
193     QueueMsg( p_main->p_msg, INTF_MSG_STD, psz_format, ap );
194     va_end( ap );
195 }
196
197 /*****************************************************************************
198  * intf_ErrMsg : print an error message                                  (ok ?)
199  *****************************************************************************
200  * This function is the same as intf_Msg, except that it prints its messages
201  * on stderr.
202  *****************************************************************************/
203 void intf_ErrMsg( char *psz_format, ... )
204 {
205     va_list ap;
206
207     va_start( ap, psz_format );
208     QueueMsg( p_main->p_msg, INTF_MSG_ERR, psz_format, ap );
209     va_end( ap );
210 }
211
212 /*****************************************************************************
213  * intf_WarnMsg : print a warning message
214  *****************************************************************************
215  * This function is the same as intf_Msg, except that it concerns warning
216  * messages for testing purpose.
217  *****************************************************************************/
218 void intf_WarnMsg( int i_level, char *psz_format, ... )
219 {
220     va_list ap;
221     
222     if( i_level >= p_main->p_intf->i_warning_level )
223     {
224         va_start( ap, psz_format );
225         QueueMsg( p_main->p_msg, INTF_MSG_WARN, psz_format, ap );
226         va_end( ap );
227     }
228 }
229
230
231 /*****************************************************************************
232  * intf_IntfMsg : print an interface message                             (ok ?)
233  *****************************************************************************
234  * In opposition to all other intf_*Msg function, this function does not print
235  * it's message on default terminal (stdout or stderr), but send it to
236  * interface (in fact to the X11 console). This means that the interface MUST
237  * be initialized and a X11 console openned before this function is used, and
238  * that once the console is closed, this call is vorbidden.
239  * Practically, only the interface thread itself should call this function, and
240  * flush all messages before intf_CloseX11Console() is called.
241  *****************************************************************************/
242 void intf_IntfMsg(char *psz_format, ...)
243 {
244     va_list ap;
245
246     va_start( ap, psz_format );
247     QueueMsg( p_main->p_msg, INTF_MSG_INTF, psz_format, ap );
248     va_end( ap );
249 }
250
251 /*****************************************************************************
252  * _intf_DbgMsg: print a debugging message                               (ok ?)
253  *****************************************************************************
254  * This function prints a debugging message. Compared to other intf_*Msg
255  * functions, it is only defined if DEBUG is defined and require a file name,
256  * a function name and a line number as additionnal debugging informations. It
257  * also prints a debugging header for each received line.
258  *****************************************************************************/
259 #ifdef DEBUG
260 void _intf_DbgMsg( char *psz_file, char *psz_function, int i_line,
261                    char *psz_format, ...)
262 {
263     va_list ap;
264
265     va_start( ap, psz_format );
266     QueueDbgMsg( p_main->p_msg, psz_file, psz_function, i_line,
267                  psz_format, ap );
268     va_end( ap );
269 }
270 #endif
271
272 /*****************************************************************************
273  * intf_MsgImm: print a message                                          (ok ?)
274  *****************************************************************************
275  * This function prints a message immediately. If the queue is used, all
276  * waiting messages are also printed.
277  *****************************************************************************/
278 void intf_MsgImm( char *psz_format, ... )
279 {
280     va_list ap;
281
282     va_start( ap, psz_format );
283     QueueMsg( p_main->p_msg, INTF_MSG_STD, psz_format, ap );
284     va_end( ap );
285     intf_FlushMsg();
286 }
287
288 /*****************************************************************************
289  * intf_ErrMsgImm: print an error message immediately                    (ok ?)
290  *****************************************************************************
291  * This function is the same as intf_MsgImm, except that it prints its message
292  * on stderr.
293  *****************************************************************************/
294 void intf_ErrMsgImm(char *psz_format, ...)
295 {
296     va_list ap;
297
298     va_start( ap, psz_format );
299     QueueMsg( p_main->p_msg, INTF_MSG_ERR, psz_format, ap );
300     va_end( ap );
301     intf_FlushMsg();
302 }
303
304 /*****************************************************************************
305  * intf_WarnMsgImm : print a warning message
306  *****************************************************************************
307  * This function is the same as intf_MsgImm, except that it concerns warning
308  * messages for testing purpose.
309  *****************************************************************************/
310 void intf_WarnMsgImm( int i_level, char *psz_format, ... )
311 {
312     va_list ap;
313
314     if( i_level >= p_main->p_intf->i_warning_level )
315     {
316         va_start( ap, psz_format );
317         QueueMsg( p_main->p_msg, INTF_MSG_WARN, psz_format, ap );
318         va_end( ap );
319     }
320     intf_FlushMsg();
321 }
322
323
324
325 /*****************************************************************************
326  * _intf_DbgMsgImm: print a debugging message immediately                (ok ?)
327  *****************************************************************************
328  * This function is the same as intf_DbgMsgImm, except that it prints its
329  * message immediately. It should only be called through the macro
330  * intf_DbgMsgImm().
331  *****************************************************************************/
332 #ifdef DEBUG
333 void _intf_DbgMsgImm( char *psz_file, char *psz_function, int i_line,
334                       char *psz_format, ...)
335 {
336     va_list ap;
337
338     va_start( ap, psz_format );
339     QueueDbgMsg( p_main->p_msg, psz_file, psz_function, i_line,
340                  psz_format, ap );
341     va_end( ap );
342     intf_FlushMsg();
343 }
344 #endif
345
346 /*****************************************************************************
347  * intf_FlushMsg                                                        (ok ?)
348  *****************************************************************************
349  * Print all messages remaining in queue: get lock and call FlushLockedMsg.
350  * This function does nothing if the message queue isn't used.
351  * This function is only implemented if message queue is used. If not, it is
352  * an empty macro.
353  *****************************************************************************/
354 #ifdef INTF_MSG_QUEUE
355 void intf_FlushMsg( void )
356 {
357     vlc_mutex_lock( &p_main->p_msg->lock );                      /* get lock */
358     FlushLockedMsg( p_main->p_msg );                       /* flush messages */
359     vlc_mutex_unlock( &p_main->p_msg->lock );              /* give lock back */
360 }
361 #endif
362
363 /* following functions are local */
364
365 /*****************************************************************************
366  * QueueMsg: add a message to a queue
367  *****************************************************************************
368  * This function provide basic functionnalities to other intf_*Msg functions.
369  * It add a message to a queue (after having printed all stored messages if it
370  * is full. If the message can't be converted to string in memory, it exit the
371  * program. If the queue is not used, it prints the message immediately.
372  *****************************************************************************/
373 static void QueueMsg( intf_msg_t *p_msg, int i_type, char *psz_format, va_list ap )
374 {
375     char *                  psz_str;             /* formatted message string */
376     intf_msg_item_t *       p_msg_item;                /* pointer to message */
377
378 #ifndef INTF_MSG_QUEUE /*................................... instant mode ...*/
379     intf_msg_item_t         msg_item;                             /* message */
380     p_msg_item =           &msg_item;
381 #endif /*....................................................................*/
382
383     /*
384      * Convert message to string
385      */
386 #ifdef HAVE_VASPRINTF
387     vasprintf( &psz_str, psz_format, ap );
388 #else
389     psz_str = (char*) malloc( strlen(psz_format) + INTF_MAX_MSG_SIZE );
390     vsprintf( psz_str, psz_format, ap );
391 #endif
392     if( psz_str == NULL )
393     {
394         fprintf(stderr, "warning: can't store following message (%s): ",
395                 strerror(errno) );
396         vfprintf(stderr, psz_format, ap );
397         fprintf(stderr, "\n" );
398         exit( errno );
399     }
400
401 #ifdef INTF_MSG_QUEUE /*...................................... queue mode ...*/
402     vlc_mutex_lock( &p_msg->lock );                              /* get lock */
403     if( p_msg->i_count == INTF_MSG_QSIZE )          /* flush queue if needed */
404     {
405 #ifdef DEBUG               /* in debug mode, queue overflow causes a warning */
406         fprintf(stderr, "warning: message queue overflow\n" );
407 #endif
408         FlushLockedMsg( p_msg );
409     }
410     p_msg_item = p_msg->msg + p_msg->i_count++;            /* select message */
411 #endif /*.............................................. end of queue mode ...*/
412
413     /*
414      * Fill message information fields
415      */
416     p_msg_item->i_type =     i_type;
417     p_msg_item->psz_msg =    psz_str;
418 #ifdef DEBUG    
419     p_msg_item->date =         mdate();
420 #endif
421
422 #ifdef INTF_MSG_QUEUE /*......................................... queue mode */
423     vlc_mutex_unlock( &p_msg->lock );                      /* give lock back */
424 #else /*....................................................... instant mode */
425     PrintMsg( p_msg_item );                                 /* print message */
426     free( psz_str );                                    /* free message data */
427 #endif /*....................................................................*/
428 }
429
430 /*****************************************************************************
431  * QueueDbgMsg: add a message to a queue with debugging informations
432  *****************************************************************************
433  * This function is the same as QueueMsg, except that it is only defined when
434  * DEBUG is define, and require additionnal debugging informations.
435  *****************************************************************************/
436 #ifdef DEBUG
437 static void QueueDbgMsg(intf_msg_t *p_msg, char *psz_file, char *psz_function,
438                         int i_line, char *psz_format, va_list ap)
439 {
440     char *                  psz_str;             /* formatted message string */
441     intf_msg_item_t *       p_msg_item;                /* pointer to message */
442
443 #ifndef INTF_MSG_QUEUE /*................................... instant mode ...*/
444     intf_msg_item_t         msg_item;                             /* message */
445     p_msg_item =           &msg_item;
446 #endif /*....................................................................*/
447
448     /*
449      * Convert message to string
450      */
451 #ifdef HAVE_VASPRINTF
452     vasprintf( &psz_str, psz_format, ap );
453 #else
454     psz_str = (char*) malloc( INTF_MAX_MSG_SIZE );
455     vsprintf( psz_str, psz_format, ap );
456 #endif
457     if( psz_str == NULL )
458     {
459         fprintf(stderr, "warning: can't store following message (%s): ",
460                 strerror(errno) );
461         fprintf(stderr, INTF_MSG_DBG_FORMAT, psz_file, psz_function, i_line );
462         vfprintf(stderr, psz_format, ap );
463         fprintf(stderr, "\n" );
464         exit( errno );
465     }
466
467 #ifdef INTF_MSG_QUEUE /*...................................... queue mode ...*/
468     vlc_mutex_lock( &p_msg->lock );                              /* get lock */
469     if( p_msg->i_count == INTF_MSG_QSIZE )          /* flush queue if needed */
470     {
471         fprintf(stderr, "warning: message queue overflow\n" );
472         FlushLockedMsg( p_msg );
473     }
474     p_msg_item = p_msg->msg + p_msg->i_count++;            /* select message */
475 #endif /*.............................................. end of queue mode ...*/
476
477     /*
478      * Fill message information fields
479      */
480     p_msg_item->i_type =       INTF_MSG_DBG;
481     p_msg_item->psz_msg =      psz_str;
482     p_msg_item->psz_file =     psz_file;
483     p_msg_item->psz_function = psz_function;
484     p_msg_item->i_line =       i_line;
485     p_msg_item->date =         mdate();
486
487 #ifdef INTF_MSG_QUEUE /*......................................... queue mode */
488     vlc_mutex_unlock( &p_msg->lock );                      /* give lock back */
489 #else /*....................................................... instant mode */
490     PrintMsg( p_msg_item );                                 /* print message */
491     free( psz_str );                                    /* free message data */
492 #endif /*....................................................................*/
493 }
494 #endif
495
496 /*****************************************************************************
497  * FlushLockedMsg                                                       (ok ?)
498  *****************************************************************************
499  * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
500  * this function does not check the lock. This function is only defined if
501  * INTF_MSG_QUEUE is defined.
502  *****************************************************************************/
503 #ifdef INTF_MSG_QUEUE
504 static void FlushLockedMsg ( intf_msg_t *p_msg )
505 {
506     int i_index;
507
508     for( i_index = 0; i_index < p_msg->i_count; i_index++ )
509     {
510         /* Print message and free message data */
511         PrintMsg( &p_msg->msg[i_index] );
512         free( p_msg->msg[i_index].psz_msg );
513     }
514
515     p_msg->i_count = 0;
516 }
517 #endif
518
519 /*****************************************************************************
520  * PrintMsg: print a message                                             (ok ?)
521  *****************************************************************************
522  * Print a single message. The message data is not freed. This function exists
523  * in two version. The DEBUG version prints a date with each message, and is
524  * able to log messages (if DEBUG_LOG is defined).
525  * The normal one just prints messages to the screen.
526  *****************************************************************************/
527 #ifdef DEBUG
528
529 static void PrintMsg( intf_msg_item_t *p_msg )
530 {
531     char    psz_date[MSTRTIME_MAX_SIZE];            /* formatted time buffer */
532     char *  psz_msg;                                       /* message buffer */
533
534     /* Format message - the message is formatted here because in case the log
535      * file is used, it avoids another format string parsing */
536     switch( p_msg->i_type )
537     {
538     case INTF_MSG_STD:                                   /* regular messages */
539     case INTF_MSG_ERR:
540         asprintf( &psz_msg, "%s", p_msg->psz_msg );
541         break;
542
543     case INTF_MSG_WARN:                                   /* Warning message */
544         mstrtime( psz_date, p_msg->date );
545         asprintf( &psz_msg, "(%s) %s",
546                   psz_date, p_msg->psz_msg );
547
548         break;
549         
550     case INTF_MSG_INTF:                                /* interface messages */
551         asprintf( &psz_msg, "%s", p_msg->psz_msg );
552         break;
553
554     case INTF_MSG_DBG:                                     /* debug messages */
555         mstrtime( psz_date, p_msg->date );
556         asprintf( &psz_msg, "(%s) " INTF_MSG_DBG_FORMAT "%s",
557                   psz_date, p_msg->psz_file, p_msg->psz_function, p_msg->i_line,
558                   p_msg->psz_msg );
559         break;
560     }
561
562     /* Check if formatting function succeeded */
563     if( psz_msg == NULL )
564     {
565         fprintf( stderr, "error: can not format message (%s): %s\n",
566                  strerror( errno ), p_msg->psz_msg );
567         return;
568     }
569
570     /*
571      * Print messages
572      */
573     switch( p_msg->i_type )
574     {
575     case INTF_MSG_STD:                                  /* standard messages */
576         fprintf( stdout, "%s\n", psz_msg );
577         break;
578     case INTF_MSG_ERR:                                     /* error messages */
579     case INTF_MSG_WARN:
580 #ifndef DEBUG_LOG_ONLY
581     case INTF_MSG_DBG:                                 /* debugging messages */
582 #endif
583         fprintf( stderr, "%s\n", psz_msg );
584         break;
585     case INTF_MSG_INTF:                                /* interface messages */
586         intf_ConsolePrint( p_main->p_intf->p_console, psz_msg );
587         break;
588     }
589
590 #ifdef DEBUG_LOG
591     /* Append all messages to log file */
592     if( p_main->p_msg->p_log_file != NULL )
593     {
594         fwrite( psz_msg, strlen( psz_msg ), 1, p_main->p_msg->p_log_file );
595         fwrite( "\n", 1, 1, p_main->p_msg->p_log_file );
596     }
597 #endif
598
599     /* Free formatted message */
600     free( psz_msg );
601 }
602
603 #else
604
605 static void PrintMsg( intf_msg_item_t *p_msg )
606 {
607     /*
608      * Print messages on screen
609      */
610     switch( p_msg->i_type )
611     {
612     case INTF_MSG_STD:                                  /* standard messages */
613     case INTF_MSG_DBG:                                     /* debug messages */
614         fprintf( stdout, "%s\n", p_msg->psz_msg );
615         break;
616     case INTF_MSG_ERR:                                     /* error messages */
617     case INTF_MSG_WARN:
618         fprintf( stderr, "%s\n", p_msg->psz_msg );        /* warning message */
619         break;
620     case INTF_MSG_INTF:                                /* interface messages */
621         intf_ConsolePrint( p_main->p_intf->p_console, p_msg->psz_msg );
622         break;
623     }
624 }
625
626 #endif
627