25.08.2013, 19:18
Tutorial - Logging things that happen on your server
Description:
As people are making more and more servers, and more and more people are using SA:MP, server owners would want to keep track of what's going on on their servers. Therefore, I decided to make a topic showing you guys how to log things that happen on your server. It's very simple once you get the hang of it. I'll explain everything as we go along. This is my first tutorial and my first time explaining pawn to someone else. If I went wrong somewhere, please, tell me.
Tutorial:
First of all, add these to the top of your script, somewhere before main().
pawn Code:
new receivername[MAX_PLAYER_NAME];
new sendername[MAX_PLAYER_NAME];
new giveplayerid[MAX_PLAYERS];
The next thing you'd want to do is create a forward and public declaration for the logs you want to make. I'd highly recommend that you don't log every single command onto a different file, as there would be too many files and it'll be a lot harder work. I'll make a KickLog for this tutorial. For that, we need to forward the callback.
pawn Code:
forward KickLog(string[])
So, anywhere under main(), you can add the following, and it's good because you can even add it at the bottom of your script so you know where it is at all times.
pawn Code:
public KickLog(string[])
{
new entry[250];
format(entry, sizeof(entry), "%s",string);
new File:hFile;
hFile = fopen("/Logs/kick.log", io_append);
fwrite(hFile, entry);
fclose(hFile);
}
pawn Code:
new entry[250];
pawn Code:
format(entry, sizeof(entry), "%s",string);
pawn Code:
new File:hFile;
pawn Code:
hFile = fopen("/Logs/kick.log", io_append);
pawn Code:
fwrite(hFile, entry);
pawn Code:
fclose(hFile);
Now, the next step is to create what the log will log will look like once something happens. Let's do that now. So, if you are making a kick log, then obviously you'd want to go to the command /kick. So go there and then somewhere near the bottom you can add the following:
pawn Code:
new year, month, day; getdate(year, month, day); new hour, minute, second; gettime(hour, minute, second);
pawn Code:
new senderip[64]; GetPlayerIp(playerid, senderip, sizeof(senderip));
pawn Code:
new receiverip[64]; GetPlayerIp(giveplayerid, receiverip, sizeof(receiverip));
pawn Code:
format(string, sizeof(string), "%d/%d/%d %d:%d:%d Sendername: %s SenderIP: %s Receivername: %s ReceiverIP: %s Reason: %s \r\n", day, month, year, hour, minute, second, sendername, senderip, receivername, receiverip, reason);
What it will all look like overall:
Your code would look eventually something like the code below. PS: I am using y_commands, it's pretty much the same process with any command processor, including the default one.
pawn Code:
new receivername[MAX_PLAYER_NAME];
new sendername[MAX_PLAYER_NAME];
new giveplayerid[MAX_PLAYERS];
forward KickLog(string[]);
public KickLog(string[])
{
new entry[256];
format(entry, sizeof(entry), "%s",string);
new File:hFile;
hFile = fopen("/Logs/kick.log", io_append);
fwrite(hFile, entry);
fclose(hFile);
}
YCMD:kick(playerid, params[], help)
{
if(help)
{
SendClientMessage(playerid, COLOR_WHITE, "{AFAFAF}Command /kick: {FFFFFF}Admins can use this command to kick other players.");
}
else
{
if(PlayerInfo[playerid][pAdmin] >= 1)
{
new string[158], reason[120], giveplayerid;
if(sscanf(params, "us[120]", giveplayerid, reason)) return SendClientMessage(playerid, COLOR_WHITE, "{AFAFAF}Syntax: {FFFFFF}/kick [playerid/name] [reason]");
if(giveplayerid == INVALID_PLAYER_ID) return SendClientMessage(playerid, COLOR_WHITE, "{AFAFAF}Error: {FFFFFF}Player not connected.");
GetPlayerName(playerid, sendername, sizeof(sendername)); GetPlayerName(giveplayerid, receivername, sizeof(receivername));
format(string, sizeof(string), "{FF6347}Admin %s kicked %s, reason: %s", sendername, receivername, reason);
SendClientMessageToAll(COLOR_WHITE, string); SetTimerEx("giveplayeridKick", 1000, false, "i", giveplayerid);
new year, month, day; getdate(year, month, day); new hour, minute, second; gettime(hour, minute, second);
new senderip[64]; GetPlayerIp(playerid, senderip, sizeof(senderip));
new receiverip[64]; GetPlayerIp(giveplayerid, receiverip, sizeof(receiverip));
format(string, sizeof(string), "%d/%d/%d %d:%d:%d Sendername: %s SenderIP: %s Receivername: %s ReceiverIP: %s Reason: %s \r\n", day, month, year, hour, minute, second, sendername, senderip, receivername, receiverip, reason); KickLog(string);
} else { SendClientMessage(playerid, COLOR_WHITE, ""MinimumAdmin1""); return 1; }
}
return 1;
}
That's it, you've made a kick log. Make sure you have a folder called Logs in your scriptfiles, or it will not work. This is what the log would look like after you've completed it:
Code:
24/8/2013 18:57:25 Sendername: DanishHaq SenderIP: 192.168.0.8 Receivername: testaccount ReceiverIP: 192.168.0.8 Reason: this is the reason
I recommend opening the file with gVim (http://www.vim.org/download.php) as it will show the lines between the kick entries and makes it easier for you to read and understand.