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

Problem with "sandboxing"/logging program

Started by Kouksi44, 10 December 2014 - 06:00 PM
Kouksi44 #1
Posted 10 December 2014 - 07:00 PM
Hey everybody !

I was bored and decided to make some kind of program that runs a program and logs most of the things it does.

Well, turned out that it is not that easy…

Currently I am really not that far in my code so… This is it (probably terrible wrong ^^) Or atleast the relevant parts:


setmetatable(mt,{__index=function(t,k) logger.writeLine("program tried to access undefined value at key"..k.." in  table "..t) logger.flush() return _G.t[k] end, __newindex=function(t,k,v) logger.writeLine("Program tried to create new value at "..k.."  with value"..v.."  in table "..t) logger.flush()  end})
setfenv(content, mt)
content()

Right now, I am always getting an error when trying to write something to my logger file. It gives me an "attempt to concatenate string an function/table whatever".

Im pretty sure its because I am trying to write k or v or so in the file without converting it into the right format.

However I have absolutely no idea how to fix this ?

It would be pretty awesome if someone could help me :)/>

mfG Kouksi44
SquidDev #2
Posted 10 December 2014 - 07:38 PM
What is happening is you are trying to concatenate t (which is a table) and your string ("program tried to access undefined value at key"). Wrapping t in a tostring() call should solve the problem:
Spoiler

local env = setmetatable({},
{
  __index = function(t,k)
   print("program tried to access undefined value at key"..k.." in  table "..tostring(t))
   return _G[k]
  end,
  __newindex = function(t,k,v)
   print("Program tried to create new value at "..k.."  with value"..tostring(v).."  in table "..tostring(t))
  end
})

__index takes the current table and the key
__newindex takes the current table, the key and the value.

So the first parameter will always be a table.
KingofGamesYami #3
Posted 10 December 2014 - 07:45 PM
What is the point of printing the table? It'll just spit out some random stuff (eg. "table: 0x547578" )
Kouksi44 #4
Posted 10 December 2014 - 08:54 PM
Ah okay thank you ! :)/>
I'l have a look tomorrow !

Well the code above is not really useful I just wanted to see how this works and what I can do with it :)/>
Bomb Bloke #5
Posted 10 December 2014 - 09:10 PM
Edit: Whoops, seems I had this tab open for a couple of hours. Go me.
Edited on 10 December 2014 - 08:11 PM