View difference between Paste ID: FwtEq2Ww and dyVnAcQt
SHOW: | | - or go back to the newest paste.
1-
--The Log API 1.0
1+
--The Log API 1.0_A
2
3
-- open: opens the file
4-
	h = fs.open("log/"..name..".log", a)
4+
5
	h = fs.open("logs/"..name..".log", "a")
6
end
7
8
function closeLog()
9
	h.close()
10
end
11-
function generateLog() --will create the Dir "log" in the root and create a .log file based on the running program (if if is not already there)
11+
12
function setRunningProgram(name)
13-
	local runningProgram = shell.getRunningProgram()
13+
	runningProgram = name
14
end
15-
	if not fs.exists("log") then
15+
16-
		fs.mkDir("log")
16+
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)
17
18
	if not fs.exists("logs") then
19-
	if not fs.exists("log/"..runningProgram..".log")
19+
		fs.mkDir("logs")
20-
		h = fs.open("log/"..runningProgram..".log", a)
20+
21
	
22
	if runningProgram == nil then
23
		error("program not specified")
24
	end
25
26
		h = fs.open("logs/"..runningProgram..".log", "a")
27
		h.writeLine("--- The "..runningProgram.." event log ---")
28
		h.writeLine(" ")
29
		h.close()
30-
	h.writeLine("["..day.."]".."["..time.."]".."["..type.."] | #"..str)
30+
31
	
32
33
function addEntry(type, str)
34
	local time = round(os.time(), 2)
35
	local day = os.day()
36-
	h.writeLine("["..day.."]".."["..time.."] | #STARTING LOG")
36+
	h.writeLine("["..day.."]".."["..round(time, 2).."]".."["..type.."] | #"..str)
37
end
38
39
-- start/end add start/end messages
40
function startLog()
41
	open = true
42-
	h.writeLine("["..day.."]".."["..time.."] | #ENDING LOG")
42+
43
	local day = os.day()
44
	h.writeLine("["..day.."]".."["..round(time, 2).."] | #STARTING LOG")
45
end
46-
function printLog(log, fromPage, toPage,)
46+
47
function endLog()
48
	open = false
49
	local time = os.time()
50
	local day = os.day()
51
	h.writeLine("["..day.."]".."["..round(time, 2).."] | #ENDING LOG")
52
	
53
end
54
55
-- a function you cant access ;P
56
local function round(num, idp)
57
  local mult = 10 ^ (idp or 0)
58
  return (math.floor(num * mult + 0.5) / mult)
59
end
60
61
--will print the desired log
62
function printLog(log, side, fromLn, toLn)
63
64
	 if open == true then
65
		  error("Log must be ended before printing")
66
	 end
67
68
  if not fs.exists( "logs/"..log..".log") then
69
    error("Log does not exist")
70
  end
71
  
72
  h = fs.open("logs/"..log..".log", "r")
73
  page = {}
74
75
  while h.readLine() ~= nil do
76
    local ln = h.readLine()
77
    table.insert(page, ln)
78
  end
79
  
80
  p = peripheral.wrap(side)
81
  p.newPage()
82
  local paper = p.getPaperLevel()
83
  local ink = p.getInkLevel()
84
  local w, h = p.getPageSize()
85
  
86
  local doc = {}
87
  for i = fromLn, toLn do
88
    table.insert(doc, page[i])
89
  end
90
  
91
  local length = 0
92
  for i = 1, #doc do
93
    length = length + #doc[i]
94
  end
95
  
96
  local size = w*h
97
  local pages = round(length/size, 0)
98
  if pages > paper then
99
    error("not enough paper")
100
  end
101
  if pages > ink then
102
    error("not enough ink")
103
  end
104
  for i = 1, pages do
105
  p.newPage()
106
  p.setPageTitle(log.." page:"..i)
107
    for indx = 1, size do
108
      p.write(doc[indx])
109
    end
110
  p.endPage()
111
  end
112
  
113
end