• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Register talkactions in Logs

AGS

DeathSouls Owner
Joined
Oct 29, 2007
Messages
400
Reaction score
10
Location
Mexico
I really don't know how to use file functions, so I'm requesting this:

I want a function, that will write "x" text at the end of the player's log(data\logs\playername commands.log)

Like:
[21/03/2008 22:29] /makesay "God Lord,hi

So, the function could be like loglogRegister(name,text), so, to use it in talkactions you would use it like:
logRegister(getCreatureName(cid),"".. word .." \"".. param ..") <-- the date should be in the function.

You know what I mean?
I dont think is that hard, but I don't know how to use file functions, and this may help me understand how to use them :p

Thanks in advance
 
Here :p
PHP:
local saveFile = {folder="logs/", filename="saves.txt", saveLog=true, linesMax=1000} 

if (saveFile.saveLog == true) then 
    local lines = 0 
    for _ in io.lines(saveFile.folder .. saveFile.filename) do 
        lines = lines+1 
    end 
    local file = io.open(saveFile.folder .. saveFile.filename, (lines < linesMax-1 and "a") or "w") 
    file:write("[" .. os.date("%Y-%m-%d %H:%M:%S") .. "] HERE GOES TEXT\n") 
    file:close() 
end

This script saves the text "HERE GOES THE TEXT" BUT it also reset the whole file when it reach 1000 lines :blink:
If you don't want that:
PHP:
local saveFile = {folder="logs/", filename="saves.txt", saveLog=true} 

if (saveFile.saveLog == true) then 
    local file = io.open(saveFile.folder .. saveFile.filename, "a") 
    file:write("[" .. os.date("%Y-%m-%d %H:%M:%S") .. "] HERE GOES TEXT\n") 
    file:close() 
end

And keep \n there, it makes the newline, otherwise it will all come like "[date here] TEXT GOES HERE[date here] TEXT GOES HERE[date here] TEXT GOES HERE[date here] TEXT GOES HERE" and you don't want that to happen, huh?


Regards,
Colandus
 
Last edited:
Thanks!
I would give you rep but I can't =O

You must spread some Reputation around before giving it to Colandus again.

EDIT: I tested it and everything worked, I just changed it a bit:
PHP:
function saveLogs(name,text)
	logs = io.open("./data/logs/".. name .." commands.log", "a")    
    logs:write("[" .. os.date("%d/%m/%Y %H:%M") .. "] ".. text .."\n")  
    logs:close()  
end

I was wondering, how do I make it so, if the file doesn't exists, it's created?
 
Last edited:
I think it already does :eek:


You might want this too:
"r" read mode (the default);
"w" write mode;
"a" append mode;
"r+" update mode, all previous data is preserved;
"w+" update mode, all previous data is erased;
"a+" append update mode, previous data is preserved, writing is only allowed at the end of file.
That's for io.open("file", "a")


And a simple fileExist function:
PHP:
function fileExists(filename) 
    file=io.open(filename, "r") 
    return (file == nil and 0) or file:close() and 1
end
Returns 1 if file exists, and 0 if not exists.
PHP:
if fileExists(filename) == 1 then
    ...
end


PHP:
function fileExists(filename) 
    file=io.open(filename, "r") 
    return (file ~= nil and file:close())
end
Returns true if file exists, and false if not exists.
PHP:
if fileExists(filename) then
    ...
end


And the date formats: http://www.lua.org/pil/22.1.html
 
Last edited:
@master-m's reputation (offtopic?)
I would like to create a new thread for this simple shiat, but where shall I post it?! There is no section for it :(
 
And a simple fileExist function:
PHP:
function fileExists(filename)
	file=io.open(filename, "r")
	return (file==nil and 0) or 1
end

It's a memory leak if the file exists and you don't run file:close() or io.close(file) or io.close().
 
It's a memory leak if the file exists and you don't run file:close() or io.close(file) or io.close().

Yes I thought about it, but when I was gonna edit it this damn internet crashed xD I'm not on my comp tho lol´
I think this should do it:
PHP:
function fileExists(filename) 
    file=io.open(filename, "r") 
    return (file == nil and 0) or file:close() and 1
end
Returns 1 if found, 0 if not found.

PHP:
function fileExists(filename) 
    file=io.open(filename, "r") 
    return (file ~= nil and file:close())
end
Returns true if found, false if not found.

Updating the old post, to ensure nobody uses it ^^
 
Last edited:
Back
Top