Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

logger.c

00001 /*****************************************************************************
00002  * logger.c : file logging plugin for vlc
00003  *****************************************************************************
00004  * Copyright (C) 2002 the VideoLAN team
00005  * $Id: logger.c 12036 2005-08-05 18:44:47Z massiot $
00006  *
00007  * Authors: Samuel Hocevar <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 #include <stdlib.h>                                      /* malloc(), free() */
00028 #include <string.h>
00029 
00030 #include <errno.h>                                                 /* ENOMEM */
00031 #include <stdio.h>
00032 
00033 #ifdef UNDER_CE
00034 #   define _IONBF 0x0004
00035 #endif
00036 
00037 #include <vlc/vlc.h>
00038 #include <vlc/intf.h>
00039 
00040 #define MODE_TEXT 0
00041 #define MODE_HTML 1
00042 
00043 #ifdef SYS_DARWIN
00044 #define LOG_DIR "Library/Logs/"
00045 #endif
00046 
00047 #define LOG_FILE_TEXT "vlc-log.txt"
00048 #define LOG_FILE_HTML "vlc-log.html"
00049 
00050 #define LOG_STRING( msg, file ) fwrite( msg, strlen( msg ), 1, file );
00051 
00052 #define TEXT_HEADER "-- logger module started --\n"
00053 #define TEXT_FOOTER "-- logger module stopped --\n"
00054 
00055 #define HTML_HEADER \
00056     "<html>\n" \
00057     "  <head>\n" \
00058     "    <title>vlc log</title>\n" \
00059     "  </head>\n" \
00060     "  <body bgcolor=\"#000000\" text=\"#aaaaaa\">\n" \
00061     "    <pre>\n" \
00062     "      <b>-- logger module started --</b>\n"
00063 #define HTML_FOOTER \
00064     "      <b>-- logger module stopped --</b>\n" \
00065     "    </pre>\n" \
00066     "  </body>\n" \
00067     "</html>\n"
00068 
00069 /*****************************************************************************
00070  * intf_sys_t: description and status of log interface
00071  *****************************************************************************/
00072 struct intf_sys_t
00073 {
00074     int i_mode;
00075 
00076     FILE *    p_file; /* The log file */
00077     msg_subscription_t *p_sub;
00078 };
00079 
00080 /*****************************************************************************
00081  * Local prototypes
00082  *****************************************************************************/
00083 static int  Open    ( vlc_object_t * );
00084 static void Close   ( vlc_object_t * );
00085 static void Run     ( intf_thread_t * );
00086 
00087 static void FlushQueue        ( msg_subscription_t *, FILE *, int );
00088 static void TextPrint         ( const msg_item_t *, FILE * );
00089 static void HtmlPrint         ( const msg_item_t *, FILE * );
00090 
00091 /*****************************************************************************
00092  * Module descriptor
00093  *****************************************************************************/
00094 static char *mode_list[] = { "text", "html" };
00095 static char *mode_list_text[] = { N_("Text"), "HTML" };
00096 
00097 #define LOGMODE_TEXT N_("Log format")
00098 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are \"text\" (default) and \"html\".")
00099 
00100 vlc_module_begin();
00101     set_category( CAT_INTERFACE );
00102     set_subcategory( SUBCAT_INTERFACE_CONTROL );
00103     set_shortname( N_( "Logging" ) );
00104     set_description( _("File logging") );
00105 
00106     add_file( "logfile", NULL, NULL, N_("Log filename"), N_("Specify the log filename."), VLC_FALSE );
00107     add_string( "logmode", "text", NULL, LOGMODE_TEXT, LOGMODE_LONGTEXT,
00108                 VLC_FALSE );
00109         change_string_list( mode_list, mode_list_text, 0 );
00110 
00111     set_capability( "interface", 0 );
00112     set_callbacks( Open, Close );
00113 vlc_module_end();
00114 
00115 /*****************************************************************************
00116  * Open: initialize and create stuff
00117  *****************************************************************************/
00118 static int Open( vlc_object_t *p_this )
00119 {   
00120     intf_thread_t *p_intf = (intf_thread_t *)p_this;
00121     char *psz_mode, *psz_file;
00122 
00123     CONSOLE_INTRO_MSG;
00124     msg_Info( p_intf, "Using the logger interface module..." );
00125 
00126     /* Allocate instance and initialize some members */
00127     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
00128     if( p_intf->p_sys == NULL )
00129     {
00130         msg_Err( p_intf, "out of memory" );
00131         return -1;
00132     }
00133 
00134     psz_mode = config_GetPsz( p_intf, "logmode" );
00135     if( psz_mode )
00136     {
00137         if( !strcmp( psz_mode, "text" ) )
00138         {
00139             p_intf->p_sys->i_mode = MODE_TEXT;
00140         }
00141         else if( !strcmp( psz_mode, "html" ) )
00142         {
00143             p_intf->p_sys->i_mode = MODE_HTML;
00144         }
00145         else
00146         {
00147             msg_Err( p_intf, "invalid log mode `%s', using `text'", psz_mode );
00148             p_intf->p_sys->i_mode = MODE_TEXT;
00149         }
00150 
00151         free( psz_mode );
00152     }
00153     else
00154     {
00155         msg_Warn( p_intf, "no log mode specified, using `text'" );
00156         p_intf->p_sys->i_mode = MODE_TEXT;
00157     }
00158 
00159     psz_file = config_GetPsz( p_intf, "logfile" );
00160     if( !psz_file )
00161     {
00162 #ifdef SYS_DARWIN
00163         char *psz_homedir = p_this->p_vlc->psz_homedir; 
00164 
00165         if( !psz_homedir )
00166         {
00167             msg_Err( p_this, "psz_homedir is null" );
00168             return -1;
00169         }
00170         psz_file = (char *)malloc( sizeof("/" LOG_DIR "/" LOG_FILE_HTML) +
00171                                        strlen(psz_homedir) );
00172         if( psz_file )
00173         {
00174             switch( p_intf->p_sys->i_mode )
00175             {
00176             case MODE_HTML:
00177                 sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_HTML,
00178                      psz_homedir );
00179                 break;
00180             case MODE_TEXT:
00181             default:
00182                 sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_TEXT,
00183                      psz_homedir );
00184                 break;
00185             }
00186         }
00187 #else
00188         switch( p_intf->p_sys->i_mode )
00189         {
00190         case MODE_HTML:
00191             psz_file = strdup( LOG_FILE_HTML );
00192             break;
00193         case MODE_TEXT:
00194         default:
00195             psz_file = strdup( LOG_FILE_TEXT );
00196             break;
00197         }
00198 #endif
00199         msg_Warn( p_intf, "no log filename provided, using `%s'", psz_file );
00200     }
00201 
00202     /* Open the log file and remove any buffering for the stream */
00203     msg_Dbg( p_intf, "opening logfile `%s'", psz_file );
00204     p_intf->p_sys->p_file = fopen( psz_file, "wt" );
00205     if( p_intf->p_sys->p_file == NULL )
00206     {
00207         msg_Err( p_intf, "error opening logfile `%s'", psz_file );
00208         free( p_intf->p_sys );
00209         free( psz_file );
00210         return -1;
00211     }
00212     setvbuf( p_intf->p_sys->p_file, NULL, _IONBF, 0 );
00213     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
00214 
00215     free( psz_file );
00216 
00217     switch( p_intf->p_sys->i_mode )
00218     {
00219     case MODE_HTML:
00220         LOG_STRING( HTML_HEADER, p_intf->p_sys->p_file );
00221         break;
00222     case MODE_TEXT:
00223     default:
00224         LOG_STRING( TEXT_HEADER, p_intf->p_sys->p_file );
00225         break;
00226     }
00227 
00228     p_intf->pf_run = Run;
00229 
00230     return 0;
00231 }
00232 
00233 /*****************************************************************************
00234  * Close: destroy interface stuff
00235  *****************************************************************************/
00236 static void Close( vlc_object_t *p_this )
00237 {       
00238     intf_thread_t *p_intf = (intf_thread_t *)p_this;
00239         
00240     /* Flush the queue and unsubscribe from the message queue */
00241     FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
00242                 p_intf->p_sys->i_mode );
00243     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
00244 
00245     switch( p_intf->p_sys->i_mode )
00246     {
00247     case MODE_HTML:
00248         LOG_STRING( HTML_FOOTER, p_intf->p_sys->p_file );
00249         break;
00250     case MODE_TEXT:
00251     default:
00252         LOG_STRING( TEXT_FOOTER, p_intf->p_sys->p_file );
00253         break;
00254     }
00255 
00256     /* Close the log file */
00257     fclose( p_intf->p_sys->p_file );
00258 
00259     /* Destroy structure */
00260     free( p_intf->p_sys );
00261 }
00262 
00263 /*****************************************************************************
00264  * Run: rc thread
00265  *****************************************************************************
00266  * This part of the interface is in a separate thread so that we can call
00267  * exec() from within it without annoying the rest of the program.
00268  *****************************************************************************/
00269 static void Run( intf_thread_t *p_intf )
00270 {
00271     while( !p_intf->b_die )
00272     {
00273         FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
00274                     p_intf->p_sys->i_mode );
00275 
00276         msleep( INTF_IDLE_SLEEP );
00277     }
00278 }
00279 
00280 /*****************************************************************************
00281  * FlushQueue: flush the message queue into the log file
00282  *****************************************************************************/
00283 static void FlushQueue( msg_subscription_t *p_sub, FILE *p_file, int i_mode )
00284 {
00285     int i_start, i_stop;
00286 
00287     vlc_mutex_lock( p_sub->p_lock );
00288     i_stop = *p_sub->pi_stop;
00289     vlc_mutex_unlock( p_sub->p_lock );
00290 
00291     if( p_sub->i_start != i_stop )
00292     {
00293         /* Append all messages to log file */
00294         for( i_start = p_sub->i_start;
00295              i_start != i_stop;
00296              i_start = (i_start+1) % VLC_MSG_QSIZE )
00297         {
00298             switch( i_mode )
00299             {
00300             case MODE_HTML:
00301                 HtmlPrint( &p_sub->p_msg[i_start], p_file );
00302                 break;
00303             case MODE_TEXT:
00304             default:
00305                 TextPrint( &p_sub->p_msg[i_start], p_file );
00306                 break;
00307             }
00308         }
00309 
00310         vlc_mutex_lock( p_sub->p_lock );
00311         p_sub->i_start = i_start;
00312         vlc_mutex_unlock( p_sub->p_lock );
00313     }
00314 }
00315 
00316 static const char *ppsz_type[4] = { ": ", " error: ",
00317                                     " warning: ", " debug: " };
00318 
00319 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
00320 {
00321     LOG_STRING( p_msg->psz_module, p_file );
00322     LOG_STRING( ppsz_type[p_msg->i_type], p_file );
00323     LOG_STRING( p_msg->psz_msg, p_file );
00324     LOG_STRING( "\n", p_file );
00325 }
00326 
00327 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
00328 {
00329     static const char *ppsz_color[4] = { "<font color=\"#ffffff\">",
00330                                          "<font color=\"#ff6666\">",
00331                                          "<font color=\"#ffff66\">",
00332                                          "<font color=\"#aaaaaa\">" };
00333 
00334     LOG_STRING( p_msg->psz_module, p_file );
00335     LOG_STRING( ppsz_type[p_msg->i_type], p_file );
00336     LOG_STRING( ppsz_color[p_msg->i_type], p_file );
00337     LOG_STRING( p_msg->psz_msg, p_file );
00338     LOG_STRING( "</font>\n", p_file );
00339 }
00340 

Generated on Tue Dec 20 10:14:48 2005 for vlc-0.8.4a by  doxygen 1.4.2