summaryrefslogtreecommitdiff
path: root/common.lua
blob: fe29d8cd52a516acc1f8fe5c0ede066bf2851bd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
  }