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

[Lua] [FS API] [Forums] How do I make a crash log maker? How do I count/list files? How do I make spoilders

Started by mrSLIMEguy, 09 November 2012 - 10:41 PM
mrSLIMEguy #1
Posted 09 November 2012 - 11:41 PM
I just figured out how to use the fs API to edit files. As I worked on my project: SlimeOS I forgot an "end" in an if statement. This made me wonder if I could make a crash log for an error in a program, that can be reported. Even ones using error("ejiretjoer")

Does anybody know how to do this, if possible?
Is it easy? Can I simply make it in any program easily?

Also, how do I count files? Does shell.dir() make a table that I can use a # on?

Finally, how do I make those "spoiler" things with show and hide on a post?

Thanks!

-I love that CC forum postes are all equal in being noticed. Unlike YouTube…
Sammich Lord #2
Posted 09 November 2012 - 11:59 PM
Well first off, the only way I can think of is to overwrite the "error" function and when ever it is called it saves the output to a file.
The way to count files is this:

listTable = fs.list("putYourDirHere")
print(#listTable.." Files or folders in this directory.")
for k,v in pairs(listTable) do
  print(v)
end
That will give you the number of files/folders and then list each one.
Use the
Spoiler["SPOILER"] tags ["/SPOILER"]. Just remove the quotes.
AfterLifeLochie #3
Posted 10 November 2012 - 12:08 AM
This made me wonder if I could make a crash log for an error in a program, that can be reported. Even ones using error("ejiretjoer")

Does anybody know how to do this, if possible?
Is it easy? Can I simply make it in any program easily?

You may also be interested in using pcall() in order to catch errors thrown. (See the Lua 5.1 PIL, or, http://www.lua.org/pil/8.4.html for full info on pcall).

For example;

local ok, error = pcall(someFunction)
if (ok == false) or (error ~= nil) then
  -- Do something with "error"
end

Errata: Note, that doing a pcall() doesn't provide a stack, only the location + file the error occurred in, and by stack, I mean the execution path the interpreter took to stumble to the error. To get a full stack with CC is impossible, as we don't have access to the debug tools, but we do in normal Lua.
Edited on 09 November 2012 - 11:17 PM
Sammich Lord #4
Posted 10 November 2012 - 12:11 AM
This made me wonder if I could make a crash log for an error in a program, that can be reported. Even ones using error("ejiretjoer")

Does anybody know how to do this, if possible?
Is it easy? Can I simply make it in any program easily?

You may also be interested in using pcall() in order to catch errors thrown. (See the Lua 5.1 PIL, or, http://www.lua.org/pil/8.4.html for full info on pcall).

For example;

local ok, error = pcall(someFunction)
if (ok == false) or (error ~= nil) then
  -- Do something with "error"
end
Lochie, stop being smart.
AfterLifeLochie #5
Posted 10 November 2012 - 12:12 AM
This made me wonder if I could make a crash log for an error in a program, that can be reported. Even ones using error("ejiretjoer")

Does anybody know how to do this, if possible?
Is it easy? Can I simply make it in any program easily?

You may also be interested in using pcall() in order to catch errors thrown. (See the Lua 5.1 PIL, or, http://www.lua.org/pil/8.4.html for full info on pcall).

For example;

local ok, error = pcall(someFunction)
if (ok == false) or (error ~= nil) then
  -- Do something with "error"
end
Lochie, stop being smart.

Sorry, Human. You can put me inside a box labelled "SPOILERS: Do not open!", you know. :unsure:/>/>
Sammich Lord #6
Posted 10 November 2012 - 12:42 AM
Spoiler
This made me wonder if I could make a crash log for an error in a program, that can be reported. Even ones using error("ejiretjoer")

Does anybody know how to do this, if possible?
Is it easy? Can I simply make it in any program easily?

You may also be interested in using pcall() in order to catch errors thrown. (See the Lua 5.1 PIL, or, http://www.lua.org/pil/8.4.html for full info on pcall).

For example;

local ok, error = pcall(someFunction)
if (ok == false) or (error ~= nil) then
  -- Do something with "error"
end
Lochie, stop being smart.

Sorry, Human. You can put me inside a box labelled "SPOILERS: Do not open!", you know. :unsure:/>/>
Now stay in there.
mrSLIMEguy #7
Posted 10 November 2012 - 11:51 AM
This made me wonder if I could make a crash log for an error in a program, that can be reported. Even ones using error("ejiretjoer")

Does anybody know how to do this, if possible?
Is it easy? Can I simply make it in any program easily?

You may also be interested in using pcall() in order to catch errors thrown. (See the Lua 5.1 PIL, or, http://www.lua.org/pil/8.4.html for full info on pcall).

For example;

local ok, error = pcall(someFunction)
if (ok == false) or (error ~= nil) then
  -- Do something with "error"
end

Errata: Note, that doing a pcall() doesn't provide a stack, only the location + file the error occurred in, and by stack, I mean the execution path the interpreter took to stumble to the error. To get a full stack with CC is impossible, as we don't have access to the debug tools, but we do in normal Lua.

Um… How do I use that? What exactly does that code do?
AfterLifeLochie #8
Posted 10 November 2012 - 02:32 PM
This made me wonder if I could make a crash log for an error in a program, that can be reported. Even ones using error("ejiretjoer")

Does anybody know how to do this, if possible?
Is it easy? Can I simply make it in any program easily?

You may also be interested in using pcall() in order to catch errors thrown. (See the Lua 5.1 PIL, or, http://www.lua.org/pil/8.4.html for full info on pcall).

For example;

local ok, error = pcall(someFunction)
if (ok == false) or (error ~= nil) then
  -- Do something with "error"
end

Errata: Note, that doing a pcall() doesn't provide a stack, only the location + file the error occurred in, and by stack, I mean the execution path the interpreter took to stumble to the error. To get a full stack with CC is impossible, as we don't have access to the debug tools, but we do in normal Lua.

Um… How do I use that? What exactly does that code do?
CONSOLE: Releasing AfterLifeLochie.

Here's a bit more of an explanatory example:


function iThrowAnError()
  error("This error is an error caught by pcall!")
end

function iDontThrowAnError(anArgument)
  print("I don't throw an error: " .. anArgument)
end

local ok, error = pcall(iThrowAnError)
if (ok == false) or (error ~= nil) then
 print("pcall threw error (caught): " .. error)
end

local ok, error = pcall(iDontThrowAnError, "this is a test argument")
if (ok == false) or (error ~= nil) then
 print("pcall threw error (caught): " .. error)
end

You'd see, as output:

> test
pcall threw error (caught): This error is an error caught by pcall!
"I don't throw an error: this is a test argument
>

In short, pcall() allows you to "catch" errors and then do something with them. Note, the script doesn't stop, because pcall() caught the error.
mrSLIMEguy #9
Posted 10 November 2012 - 03:38 PM
Spoiler
This made me wonder if I could make a crash log for an error in a program, that can be reported. Even ones using error("ejiretjoer")

Does anybody know how to do this, if possible?
Is it easy? Can I simply make it in any program easily?

You may also be interested in using pcall() in order to catch errors thrown. (See the Lua 5.1 PIL, or, http://www.lua.org/pil/8.4.html for full info on pcall).

For example;

local ok, error = pcall(someFunction)
if (ok == false) or (error ~= nil) then
  -- Do something with "error"
end

Errata: Note, that doing a pcall() doesn't provide a stack, only the location + file the error occurred in, and by stack, I mean the execution path the interpreter took to stumble to the error. To get a full stack with CC is impossible, as we don't have access to the debug tools, but we do in normal Lua.

Um… How do I use that? What exactly does that code do?
CONSOLE: Releasing AfterLifeLochie.

Here's a bit more of an explanatory example:


function iThrowAnError()
  error("This error is an error caught by pcall!")
end

function iDontThrowAnError(anArgument)
  print("I don't throw an error: " .. anArgument)
end

local ok, error = pcall(iThrowAnError)
if (ok == false) or (error ~= nil) then
print("pcall threw error (caught): " .. error)
end

local ok, error = pcall(iDontThrowAnError, "this is a test argument")
if (ok == false) or (error ~= nil) then
print("pcall threw error (caught): " .. error)
end

You'd see, as output:

> test
pcall threw error (caught): This error is an error caught by pcall!
"I don't throw an error: this is a test argument
>
In short, pcall() allows you to "catch" errors and then do something with them. Note, the script doesn't stop, because pcall() caught the error.


1. How do I save it to a file?
2. What do you mean by anArgument?
3. Is it possible to just save all output to a file? Including bios errors?
AfterLifeLochie #10
Posted 10 November 2012 - 03:46 PM
1. How do I save it to a file?


local ok, error = pcall(iThrowAnError)
if (ok == false) or (error ~= nil) then
  print("pcall threw error (caught): " .. error)
  local crashLog = io.open("/crash.log", "a")
  crashLog:write("Crashed: " .. error)
  crashLog:close()
end
This appends "Crashed: <errormessage>" to "/crash.log".

2. What do you mean by anArgument?
Arguments (aka parameters) are things you pass to a function. From the above demo:

function iDontThrowAnError(anArgument)
  print("I don't throw an error: " .. anArgument)
end

Normally you could just do "iDontThrowAnError('print me out!')", but because we're using a pcall, we have to put each argument we want to send after the function.

3. Is it possible to just save all output to a file? Including bios errors?

You're talking about "wrapping" the error function: you can't catch BIOS errors, but you can catch everything else. Take this, for example:

local realPrint = print
print = function(text)
    -- We could do something else with "text"
    -- Now, send it to the "real" print!
    realPrint(text)
end

This changes print() so if you print anything, you can do something with it first, and it gets printed anyway.