Commit 6afc322b authored by Marc Ariberti's avatar Marc Ariberti

* The logger is now a C++ object

parent 0ed6c692
......@@ -8,16 +8,16 @@ CFLAGS=-O3 -g -I. -Wall
all: vlcs
vlcs.o: vlcs.c vlcs.h logger.h
vlcs.o: vlcs.c vlcs.h logger.h socket.h config.h
$(CC) $(CFLAGS) -c -o vlcs.o vlcs.c
socket.o: socket.c socket.h logger.h vlcs.h
socket.o: socket.c socket.h logger.h vlcs.h config.h
$(CC) $(CFLAGS) -c -o socket.o socket.c
logger.o: logger.c logger.h vlcs.h
$(CC) $(CFLAGS) -c -o logger.o logger.c
config.o: config.c config.h logger.h vlcs.h
config.o: config.c config.h logger.h vlcs.h socket.h
$(CC) $(CFLAGS) -c -o config.o config.c
vlcs: vlcs.o logger.o config.o socket.o
......
......@@ -2,7 +2,7 @@
* config.c: mini-VideoLAN Channel Server, configuration system
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: config.c,v 1.3 2002/06/08 18:09:18 marcari Exp $
* $Id: config.c,v 1.4 2002/06/08 18:30:17 marcari Exp $
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
* Marc Ariberti <marcari@via.ecp.fr>
......@@ -43,6 +43,11 @@
#include "logger.h"
#include "config.h"
C_Config::C_Config(C_Logger * logger)
{
this->logger = logger;
}
/*
* Destroy the linked list
*/
......@@ -59,7 +64,7 @@ void C_Config::DestroyList( void )
}
p_channels = NULL;
log_Info("List of channels initialized\n");
logger->Info("List of channels initialized\n");
}
char * C_Config::RemoveLF( char * str )
......@@ -103,7 +108,7 @@ void C_Config::AddList( char * psz_item )
i_channel_max++;
log_Info("Adding channel %d : %s\n", i_channel_max, p_new->psz_vlc_config);
logger->Info("Adding channel %d : %s\n", i_channel_max, p_new->psz_vlc_config);
}
time_t C_Config::GetModificationTime( void )
......@@ -128,7 +133,7 @@ void C_Config::ReadFile( void )
p_config = fopen( CHANNEL_CONF, "r" );
if( p_config == NULL )
{
log_Alert( "Cannot fopen " CHANNEL_CONF " (%s)\n", strerror(errno) );
logger->Alert( "Cannot fopen " CHANNEL_CONF " (%s)\n", 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.2 2002/06/08 18:09:18 marcari Exp $
* $Id: config.h,v 1.3 2002/06/08 18:30:17 marcari Exp $
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
* Marc Ariberti <marcari@via.ecp.fr>
......@@ -32,6 +32,7 @@ typedef struct channel_s
class C_Config
{
public:
C_Config(C_Logger * logger);
void DestroyList( void );
void AddList( char * psz_item );
time_t GetModificationTime( void );
......@@ -46,4 +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;
};
......@@ -2,7 +2,7 @@
* logger.c: VideoLAN Channel Server logging facility
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: logger.c,v 1.1 2002/05/25 20:59:15 marcari Exp $
* $Id: logger.c,v 1.2 2002/06/08 18:30:17 marcari Exp $
*
* Authors: Marc Ariberti <marcari@via.ecp.fr>
*
......@@ -31,14 +31,11 @@
#include <time.h>
#include "vlcs.h"
#ifdef LOG_FILE
static FILE * log_file;
#endif
#include "logger.h"
/* logging system */
void log_Init( void )
C_Logger::C_Logger( void )
{
#ifdef SYSLOG_NAME
openlog( SYSLOG_NAME, LOG_PERROR, SYSLOG_FACILITY );
......@@ -49,7 +46,7 @@ void log_Init( void )
#endif
}
void log_Close( void )
C_Logger::~C_Logger( void )
{
#ifdef SYSLOG_NAME
closelog();
......@@ -60,7 +57,7 @@ void log_Close( void )
#endif
}
void log_Info(char * format, ...)
void C_Logger::Info(char * format, ...)
{
va_list ap;
va_start(ap, format);
......@@ -80,7 +77,7 @@ void log_Info(char * format, ...)
va_end(ap);
}
void log_Warn(char * format, ...)
void C_Logger::Warn(char * format, ...)
{
va_list ap;
va_start(ap, format);
......@@ -100,7 +97,7 @@ void log_Warn(char * format, ...)
va_end(ap);
}
void log_Error(char * format, ...)
void C_Logger::Error(char * format, ...)
{
va_list ap;
va_start(ap, format);
......@@ -120,7 +117,7 @@ void log_Error(char * format, ...)
va_end(ap);
}
void log_Alert(char * format, ...)
void C_Logger::Alert(char * format, ...)
{
va_list ap;
va_start(ap, format);
......
......@@ -2,7 +2,7 @@
* logger.h: VideoLAN Channel Server logging facility
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: logger.h,v 1.1 2002/05/25 20:59:15 marcari Exp $
* $Id: logger.h,v 1.2 2002/06/08 18:30:17 marcari Exp $
*
* Authors: Marc Ariberti <marcari@via.ecp.fr>
*
......@@ -21,10 +21,19 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
void log_Init( void );
void log_Close( void );
void log_Info(char * format, ...);
void log_Warn(char * format, ...);
void log_Error(char * format, ...);
void log_Alert(char * format, ...);
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
};
......@@ -2,7 +2,7 @@
* socket.c: VideoLAN Channel Server network facilities
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: socket.c,v 1.2 2002/06/08 18:09:18 marcari Exp $
* $Id: socket.c,v 1.3 2002/06/08 18:30:17 marcari Exp $
*
* Authors: Marc Ariberti <marcari@via.ecp.fr>
*
......@@ -21,6 +21,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
......@@ -30,9 +31,9 @@
#include <time.h>
#include <iostream>
#include "config.h"
#include "vlcs.h"
#include "logger.h"
#include "config.h"
#include "socket.h"
void C_Socket::Close( int i_ret )
......@@ -44,24 +45,26 @@ void C_Socket::Close( int i_ret )
* C_IPv4Socket class implementation
*/
C_IPv4Socket::C_IPv4Socket(int port, char * bind_addr = NULL)
C_IPv4Socket::C_IPv4Socket(C_Logger * logger, int port, char * bind_addr = NULL)
{
int i_opt;
struct sockaddr_in sa_server;
struct in_addr s_bind_addr;
this->logger = logger;
i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
if( i_handle < 0 )
{
log_Alert( "Cannot open socket (%s), exiting\n", strerror(errno) );
logger->Alert( "Cannot open socket (%s), exiting\n", strerror(errno) );
::Close( -1 );
}
if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR, &i_opt, sizeof(i_opt) )
< 0 )
{
log_Alert("Cannot setsockopt (%s), exiting\n", strerror(errno) );
logger->Alert("Cannot setsockopt (%s), exiting\n", strerror(errno) );
::Close( -1 );
}
......@@ -80,7 +83,7 @@ C_IPv4Socket::C_IPv4Socket(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 )
{
log_Alert("Cannot bind (%s), exiting\n", strerror(errno) );
logger->Alert("Cannot bind (%s), exiting\n", strerror(errno) );
::Close( -1 );
}
}
......@@ -99,7 +102,7 @@ void C_IPv4Socket::Reply( char * psz_message )
time(&tm);
/* TODO: remove this ugly kludge */
log_Info("%s : %s => %s\n",
logger->Info("%s : %s => %s\n",
C_Config::RemoveLF( asctime( localtime( &tm ) ) ),
Ntop( ), psz_message );
for( ; ; )
......@@ -112,7 +115,7 @@ void C_IPv4Socket::Reply( char * psz_message )
{
continue;
}
log_Alert( "Cannot sendto (%s)\n", strerror(errno) );
logger->Alert( "Cannot sendto (%s)\n", strerror(errno) );
}
break;
}
......@@ -126,24 +129,26 @@ const char * C_IPv4Socket::Ntop( void )
* C_IPv6Socket class implementation
*/
C_IPv6Socket::C_IPv6Socket(int port, char * bind_addr = NULL)
C_IPv6Socket::C_IPv6Socket(C_Logger * logger, int port, char * bind_addr = NULL)
{
int i_opt;
struct sockaddr_in6 sa_server;
struct in6_addr s_bind_addr;
this->logger = logger;
i_handle = socket( AF_INET6, SOCK_DGRAM, 0 );
if( i_handle < 0 )
{
log_Alert( "Cannot open socket (%s), exiting\n", strerror(errno) );
logger->Alert( "Cannot open socket (%s), exiting\n", strerror(errno) );
::Close( -1 );
}
if( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR, &i_opt, sizeof(i_opt) )
< 0 )
{
log_Alert("Cannot setsockopt (%s), exiting\n", strerror(errno) );
logger->Alert("Cannot setsockopt (%s), exiting\n", strerror(errno) );
::Close( -1 );
}
......@@ -162,7 +167,7 @@ C_IPv6Socket::C_IPv6Socket(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 )
{
log_Alert("Cannot bind (%s), exiting\n", strerror(errno) );
logger->Alert("Cannot bind (%s), exiting\n", strerror(errno) );
::Close( -1 );
}
}
......@@ -181,7 +186,7 @@ void C_IPv6Socket::Reply( char * psz_message )
time(&tm);
/* TODO: remove this ugly kludge */
log_Info("%s : %s => %s\n",
logger->Info("%s : %s => %s\n",
C_Config::RemoveLF( asctime( localtime( &tm ) ) ),
Ntop( ), psz_message );
for( ; ; )
......@@ -194,7 +199,7 @@ void C_IPv6Socket::Reply( char * psz_message )
{
continue;
}
log_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.2 2002/06/08 18:09:18 marcari Exp $
* $Id: socket.h,v 1.3 2002/06/08 18:30:17 marcari Exp $
*
* Authors: Marc Ariberti <marcari@via.ecp.fr>
*
......@@ -31,12 +31,13 @@ public:
virtual const char * Ntop( ) = 0;
int i_handle;
C_Logger * logger;
};
class C_IPv4Socket : public C_Socket
{
public:
C_IPv4Socket( 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( );
......@@ -49,7 +50,7 @@ protected:
class C_IPv6Socket : public C_Socket
{
public:
C_IPv6Socket( 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.10 2002/06/08 18:09:18 marcari Exp $
* $Id: vlcs.c,v 1.11 2002/06/08 18:30:17 marcari Exp $
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
* Marc Ariberti <marcari@via.ecp.fr>
......@@ -52,6 +52,7 @@
C_Socket * ip_socket = NULL;
C_Config * config = NULL;
C_Logger * logger = NULL;
void socket_Get( void )
{
......@@ -71,7 +72,7 @@ void socket_Get( void )
{
return;
}
log_Alert( "Cannot select (%s), exiting\n", strerror(errno) );
logger->Alert( "Cannot select (%s), exiting\n", strerror(errno) );
Close( -1 );
}
......@@ -87,12 +88,12 @@ void socket_Get( void )
{
return;
}
log_Alert( "Duh my controlling terminal has gone fishing\n" );
logger->Alert( "Duh my controlling terminal has gone fishing\n" );
}
else if( p_buffer[0] == 'q' )
{
/* Quit */
log_Alert( "Exiting now on admin's request\n" );
logger->Alert( "Exiting now on admin's request\n" );
Close( 0 );
}
}
......@@ -114,7 +115,7 @@ void socket_Get( void )
{
return;
}
log_Error( "Cannot recvfrom (%s)\n", strerror(errno) );
logger->Error( "Cannot recvfrom (%s)\n", strerror(errno) );
return;
}
p_buffer[i_len] = '\0';
......@@ -123,14 +124,14 @@ void socket_Get( void )
&i_time, psz_macad ) != 4 )
{
ip_socket->Reply( "E: Bad request" );
log_Error( "sscanf failed (%s)\n", p_buffer );
logger->Error( "sscanf failed (%s)\n", p_buffer );
return;
}
if ( i_version < 13 )
{
ip_socket->Reply( "E: Version mismatch, please upgrade your software" );
log_Alert( "Break-in attempt ! (%s)\n", ip_socket->Ntop() );
logger->Alert( "Break-in attempt ! (%s)\n", ip_socket->Ntop() );
return;
}
......@@ -139,14 +140,14 @@ void socket_Get( void )
i_channel >= config->GetChannelNumber() )
{
ip_socket->Reply( "E: Channel doesn't exist" );
log_Error( "Bad channel (%s)\n", psz_channel );
logger->Error( "Bad channel (%s)\n", psz_channel );
return;
}
p_channel = config->GetChannel( i_channel );
if( p_channel == NULL )
{
log_Error( "Bad channel (%s)\n", psz_channel );
logger->Error( "Bad channel (%s)\n", psz_channel );
ip_socket->Reply( "E: Channel doesn't exist" );
return;
}
......@@ -157,34 +158,43 @@ void socket_Get( void )
void Close( int i_ret )
{
if (ip_socket != NULL)
if( ip_socket != NULL )
{
ip_socket->Close( i_ret );
delete ip_socket;
}
log_Close();
if( config != NULL )
{
delete config;
}
if( logger != NULL )
{
delete logger;
}
exit( i_ret );
}
int main( int argc, char ** argv )
{
signal(SIGINT, Close);
log_Init();
config = new C_Config();
logger = new C_Logger();
config = new C_Config(logger);
config->ReadFile();
#ifdef IPV6
# ifdef BIND_ADDRESS
ip_socket = new C_IPv6Socket(VLCS_PORT, BIND_ADDRESS);
ip_socket = new C_IPv6Socket(logger, VLCS_PORT, BIND_ADDRESS);
# else
ip_socket = new C_IPv6Socket(VLCS_PORT);
ip_socket = new C_IPv6Socket(logger, VLCS_PORT);
# endif
#else
# ifdef BIND_ADDRESS
ip_socket = new C_IPv4Socket(VLCS_PORT, BIND_ADDRESS);
ip_socket = new C_IPv4Socket(logger, VLCS_PORT, BIND_ADDRESS);
# else
ip_socket = new C_IPv4Socket(VLCS_PORT);
ip_socket = new C_IPv4Socket(logger, VLCS_PORT);
# endif
#endif
......
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