Post tutorial Report RSS Standard Logging Support

This tutorial will show you how to make a standard logging support for your mod or game. I am using C's file opening/closing/writing support to do this! It is pretty easy, but you might need a bit of advanced knowledge to know how to use it correctly

Posted by on - Intermediate Server Side Coding

[page=Introduction]
This tutorial will show you how to make a standard logging support for your mod or game. I am using C's file opening/closing/writing support to do this! It is pretty easy, but you might need a bit of advanced knowledge to know how to use it correctly!

It'll be quick and painless until we get to the big part, which you do yourself!

[page=Opening and Closing]
Alright, first we need a local file.
In some include file that every file you need uses, add this:

// Paril Logging
FILE *logged;
// Paril Logging

Now, that file is everywhere!

Now, we will make a new function to initiate the logging.
Also, it will log your date and time it started too

// Paril Logging
void InitLogging (void)
{
    char tmpbuf[20]; // Temp
	char buf2[80]; // Temp
    _strtime(tmpbuf); // Set tmpbuf to time
	_strdate(buf2); // Set buf2 to date

// NOTE HERE:
// For logging, as you can see, I used "a"
// which means appending (adding from end of file)
// you can use "r" for reading and "w" for write and delete 
// contents when you open it again. Appending means
// that it will stay no matter what, so every match is recorded

	if ((logged = fopen("logged.txt", "a")) == NULL) // if no file found..
		printf ("File not found, creating one.."); // Change this to your mod/game' print function or whatever.

	fprintf (logged, "Logging started at %s, on %s\n", tmpbuf, buf2); // Print this to a file
}
// Paril Logging

Run that function somewhere when your game server starts.
Now, go to your function where the game server ends.

Add this:

char tmpbuf[20];
	char buf2[80];
    _strtime(tmpbuf);
	_strdate(buf2);

	// Paril Logging
	fprintf (logged, "Logging stopped at %s, on %s\n\n\n\n", tmpbuf, buf2); // Skip a few lines aswell, make room for next log.
	fclose (logged);

Done! It'll open and close, and print those two. To log something else, use fprintf like so:

fprintf (logged, TEXTHERE, ARGUMENTS);
^^^^^
|||||
the FILE we made

We're done! Have fun with it!

I recommend you stick one where someone connects, enters the game, dies/gets killed, changes name, changes model, says something and disconnects.

-Paril

Post comment Comments
Arxae
Arxae - - 718 comments

and it is for what game???
i guess hl or another quake based game

Reply Good karma Bad karma+1 vote
BigBird
BigBird - - 1,161 comments

I'm sure this could be used for any mod/game engine with a C / C++ base, unfortuanly will not work for scripting based mod engines for obvious reasons.

Reply Good karma Bad karma+1 vote
Paril Author
Paril - - 24 comments

BigBird is correct, any C/C++ base. I did this in C, on the Quake2 DLL.

Reply Good karma+1 vote
Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: