[Tutorial] Creating a ChatLog
#1

I have looked around the forums a bit before, and noticed that no one has ever made a tutorial on how to create a chat log. I believe chat logs are an important part in a server. As they help you catch the people spamming, and or being racist etc. This is the most up to date info as of 10/17/10 (please correct me if i am wrong).

So with that, lets get started!



File Creation
Lets start off by creating the "logs folder" and "chatlog.txt"We will be putting these into your "scriptfiles folder", so lets go into that folder and create a new folder. Then name it "logs", open your newly created logs folder and create a new text doccument(Win7: Right click> new > Text Doccument). Lets name this new text Doc. ChatLog (Vista: ChatLog.txt). Ok so we have completed the file creation part of this tutorial, now its time to get into the scripting.

Scripting
Now its time to get into the fun stuff, lets open up a new or existing gamemode in pawno (Or any other editor you may posses). Lets search for
pawn Code:
OnPlayerText
Now under that callback we will add the following code:
pawn Code:
new File:chatlog = fopen("/logs/ChatLog.txt",io_append),chat[128],chater[128],hour,minute,second;
    GetPlayerName(playerid,chater,sizeof(chater));
     gettime(hour,minute,second);
    format(chat,sizeof(chat),"[%d:%d:%d]: ( %s ): %s\n",hour,minute,second,chater,text); // \n is used to make a new line
    if(chatlog)
    {
        if(fexist("chatlog.txt"))
        {
            fwrite(chatlog,chat);
            fclose(chatlog);
        }
    }
I know what you are thinking, what the heck does all this gibberish mean? don`t worry i will explain it.
pawn Code:
new File:chatlog = fopen("/logs/ChatLog.txt",io_append),chat[128],chater[128],hour,minute,second;
This line will create a new file named chat log, as indicated by
pawn Code:
new File:chatlog
this line will represent the opening of the chat log file as indicated here
pawn Code:
fopen("/logs/ChatLog.txt"
now if you read a little further you will see
pawn Code:
io_append
I believe there are only two writing parameters (correct me if i am wrong) And they are:
pawn Code:
io_write - this will overwrite the whole entire file, basically it creates a new file every time its used
io_append - this will add the line after the last documented line.
Since we are making a chat log we want to use io_append. Now we are going to create a couple of variables and strings to hold our information.
pawn Code:
chat[128],chater[128],hour,minute,second;
The next line is the line that will get the players name when they speak and store it to a variable named chater.
pawn Code:
GetPlayerName(playerid,chater,sizeof(chater))
Parameters of GetPlayerName :
pawn Code:
GetPlayerName(playerid, const name[], len);
this next part is where we get the time of day that they spoke, so we use the variables Hour, Minute, and Second

pawn Code:
gettime(hour,minute,second);
The time that will display in the log is military time, so it is on a 24 hour basis instead of a 12 hour.

gettime parameters:

pawn Code:
gettime(&hour,&minute,&second);

Now we format the text to display the time first, then the players name, then the message they spoke. this is where the string chat[128] comes in.

pawn Code:
format(chat,sizeof(chat),"[%d:%d:%d]: ( %s ): %s\n",hour,minute,second,chater,text);
Now i know alot of people have trouble understanding the format function, so i will explain it to you.

Format Parameters :

pawn Code:
format(output[], len, const format[], {float,_....});
So lets break this down,

Output is the string you declared up above, so in our case it is chat[128].
len is of course the length of that string, so we use
pawn Code:
sizeof(chat)
to get the length.
const format[], is the was you would like your text to display, in our case we want the time to come first then the name then the text. So we use the percent values, %d(whole number), and %s(string).
float, this is where we place what those values are. The values are assigned left to right, so the %d`s come first, then the %s`s.


We are almost done, so lets now check if chatlog is open, then if it exists, and if it does we write the contents of chat to the file.

[pawn]if(chatlog)[pawn]
This will check is the chatlog is open.

pawn Code:
if(fexits(chatlog))
This is checking if the file chatlog.txt exists

pawn Code:
fwrite(chatlog,chat);
fclose(chatlog);
Now, this writes what ever the player said too the chatlog.txt file in the format we formatted in the format function (sorry for the confusion there :P)

After that we just close all of the brackets and return a value.



Well, that is the end of this tutorial. I hope you guys learned something. There may be easier ways to do this, if so please post comments below telling me how. I will gladly adjust my post to fit that specific way.



If any of this information is wrong please PM me or leave a comment containing the correct information.
Reply
#2

Looks good
Reply
#3

Thanks, put my best effort into this tutorial, hope people will use it.
Reply
#4

I have used it
Reply
#5

Nice :P, thanks
Reply
#6

FYI the server already has a chatlog in server_log.txt, anything that's printed to the server window using print/printf will also be written to this log.

However, good tutorial, quite thorough and should be easy to understand; there are some typos ("fexits", improper Pawn tags). It shows off the file writing, format, time functions and allows there to be a 'clean' chatlog (without any other crap in there)
Reply
#7

Very nice...
Reply
#8

Like Simon mentioned, the server already logs chat, but nice tutorial, CSMajor.
(By the way, your stuff you asked for is done )
Reply
#9

Quote:
Originally Posted by Las Venturas CNR
View Post
Like Simon mentioned, the server already logs chat, but nice tutorial, CSMajor.
(By the way, your stuff you asked for is done )
whats this Stuff?


Gah i wish i was a better programmer lol
Reply
#10

Quote:
Originally Posted by Las Venturas CNR
View Post
Like Simon mentioned, the server already logs chat, but nice tutorial, CSMajor.
(By the way, your stuff you asked for is done )
A tutorial is meant to show how functions are to be used, not how to create a script.

This tutorial can, for example, be used for logging other things, like bans and special events. It's always handy to have special logs to search up on things quickly.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)