Commit e7f23953 authored by Marc Ariberti's avatar Marc Ariberti

* now a fully OO logging system (logging to syslog is still not possible yet)

parent 6afc322b
......@@ -4,6 +4,8 @@
HEAD
* logger, config and network are now C++ objects
* IPv6 support
* vlcs.c file splited into vlcs.c logger.c and config.c
* More elaborated logging system
* Now takes only the first field (space separated) in the config file
......
......@@ -2,7 +2,7 @@
* config.c: mini-VideoLAN Channel Server, configuration system
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: config.c,v 1.4 2002/06/08 18:30:17 marcari Exp $
* $Id: config.c,v 1.5 2002/06/08 22:14:28 marcari Exp $
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
* Marc Ariberti <marcari@via.ecp.fr>
......@@ -43,9 +43,8 @@
#include "logger.h"
#include "config.h"
C_Config::C_Config(C_Logger * logger)
C_Config::C_Config(C_Logger &logger) : logger(logger)
{
this->logger = logger;
}
/*
......@@ -64,7 +63,7 @@ void C_Config::DestroyList( void )
}
p_channels = NULL;
logger->Info("List of channels initialized\n");
logger << INFO << "List of channels initialized" << END;
}
char * C_Config::RemoveLF( char * str )
......@@ -108,7 +107,8 @@ void C_Config::AddList( char * psz_item )
i_channel_max++;
logger->Info("Adding channel %d : %s\n", i_channel_max, p_new->psz_vlc_config);
logger << INFO << "Adding channel " << i_channel_max << " : "
<< p_new->psz_vlc_config << END;
}
time_t C_Config::GetModificationTime( void )
......@@ -133,7 +133,7 @@ void C_Config::ReadFile( void )
p_config = fopen( CHANNEL_CONF, "r" );
if( p_config == NULL )
{
logger->Alert( "Cannot fopen " CHANNEL_CONF " (%s)\n", strerror(errno) );
logger << ALERT << "Cannot fopen " CHANNEL_CONF " : "<< strerror(errno);
exit( -1 );
}
......
......@@ -2,7 +2,7 @@
* config.c: mini-VideoLAN Channel Server, configuration system
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: config.h,v 1.3 2002/06/08 18:30:17 marcari Exp $
* $Id: config.h,v 1.4 2002/06/08 22:14:28 marcari Exp $
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
* Marc Ariberti <marcari@via.ecp.fr>
......@@ -32,7 +32,7 @@ typedef struct channel_s
class C_Config
{
public:
C_Config(C_Logger * logger);
C_Config(C_Logger &logger);
void DestroyList( void );
void AddList( char * psz_item );
time_t GetModificationTime( void );
......@@ -47,5 +47,5 @@ protected:
int i_channel_max;
time_t st_mtime; /* last modification time of the config file */
channel_t * p_channels;
C_Logger * logger;
C_Logger &logger;
};
......@@ -2,7 +2,7 @@
* logger.c: VideoLAN Channel Server logging facility
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: logger.c,v 1.2 2002/06/08 18:30:17 marcari Exp $
* $Id: logger.c,v 1.3 2002/06/08 22:14:28 marcari Exp $
*
* Authors: Marc Ariberti <marcari@via.ecp.fr>
*
......@@ -33,19 +33,134 @@
#include "vlcs.h"
#include "logger.h"
/* logging system */
C_Logger::~C_Logger()
{
}
/* Logger chain */
C_Logger::C_Logger( void )
C_ChainedLogger::C_ChainedLogger() : logger_list()
{
#ifdef SYSLOG_NAME
openlog( SYSLOG_NAME, LOG_PERROR, SYSLOG_FACILITY );
#endif
}
#ifdef LOG_FILE
log_file = fopen( LOG_FILE, "a" );
#endif
C_ChainedLogger::~C_ChainedLogger()
{
}
void C_ChainedLogger::Add(C_Logger * logger)
{
logger_list.push_back(logger);
}
void C_ChainedLogger::Remove(C_Logger * logger)
{
logger_list.remove(logger);
}
C_Logger &C_ChainedLogger::operator<<(const char * msg)
{
for(list<C_Logger *>::iterator from = logger_list.begin();
from != logger_list.end() ; ++from)
{
(**from) << msg;
}
return *this;
}
C_Logger &C_ChainedLogger::operator<<(int value)
{
for(list<C_Logger *>::iterator from = logger_list.begin();
from != logger_list.end() ; ++from)
{
(**from) << value;
}
return *this;
}
C_Logger &C_ChainedLogger::operator<<(log_command value)
{
for(list<C_Logger *>::iterator from = logger_list.begin();
from != logger_list.end() ; ++from)
{
(**from) << value;
}
return *this;
}
/* file logger */
C_FileLogger::C_FileLogger(int fd) : log_file(fd)
{
}
C_FileLogger::C_FileLogger(const char * filename) : log_file(filename, ios::app)
{
}
C_FileLogger::~C_FileLogger()
{
log_file.close();
}
C_Logger &C_FileLogger::operator<<(const char * msg)
{
log_file << msg;
return *this;
}
C_Logger &C_FileLogger::operator<<(int value)
{
log_file << value;
return *this;
}
C_Logger &C_FileLogger::operator<<(log_command value)
{
switch(value)
{
case DEBUG:
log_file << "DEBUG : ";
break;
case INFO:
log_file << "INFO : ";
break;
case WARNING:
log_file << "WARNING : ";
break;
case ERROR:
log_file << "ERROR : ";
break;
case ALERT:
log_file << "ALERT : ";
break;
case END:
log_file << endl;
}
return *this;
}
/* Standard logger */
C_StdLogger::C_StdLogger() : C_FileLogger(STDERR_FILENO)
{
}
C_StdLogger::~C_StdLogger()
{
}
/* Syslog logger */
/*
C_SysLogger::C_SysLogger(const char * syslog_name, int facility)
{
openlog( syslog_name, LOG_PERROR, facility );
}
*/
/* logging system */
/*
C_Logger::~C_Logger( void )
{
#ifdef SYSLOG_NAME
......@@ -135,4 +250,4 @@ void C_Logger::Alert(char * format, ...)
#endif
va_end(ap);
}
}*/
......@@ -2,7 +2,7 @@
* logger.h: VideoLAN Channel Server logging facility
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: logger.h,v 1.2 2002/06/08 18:30:17 marcari Exp $
* $Id: logger.h,v 1.3 2002/06/08 22:14:28 marcari Exp $
*
* Authors: Marc Ariberti <marcari@via.ecp.fr>
*
......@@ -21,19 +21,65 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
#include <list>
#include <iostream>
#include <fstream>
typedef enum { DEBUG, INFO, WARNING, ERROR, ALERT, END } log_command;
class C_Logger
{
public:
C_Logger( void );
~C_Logger( void );
void Info(char * format, ...);
void Warn(char * format, ...);
void Error(char * format, ...);
void Alert(char * format, ...);
private:
#ifdef LOG_FILE
FILE * log_file;
#endif
virtual ~C_Logger( void ) = 0;
virtual C_Logger &operator<<(const char * msg) = 0;
virtual C_Logger &operator<<(int value) = 0;
virtual C_Logger &operator<<(log_command value) = 0;
};
class C_ChainedLogger : public C_Logger
{
public:
C_ChainedLogger();
virtual ~C_ChainedLogger( void );
virtual C_Logger &operator<<(const char * msg);
virtual C_Logger &operator<<(int value);
virtual C_Logger &operator<<(log_command value);
void Add(C_Logger * logger);
void Remove(C_Logger * logger);
protected:
list<C_Logger *> logger_list;
};
/*
class C_SysLogger
{
public:
C_SysLogger(const char * syslog_name, int facility);
virtual ~C_SysLogger( void );
};
*/
class C_FileLogger : public C_Logger
{
public:
C_FileLogger(int fd);
C_FileLogger(const char * filename);
virtual ~C_FileLogger( void );
virtual C_Logger &operator<<(const char * msg);
virtual C_Logger &operator<<(int value);
virtual C_Logger &operator<<(log_command value);
protected:
ofstream log_file;
};
class C_StdLogger : public C_FileLogger
{
public:
C_StdLogger( void );
virtual ~C_StdLogger( void );
};
......@@ -2,7 +2,7 @@
* socket.c: VideoLAN Channel Server network facilities
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: socket.c,v 1.3 2002/06/08 18:30:17 marcari Exp $
* $Id: socket.c,v 1.4 2002/06/08 22:14:28 marcari Exp $
*
* Authors: Marc Ariberti <marcari@via.ecp.fr>
*
......@@ -36,6 +36,10 @@
#include "config.h"
#include "socket.h"
C_Socket::C_Socket(C_Logger &logger) : i_handle(0), logger(logger)
{
}
void C_Socket::Close( int i_ret )
{
close( i_handle );
......@@ -45,7 +49,8 @@ void C_Socket::Close( int i_ret )
* C_IPv4Socket class implementation
*/
C_IPv4Socket::C_IPv4Socket(C_Logger * logger, int port, char * bind_addr = NULL)
C_IPv4Socket::C_IPv4Socket(C_Logger &logger, int port, char * bind_addr = NULL)
: C_Socket(logger)
{
int i_opt;
struct sockaddr_in sa_server;
......@@ -57,14 +62,16 @@ C_IPv4Socket::C_IPv4Socket(C_Logger * logger, int port, char * bind_addr = NULL)
if( i_handle < 0 )
{
logger->Alert( "Cannot open socket (%s), exiting\n", strerror(errno) );
logger << ALERT << "Cannot open socket (" << strerror(errno)
<< "), exiting" << END;
::Close( -1 );
}
if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR, &i_opt, sizeof(i_opt) )
< 0 )
{
logger->Alert("Cannot setsockopt (%s), exiting\n", strerror(errno) );
logger << ALERT << "Cannot setsockopt (" << strerror(errno)
<< "), exiting" << END;
::Close( -1 );
}
......@@ -83,7 +90,8 @@ C_IPv4Socket::C_IPv4Socket(C_Logger * logger, int port, char * bind_addr = NULL)
sa_server.sin_port = ntohs( VLCS_PORT );
if( bind( i_handle, (struct sockaddr *)&sa_server, sizeof(sa_server) ) < 0 )
{
logger->Alert("Cannot bind (%s), exiting\n", strerror(errno) );
logger << ALERT << "Cannot bind (" << strerror(errno) << "), exiting"
<< END;
::Close( -1 );
}
}
......@@ -102,9 +110,8 @@ void C_IPv4Socket::Reply( char * psz_message )
time(&tm);
/* TODO: remove this ugly kludge */
logger->Info("%s : %s => %s\n",
C_Config::RemoveLF( asctime( localtime( &tm ) ) ),
Ntop( ), psz_message );
logger << INFO << C_Config::RemoveLF( asctime( localtime( &tm ))) << " : "
<< Ntop( ) << " => " << psz_message << END;
for( ; ; )
{
if( sendto( i_handle, psz_message, strlen( psz_message ) + 1, 0,
......@@ -115,7 +122,7 @@ void C_IPv4Socket::Reply( char * psz_message )
{
continue;
}
logger->Alert( "Cannot sendto (%s)\n", strerror(errno) );
logger << ALERT << "Cannot sendto : " << strerror(errno) << END;
}
break;
}
......@@ -129,7 +136,8 @@ const char * C_IPv4Socket::Ntop( void )
* C_IPv6Socket class implementation
*/
C_IPv6Socket::C_IPv6Socket(C_Logger * logger, int port, char * bind_addr = NULL)
C_IPv6Socket::C_IPv6Socket(C_Logger &logger, int port, char * bind_addr = NULL)
: C_Socket(logger)
{
int i_opt;
struct sockaddr_in6 sa_server;
......@@ -141,14 +149,16 @@ C_IPv6Socket::C_IPv6Socket(C_Logger * logger, int port, char * bind_addr = NULL)
if( i_handle < 0 )
{
logger->Alert( "Cannot open socket (%s), exiting\n", strerror(errno) );
logger << ALERT << "Cannot open socket (" << strerror(errno)
<< "), exiting" << END;
::Close( -1 );
}
if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR, &i_opt, sizeof(i_opt) )
< 0 )
{
logger->Alert("Cannot setsockopt (%s), exiting\n", strerror(errno) );
logger << ALERT << "Cannot setsockopt (" << strerror(errno)
<< "), exiting" << END;
::Close( -1 );
}
......@@ -167,7 +177,8 @@ C_IPv6Socket::C_IPv6Socket(C_Logger * logger, int port, char * bind_addr = NULL)
sa_server.sin6_port = ntohs( VLCS_PORT );
if( bind( i_handle, (struct sockaddr *)&sa_server, sizeof(sa_server) ) < 0 )
{
logger->Alert("Cannot bind (%s), exiting\n", strerror(errno) );
logger << ALERT << "Cannot bind (" << strerror(errno) << "), exiting"
<< END;
::Close( -1 );
}
}
......@@ -186,9 +197,9 @@ void C_IPv6Socket::Reply( char * psz_message )
time(&tm);
/* TODO: remove this ugly kludge */
logger->Info("%s : %s => %s\n",
C_Config::RemoveLF( asctime( localtime( &tm ) ) ),
Ntop( ), psz_message );
logger << INFO << C_Config::RemoveLF( asctime( localtime( &tm ))) << " : "
<< Ntop( ) << " => " << psz_message << END;
for( ; ; )
{
if( sendto( i_handle, psz_message, strlen( psz_message ) + 1, 0,
......@@ -199,7 +210,7 @@ void C_IPv6Socket::Reply( char * psz_message )
{
continue;
}
logger->Alert( "Cannot sendto (%s)\n", strerror(errno) );
//logger->Alert( "Cannot sendto (%s)\n", strerror(errno) );
}
break;
}
......
......@@ -2,7 +2,7 @@
* socket.h: VideoLAN Channel Server network facilities
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: socket.h,v 1.3 2002/06/08 18:30:17 marcari Exp $
* $Id: socket.h,v 1.4 2002/06/08 22:14:28 marcari Exp $
*
* Authors: Marc Ariberti <marcari@via.ecp.fr>
*
......@@ -24,6 +24,7 @@
class C_Socket
{
public:
C_Socket(C_Logger &logger);
virtual int Receive( char * p_buffer, int buffer_len ) = 0;
virtual void Reply( char * psz_message ) = 0;
virtual void Close( int i_ret );
......@@ -31,13 +32,13 @@ public:
virtual const char * Ntop( ) = 0;
int i_handle;
C_Logger * logger;
C_Logger &logger;
};
class C_IPv4Socket : public C_Socket
{
public:
C_IPv4Socket( C_Logger * logger, int port, char * bind_addr = NULL );
C_IPv4Socket( C_Logger &logger, int port, char * bind_addr = NULL );
virtual int Receive( char * p_buffer, int buffer_len );
virtual void Reply( char * psz_message );
virtual const char * Ntop( );
......@@ -50,7 +51,7 @@ protected:
class C_IPv6Socket : public C_Socket
{
public:
C_IPv6Socket( C_Logger * logger, int port, char * bind_addr = NULL );
C_IPv6Socket( C_Logger &logger, int port, char * bind_addr = NULL );
virtual int Receive( char * p_buffer, int buffer_len );
virtual void Reply( char * psz_message );
virtual const char * Ntop( );
......
......@@ -2,7 +2,7 @@
* vlcs.c: mini-VideoLAN Channel Server
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: vlcs.c,v 1.11 2002/06/08 18:30:17 marcari Exp $
* $Id: vlcs.c,v 1.12 2002/06/08 22:14:28 marcari Exp $
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
* Marc Ariberti <marcari@via.ecp.fr>
......@@ -52,9 +52,9 @@
C_Socket * ip_socket = NULL;
C_Config * config = NULL;
C_Logger * logger = NULL;
C_ChainedLogger * logger;
void socket_Get( void )
void socket_Get( C_Logger &logger )
{
fd_set sockets;
struct timeval timeout;
......@@ -72,7 +72,8 @@ void socket_Get( void )
{
return;
}
logger->Alert( "Cannot select (%s), exiting\n", strerror(errno) );
logger << ALERT << "Cannot select (" << strerror(errno) << "), exiting"
<< END;
Close( -1 );
}
......@@ -88,12 +89,13 @@ void socket_Get( void )
{
return;
}
logger->Alert( "Duh my controlling terminal has gone fishing\n" );
logger << ALERT << "Duh my controlling terminal has gone fishing"
<< END;
}
else if( p_buffer[0] == 'q' )
{
/* Quit */
logger->Alert( "Exiting now on admin's request\n" );
logger << ALERT << "Exiting now on admin's request" << END;
Close( 0 );
}
}
......@@ -115,7 +117,7 @@ void socket_Get( void )
{
return;
}
logger->Error( "Cannot recvfrom (%s)\n", strerror(errno) );
logger << ERROR << "Cannot recvfrom :" << strerror(errno) << END;
return;
}
p_buffer[i_len] = '\0';
......@@ -124,14 +126,14 @@ void socket_Get( void )
&i_time, psz_macad ) != 4 )
{
ip_socket->Reply( "E: Bad request" );
logger->Error( "sscanf failed (%s)\n", p_buffer );
logger << ERROR << "sscanf failed : " << p_buffer;
return;
}
if ( i_version < 13 )
{
ip_socket->Reply( "E: Version mismatch, please upgrade your software" );
logger->Alert( "Break-in attempt ! (%s)\n", ip_socket->Ntop() );
logger << ALERT << "Break-in attempt ! " << ip_socket->Ntop();
return;
}
......@@ -140,14 +142,14 @@ void socket_Get( void )
i_channel >= config->GetChannelNumber() )
{
ip_socket->Reply( "E: Channel doesn't exist" );
logger->Error( "Bad channel (%s)\n", psz_channel );
logger << ERROR << "Bad channel : " << psz_channel;
return;
}
p_channel = config->GetChannel( i_channel );
if( p_channel == NULL )
{
logger->Error( "Bad channel (%s)\n", psz_channel );
logger << ERROR << "Bad channel : " << psz_channel;
ip_socket->Reply( "E: Channel doesn't exist" );
return;
}
......@@ -171,7 +173,7 @@ void Close( int i_ret )
{
delete logger;
}
exit( i_ret );
}
......@@ -179,28 +181,40 @@ int main( int argc, char ** argv )
{
signal(SIGINT, Close);
logger = new C_Logger();
logger = new C_ChainedLogger();
#ifdef LOG_FILE
logger->Add(new C_FileLogger(LOG_FILE));
#endif
#ifdef LOG_STDOUT
logger->Add(new C_StdLogger());
#endif
#ifdef SYSLOG_NAME
// logger.add(new SysLogger(SYSLOG_NAME, SYSLOG_FACILITY));
#endif
config = new C_Config(logger);
config = new C_Config(*logger);
config->ReadFile();
#ifdef IPV6
# ifdef BIND_ADDRESS
ip_socket = new C_IPv6Socket(logger, VLCS_PORT, BIND_ADDRESS);
ip_socket = new C_IPv6Socket(*logger, VLCS_PORT, BIND_ADDRESS);
# else
ip_socket = new C_IPv6Socket(logger, VLCS_PORT);
ip_socket = new C_IPv6Socket(*logger, VLCS_PORT);
# endif
#else
# ifdef BIND_ADDRESS
ip_socket = new C_IPv4Socket(logger, VLCS_PORT, BIND_ADDRESS);
ip_socket = new C_IPv4Socket(*logger, VLCS_PORT, BIND_ADDRESS);
# else
ip_socket = new C_IPv4Socket(logger, VLCS_PORT);
ip_socket = new C_IPv4Socket(*logger, VLCS_PORT);
# endif
#endif
for( ; ; )
{
socket_Get();
socket_Get(*logger);
}
return( 0 );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment