summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Gesang <phg@phi-gamma.net>2018-07-05 21:55:54 +0200
committerPhilipp Gesang <phg@phi-gamma.net>2018-07-05 23:27:26 +0200
commitabfa75eae1e8ac1686ddf855f08595818a25b7bc (patch)
tree4c2bb5db631e062a8c98d92ea1ed2876f5e72d48
parentdd3a98c473c723919d7a52658f19add97638b858 (diff)
downloadcaldr-abfa75eae1e8ac1686ddf855f08595818a25b7bc.tar.gz
cal: common: factor out logging
-rw-r--r--cal.lua31
-rw-r--r--common.lua28
2 files changed, 34 insertions, 25 deletions
diff --git a/cal.lua b/cal.lua
index b7f3a9d..3a5c509 100644
--- a/cal.lua
+++ b/cal.lua
@@ -22,27 +22,11 @@ local stringsub = string.sub
local table = require "table"
local tableconcat = table.concat
-local verboselvl = 3
-
-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 println = mk_out ("stdout", 0, true)
-local errorln = mk_out ("stderr", 0, true)
-local noiseln = mk_out ("stderr", 1, true)
-local debugln = mk_out ("stderr", 2, true)
+local common = require "common"
+local println = common.println
+local errorln = common.errorln
+local noiseln = common.noiseln
+local debugln = common.debugln
--[[--
@@ -460,8 +444,6 @@ end
--- API
-------------------------------------------------------------------------------
-local set_verbosity = function (n) verboselvl = n end
-
local parse = function (raw)
local ok, lines = pcall (parse_calendar_lines, raw)
if not ok then
@@ -509,8 +491,7 @@ local output = function (cal)
return output_handle (cal, iostdout)
end
-return { set_verbosity = set_verbosity
- , parse = parse
+return { parse = parse
, parse_handle = parse_handle
, parse_file = parse_file
, parse_stdin = parse_stdin
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
+ }
+