From 69e11a422a480f140a0adc36f5bfab8e2a922d40 Mon Sep 17 00:00:00 2001 From: Tobias Ulmer Date: Wed, 27 Nov 2013 20:00:34 +0100 Subject: [PATCH] Use console module everywhere Signed-off-by: Tobias Ulmer --- generic/e2lib.lua | 19 +++++++++++-------- generic/e2option.lua | 21 +++++++++++---------- generic/plugin.lua | 6 +++--- global/e2.lua | 7 ++++--- local/e2-build.lua | 3 ++- local/e2-dlist.lua | 5 +++-- local/e2-dsort.lua | 5 ++++- local/e2-help.lua | 7 ++++--- local/e2-ls-project.lua | 37 +++++++++++++++++++------------------ local/e2-playground.lua | 3 ++- 10 files changed, 63 insertions(+), 50 deletions(-) diff --git a/generic/e2lib.lua b/generic/e2lib.lua index 97243b4..dae915a 100644 --- a/generic/e2lib.lua +++ b/generic/e2lib.lua @@ -574,32 +574,35 @@ function e2lib.log(level, msg) if level < 1 or level > 4 then e2lib.log(1, "Internal error: invalid log level") end + if not msg then e2lib.log(1, "Internal error: calling log() without log message") end + local log_prefix = "[" .. level .. "] " - -- remove end of line if it exists - if msg:match("\n$") then - msg = msg:sub(1, msg:len() - 1) + + if string.sub(msg, -1) ~= "\n" then + msg = msg.."\n" end if e2lib.globals.debuglogfile then - -- write out buffered messages first for _,m in ipairs(e2lib.globals.debuglogfilebuffer) do eio.fwrite(e2lib.globals.debuglogfile, m) end e2lib.globals.debuglogfilebuffer = {} - eio.fwrite(e2lib.globals.debuglogfile, log_prefix .. msg .. "\n") + eio.fwrite(e2lib.globals.debuglogfile, log_prefix .. msg) else - table.insert(e2lib.globals.debuglogfilebuffer, log_prefix .. msg .. "\n") + table.insert(e2lib.globals.debuglogfilebuffer, log_prefix .. msg) end + if e2lib.getlog(level) then if e2lib.globals.log_debug then - io.stderr:write(log_prefix) + console.eout(log_prefix .. msg) + else + console.eout(msg) end - io.stderr:write(msg .. "\n") end end diff --git a/generic/e2option.lua b/generic/e2option.lua index 6052fd6..c9788c0 100644 --- a/generic/e2option.lua +++ b/generic/e2option.lua @@ -31,12 +31,13 @@ -- Parsing of command-line options local e2option = {} +local buildconfig = require("buildconfig") +local console = require("console") local e2lib = require("e2lib") local plugin = require("plugin") local err = require("err") local strict = require("strict") local tools = require("tools") -local buildconfig = require("buildconfig") local options = {} local aliases = {} @@ -200,7 +201,7 @@ local function defaultoptions() e2option.flag("version", "show version number", function() - print(buildconfig.VERSIONSTRING) + console.infonl(buildconfig.VERSIONSTRING) plugin.print_descriptions() e2lib.finish(0) end, @@ -208,9 +209,9 @@ local function defaultoptions() e2option.flag("licence", "show licence information", function() - print(e2lib.globals._version) - print() - print(e2lib.globals._licence) + console.infonl(e2lib.globals._version) + console.infonl() + console.infonl(e2lib.globals._licence) e2lib.finish(0) end, category) @@ -404,15 +405,15 @@ end -- @return This function does not return. function e2option.usage(rc) local out + local m = string.format("usage: %s --help for more information\n", + toolname()) + if rc == 0 then - out = io.stdout + console.info(m) else - out = io.stderr + console.eout(m) end - local m = string.format("usage: %s --help for more information\n", - toolname()) - out:write(m) e2lib.finish(rc) end diff --git a/generic/plugin.lua b/generic/plugin.lua index 40acc26..30e5f01 100644 --- a/generic/plugin.lua +++ b/generic/plugin.lua @@ -30,6 +30,7 @@ ]] local plugin = {} +local console = require("console") local err = require("err") local e2lib = require("e2lib") local strict = require("strict") @@ -260,11 +261,10 @@ function plugin.exit_plugins() end --- print a description for each plugin. This is for use with the --version --- option. This version always succeeds. --- @return nil +-- option. This function always succeeds. function plugin.print_descriptions() for i,pd in ipairs(plugins) do - print(pd.description) + console.infonl(pd.description) end end diff --git a/global/e2.lua b/global/e2.lua index 4c0fd5c..b5b3069 100644 --- a/global/e2.lua +++ b/global/e2.lua @@ -28,10 +28,11 @@ along with this program. If not, see . ]] +local buildconfig = require("buildconfig") +local console = require("console") local e2lib = require("e2lib") local e2option = require("e2option") local err = require("err") -local buildconfig = require("buildconfig") local function e2(arg) local rc, re = e2lib.init() @@ -41,8 +42,8 @@ local function e2(arg) e2option.flag("prefix", "print installation prefix", function() - print(buildconfig.PREFIX) - os.exit(0) + console.infonl(buildconfig.PREFIX) + e2lib.finish(0) end) local root = e2lib.locate_project_root() diff --git a/local/e2-build.lua b/local/e2-build.lua index 2a45e11..168793d 100644 --- a/local/e2-build.lua +++ b/local/e2-build.lua @@ -28,6 +28,7 @@ along with this program. If not, see . ]] +local console = require("console") local e2lib = require("e2lib") local e2tool = require("e2tool") local e2build = require("e2build") @@ -223,7 +224,7 @@ local function e2_build(arg) if not bid then return false, re end - print(string.format("%-20s [%s]", r, bid)) + console.infof("%-20s [%s]\n", r, bid) end return true diff --git a/local/e2-dlist.lua b/local/e2-dlist.lua index e604861..5c5e760 100644 --- a/local/e2-dlist.lua +++ b/local/e2-dlist.lua @@ -28,6 +28,7 @@ along with this program. If not, see . ]] +local console = require("console") local e2lib = require("e2lib") local e2tool = require("e2tool") local e2option = require("e2option") @@ -75,8 +76,8 @@ local function e2_dlist(arg) return false, re end - for i = 1, #dep do - print(dep[i]) + for _,d in ipairs(dep) do + console.infonl(d) end return true diff --git a/local/e2-dsort.lua b/local/e2-dsort.lua index c2b9f83..8b6dd71 100644 --- a/local/e2-dsort.lua +++ b/local/e2-dsort.lua @@ -28,6 +28,7 @@ along with this program. If not, see . ]] +local console = require("console") local e2lib = require("e2lib") local e2tool = require("e2tool") local e2option = require("e2option") @@ -55,7 +56,9 @@ local function e2_dsort(arg) local d = e2tool.dsort(info) if d then - for i = 1, #d do print(d[i]) end + for _,dep in ipairs(d) do + console.infonl(dep) + end end return true diff --git a/local/e2-help.lua b/local/e2-help.lua index dc7dfbf..0759ab5 100644 --- a/local/e2-help.lua +++ b/local/e2-help.lua @@ -28,6 +28,7 @@ along with this program. If not, see . ]] +local console = require("console") local e2lib = require("e2lib") local e2tool = require("e2tool") local err = require("err") @@ -71,11 +72,11 @@ local function list_manpage_section(documentation, header, section) end table.sort(sorted) - print(header) + console.infonl(header) for _,displayname in ipairs(sorted) do - print(string.format(" %s", displayname)) + console.infof(" %s\n", displayname) end - print() + console.infonl() return true end diff --git a/local/e2-ls-project.lua b/local/e2-ls-project.lua index 9363f81..7587496 100644 --- a/local/e2-ls-project.lua +++ b/local/e2-ls-project.lua @@ -30,6 +30,7 @@ -- ls-project - show project information -*- Lua -*- +local console = require("console") local e2lib = require("e2lib") local e2tool = require("e2tool") local err = require("err") @@ -114,16 +115,16 @@ local function e2_ls_project(arg) table.sort(sources) local function pempty(s1, s2, s3) - print(string.format(" %s %s %s", s1, s2, s3)) + console.infof(" %s %s %s\n", s1, s2, s3) end local function p0(s1, s2, v) - print(string.format("%s", v)) + console.infonl(v) end local function p1(s1, s2, v) - print(string.format(" o--%s", v)) + console.infof(" o--%s\n", v) end local function p2(s1, s2, v) - print(string.format(" %s o--%s", s1, v)) + console.infof(" %s o--%s\n", s1, v) end local function p3(s1, s2, k, v) if v then @@ -133,9 +134,9 @@ local function e2_ls_project(arg) v = v:sub(2) end - print(string.format(" %s %s o--%-10s = %s", s1, s2, k, v)) + console.infof(" %s %s o--%-10s = %s\n", s1, s2, k, v) else - print(string.format(" %s %s o--%s", s1, s2, k)) + console.infof(" %s %s o--%s\n", s1, s2, k) end end @@ -150,7 +151,7 @@ local function e2_ls_project(arg) i = i + 1 if l then if (l:len() + v:len() + 1) > col then - print(l) + console.infonl(l) l = nil end end @@ -162,44 +163,44 @@ local function e2_ls_project(arg) header = header2 end if l then - print(l) + console.infonl(l) end end if opts.dot or opts["dot-sources"] then local arrow = "->" - print("digraph \"" .. info.project.name .. "\" {") + console.infof("digraph \"%s\" {\n", info.project.name) for _, r in pairs(results) do local res = info.results[r] local deps = e2tool.dlist(info, r) if #deps > 0 then for _, dep in pairs(deps) do if opts.swap then - print(string.format(" \"%s\" %s \"%s\"", dep, arrow, r)) + console.infof(" \"%s\" %s \"%s\"\n", dep, arrow, r) else - print(string.format(" \"%s\" %s \"%s\"", r, arrow, dep)) + console.infof(" \"%s\" %s \"%s\"\n", r, arrow, dep) end end else - print(string.format(" \"%s\"", r)) + console.infof(" \"%s\"\n", r) end if opts["dot-sources"] then for _, src in ipairs(res.sources) do if opts.swap then - print(string.format(" \"%s-src\" %s \"%s\"", src, arrow, r)) + console.infof(" \"%s-src\" %s \"%s\"\n", src, arrow, r) else - print(string.format(" \"%s\" %s \"%s-src\"", r, arrow, src)) + console.infof(" \"%s\" %s \"%s-src\"\n", r, arrow, src) end end end end if opts["dot-sources"] then for _, s in pairs(sources) do - print(string.format(" \"%s-src\" [label=\"%s\", shape=box]", s, s)) + console.infof(" \"%s-src\" [label=\"%s\", shape=box]\n", s, s) end end - print("}") - e2lib.finish() + console.infonl("}") + e2lib.finish(0) end --------------- project name @@ -230,7 +231,7 @@ local function e2_ls_project(arg) p3(s1, s2, k, tostring(ce.flags[k])) end end - print(" |") + console.infonl(" |") --------------------- sources local s1 = "|" diff --git a/local/e2-playground.lua b/local/e2-playground.lua index 7d70439..8d5b660 100644 --- a/local/e2-playground.lua +++ b/local/e2-playground.lua @@ -30,6 +30,7 @@ -- playground - enter existing chroot(1) environment -*- Lua -*- +local console = require("console") local e2lib = require("e2lib") local e2tool = require("e2tool") local e2build = require("e2build") @@ -93,7 +94,7 @@ local function e2_playground(arg) return false, err.new("playground does not exist") end if opts.showpath then - print(info.results[r].build_config.c) + console.infonl(info.results[r].build_config.c) e2lib.finish(0) end -- interactive mode, use bash profile -- 2.39.5