How can I put logs to a log file using ruby code in sketchup?
Another question, how to write to an existing log file?
I used this to create a new one -
log = Logger.new(‘C:\Users\Dell\Desktop\my_logs.log’, 2, 10241024)
log.info(“I’m an info log”)
Now, I want to write to this file from another ruby module, what will happen if I use the same line again? (log = Logger.new(‘C:\Users\Dell\Desktop\my_logs.log’, 2, 10241024))
def test_log(path, logtext)
begin
file = File.new(path,"w")
rescue
UI.messagebox('Error')
end
file.puts(logtext)
file.close
end
test_log('log.txt', 'mylogtext')
… I agree. You cannot ever expect the current working directory to be where you think it should be. (Ie, people tend to write lazy code and don’t restore the working directory.)
Thanks!
Looks like path was the only problem.
This is working -
log = Logger.new(‘C:\Users\Dell\Desktop\my_logs.log’, 2, 1024*1024)
log.info(“I’m an info log”)
how to write to an existing log file?
I used this to create a new one -
log = Logger.new(‘C:\Users\Dell\Desktop\my_logs.log’, 2, 1024 x1024)
log.info(“I’m an info log”)
Now, I want to write to this file from another ruby module, should I use the same line again? (log = Logger.new(‘C:\Users\Dell\Desktop\my_logs.log’, 2, 1024 x 1024))
The answer to this is a bit longer than what I am able to type in here on my cellphone.
Ideally you would create only one logger and share it with the rest of your code that needs it.
There are a few options: you make a logger module and call it from your other code. Alternatively you make a singleton logger class instance, or, you do some dependency injection and inject a logger instance into your other class instances, or, create one global service provider that is a module or singleton that distributes your logger, or, make the path a constant and just log how you do it now, without having the literal path defined in every piece of code, but by using the constant.
So many options, I go with dependency injection, but that requires a specific way your extension should be built from the ground up.
Files will get written to the current working directory.
After SketchUp gets done loading it will set it to the User’s “Documents” directory.
(But again, extensions can change it, so either use absolute paths, or use the block form of the Dir.chdir method, which restores the previous directory when the block exits.)
I believe the examples you have provided should help TS enough to get started.
Explaining dependency injection, why I do not like to use modules to hold state, or why I wrap everything in proxies… will just confuse TS.