Commit 31dd202d authored by Damien Lucas's avatar Damien Lucas

. Few changes in telnet

. Now we are able to log inside a log file
parent 49e25f05
......@@ -36,8 +36,8 @@ static void IF_telnet_commands (int iSocket, char* sUser);
static void Menu_init (struct IF_Menu* mMenu);
static void Menu_send (int iSocket, struct IF_Menu mMenu);
ERR_CODE TELNET_stop(unsigned int * uStop);
ERR_CODE TELNET_reload(unsigned int * uStop);
ERR_CODE TELNET_stop();
ERR_CODE TELNET_reload();
static char* Catch_Word (int iSocket);
static char* Catch_Passwd (int iSocket);
......@@ -47,6 +47,7 @@ static void Message_send (int iSocket, char* sMessage, size_t size);
static void Prompt_send (int iSocket, char* sUser);
static unsigned int uStop;
/******************************************************************************
......@@ -183,7 +184,6 @@ static char* IF_telnet_login (int iSocket)
static void IF_telnet_commands (int iSocket, char* sUser)
{
char* sCmd;
unsigned int uStop;
struct IF_Menu mMenu;
struct IFT_Command* z;
......@@ -200,7 +200,7 @@ static void IF_telnet_commands (int iSocket, char* sUser)
{
if(!strcmp(sCmd,z->sName))
{
z->function(&uStop);
z->function();
}
}
free(sCmd);
......@@ -216,30 +216,31 @@ static void IF_telnet_commands (int iSocket, char* sUser)
static void Menu_init (struct IF_Menu* mMenu)
{
mMenu->Command=malloc(sizeof(struct IFT_Command));
mMenu->Command->next=malloc(sizeof(struct IFT_Command));
mMenu->Command->iIndex='0';
mMenu->Command->function=TELNET_stop;
sprintf(mMenu->Command->sName,"logout");
sprintf(mMenu->Command->sDescr,"Close the current connection");
mMenu->Command->next=malloc(sizeof(struct IFT_Command));
mMenu->Command->next->iIndex='1';
mMenu->Command->next->function=TELNET_reload;
sprintf(mMenu->Command->next->sName,"reload");
sprintf(mMenu->Command->next->sDescr,"Reload the database");
mMenu->Command->next->next=NULL;
}
ERR_CODE TELNET_stop(unsigned int * uStop)
ERR_CODE TELNET_stop()
{
*uStop=1;
uStop=1;
return 0;
}
ERR_CODE TELNET_reload(unsigned int * uStop)
ERR_CODE TELNET_reload()
{
IF_reload();
return 1;
return 0;
}
/******************************************************************************
* Menu_send() *
......
#include <pthread.h>
#include "types.h"
#include "logger.h"
#define LOGBUFF_MAXSIZE 120
char * VS_log_errstr(ERR_CODE err)
/* We just use a single logFile */
static FILE * fdLogFile;
static unsigned char method;
pthread_mutex_t ptmLogFile;
pthread_mutex_t ptmLogScreen;
static void VS_log_File(LOG_LEVELS level, LOG_MODULES module, char* format, va_list ap)
{
static char * str[]=
char MsgBuff[LOGBUFF_MAXSIZE+20];
time_t MsgTime;
struct tm * ptmTime;
#ifndef DEBUG
if(level!=LOGDEBUG)
{
"no error",
"unable to send data to remote host",
"unable to connect to remote host",
"unable to execute a select() operation",
"unable to receive data from remote host",
"unable to open a socket",
"unable to bind a socket",
"timeout",
"unable to allocate memory",
"object not found",
"unable to spawn a thread",
"unable to open, read or write a file",
"parse error",
"snmp error",
"pipe creation error",
"unable to write to a pipe",
"trying to do a walk on a switch is currently walked by an other thread",
"initiatization of the switch failed"
};
return str[err];
#endif
/* Record the time */
time(&MsgTime);
ptmTime=localtime(&MsgTime);
sprintf(MsgBuff, "%02i/%02i %02i:%02i:%02i ", ptmTime->tm_mday,
ptmTime->tm_mon+1, ptmTime->tm_hour,
ptmTime->tm_min, ptmTime->tm_sec);
/* Determinate in which module it happened */
switch (module)
{
case SNMP:
sprintf(MsgBuff+strlen(MsgBuff), "[SNMP : ");
break;
case SERVER:
sprintf(MsgBuff+strlen(MsgBuff), "[SERVER: ");
break;
case DB:
sprintf(MsgBuff+strlen(MsgBuff), "[DB : ");
break;
case CFG:
sprintf(MsgBuff+strlen(MsgBuff), "[CFG : ");
break;
case IF:
sprintf(MsgBuff+strlen(MsgBuff), "[IF : ");
break;
case TELNET:
sprintf(MsgBuff+strlen(MsgBuff), "[TELNET: ");
break;
case LOGGER:
sprintf(MsgBuff+strlen(MsgBuff), "[LOGGER: ");
break;
}
/* Determinate the error level */
switch (level)
{
case LOGDEBUG:
sprintf(MsgBuff+strlen(MsgBuff), "debug ] ");
break;
case LOGINFO:
sprintf(MsgBuff+strlen(MsgBuff), "info ] ");
break;
case LOGWARNING:
sprintf(MsgBuff+strlen(MsgBuff), "Warning ] ");
break;
case LOGFATAL:
sprintf(MsgBuff+strlen(MsgBuff), "FATAL ] ");
break;
case LOGERROR:
sprintf(MsgBuff+strlen(MsgBuff), "ERROR ] ");
break;
}
/* XXX No error checking ! What length is the message ? */
vsnprintf(MsgBuff+strlen(MsgBuff), LOGBUFF_MAXSIZE, format, ap);
{
int i=strlen(MsgBuff);
MsgBuff[i]=27;
MsgBuff[i+1]=0;
sprintf(MsgBuff+strlen(MsgBuff), "[0m\n");
}
pthread_mutex_lock(&ptmLogFile);
fprintf(fdLogFile, MsgBuff);
fflush(fdLogFile);
pthread_mutex_unlock(&ptmLogFile);
#ifndef DEBUG
}
#endif
}
ERR_CODE VS_log(LOG_LEVELS level, LOG_MODULES module, char* format, ...)
static void VS_log_Screen(LOG_LEVELS level, LOG_MODULES module,
char* format, va_list ap)
{
va_list ap;
char MsgBuff[LOGBUFF_MAXSIZE+20];
time_t MsgTime;
struct tm * ptmTime;
......@@ -46,6 +125,8 @@ ERR_CODE VS_log(LOG_LEVELS level, LOG_MODULES module, char* format, ...)
ptmTime->tm_mon+1, ptmTime->tm_hour,
ptmTime->tm_min, ptmTime->tm_sec);
/* Determinate in which module it happened */
switch (module)
{
......@@ -72,6 +153,10 @@ ERR_CODE VS_log(LOG_LEVELS level, LOG_MODULES module, char* format, ...)
case TELNET:
sprintf(MsgBuff+strlen(MsgBuff), "[TELNET: ");
break;
case LOGGER:
sprintf(MsgBuff+strlen(MsgBuff), "[LOGGER: ");
break;
}
......@@ -112,7 +197,6 @@ ERR_CODE VS_log(LOG_LEVELS level, LOG_MODULES module, char* format, ...)
}
/* XXX No error checking ! What length is the message ? */
va_start(ap, format);
vsnprintf(MsgBuff+strlen(MsgBuff), LOGBUFF_MAXSIZE, format, ap);
{
int i=strlen(MsgBuff);
......@@ -121,10 +205,144 @@ ERR_CODE VS_log(LOG_LEVELS level, LOG_MODULES module, char* format, ...)
sprintf(MsgBuff+strlen(MsgBuff), "[0m\n");
}
va_end(ap);
pthread_mutex_lock(&ptmLogScreen);
printf("%s", MsgBuff);
pthread_mutex_unlock(&ptmLogScreen);
#ifndef DEBUG
}
#endif
}
void VS_log(LOG_LEVELS level, LOG_MODULES module, char* format, ...)
{
va_list ap;
va_start(ap,format);
if (method==FILE_ONLY)
{
VS_log_File(level, module,format,ap);
}
else if (method==SCREEN_ONLY)
{
VS_log_Screen(level, module,format,ap);
}
else if (method==BOTH)
{
VS_log_File(level, module,format,ap);
VS_log_Screen(level, module,format,ap);
}
va_end(ap);
}
/******************************************************************************
* Open the log file *
******************************************************************************/
int OpenLogFile (char * sLogName)
{
int iRc;
iRc=0;
if( !(fdLogFile = fopen (sLogName, "w+")))
{
iRc=1;
VS_log(LOGFATAL, LOGGER, "Unable to open %s log file ", sLogName);
}
else
{
/* Init the mutex */
iRc = pthread_mutex_init (&ptmLogFile, NULL);
}
return iRc;
}
/******************************************************************************
* Close the LogFile *
******************************************************************************/
int CloseLogFile ()
{
int iRc;
iRc=0;
if(fdLogFile)
{
iRc=fclose(fdLogFile);
if(iRc)
{
VS_log(LOGERROR, LOGGER, "Unable to close file");
}
iRc=pthread_mutex_destroy(&ptmLogFile);
}
else
{
iRc=1;
VS_log(LOGERROR, LOGGER, "Unable to close log file: file not open");
}
return iRc;
}
/******************************************************************************
* LOG_init() : Initialize the log process
******************************************************************************
* TODO Gestion d'erreurs *
******************************************************************************/
int LOG_init(char * sLogName, unsigned char ucM)
{
int iRc;
iRc=OpenLogFile(sLogName);
method=ucM;
if(method==BOTH || method==SCREEN_ONLY)
{
pthread_mutex_init(&ptmLogScreen, NULL);
}
return 0;
}
/*****************************************************************************
* LOG_close() : Close the log process *
*****************************************************************************
* TODO Write it ! *
*****************************************************************************/
char * VS_log_errstr(ERR_CODE err)
{
static char * str[]=
{
"no error",
"unable to send data to remote host",
"unable to connect to remote host",
"unable to execute a select() operation",
"unable to receive data from remote host",
"unable to open a socket",
"unable to bind a socket",
"timeout",
"unable to allocate memory",
"object not found",
"unable to spawn a thread",
"unable to open, read or write a file",
"parse error",
"snmp error",
"pipe creation error",
"unable to write to a pipe",
"trying to do a walk on a switch is currently walked by an other thread",
"initiatization of the switch failed"
};
return str[err];
}
......@@ -5,8 +5,14 @@
#include <stdarg.h>
#include <time.h>
/* Methods for logger */
#define FILE_ONLY 1
#define SCREEN_ONLY 2
#define BOTH 3
char * VS_log_errstr(ERR_CODE err);
ERR_CODE VS_log(LOG_LEVELS level, LOG_MODULES module, char * format, ...);
void VS_log(LOG_LEVELS level, LOG_MODULES module, char * format, ...);
int LOG_init(char * sLogName, unsigned char ucM);
#endif
......@@ -44,7 +44,11 @@ unsigned int VS_init(int argc,char *argv[])
}
#endif
pthread_mutex_init(&(vs->cnx_lock),NULL);
grumpf=pthread_mutex_init(&(vs->cnx_lock),NULL);
// Initializing the log process
LOG_init("test.log",BOTH);
// Initializing the DataBase
grumpf=DB_start(&vs->db);
......
......@@ -49,7 +49,8 @@ typedef enum {SERVER,
DB,
CFG,
IF,
TELNET} LOG_MODULES;
TELNET,
LOGGER} LOG_MODULES;
/* LOG_LEVELS : levels of loggs */
typedef enum {LOGDEBUG,
......
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