I think you should check for the mode you are opening the log file to write.

Are you opening this file in append mode to write?  

- Niranjan.
 
-------Original Message-------
 
From: Tyler Littlefield
Date: 01/01/2009 3:51:37 PM
To: [email protected]
Subject: Re: [c-prog] file getting set to 0 length?
 
P.S.
I've ran gdb on this and traced it through about 30 loops. I watched the
while loop for a bit, then dropped down in to the pole function and watched
that loop. doesn't seem to be a prob with execution.

----- Original Message ----- 
From: Tyler Littlefield 
To: [email protected] 
Sent: Thursday, January 01, 2009 1:43 PM
Subject: [c-prog] file getting set to 0 length?

Hello list,
I've got a quick question.
I have a function that is being called numerous times in a loop.
Before this function is called, things seem to work great.
Now when I call this function, my log, or the length of the log anyway is
set to 0.
Any ideas why this might be? here's the function.
POLE_ERR Server::Pole()
{
fd_set read;
int incoming=0;
sockaddr_in addr;
memset(&addr,0,sizeof(addr));
if ((select((this->maxfd+1),&read,NULL,NULL,NULL)==-1))
{
return E_SELECT;
}
for (int i=0;i<=this->maxfd;i++)
{
//see if the fd is in the list of active connections.
if ((FD_ISSET(i,&read)))
{
//see if a connection came in on the listening socket.
if (this->listener==i)
{
//accept the connection
socklen_t len=sizeof(addr);
if ((incoming=accept(this->listener,(sockaddr*)&addr,&len)==-1))
{
return E_ACCEPT;
}
User *usr=new User(incoming,addr);
FD_SET(incoming,&this->master);
//update the max file descriptor
if (incoming<this->maxfd)
{
this->maxfd=incoming;
}
this->connections.push_back(usr);
char* msg=new char[128];
msg=(char*)"New connection from: ";
strcat(msg,inet_ntoa(addr.sin_addr));
string message=msg;
cout << message << endl;
cout << "Wrote message" << endl;
delete []msg;
Log::WriteLog(message);
}
}
}
return E_SUCCESS;
}

Before I had this function returning an error enum, it would just return
true or false, and the log would be written with the error.
Now it returns an error, but the log still gets erased.
here's my write function also, if it helps.
Well, I'll provide the whole log setup.
#include "log.h"
//we define our namespace so that we won't have conflicting functions:
namespace Log
{
static ofstream LogOut;
static BOOL LoggerInitialized=false;

BOOL InitializeLogger()
{
#ifdef WRITE_SYSLOG
openlog(MUD_NAME.c_str(),LOG_PID,0);
#endif
LogOut.open("log.log");
LoggerInitialized=true; return true;
}

BOOL WriteLog(string message)
{
if (!LoggerInitialized)
{
return false;
}
cout << message << endl;
#ifdef WRITE_SYSLOG
syslog(LOG_MAKEPRI(LOG_DAEMON,LOG_INFO),message.c_str());
#endif
int i=message.length();
if (message[i]!='\n')
{
message+="\n";
}
int pid=getpid();
string output;
stringstream conv;
conv << pid;
output=conv.str();
output+=":"+message;
LogOut << output;
cout << output;
return true;
}

BOOL CloseLog()
{
if (!LoggerInitialized)
{
return false;
}
#ifdef WRITE_SYSLOG
closelog();
#endif
LogOut.close();
LoggerInitialized=false;
return true;
}

}
Your help on this would be great.
TIA,

[Non-text portions of this message have been removed]

[Non-text portions of this message have been removed]


 
 

[Non-text portions of this message have been removed]

Reply via email to