[Tutorial] How to Make a Simple Server Logging System
#1

If a player tells you that the car he bought in game disappeared, what do you do? It might be a bug or he might lie to get money in-game. One way of knowing for sure what happened is to save logs with the actions that your players do.

In this tutorial I will explain how to log stuff using text files.
I am using a function called fileLog to log stuff. The function has 2 parameters: file and string. The file parameter is used to select the file where the data is stored. I use separate files for each actions (bans.log for bans and money.log for money stuff). The string parameter is used to pass the data that will be added to the file.

The function looks like this:
Код:
stock fileLog(file[], string[]){
        // get the time and date.
	new time[6];
	gettime(time[0], time[1], time[2]);
	getdate(time[3], time[4], time[5]);

        // store the time and date in the timestr variable.
	new timestr[32], data[128];
	format(timestr,32,"[%02d.%02d|%02d:%02d] ",time[5],time[4], time[0], time[1]);

        // store the data passed using the string variable in the data variable.
        // '\r\n' is used to create a new line for each action
	format(data, sizeof(data), "%s%s\r\n",timestr,string);

        // open the file that was passed through the file parameter. I am using the logs folder to store the logs. you can use another folder.
        new File:hFile, thefile[32];
	format(thefile, sizeof(thefile), "/logs/%s.log", file);
        hFile = fopen(thefile, io_append);

        // write the data that was passed through the string parameter to the file
        fwrite(hFile, data);

        // close the file
        fclose(hFile);

	return 1;
}
The logs are saved in the scriptfiles/logs folder.

To log player deaths, you would do this:
Код:
public OnPlayerDeath(playerid, killedid, reason){
new playerName[MAX_PLAYER_NAME], log[128];
GetPlayerName(playerid);
format(log, sizeof(log), "%s died.", playerName);
fileLog("deaths",)
}
Then you should go to the folder scriptfiles/logs and create a file called deaths.log

The logs in the deaths.log file will look like this:
Код:
[29.11|00:26] im died.
[29.11|00:28] im died.
[29.11|00:30] im died.
I use logs for money transfers (money.log), cars bought/sold (cars.log), houses bought/sold (houses.log), businesses bought/sold (businesses.log), server restarts - using OnGameModeInit (init.log), news (news.log), admin actions (admin.log), kicks and bans (bans.log), mysql errors (errors.log), robs (rob.log), kills (kills.log).

The logs are very useful when something goes wrong with your server.

So that's it.
Thanks for reading.
Not the most useful tutorial in the world, but it may be useful to some people.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)