Raised This Month: $ Target: $400
 0% 

Server crashes because of my plugin?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Drocona
Junior Member
Join Date: Oct 2005
Location: The Netherlands
Old 10-30-2005 , 08:43   Server crashes because of my plugin?
Reply With Quote #1

ok i made this plugin.. everything seems to be working but after a while it just crashes (read error acces memory shit)

i dont know whats the problem.... it just logs some things ingame, writing it to a .txt file.



Code:
void CTSlogging::FireGameEvent( KeyValues * event )
{
	const char * name = event->GetName();
	FileHandle_t tslogs;
    tslogs = filesystem->Open("TSlog.txt","a+t");

	if ( FILESYSTEM_INVALID_HANDLE != tslogs )
	{
		if (FStrEq(name, "round_start"))
		{
			filesystem->FPrintf(tslogs,"Round Started \n");
			filesystem->Close(tslogs);
		}

		else if (FStrEq(name, "player_say"))
		{
			int pname = event->GetInt("userid");
			const char* text = event->GetString("text");

			char buffer[300];
			Q_snprintf(buffer,300,"%s%s%s%s\n","say ",pname,": ",text);

			filesystem->FPrintf(tslogs,buffer);
			filesystem->Close(tslogs);
		}			

		else if (FStrEq(name, "player_death"))
		{
			int killer = event->GetInt("attacker");
			int victim = event->GetInt("userid");

			if (victim == killer)
			{
				filesystem->FPrintf(tslogs, killer + "comitted suicide \n");
				filesystem->Close(tslogs);
			}

			else
			{
				char buf[300];
				Q_snprintf(buf,300,"%s%s%s\n",killer," killed ",victim);

				filesystem->FPrintf(tslogs,buf);
				filesystem->Close(tslogs);
			}
		}
	} 
}
i think it might have something to do with the filesystem->FPrintf function because mani said it makes alot of crashes?
if it is that function, can someone tell me how i could write to a file without the FPrintf function and without any extra things i need to have in C++

***EDIT***
after a little testing i noticed that the server crashes after shooting a player a few times ingame
__________________
Team .Mystic - Drocona
Drocona is offline
Send a message via MSN to Drocona
BAILOPAN
Join Date: Jan 2004
Old 10-30-2005 , 08:47  
Reply With Quote #2

Just use fopen(), fprintf(), fclose() for file writing. They're fully documented, platform independent, and part of the C standard.

Why people use Valve's breakity interfaces for standard things is a mystery to me.
__________________
egg
BAILOPAN is offline
Drocona
Junior Member
Join Date: Oct 2005
Location: The Netherlands
Old 10-30-2005 , 08:55  
Reply With Quote #3

now it comes with this error after replacing filesystem things with the functions you told me and i dont get it...


Quote:
TSlogger2.cpp(311) : error C2664: 'fprintf' : cannot convert parameter 1 from 'FileHandle_t' to 'FILE *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast

edit:

ok here we go again, i looked at the help stuff on fprintf function and what do i see in the example: #include <stdio.h>
1: what the hell is this stdio.h
2: where can i find it
3: when i download it it comes up with 400 errors that it needs other files which i cant find anywhere
4: it says its standard in C++ library stuff but its not findable/usable
5: no help on this stdio.h ANYWHERE
6: no info to obtain it
7: no info how to use it in a project

this is why i used the Valve functions because stdio doesnt work
i need this stdio stuff but dont have it so is there a function which i can use WITHOUT ANY EXTRA things to C++
btw i have Visual C++
__________________
Team .Mystic - Drocona
Drocona is offline
Send a message via MSN to Drocona
BAILOPAN
Join Date: Jan 2004
Old 10-30-2005 , 09:50  
Reply With Quote #4

Quote:
Originally Posted by Drocona
1: what the hell is this stdio.h
It's "standard I/O".
Quote:
Originally Posted by Drocona
2: where can i find it
Any C compiler comes with it, it's standard.
Quote:
Originally Posted by Drocona
3: when i download it it comes up with 400 errors that it needs other files which i cant find anywhere
You did something wrong. Find the problem, then fix it. You shouldn't even be downloading stdio.h, it comes with any C compiler.

The other comments you make suggest that you should learn more about C++ and wait a bit until writing plugins. A quick example of how to use it:
Code:
#include <stdio.h>

void WriteFile(const char *file, const char *line)
{
    FILE *fp = fopen(file, "at");
    if (!fp)
       return;
    fprintf(fp, "%s", line);
    fclose(fp);
}
Use resources. It took me two seconds to google for "stdio.h" and find numerous resources:
http://www.cplusplus.com/ref/cstdio/
http://www.linuxgazette.com/issue24/rogers.html

If you have a specific problem, research it, then detail it and ask for help (specifically, ask "why?"). In your above example, you were mixing Valve's API with C API illegally. Filehandle_t * or whatever is something from Valve, if you read the prototype of fprintf() it requires a FILE *.
__________________
egg
BAILOPAN is offline
Drocona
Junior Member
Join Date: Oct 2005
Location: The Netherlands
Old 10-30-2005 , 12:04  
Reply With Quote #5

ok thanks for help.

i think i'm kinda getting the hang of it now, at least i hope...

i changed the filehandle_t from valve to FILE from the stdio, it all seems to work now with the stdio stuff

Now i made this, it compiles correct and runs on my server, it gives a console message when a round starts etc.
But it doesnt make my file "TSlog.txt" and doesnt write anything to it ofcourse because it doesnt exist.
Also my server still keeps crashing after a while, (i noticed ingame that after i shoot some people, deal some damage it crashes) maybe it's something i did wrong with the kill logging.

please check what i did wrong, heres my code again:
Code:
void CTSlogging::FireGameEvent( KeyValues * event )
{
	const char * name = event->GetName();
	const char * logfile = "TSlog.txt";

	FILE *tslogs = fopen(logfile, "at");

	if ( FILESYSTEM_INVALID_HANDLE != tslogs )
	{
		if (FStrEq(name, "round_start"))
		{
			Msg("Round Started \n");
			fprintf(tslogs,"%s","Round Started \n");
			fclose(tslogs);
		}

		else if (FStrEq(name, "player_say"))
		{
			int pname = event->GetInt("userid");
			const char* text = event->GetString("text");

			char buffer[300];
			Q_snprintf(buffer,300,"%s%s%s%s\n","say ",pname,": ",text);

			fprintf(tslogs,"%s",buffer);
			fclose(tslogs);
		}			

		else if (FStrEq(name, "player_death"))
		{
			int killer = event->GetInt("attacker");
			int victim = event->GetInt("userid");

			if (victim == killer)
			{
				Msg(killer + "comitted suicide \n");
				fprintf(tslogs,"%s",killer + "comitted suicide \n");
				fclose(tslogs);
			}

			else
			{
				char buf[300];
				Q_snprintf(buf,300,"%s%s%s\n",killer," killed ",victim);
				
				Msg(buf);
				fprintf(tslogs,"%s",buf);
				fclose(tslogs);
			}
		}
	}
}
__________________
Team .Mystic - Drocona
Drocona is offline
Send a message via MSN to Drocona
sslice
Senior Member
Join Date: Feb 2005
Location: Texas, USA
Old 10-30-2005 , 12:15  
Reply With Quote #6

Try this...
Code:
void CTSlogging::FireGameEvent( KeyValues * event )
{
	const char * name = event->GetName();
	const char * logfile = "TSlog.txt";

	FILE *tslogs = fopen(logfile, "at");

	if (tslogs)
	{
		if (FStrEq(name, "round_start"))
		{
			Msg("Round Started\n");
			fputs("Round Started\n",tslogs);
		}

		else if (FStrEq(name, "player_say"))
		{
			int userid = event->GetInt("userid");
			const char* text = event->GetString("text");

			char buffer[300];
			Q_snprintf(buffer,300,"say %d: %s\n",userid,text);

			fputs(buffer,tslogs);
		}			

		else if (FStrEq(name, "player_death"))
		{
			int killer = event->GetInt("attacker");
			int victim = event->GetInt("userid");

			if (victim == killer)
			{
 				char buffer [300];
				Q_snprintf(buffer,300,"%d commited suicide\n",victim);
				Msg(buffer);
				fputs(buffer,tslogs);
			}

			else
			{
				char buf[300];
				Q_snprintf(buf,300,"%d killed %d\n",killer,victim);
				
				Msg(buf);
				fputs(buf,tslogs);
			}
		}
		fclose(tslogs);
	}
}
If you wanted to output the player's name instead of the user id, then you will need to make a function that finds the entity of the player by looping through every index.
__________________
sslice is offline
Drocona
Junior Member
Join Date: Oct 2005
Location: The Netherlands
Old 10-30-2005 , 15:03  
Reply With Quote #7

wow thanks alot, no more crashes and it reports everything correct now!

but theres still a problem....

the TSlog.txt (the logfile) isnt there when the server starts up and it doesnt log because of that
__________________
Team .Mystic - Drocona
Drocona is offline
Send a message via MSN to Drocona
devicenull
Veteran Member
Join Date: Mar 2004
Location: CT
Old 10-30-2005 , 23:24  
Reply With Quote #8

I believe you need the path to the file, use this:

Code:
	
char tdir[512],temp[786]
m_Engine->GetGameDir(tdir,512);
	sprintf(temp,"%s\tslog.log",tdir);
Then open the file with temp
__________________
Various bits of semi-useful code in a bunch of languages: http://code.devicenull.org/
devicenull is offline
Drocona
Junior Member
Join Date: Oct 2005
Location: The Netherlands
Old 10-31-2005 , 11:09  
Reply With Quote #9

i added it but now it doesnt even write the event to the console anymore...

still no luck


*****EDIT*****

It all works now...

forgot the double backslash in the file dir
__________________
Team .Mystic - Drocona
Drocona is offline
Send a message via MSN to Drocona
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 19:02.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode