local lpeg = require "lpeg" local string = require "string" local stringformat = string.format local verboselvl = 0 local mk_out = function (stream, threshold, newlinep) local out = io.stdout if stream == "err" or stream == "stderr" then out = io.stderr end return function (...) if verboselvl >= threshold then local ok, msg = pcall (stringformat, ...) if ok then out:write (msg) if newlinep == true then out:write "\n" end ---else silently ignore end end end end local set_verbosity = function (n) verboselvl = n end local trim_whitespace local unescape_string do local P = lpeg.P local S = lpeg.S local Cs = lpeg.Cs local p_ws_char = S" \n\r\t\v" local p_ws = p_ws_char^1 local p_ws_trailing = p_ws * P (-1) local p_ws_drop = Cs ( (p_ws^-1 / "") * (1 - p_ws_trailing)^0 * (p_ws^-1 * P (-1) / "")) local p_escape_char = (P"\\n" / "\n") + (P"\\r" / "\r") + (P"\\t" / "\t") + (P"\\v" / "\v") local p_unescape = Cs ((p_escape_char + 1)^0) local lpegmatch = lpeg.match trim_whitespace = function (s) return lpegmatch (p_ws_drop , s) end unescape_string = function (s) return lpegmatch (p_unescape, s) end end return { println = mk_out ("stdout", 0, true) , errorln = mk_out ("stderr", 0, true) , noiseln = mk_out ("stderr", 1, true) , debugln = mk_out ("stderr", 2, true) , set_verbosity = set_verbosity , trim_whitespace = trim_whitespace , unescape_string = unescape_string }