diff options
author | Philipp Gesang <phg@phi-gamma.net> | 2018-07-05 21:55:54 +0200 |
---|---|---|
committer | Philipp Gesang <phg@phi-gamma.net> | 2018-07-05 23:27:26 +0200 |
commit | abfa75eae1e8ac1686ddf855f08595818a25b7bc (patch) | |
tree | 4c2bb5db631e062a8c98d92ea1ed2876f5e72d48 /common.lua | |
parent | dd3a98c473c723919d7a52658f19add97638b858 (diff) | |
download | caldr-abfa75eae1e8ac1686ddf855f08595818a25b7bc.tar.gz |
cal: common: factor out logging
Diffstat (limited to 'common.lua')
-rw-r--r-- | common.lua | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/common.lua b/common.lua new file mode 100644 index 0000000..fe29d8c --- /dev/null +++ b/common.lua @@ -0,0 +1,28 @@ + +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 + +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 + } + |