Tyler Littlefield wrote: > > 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] <mailto:c-prog%40yahoogroups.com> > 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] > > I think I should point this out really quick. You aren't setting your "msg" string correctly. Whenever the compiler sees a string literal, "New connection from:", it is going to allocate that on the stack. You then assign it to the "msg" variable, which gives the "msg" variable a whole new memory address (the memory address of the "New conenction from:" string). Here's a little visual:
char* msg=new char[128]; std::cout << (unsigned int)msg << "\n"; msg=(char*)"New connection from: "; std::cout << (unsigned int)msg << "\n"; It's showing you the address of the same variable before and after you assign the string literal. So, you need to use "strcpy" or another method to set the actual string. strcpy(msg, "New connection from: "); That may not be the problem you are having, but that's a big bug, and who knows, maybe it might fix the problem. ;)
