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

[ALPHA] The log API, document whats been happening!

Started by samdeman22, 11 April 2013 - 04:33 AM
samdeman22 #1
Posted 11 April 2013 - 06:33 AM
My first ever API, with it you can create neat .log files to read later and soon (I hope) print them!

This is very boring I know, but its the first in my line of APIs that are to come.


Functions:

setRunningProgram(name) Im not too sure about the need for this, but comment if you know a better solution. This function will set the name of the current running program and is only used in conjunction with the next function, generateLog()

generateLog() this will create the "logs" folder (if it doesn't already exist) and will make a .log file from the variable "runningProgram" (set in the last function)

openLog(name) this opens the log file with the desired name, must be used to do anything with the log.

closeLog() this should save the log, must be called before doing printLog() to avoid errors (I think?)

startLog() this will create a header inside the log file and is not required, just thought it could be handy, containing the time, day and a message "#STARTING LOG".

endLog() will add an ending statement to the log, similar to startlog(), this is not required, but might be handy.

addEntry(type, str) this will add an entry containing the time, date, type of message (not sure if anyone actually wants this) and the desired message

printLog(log, side, fromLn, toLn) printLog is currently broken, it should print the log from the desired line to the desired line, but instead it prints one line of a log to the page, nothing else. It also doesn't throw errors if you don't have enough paper or ink (which it should). If anyone wants to examine the code, figure out whats wrong and ultimately move the API onto beta, please do so.

Im in two minds as to whether I should leave the addEntry() function to be made up by you, i.e wont contain the time, date, type, square brackets etc. or whether to leave it as it is. That way, I wouldn't even need the startlog() and endLog() functions.




Here is the pastebin: http://pastebin.com/FwtEq2Ww

Here is the code for viewing:

--The Log API 1.0_A
-- open: opens the file
function openLog(name)
h = fs.open("logs/"..name..".log", "a")
end
function closeLog()
h.close()
end
function setRunningProgram(name)
runningProgram = name
end
function generateLog() --will create the Dir "logs" in the root and create a .log file based on the running program (if if is not already there)
if not fs.exists("logs") then
  fs.mkDir("logs")
end

if runningProgram == nil then
  error("program not specified")
end
  h = fs.open("logs/"..runningProgram..".log", "a")
  h.writeLine("--- The "..runningProgram.." event log ---")
  h.writeLine(" ")
  h.close()
end
function addEntry(type, str)
local time = round(os.time(), 2)
local day = os.day()
h.writeLine("["..day.."]".."["..round(time, 2).."]".."["..type.."] | #"..str)
end
-- start/end add start/end messages
function startLog()
open = true
local time = os.time()
local day = os.day()
h.writeLine("["..day.."]".."["..round(time, 2).."] | #STARTING LOG")
end
function endLog()
open = false
local time = os.time()
local day = os.day()
h.writeLine("["..day.."]".."["..round(time, 2).."] | #ENDING LOG")

end
-- a function you cant access ;P
local function round(num, idp)
  local mult = 10 ^ (idp or 0)
  return (math.floor(num * mult + 0.5) / mult)
end
--will print the desired log
function printLog(log, side, fromLn, toLn)
  if open == true then
	error("Log must be ended before printing")
  end
  if not fs.exists( "logs/"..log..".log") then
	error("Log does not exist")
  end

  h = fs.open("logs/"..log..".log", "r")
  page = {}
  while h.readLine() ~= nil do
	local ln = h.readLine()
	table.insert(page, ln)
  end

  p = peripheral.wrap(side)
  p.newPage()
  local paper = p.getPaperLevel()
  local ink = p.getInkLevel()
  local w, h = p.getPageSize()

  local doc = {}
  for i = fromLn, toLn do
	table.insert(doc, page[i])
  end

  local length = 0
  for i = 1, #doc do
	length = length + #doc[i]
  end

  local size = w*h
  local pages = round(length/size, 0)
  if pages > paper then
	error("not enough paper")
  end
  if pages > ink then
	error("not enough ink")
  end
  for i = 1, pages do
  p.newPage()
  p.setPageTitle(log.." page:"..i)
	for indx = 1, size do
	  p.write(doc[indx])
	end
  p.endPage()
  end

end
samdeman22 #2
Posted 11 April 2013 - 07:03 AM
Please note, I just thought it would be better to release the API in its alpha stage as I am hoping you can all point out solutions to my problems.
AfterLifeLochie #3
Posted 11 April 2013 - 09:42 PM
Moved to the API Subforum.
samdeman22 #4
Posted 12 April 2013 - 01:25 AM
Moved to the API Subforum.
oh, they have one of those? cool
H4X0RZ #5
Posted 12 April 2013 - 11:10 AM
Please take the code into a spoiler ^^

SpoilerI have just spoiled your mind
samdeman22 #6
Posted 12 April 2013 - 12:06 PM
Please take the code into a spoiler ^^

SpoilerI have just spoiled your mind

dammit