[Tutorial] How to make simple logs for your server.
#1

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:

pawn Code:
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:

pawn Code:
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:

pawn Code:
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.

pawn Code:
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:

pawn Code:
fwrite(hFile, entry);
And after writing,it will be closed with:

pawn Code:
fclose(hFile);
Well,we got now the forward and the public!

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


pawn Code:
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:

pawn Code:
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 ^^.
Reply
#2

Wouldn't BanEx be easier..?
Reply
#3

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 ^^.
Reply
#4

Genious I need this because I tried it and my server crashed
Reply
#5

Have u created the log file?And in what folder you have placed it?
Reply
#6

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
Reply
#7

Are you using CallRemoteFunction, CallLocalFunction, SetTimer or SetTimerEx? No, then it doesn't have to be a callback. You can use stock instead.
Reply
#8

Quote:
Originally Posted by [03]Garsino
View Post
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.
Reply
#9

hmmm not so good tut much people got crashed with this and got errors on creating, try well next time.
Reply
#10

- 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.
Reply
#11

Quote:
Originally Posted by Logitech90
View Post
Are a talking to who?

@admantis,np.
I'm talking to you / the tutorial / the topic / everyone else.
Reply
#12

I think you should use a stock for this! But it's a fine explained tutorial.
Reply
#13

The BanLog looks pretty familiar...

pawn Code:
// 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.
Reply
#14

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.
Reply
#15

Nice tutorial!
Reply
#16

:S ? No Crashes For Me

Nice Tutorial Dude, Thanks
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)