SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 16/11/2011, 01:44 PM   #1
Elka_Blazer
Big Clucker
 
Elka_Blazer's Avatar
 
Join Date: Apr 2011
Posts: 160
Reputation: 1
Default Log

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

__________________
Did I help you ? well you can help me too by pressing this star(on my post)


[/COLOR]
Elka_Blazer is offline   Reply With Quote
Old 16/11/2011, 01:47 PM   #2
Astralis
Guest
 
Posts: n/a
Default Re: Log

fix the word tutorial at this post :P

Nice tutorial man. How long you spent on making it ?
  Reply With Quote
Old 16/11/2011, 01:50 PM   #3
Elka_Blazer
Big Clucker
 
Elka_Blazer's Avatar
 
Join Date: Apr 2011
Posts: 160
Reputation: 1
Default Re: Log

Quote:
Originally Posted by Neonman View Post
fix the word tutorial at this post :P

Nice tutorial man. How long you spent on making it ?
TY
and about 20 minutes .
__________________
Did I help you ? well you can help me too by pressing this star(on my post)


[/COLOR]
Elka_Blazer is offline   Reply With Quote
Old 24/12/2011, 12:20 PM   #4
kevinhbk1
Little Clucker
 
Join Date: Mar 2011
Posts: 17
Reputation: 2
Default Re: Log

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
kevinhbk1 is offline   Reply With Quote
Old 24/12/2011, 12:22 PM   #5
kevinhbk1
Little Clucker
 
Join Date: Mar 2011
Posts: 17
Reputation: 2
Default Re: Log

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
kevinhbk1 is offline   Reply With Quote
Old 26/12/2011, 10:29 AM   #6
System64
Gangsta
 
System64's Avatar
 
Join Date: May 2011
Location: Zadar, Croatia
Posts: 933
Reputation: 143
Default Re: Log

http://forum.sa-mp.com/showthread.php?t=264770


what's that?
__________________
All my works
I left SA:MP! Maybe once I start scripting again but now I'm based on web programming
Soon, I'll buy domain system32.com.hr but for now you can contact me on LogoOff or on my ******* channel!
System64 is offline   Reply With Quote
Old 14/04/2014, 06:47 PM   #7
d0nTtoucH
Huge Clucker
 
d0nTtoucH's Avatar
 
Join Date: Feb 2013
Posts: 358
Reputation: -17
Default Re: Log

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 ..
Attached Images
File Type: png asd.png (108.6 KB, 10 views)
__________________
GameModes
Filterscripts

Last edited by d0nTtoucH; 16/04/2014 at 09:13 PM.
d0nTtoucH is offline   Reply With Quote
Old 14/04/2014, 06:55 PM   #8
Konstantinos
Spam Machine
 
Konstantinos's Avatar
 
Join Date: Dec 2011
Posts: 11,866
Reputation: 1394
Default Re: Log

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 Code:
new File: LogFile = fopen(logtype,io_append);
...
if (LogFile)
{
    fwrite(LogFile,stringformat);
    fclose(LogFile);
}
__________________
Life is like riding a bicycle. To keep your balance, you must keep moving.

[Tutorial] How to use SQLite
[FilterScript] Tune System
Konstantinos is offline   Reply With Quote
Old 15/04/2014, 12:42 AM   #9
Lordzy
High-roller
 
Lordzy's Avatar
 
Join Date: Mar 2012
Location: NetherRealm
Posts: 2,707
Reputation: 1176
Default Re: Log

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)
__________________
Currently inactive - I don't play at any SA-MP servers nor work on anything in PAWN for now. The projects that I've done so far in PAWN, which requires updates will be taking some time.
Lordzy is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



All times are GMT. The time now is 09:35 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.