Advertisement
theoriginalbit

Simple Log File API

Sep 23rd, 2013
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.37 KB | None | 0 0
  1. --[[
  2.     Log API. This API allows calls to write errors, warnings, and information to a log file for later review
  3.  
  4.     @version 1.0, 24 September 2013, BIT
  5.     @author  TheOriginalBIT, BIT
  6.  
  7.     License:
  8.  
  9.       COPYRIGHT NOTICE
  10.       Copyright © 2013 Joshua Asbury a.k.a TheOriginalBIT [theoriginalbit@me.com]
  11.  
  12.       Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  13.       associated documentation files (the "Software"), to deal in the Software without restriction,
  14.       including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15.       copies of the Software, and to permit persons to whom the Software is furnished to do so,
  16.       subject to the following conditions:
  17.  
  18.       - The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  19.       - Visible credit is given to the original author.
  20.       - The software is distributed in a non-profit way.
  21.  
  22.       THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  23.       WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  24.       COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25.       ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. --]]
  27.  
  28. local file
  29. local nativeError = _G.error
  30.  
  31. --# internal function that writes to the log file
  32. local function logWrite(output)
  33.   if not file then
  34.     nativeError("Log API not initialised!", 3)
  35.   end
  36.   file.writeLine(output)
  37.   file.flush()
  38. end
  39.  
  40. local callerThrowback = 5
  41.  
  42. --# gets the name of the calling file without the extension if present
  43. local function getCaller()
  44.   local ok, err = pcall(nativeError, "", callerThrowback)
  45.   return err:match("(%a+)%.?.-:.-") or "unknown" --# extract file name, remove extension
  46. end
  47.  
  48. --# get the line number the function in the API was called from in the calling file (used in error)
  49. local function getCallerLineNumber()
  50.   local ok, err = pcall(nativeError, "", callerThrowback)
  51.   return err:match("%a+:(%d+).-") or 0
  52. end
  53.  
  54. local function formatMessage(_type, needsLine, ...)
  55.   if needsLine then
  56.     return string.format("[%s] [%s:%d] [%s] %s", _type, getCaller(), getCallerLineNumber(), os.clock(), table.concat({...}, ' '))
  57.   end
  58.   return string.format("[%s] [%s] [%s] %s", _type, getCaller(), os.clock(), table.concat({...}, ' '))
  59. end
  60.  
  61. --[[
  62.     Opens the log file handle so that the API can function
  63.  
  64.     @param  filename  string  [optional] the name of the log file, if not supplied it takes on the name of the calling file and appends '.log'
  65. --]]
  66. function init(filename)
  67.   file = fs.open(filename or getCaller()..".log", 'w')
  68.   logWrite("============= LOG START =============")
  69. end
  70.  
  71. --[[
  72.     Logs an error string to the log file with the prefix of [ERROR] and the clock time
  73.  
  74.     @param  ...  any number of strings (or numbers) to output to the log file
  75. --]]
  76. function e(...)
  77.   local msg = formatMessage("ERROR", true, ...)
  78.   logWrite(msg)
  79. end
  80.  
  81. --[[
  82.     Logs a warning string to the log file with the prefix of [WARNING] and the clock time
  83.    
  84.     @param  ...  any number of strings (or numbers) to output to the log file
  85. --]]
  86. function w(...)
  87.   local msg = formatMessage("WARNING", false, ...)
  88.   logWrite(msg)
  89. end
  90.  
  91. --[[
  92.     Logs an information string to the log file with the prefix of [INFORMATION] and the clock time
  93.    
  94.     @param  ...  any number of strings (or numbers) to output to the log file
  95. --]]
  96. function i(...)
  97.   local msg = formatMessage("INFO", false, ...)
  98.   logWrite(msg)
  99. end
  100.  
  101. --[[
  102.     Writes the log end and closes the log file handle, should only be used when the program is about to end
  103.    
  104.     @param  ...  any number of strings (or numbers) to output to the log file
  105. --]]
  106. function close()
  107.   logWrite("============== LOG END ==============")
  108.   file.close()
  109.   _G.error = nativeError
  110. end
  111.  
  112. --[[
  113.   An override to error that will make use of the Log API
  114.  
  115.   @param  (same as default error function)
  116. --]]
  117. function _G.error(msg, lvl)
  118.   callerThrowback = 6 -- trick the caller system into not pointing here
  119.   e(msg)
  120.   callerThrowback = 5
  121.   nativeError(msg, (lvl == 0 and 0 or lvl and (lvl + 1) or 2))
  122. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement