[Tutorial] Log
#1

Log Tutorial


This is an easy Tutorial for you , teachs you how to create a log file !

I will now post the code then explane

pawn Код:
forward Log(logtype[],string[]);
public Log(logtype[],string[])
{
    new File: LogFile = fopen(logtype,io_append);
    new stringformat[256];
    format(stringformat,sizeof(stringformat),"%s\r\n",string);
    fwrite(LogFile,stringformat);
    fclose(LogFile);
    return 1;
}
So , on the public , the logtype[] is a string ( parameter ) that will contain the name of the log .
What do I mean ? If I want to input the string into a log ( .txt ) file , that named "samp.txt",I will put "samp.txt"

new File: LogFile = fopen(logtype,io_append);
it means , that we create a new file variable named LogFile.
That variable will open the 'logtype' we entered , in append mode .
Append mode means it will add a string to the .txt file without deleteing the other strings.

new stringformat[256];
format(stringformat,sizeof(stringformat),"%s\r\n", string);

Creating a string named stringformat, that will hold the variable "string[]" that you created with the public .
format .......... we gonna input the "string[]" into the variable "stringformat" .... the "%s\r\n" means :
%s = string ,
\r\n = we gotta make a new line .
if we wont add this %s\r\n
it will just be in 1 line , example :
"tonny was kicked by andrey " and right after it "sasa was kicked by sarasr" so it will be
"tonny was kicked by andreysasa was kicked by sarasr"

fwrite(LogFile,stringformat);
Here we are the "stringformat" text to the file that the variable "LogFile" is holding .

fclose(LogFile);
Just like a door , when you open a file you need to close it right ?

return 1;

return a valid value ?

Now how do I use this ****?

Lets say you got the "kick" command

After I kicked the player and all thats , and used sendclientmessagetoall or whatever ..
I want to put the line into the log

so

lets say on the CMD kick I got the variable string[256];

now we gonna format the string
pawn Код:
new year, month,day;
getdate(year, month, day);//get the date
new kName[MAX_PLAYER_NAME],pName[MAX_PLAYER_NAME];
GetPlayerName(playerid,pName,sizeof(pName));//Get the player's name ( the kicker )
GetPlayername(playerid,kName,sizeof(kName));//Get the player's name ( the kickeD )
//Now that we got all the info
format(string,sizeof(string),"%d-%d-%d : %s was kicked by %s ",year,month,day,kName,pName);
Log("kick.txt",string); // we gotta input the string into the kick.txt file

Thats it with the tutroial ... now for the tip :

If you want to create the log file in another folder ( not in scriptfiles ) then do the next steps :
goto your scriptfiles folder
create any folder there and name it as you wish
create the kick file ( optional )

now when u use the log function it should be like that
Log("logsfolder/kick.txt",string);
so lets say you named your folder logsfolder and after it comes the /kick.txt

KK I hope I helped you ! ( BTW Im sorry for my bad english )

Reply
#2

fix the word tutorial at this post :P

Nice tutorial man. How long you spent on making it ?
Reply
#3

Quote:
Originally Posted by Neonman
Посмотреть сообщение
fix the word tutorial at this post :P

Nice tutorial man. How long you spent on making it ?
TY
and about 20 minutes .
Reply
#4

Lol....anyway nice .... Can u plz tell how to make it as a filterscript.....so dat i can use it for ma server

Hope u will help...... Tnx
Reply
#5

Lol....anyway nice .... Can u plz tell how to make it as a filterscript.....so dat i can use it for ma server

Hope u will help...... Tnx
Reply
#6

https://sampforum.blast.hk/showthread.php?tid=264770


what's that?
Reply
#7

not working -_-
creates the kick.txt and nothing is empty
when i kick someone it actually makes empty lines ;d
will you fix this ? or should i report your topic ..
Reply
#8

Just a note: Check if the file handle is valid before writing to/closing the file because if it's not, the server will crash.
pawn Код:
new File: LogFile = fopen(logtype,io_append);
...
if (LogFile)
{
    fwrite(LogFile,stringformat);
    fclose(LogFile);
}
Reply
#9

A boolean being added as parameter whether to store date/time before writing the log text would make things much more easier.
(Yes, this is an old topic)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)