This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
geech's profile picture

Output log file to monitor

Started by geech, 01 October 2013 - 06:02 PM
geech #1
Posted 01 October 2013 - 08:02 PM
Title: Output log file to monitor

Enviroment:
Minecraft 1.5.2
FTB Unleashed

I've bee looking for the better part of 2 hours and I'm trying to figure out how to output the contents of a file in the root of the turtle created by a program on the same turtle to a monitor.

I've been trying to use the fs API, Term API, and Peripheral API, to no avail…

I know I'm missing something, maybe that it just can't be done, but here's what I got so far:

mon = peripheral.wrap("top")
fs.open("test.log, "r")

mon.clear()
mon.setCursorPos(1,1)
mon.write("=====TESTING=====")
mon.write(fs.read(test.log))

The reason I want to do this is because I am running a Bee breeding program (not built by me) (http://www.computerc...olf20-for-idea/), and I want to be able to see the status of the breeding from a monitor (the program already output's a log file). I have had points where, the log is so long, it scrolls past much to quickly for anyone to see it. A monitor with all the information from the current run would make it so much simpler.

I haven't done lua in a while so I'm a little rusty, I know I'm missing something, I just don't know what.
Yevano #2
Posted 01 October 2013 - 08:19 PM
mon = peripheral.wrap("top")
fs.open("test.log, "r")

mon.clear()
mon.setCursorPos(1,1)
mon.write("=====TESTING=====")
mon.write(fs.read(test.log))

You need to save the file handle in a variable like this:


local handle = fs.open("test.log", "r")

Then, read the file and output it to console like this:


mon.write(handle.readAll())
Bubba #3
Posted 01 October 2013 - 08:19 PM
Although it depends on how long the log file is, as you're doing it currently this will run off the screen. You'll need some form of automatic line wrapping as in the following example:


local monitor = peripheral.wrap('top')
term.backup = { --#We need to do some function remapping, so we'll save the old functions in this table
  setCursorPos = term.setCursorPos,
  write = term.write
}

term.setCursorPos = monitor.setCursorPos --#Now lets remap the terminal text functions to the monitor set functions
term.write = monitor.write

local file = fs.open('test.log', 'r')
term.setCursorPos(1,1) term.write("=====TESTING=====")
term.setCursorPos(1,2)
print(file.readAll()) --#If you use file.read(), you'll only get a single line of the file
file.close()
for i,v in pairs(backup) do term[i] = v end --#Now restore the old terminal functions

Print is especially handy because it automatically handles line wrapping, so we don't have to.
geech #4
Posted 02 October 2013 - 12:42 AM

local mon = peripheral.wrap('top')
term.backup = { --#We need to do some function remapping, so we'll save the old functions in this table
  setCursorPos = term.setCursorPos,
  write = term.write
}
term.setCursorPos = mon.setCursorPos --#Now lets remap the terminal text functions to the monitor set functions
term.write = mon.write

local logOutput= fs.open('test.log', 'r')
term.setCursorPos(1,1) term.write("=====TESTING=====")
term.setCursorPos(1,2)
print(logOutput.readAll()) --#If you use file.read(), you'll only get a single line of the file
logOutput.close()
for i,v in pairs(backup) do term[i] = v end --#Now restore the old terminal functions

code, I'm currently using.

Ok, so… Here's the report of what's happened…

Yevano: you're suggestions made my program work, except that it had the problem of the entire file being on one line. Other than that it worked awesomely!

Bubba: I have a few problems, with you're implemtnation. Your code takes control of the turtle's screen and forces it into the monitor for the entire run of the turtle until it shutsdown. After the program crashes it won't let me use the console on the turtle anymore, I have to terminate the program and the turtle. I don't want to loose the monitor on the turtle, I just want to output the log file at a certain point in it's cycle, and go back to it's business.

Your code error's out at line 14 the error is:
>:14: bad argument: table expected, got nil

it stopped after the line of the log file that said

Clearing system...

Could it be the "…" in the output? Is it trying to run the file?
immibis #5
Posted 02 October 2013 - 01:29 AM
Here is an easier way to do what Bubba is trying to do:

term.redirect(peripheral.wrap('top'))

local logOutput= fs.open('test.log', 'r')
print("=====TESTING=====")
print(logOutput.readAll()) --#If you use file.read(), you'll only get a single line of the file
logOutput.close()

term.restore()
geech #6
Posted 02 October 2013 - 03:17 AM
Thanks, Immibis, work's perfectly!