create log file userfunction (logInfo)

Additional botting resources. Addons may be either for the game itself or for the RoM bot.
Forum rules
Only post additional bot resources here. Please do not ask unrelated questions.
Message
Author
User avatar
lisa
Posts: 8332
Joined: Tue Nov 09, 2010 11:46 pm
Location: Australia

create log file userfunction (logInfo)

#1 Post by lisa » Fri Jul 27, 2012 4:21 am

I create logs in so many of my userfunctions I have no idea why I didn't do this earlier.

Usage is farely simple as the userfunction itself is farely simple.

Code: Select all

function logInfo(_filename,_msg,_logtime,_subfolder,_logtype)
--=== V 2.3 ===--

-- _filename: the file name of the log file
-- _msg: the message to be logged
-- _logtime: true for logging time stamp
-- _subfolder: String with name of subfolder if data is to be stored in a subfolder of rom\logs\ if sub folder doesn't exist it will create it.

--=== _logtype ===--
--"new" overwrites existing log file with new info
--"add" add to new line at end of file (this is default if not specified)
Example

Code: Select all

logInfo("test","yes log this info",true)
output file name of test.txt

Code: Select all

07/27/12 19:12:34 yes log this info
you can also specify a sub folder, if sub folder doesn't exist it will create it.

Code: Select all

logInfo("test","yes log this info",true,"pawn")
will be in file
/logs/pawn/test.txt


As always save the userfunction to your
micromacro/scripts/rom/userfunctions
folder
Attachments
userfunction_logger.lua
V 2.3
(1.26 KiB) Downloaded 577 times
Remember no matter you do in life to always have a little fun while you are at it ;)

wiki here http://www.solarstrike.net/wiki/index.php?title=Manual

User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: create log file userfunction

#2 Post by rock5 » Fri Jul 27, 2012 5:15 am

Good Idea. I sometimes thought about it too. Although I usually thought along the lines of what info should be saved, which made it more complex, which is why I never bothered. But even this is a huge time saver. Saves you having to open and close the file, check if it exists, etc.
  • Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
  • I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan

User avatar
lisa
Posts: 8332
Joined: Tue Nov 09, 2010 11:46 pm
Location: Australia

Re: create log file userfunction

#3 Post by lisa » Fri Jul 27, 2012 5:41 am

Yeah I figure there will be improvements here and there over time and then I will add it to default bot, it sure does save a lot of time for adding in logging.
Also makes it so much easier to do ;)
Remember no matter you do in life to always have a little fun while you are at it ;)

wiki here http://www.solarstrike.net/wiki/index.php?title=Manual

User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: create log file userfunction

#4 Post by rock5 » Fri Jul 27, 2012 6:34 am

'filename' and 'file' should be declared local, I guess 'err' too.

The 'ugly' code can be abreviated to

Code: Select all

	if _logtype and string.find(_logtype,"new") then
		file, err = io.open(filename, "w")
	else
		file, err = io.open(filename, "a+")
	end
Although do you really need to do a string.find?

It would be nice if it could create subfolders if they don't exist. Should be able to parse the folder names using slash then check each in turn. I don't know if you would need to be able to deal with .. that might make it more complicated.
  • Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
  • I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan

User avatar
lisa
Posts: 8332
Joined: Tue Nov 09, 2010 11:46 pm
Location: Australia

Re: create log file userfunction

#5 Post by lisa » Fri Jul 27, 2012 8:14 am

rock5 wrote:'filename' and 'file' should be declared local, I guess 'err' too.
I usually do but guess I forgot to add them in at the top, like usual.
rock5 wrote:Although do you really need to do a string.find?
Just an allowance for people to have "add", even though it isn't needed people might have "new" in some and then think they need "add" aswell. They may also do "newer" or or "new line" or something else similar.
So just a troubleshooter as such.

I actually did the code the way you posted it originally but then changed it for some reason, can't remember why atm, little tired.
rock5 wrote:It would be nice if it could create subfolders if they don't exist.
agreed, I will probably have to look up how to do it, I seem to vaguely remember reading about it in the lua reference guide. Just adding the folder to the name will save it if the folder already exists but will error if it doesn't exist.

Had a read of the lua reference guide and couldn't find anything on creating folders, pretty sure that is what I worked out last time I went looking for it too lol
You can delete or rename folders though.

I think it is meant to be "w+" aswell, prob need to test that part to make sure.
Remember no matter you do in life to always have a little fun while you are at it ;)

wiki here http://www.solarstrike.net/wiki/index.php?title=Manual

User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: create log file userfunction

#6 Post by rock5 » Fri Jul 27, 2012 9:06 am

I believe you can use

Code: Select all

os.execute("mkdir dirname")

Code: Select all

os.execute("mkdir newdir1\newdir2\newdir3")
should work and would have made it easier but it's not working. I suspect it's interpreting the slash as the beginning of a special character.
  • Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
  • I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan

User avatar
lisa
Posts: 8332
Joined: Tue Nov 09, 2010 11:46 pm
Location: Australia

Re: create log file userfunction

#7 Post by lisa » Fri Jul 27, 2012 9:26 am

Took a lot of testing but this works

Code: Select all

__name = "scripts\\rom\\logs\\tester" os.execute("mkdir "..__name)
and this

Code: Select all

 lol = "test" __name = "scripts\\rom\\logs\\"..lol os.execute("mkdir "..__name)
This works

Code: Select all

function logInfo(_filename,_msg,_logtime,_subfolder,_logtype)
	if _subfolder then
		local __name = "scripts\\rom\\logs\\".._subfolder 
		os.execute("mkdir "..__name)
		_filename = _subfolder.."/".._filename
	end
Mind you the rom folder has to be named rom or it won't work.
getExecutionPath() can get the full execution path, as in
C:/micromacro/scripts/rom
but how much effort would it be to just get the characters after the last / to much for me to think about at this late a stage.

So yeah if you want a subfolder then make sure your rom folder is named rom and not anything else, like rombot.
I guess you could always just edit the userfunction to suit.
Remember no matter you do in life to always have a little fun while you are at it ;)

wiki here http://www.solarstrike.net/wiki/index.php?title=Manual

User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: create log file userfunction

#8 Post by rock5 » Fri Jul 27, 2012 9:42 am

Ah, when I tried double slashes I thought it didn't work because it created it in the root directory. I forgot starting a path with slash is supposed to do that. :oops:

So parse the directoy name then

Code: Select all

if not fileExists(directoryname.."\\nul") then
    os.execute("mkdir ".. directoryname)
end
  • Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
  • I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan

User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: create log file userfunction

#9 Post by rock5 » Fri Jul 27, 2012 9:48 am

Just had another thought. Maybe you should make separate "make dir" and "check dir" and I guess even "delete dir" functions so they could be used elsewhere? Or maybe Administrator would like to add them as basic functions to micromacro? Or maybe he could add a file system library to MM?
  • Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
  • I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan

User avatar
lisa
Posts: 8332
Joined: Tue Nov 09, 2010 11:46 pm
Location: Australia

Re: create log file userfunction

#10 Post by lisa » Fri Jul 27, 2012 9:54 am

Might have another look at it next week, tired tonight and I should probably be working on other things over the weekend.

It works for now as is and that is what matters for tonight =)
Remember no matter you do in life to always have a little fun while you are at it ;)

wiki here http://www.solarstrike.net/wiki/index.php?title=Manual

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: create log file userfunction

#11 Post by Administrator » Fri Jul 27, 2012 1:51 pm

Using the full path, the one returned from getExecutionPath(), should be fine to use with mkdir.

Code: Select all

os.execute("mkdir " .. string.gsub(getExecutionPath(), "/+", "\\") .. "\\userdir");
string.gsub is used to convert the standard POSIX-style directory slashes to Windows-specific backslashes. I was quite certain that I had the fixSlashes() function available to do this in MicroMacro, but apparently I never exported it, and it is a local function in the lib. Guess I'll move that for the future.

Alternatively, you could find the last instance of '/' in the path then return the substring starting after that location to get the directory name. The problem with this is that nested directories would break it.

Unless I'm mistaken, isn't there some global variable that holds the base path? I'm pretty sure at one time there was something that held the execution path at start-up so we could tell which directory the bot files were in.

Also, there is a function, isDirectory(), that will check if a directory exists. I would recommend using that instead of checking it as a file because, obviously, that would allow a file to be considered a directory.

should work and would have made it easier but it's not working. I suspect it's interpreting the slash as the beginning of a special character.
Most likely, it is because that example had single backslashes, which is the escape character. Double backslashes would be needed.
Ah, when I tried double slashes I thought it didn't work because it created it in the root directory. I forgot starting a path with slash is supposed to do that. :oops:
Kind of. I wouldn't rely on that behavior. Not that we're really focusing on running MicroMacro under a UNIX OS (it just doesn't work; I'd like to have that, but how MicroMacro uses Windows' internals just doesn't work out so well on a more secure OS), but beginning with a slash could end up being disastrous, as it essentially would be the same as putting "C:/" in there. "./" or ".\\" would be most appropriate for relative pathing.
Or maybe Administrator would like to add them as basic functions to micromacro? Or maybe she could add a file system library to MM?
Undecided. I was considering adding PhysicsFS to MicroMacro. Would be nice, but I still can't see too much use for it. Could make updating via distributing zip files much easier (just tell the user to drop the zip file in the directory, and that's it. It would take care of everything else). As for simple FS functions like creating directories, I guess I just never bothered since it could already be done with os.execute().

User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: create log file userfunction

#12 Post by rock5 » Fri Jul 27, 2012 3:39 pm

Administrator wrote:Alternatively, you could find the last instance of '/' in the path then return the substring starting after that location to get the directory name. The problem with this is that nested directories would break it
Maybe something like

Code: Select all

directory = string.match(userfile,"(.*)%/")
file = string.match(userfile,"([%d%a%s]+)$")
Administrator wrote:Unless I'm mistaken, isn't there some global variable that holds the base path? I'm pretty sure at one time there was something that held the execution path at start-up so we could tell which directory the bot files were in.
Isn't that what "getExecutionPath()" does?
Administrator wrote:Also, there is a function, isDirectory(), that will check if a directory exists. I would recommend using that instead of checking it as a file because, obviously, that would allow a file to be considered a directory.
Which raises the point, not only do we need to check if a directory exists but also if a file already exists with that name.
Administrator wrote:
Or maybe Administrator would like to add them as basic functions to micromacro? Or maybe she could add a file system library to MM?
Undecided. I was considering adding PhysicsFS to MicroMacro. Would be nice, but I still can't see too much use for it.
No biggy. It was just a suggestion. I still think adding separate dir functions in the bot would be useful instead of just localizing them to just this function.
  • Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
  • I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: create log file userfunction

#13 Post by Administrator » Fri Jul 27, 2012 4:10 pm

Isn't that what "getExecutionPath()" does?
Nope. getExecutionPath() returns the path of the currently executing script, which may change. For example, when run from rom/bot.lua, it will return ".../scripts/rom" (where ... depends on where the user is running it from). If you call it from rom/classes/player.lua", it would return ".../scripts/rom/classes". This is intended behavior.
Which raises the point, not only do we need to check if a directory exists but also if a file already exists with that name.
Not really. Just using isDirectory() will take care of both of those. If it returns false, that directory does not exist or it is a file.

User avatar
lisa
Posts: 8332
Joined: Tue Nov 09, 2010 11:46 pm
Location: Australia

Re: create log file userfunction

#14 Post by lisa » Fri Jul 27, 2012 8:04 pm

os.execute("mkdir " .. string.gsub(getExecutionPath(), "/+", "\\") .. "\\userdir");
string.gsub isn't ideal as when there is a space in a folder name when using getExecutionPath() it makes it into 2 different strings, I guess.

example
C:/micromacro 609/scripts/rom
os.execute("mkdir " .. string.gsub(getExecutionPath(), "/+", "\\") .. "\\logs")

This creates a folder
C:\micromacro
and then creates
C:\micromacro 609\609\scripts\rom\scripts\rom\logs

Obviously I don't understand fully string.gsub but from what I was reading if there is a space in the string it treates them as 2 seperate things or more if more spaces.

Code: Select all

     x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
     --> x="world hello Lua from"
So yeah trying to make that work drove me nuts for a while lol


On a good note this works

Code: Select all

if isDirectory(getExecutionPath() .. "/logs/" .. _subfolder) then
Remember no matter you do in life to always have a little fun while you are at it ;)

wiki here http://www.solarstrike.net/wiki/index.php?title=Manual

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: create log file userfunction

#15 Post by Administrator » Fri Jul 27, 2012 8:22 pm

That's not a problem with string.gsub(), it is a problem with the console. Just encapsulate the path with double-quotes.

Code: Select all

os.execute("mkdir \"" .. string.gsub(getExecutionPath(), "/+", "\\") .. "\\userdir\"");

User avatar
lisa
Posts: 8332
Joined: Tue Nov 09, 2010 11:46 pm
Location: Australia

Re: create log file userfunction

#16 Post by lisa » Fri Jul 27, 2012 8:30 pm

could do a search for spaces I guess before attempting the string.gsub with this

Code: Select all

string.match(getExecutionPath()," ")
if no space then it will return nil otherwise it will return " "


Yup the double quotes works =)

Code: Select all

os.execute("mkdir \"" .. string.gsub(getExecutionPath(), "/+", "\\") .. "\\logs\\".._subfolder.."\"");
wow what a headache this was lol

All done with V 2.0 and works fine.

ended up looking lie this

Code: Select all

	if type(_subfolder) == "string" then
		if not (isDirectory(getExecutionPath() .. "/logs/" .. _subfolder)) then
			os.execute("mkdir \"" .. string.gsub(getExecutionPath(), "/+", "\\") .. "\\logs\\".._subfolder.."\"")
		end
		_filename = _subfolder.."/".._filename
	end
I doubt it will work for sub sub folder.
example

Code: Select all

logInfo(_filename,_msg,_logtime,"pawn/OS",_logtype)
because it won't have the \\ and even if you did use \\ then the isDirectory won't work as it wants /.

So yeah only go 1 lvl of folder logs/** and not logs/**/**
example

Code: Select all

logInfo("myfile","yup this is a message",true,"OS")
Remember no matter you do in life to always have a little fun while you are at it ;)

wiki here http://www.solarstrike.net/wiki/index.php?title=Manual

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: create log file userfunction

#17 Post by Administrator » Sat Jul 28, 2012 5:17 am

because it won't have the \\ and even if you did use \\ then the isDirectory won't work as it wants /.
Are you sure? I just tested it, and it looks like isDirectory() works with either type of slash, or even mixed slashes.

User avatar
lisa
Posts: 8332
Joined: Tue Nov 09, 2010 11:46 pm
Location: Australia

Re: create log file userfunction

#18 Post by lisa » Sat Jul 28, 2012 5:54 am

Administrator wrote:Are you sure? I just tested it, and it looks like isDirectory() works with either type of slash, or even mixed slashes.
Didn't test it but cool if it works, makes it more versatile.

I only log in to RoM now days to test things or break things as sometimes the case may be lol

Should probably look at adding this to default bot I guess, not sure what else can go into it really. Tests I did earlier went well with no issues.
I tested with PlayerDetect at 5 seconds and it was logging everyones names and such to file without hassle and I mean lots of them as I sat in varanas lol
Remember no matter you do in life to always have a little fun while you are at it ;)

wiki here http://www.solarstrike.net/wiki/index.php?title=Manual

User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: create log file userfunction

#19 Post by rock5 » Sat Jul 28, 2012 6:07 am

Administrator wrote:
Which raises the point, not only do we need to check if a directory exists but also if a file already exists with that name.
Not really. Just using isDirectory() will take care of both of those. If it returns false, that directory does not exist or it is a file.
Yes but if it's a file we can't create a directory using that name.
lisa wrote:

Code: Select all

if type(_subfolder) == "string" then
    if not (isDirectory(getExecutionPath() .. "/logs/" .. _subfolder)) then
        os.execute("mkdir \"" .. string.gsub(getExecutionPath(), "/+", "\\") .. "\\logs\\".._subfolder.."\"")
    end
    _filename = _subfolder.."/".._filename
end
I doubt it will work for sub sub folder.
The way I see it the user file would have to be specified using forward slashes anyway because otherwise they might get interpreted as a special character. So I would make the full path with getExecutionPath, check if it's a directory then convert all / to \\ in the whole path. That should work.

Code: Select all

if type(_subfolder) == "string" then
    fullpath = getExecutionPath() .. "/logs/" .. _subfolder
    if not (isDirectory(fullpath )) then
        os.execute("mkdir \"" .. string.gsub(fullpath, "/+", "\\") .."\"")
    end
    _filename = _subfolder.."/".._filename
end
  • Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
  • I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan

User avatar
lisa
Posts: 8332
Joined: Tue Nov 09, 2010 11:46 pm
Location: Australia

Re: create log file userfunction

#20 Post by lisa » Sat Jul 28, 2012 6:11 am

just did some testing, after fixing an error I had in V 2.1 :oops:

Anyway both these worked fine

Code: Select all

Command> logInfo("yup","some message",true,"yup\\lol",true)
Command> logInfo("yup","some message",true,"yup/lol",true)

This is my full testing

Code: Select all

Command> logInfo("yup","some message",true,"yup\\lol",true)
Command> logInfo("yup","some message",true,"yup\\lol",true)
Command> logInfo("yup","some message",true,"yup/lol",true)
Command> logInfo("yupp","some message",true,"yup/lol",true)
Command> logInfo("yupp","some message",true,"yupp/lol",true)
Command> logInfo("yupp","some message",true,"y1upp/l1ol",true)
Command> logInfo("yupp","some message",true,"y1upp/l1ol1",true)
All worked perfectly

If nothing else this topic would give people an idea of what normally happens in the development section of forum, ideas get thrown around with different solutions and possible issues until in the end we get something that works to do what we want it to do ;)
Remember no matter you do in life to always have a little fun while you are at it ;)

wiki here http://www.solarstrike.net/wiki/index.php?title=Manual

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest