[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


Messages In This Thread
Log - by Elka_Blazer - 16.11.2011, 13:44
Re: Log - by Astralis - 16.11.2011, 13:47
Re: Log - by Elka_Blazer - 16.11.2011, 13:50
Re: Log - by kevinhbk1 - 24.12.2011, 12:20
Re: Log - by kevinhbk1 - 24.12.2011, 12:22
Re: Log - by System64 - 26.12.2011, 10:29
Re: Log - by d0nTtoucH - 14.04.2014, 18:47
Re: Log - by Konstantinos - 14.04.2014, 18:55
Re: Log - by Lordzy - 15.04.2014, 00:42

Forum Jump:


Users browsing this thread: 1 Guest(s)