Author Topic: [TUTORIAL]How to make simple logs for your server.  (Read 792 times)

Face9000

  • Newbie
  • *
  • Posts: 7
  • Reputation +0/-0
    • View Profile
[TUTORIAL]How to make simple logs for your server.
« on: February 09, 2011, 07:44:05 pm »
Since i havent finded a tutorial board,i post here.

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:

Code: [Select]
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:

Code: [Select]
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:

Code: [Select]
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.

Code: [Select]
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:

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

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

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


Code: [Select]
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:

       
Code: [Select]
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 ^^.

Share on Bluesky Share on Facebook


Bloodcell

  • Global Moderator
  • Newbie
  • ****
  • Posts: 15
  • Reputation +1/-0
    • View Profile
Re: [TUTORIAL]How to make simple logs for your server.
« Reply #1 on: February 09, 2011, 07:46:16 pm »
Good one!

Thanks :)

Jane

  • Management
  • Full
  • *******
  • Posts: 130
  • Reputation +1/-0
  • mIRC Scripters
    • View Profile
Re: [TUTORIAL]How to make simple logs for your server.
« Reply #2 on: February 09, 2011, 07:59:22 pm »
Wonderfull TUT, Keep it up Face !

Face9000

  • Newbie
  • *
  • Posts: 7
  • Reputation +0/-0
    • View Profile
Re: [TUTORIAL]How to make simple logs for your server.
« Reply #3 on: February 09, 2011, 08:02:47 pm »
Thanks,i made it for Sa-Mp forum and pasted it here.

Jane

  • Management
  • Full
  • *******
  • Posts: 130
  • Reputation +1/-0
  • mIRC Scripters
    • View Profile
Re: [TUTORIAL]How to make simple logs for your server.
« Reply #4 on: February 09, 2011, 08:06:45 pm »
Sticky.

CJ101

  • PAWN Expert
  • Global Moderator
  • Newbie
  • ****
  • Posts: 39
  • Reputation +1/-0
  • SA-MP Ftw
    • View Profile
    • PS3GamingWorld
Re: [TUTORIAL]How to make simple logs for your server.
« Reply #5 on: February 10, 2011, 03:42:21 am »
Dont forget - if the folder is not created before the script is started - you may get some errors.

Face9000

  • Newbie
  • *
  • Posts: 7
  • Reputation +0/-0
    • View Profile
Re: [TUTORIAL]How to make simple logs for your server.
« Reply #6 on: February 10, 2011, 09:45:03 am »
Dont forget - if the folder is not created before the script is started - you may get some errors.

Yes.