PDA

View Full Version : [Tutorial] How to make simple logs for your server.


Face9000
08/02/2011, 07:59 PM
Hai all,this is my first tutorial so don't get mad if i will do something wrong.

And excuse my poor english,i'm Italian ^^.

Let's start:

What is this tutorial?

This tutorial is the simplest way to learn how to make logs in the fastest and simplest way.

For this tutorial,i will use the BanLog,to log all the admin bans in a file placed in /scriptfiles folder.

As first step,we need to create the forward statement:

forward BanLog(string[]);

Copy it in the first lines of your game mode.

Now we got the forward,we need to add the public() too.

So,let's add this:

public BanLog(string[])
{
new entry[128];
format(entry, sizeof(entry), "%s\n",string);
new File:hFile;
hFile = fopen("/LOGS/bans.log", io_append);
fwrite(hFile, entry);
fclose(hFile);
}


All public functions must be forwarded. That is a rule of pawno. If you do not forward a public function, you will receive warnings from the compiler. It's best to forward a function at the top of the script (but below the #include directives, though).

Let me describe what i write above:


new entry[128];
format(entry, sizeof(entry), "%s\n",string);


I used it for create a new string to write in the file.

the %s stays for the string to write in the file. (In this case,the ban).

\n is for create a new line.

And string,is for write ALL in the text file.

new File:hFile;
hFile = fopen("/LOGS/bans.log", io_append);
fwrite(hFile, entry);
fclose(hFile);

Here,we create the file place in /scriptfiles/LOGS/ called bans.log

WARNING: ALL FILES MUST BE IN /SCRIPTFILES FOLDER,PAWNO DOESNT HAVE ACCESS TO THE ROOT OF YOUR SERVER,SO YOU CANT CREATE FILES IN THE ROOT AND READING IT.

You can change the name/folder as you decided.

Ex: U can use only bans.log to access it in the only /scriptfiles folder,but i suggest to leave as is writed: /scriptfiles/LOGS.

NO NEED EVERYTIME TO WRITE /scriptfiles.

After the file is opened,the server will write it with:

fwrite(hFile, entry);

And after writing,it will be closed with:

fclose(hFile);

Well,we got now the forward and the public!

Now we need to add at your ban command,here is an example:


dcmd_b(playerid, params[])
{
new toplayerid, reason[ 128 ];
if (sscanf(params, "us[128]", toplayerid, reason))
{
SendClientMessage(playerid, 0xAA3333AA, "Syntax Error: /ban < Playerid > < Reason >");
return 1;
}
if (toplayerid == INVALID_PLAYER_ID)
{
SendClientMessage(playerid, 0xAA3333AA, "Input Error: Player is not connected or it is yourself.");
return 1;
}
new banString[128], adminName[24], bannedName[24];
GetPlayerName(playerid, adminName, 24);
GetPlayerName(toplayerid, bannedName, 24);
format(banString, 128, "%s was banned by Admin %s. Reason: %s.", bannedName, adminName, reason);
SendClientMessageToAll(0xAA3333AA, banString);
BanLog(banString);
Ban(toplayerid);
}
return 1;
}


As you can see,i've added the BanLog(banString) under:

format(banString, 128, "%s was banned by Admin %s. Reason: %s.", bannedName, adminName, reason);
SendClientMessageToAll(0xAA3333AA, banString);

And we done.Simply uh?

WARNING: You need,when add BanLog to write the string used in the command.

Ex: In this case we used banString,if u use the normal string,u need to do:

BanLog(string);

That's all,you can create all logs you want by using the forward and public writed above.

Sorry if tutorial is long but i've to describe the functions used ^^.

WillyP
08/02/2011, 08:07 PM
Wouldn't BanEx be easier..?

Face9000
08/02/2011, 08:15 PM
Yes,but BanEx just write the ban reason in samp.ban

I made this example of bans log just to help the people.

You can do how much logs you want.

Such,if u want log the players health on disconnect,then use the code writed above ^^.

admantis
09/02/2011, 12:33 AM
Genious I need this because I tried it and my server crashed :)

Face9000
09/02/2011, 08:52 AM
Have u created the log file?And in what folder you have placed it?

admantis
09/02/2011, 06:28 PM
I meant I tried it my own way and I crashed, but when I did this it worked. It wasn't because the file, it was because my script ! Thanks :D

[03]Garsino
09/02/2011, 06:32 PM
Are you using CallRemoteFunction, CallLocalFunction, SetTimer or SetTimerEx? No, then it doesn't have to be a callback. You can use stock instead.

Face9000
09/02/2011, 07:40 PM
Garsino;1061791']Are you using CallRemoteFunction, CallLocalFunction, SetTimer or SetTimerEx? No, then it doesn't have to be a callback. You can use stock instead.

Are a talking to who?

@admantis,np.

Davz*|*Criss
10/02/2011, 01:55 PM
hmmm not so good tut much people got crashed with this and got errors on creating, try well next time.

Grim_
10/02/2011, 07:54 PM
- To remove the server crashing, make a check for the file. If it doesn't exist, create it
- Check to make sure the file was opened correctly before writing to it
- Like mentioned, there's no need to make it a public function
- Format the line to use "\r\n", not just "\n" to avoid any accidents of it not writing at the beginning of the line
- This looks just like the code (same variable names and format, spacing, etc) that Seif_'s code used (in SeifAdmin)

Just my two cent.

[03]Garsino
10/02/2011, 07:57 PM
Are a talking to who?

@admantis,np.

I'm talking to you / the tutorial / the topic / everyone else.

Mean
10/02/2011, 08:13 PM
I think you should use a stock for this! But it's a fine explained tutorial.

California
10/02/2011, 09:19 PM
The BanLog looks pretty familiar...


// Yours
public BanLog(string[])
{
new entry[128];
format(entry, sizeof(entry), "%s\n",string);
new File:hFile;
hFile = fopen("/LOGS/bans.log", io_append);
fwrite(hFile, entry);
fclose(hFile);
}

// The one I found in The Godfather
public BanLog(string[])
{
new entry[256];
format(entry, sizeof(entry), "%s\n",string);
new File:hFile;
hFile = fopen("ban.log", io_append);
fwrite(hFile, entry);
fclose(hFile);
}


We also have BanEx, but this is good for some novice players out there.

silvan
13/02/2011, 11:47 AM
erm for me its writing ok, but every time it adds a new message it replace the first one every time.. what can i do that it will always go to the next line, i tried adding \n to the message but it aint working.

Jantjuh
18/02/2011, 04:04 PM
Nice tutorial!

Roomeo
20/02/2011, 04:45 AM
:S ? No Crashes For Me

Nice Tutorial Dude, Thanks