[latex3-commits] [git/LaTeX3-latex3-luaotfload] dev: Some cleanup in logging code (2110bed)

Marcel Fabian Krüger tex at 2krueger.de
Thu Apr 16 21:10:04 CEST 2020


Repository : https://github.com/latex3/luaotfload
On branch  : dev
Link       : https://github.com/latex3/luaotfload/commit/2110bedfb9b1127efeb151d16e5c5e9da02051d1

>---------------------------------------------------------------

commit 2110bedfb9b1127efeb151d16e5c5e9da02051d1
Author: Marcel Fabian Krüger <tex at 2krueger.de>
Date:   Thu Apr 16 20:54:29 2020 +0200

    Some cleanup in logging code


>---------------------------------------------------------------

2110bedfb9b1127efeb151d16e5c5e9da02051d1
 src/luaotfload-loaders.lua |   5 +-
 src/luaotfload-log.lua     | 124 ++++++++++++++++-----------------------------
 src/luaotfload-main.lua    |  51 ++++++++++---------
 3 files changed, 71 insertions(+), 109 deletions(-)

diff --git a/src/luaotfload-loaders.lua b/src/luaotfload-loaders.lua
index 4dd2911..6f9fbfa 100644
--- a/src/luaotfload-loaders.lua
+++ b/src/luaotfload-loaders.lua
@@ -21,10 +21,9 @@ end
 
 
 
-if not lualibs    then error "this module requires Luaotfload" end
-if not luaotfload then error "this module requires Luaotfload" end
+assert(lualibs and luaotfload, "this module requires Luaotfload")
 
-local logreport = luaotfload.log and luaotfload.log.report or print
+local logreport = luaotfload.log.report
 
 local function lua_reader (specification)
   local fullname = specification.resolved
diff --git a/src/luaotfload-log.lua b/src/luaotfload-log.lua
index 6a1b080..a125147 100644
--- a/src/luaotfload-log.lua
+++ b/src/luaotfload-log.lua
@@ -29,9 +29,7 @@ because we lack a user interface to toggle per-subsystem tracing.
 local module_name       = "luaotfload" --- prefix for messages
 local debug             = debug
 
-luaotfload              = luaotfload or { }
-luaotfload.log          = luaotfload.log or { }
-local log               = luaotfload.log
+local log               = {}
 
 local ioopen            = io.open
 local iowrite           = io.write
@@ -45,38 +43,32 @@ local stringformat      = string.format
 local stringsub         = string.sub
 local tableconcat       = table.concat
 local texiowrite_nl     = texio.write_nl
-local texiowrite        = texio.write
 local type              = type
 
 local dummyfunction     = function () end
 
-local texjob = false
-if tex and (tex.jobname or tex.formatname) then
-    --- TeX
-    texjob = true
-end
+local texjob = tex and (tex.jobname or tex.formatname)
 
 local loglevel = 0 --- default
 local logout   = "log"
 
 --- int -> bool
-local function set_loglevel (n)
+function log.set_loglevel (n)
     if type(n) == "number" then
         loglevel = n
     end
     return true
 end
-log.set_loglevel   = set_loglevel
 
 --- unit -> int
-local function get_loglevel ( )
+function log.get_loglevel ( )
     return loglevel
 end
-log.get_loglevel   = get_loglevel
 
 local writeln  --- pointer to terminal/log writer
 local statusln --- terminal writer that reuses the current line
-local first_status = true --- indicate the begin of a status region
+local first_status --- indicate the begin of a status region
+local status_level --- indicate that status messages should be printed as full lines
 
 local log_msg = [[
 logging output redirected to %s
@@ -98,23 +90,15 @@ local function choose_logfile ( )
     return false
 end
 
-local function set_logout (s, finalizers)
+function log.set_logout (s, finalizers)
     if s == "stdout" then
         logout = "redirect"
     elseif s == "file" then --- inject custom logger
         logout = "redirect"
         local chan = choose_logfile ()
         chan:write (stringformat ("logging initiated at %s",
-                                  osdate ("%Y-%m-%d %H:%M:%S", --- i. e. osdate "%F %T"
-                                          ostime ())))
-        local writefile = function (...)
-            if select ("#", ...) == 2 then
-                chan:write (select (2, ...))
-            else
-                chan:write (select (1, ...))
-            end
-        end
-        local writefile_nl= function (...)
+                                  osdate ("%Y-%m-%d %H:%M:%S"))) --- i. e. osdate "%F %T"
+        local function writefile_nl (...)
             chan:write "\n"
             if select ("#", ...) == 2 then
                 chan:write (select (2, ...))
@@ -125,17 +109,14 @@ local function set_logout (s, finalizers)
 
         local writeln_orig = writeln
 
-        texiowrite    = writefile
         texiowrite_nl = writefile_nl
         writeln       = writefile_nl
         statusln      = dummyfunction
 
         finalizers[#finalizers+1] = function ()
             chan:write (stringformat ("\nlogging finished at %s\n",
-                                      osdate ("%Y-%m-%d %H:%M:%S", --- i. e. osdate "%F %T"
-                                              ostime ())))
+                                      osdate ("%Y-%m-%d %H:%M:%S"))) --- i. e. osdate "%F %T"
             chan:close ()
-            texiowrite    = texio.write
             texiowrite_nl = texio.write_nl
             writeln       = writeln_orig
         end
@@ -144,8 +125,6 @@ local function set_logout (s, finalizers)
     return finalizers
 end
 
-log.set_logout = set_logout
-
 local format_error_handler
 if debug then
     local debugtraceback = debug.traceback
@@ -193,19 +172,15 @@ io.stderr:setvbuf "no"
 
 local kill_line = "\r\x1b[K"
 
-if texjob == true then
+if texjob then
     --- We imitate the texio.* functions so the output is consistent.
     function writeln (str)
         iowrite "\n"
         iowrite(str)
     end
     function statusln (str)
-        if first_status == false then
-            iowrite (kill_line)
-        else
-            iowrite "\n"
-        end
-        iowrite (str)
+        iowrite(first_status and "\n" or kill_line)
+        iowrite(str)
     end
 else
     function writeln (str)
@@ -213,7 +188,7 @@ else
         iowrite "\n"
     end
     function statusln (str)
-        if first_status == false then
+        if not first_status then
             iowrite (kill_line)
         end
         iowrite (str)
@@ -263,7 +238,7 @@ local level_ids = { common  = 1, loading = 2, search  = 3 }
 
 --doc]]--
 
-local report = function (mode, lvl, ...)
+local function report(mode, lvl, ...)
     if type(lvl) == "string" then
         lvl = level_ids[lvl]
     end
@@ -286,8 +261,9 @@ log.report = report
 --[[doc--
 
     status_logger -- Overwrites the most recently printed line of the
-    terminal. Its purpose is to provide feedback without spamming
-    stdout with irrelevant messages, i.e. when building the database.
+    terminal if not set to a fixed level. Its purpose is to provide
+    feedback without spamming stdout with irrelevant messages,
+    i.e. when building the database.
 
     Status logging must be initialized by calling status_start() and
     properly reset via status_stop().
@@ -303,55 +279,45 @@ log.report = report
 
 --doc]]--
 
-local status_logger = function (mode, ...)
-    if mode == "log" then
-        basic_logger (...)
+function log.names_status(mode, ...)
+    if status_level then
+        return report(mode, status_level, ...)
     else
-        if mode == "both" and logout ~= "redirect" then
+        if mode == "log" then
             basic_logger (...)
-            stdout (statusln, ...)
         else
-            stdout (statusln, ...)
+            if mode == "both" and logout ~= "redirect" then
+                basic_logger (...)
+                stdout (statusln, ...)
+            else
+                stdout (statusln, ...)
+            end
+            first_status = false
         end
-        first_status = false
     end
 end
 
 --[[doc--
 
-    status_start -- Initialize status logging. This installs the status
-        logger if the loglevel is in the specified range, and the normal
-        logger otherwise.  It also resets the first line state which
-        causing the next line printed using the status logger to not kill
-        the current line.
+    status_start -- Initialize status logging. If the loglevel is in the
+        specified range the status_level is set to false to linewise
+        logging, otherwise the level for status essages is set to the upper
+        bound. It also resets the first line state which causing the next
+        line printed using the status logger to not kill the current line.
 
 --doc]]--
 
-local status_writer
-local status_low  = 99
-local status_high = 99
-
-local function status_start (low, high)
+function log.names_status_start (low, high)
     first_status = true
-    status_low   = low
-    status_high  = high
 
     if os.type == "windows" --- Assume broken terminal.
     or os.getenv "TERM" == "dumb"
     then
-        status_writer = function (mode, ...)
-            report (mode, high, ...)
-        end
+        status_level = high
         return
     end
 
-    if low <= loglevel and loglevel < high then
-        status_writer = status_logger
-    else
-        status_writer = function (mode, ...)
-            report (mode, high, ...)
-        end
-    end
+    use_status = low > loglevel or loglevel >= high and high
 end
 
 --[[doc--
@@ -361,19 +327,15 @@ end
 
 --doc]]--
 
-local status_stop = function (...)
-    if first_status == false then
-        status_writer(...)
-        if texjob == false then
+function log.names_status_stop(...)
+    if not first_status then
+        log.names_status(...)
+        if not texjob then
             writeln ""
         end
     end
 end
 
-log.names_status = function (...) status_writer (...) end
-log.names_status_start = status_start
-log.names_status_stop  = status_stop
-
 --[[doc--
 
     The fontloader comes with the Context logging mechanisms
@@ -386,9 +348,9 @@ log.names_status_stop  = status_stop
 
 --doc]]--
 
-local function texioreporter (message)
+function texio.reporter (message)
     report ("log", 2, message)
 end
 
-texio.reporter = texioreporter
+return log
 --- vim:shiftwidth=4:expandtab:ft=lua
diff --git a/src/luaotfload-main.lua b/src/luaotfload-main.lua
index 7ce6c67..6de0b05 100644
--- a/src/luaotfload-main.lua
+++ b/src/luaotfload-main.lua
@@ -36,7 +36,6 @@ local osgettimeofday              = os.gettimeofday
 config                            = config     or { }
 luaotfload                        = luaotfload or { }
 local luaotfload                  = luaotfload
-luaotfload.log                    = luaotfload.log or { }
 local logreport
 luaotfload.version                = ProvidesLuaModule.version
 luaotfload.loaders                = { }
@@ -120,14 +119,13 @@ local type             = type
 --doc]]--
 
 local function make_loader_name (prefix, name)
-    local msg = luaotfload.log and luaotfload.log.report
-             or function (stream, lvl, cat, ...)
-                 if lvl > 1 then --[[not pressing]] return end
-                 texio.write_nl ("log",
-                                 string.format ("luaotfload | %s : ",
-                                                tostring (cat)))
-                 texio.write (string.format (...))
-             end
+    local msg = logreport or function (stream, lvl, cat, ...)
+                  if lvl > 1 then --[[not pressing]] return end
+                  texio.write_nl ("log",
+                                  string.format ("luaotfload | %s : ",
+                                                 tostring (cat)))
+                  texio.write (string.format (...))
+                end
     if not name then
         msg ("both", 0, "load",
              "Fatal error: make_loader_name (%q, %q).",
@@ -163,7 +161,7 @@ local function make_loader (prefix, load_helper)
         timing_info.t_load [name] = t_end - t_0
         if not ok then
             io.write "\n"
-            local msg = luaotfload.log and luaotfload.log.report or print
+            local msg = logreport or print
             msg ("both", 0, "load", "FATAL ERROR")
             msg ("both", 0, "load", "  × Failed to load %q module %q.",
                  tostring (prefix), tostring (name))
@@ -196,9 +194,9 @@ end
 --doc]]--
 
 local function dummy_loader (name)
-    luaotfload.log.report ("log", 3, "load",
-                           "Skipping module %q on purpose.",
-                           name)
+    logreport ("log", 3, "load",
+               "Skipping module %q on purpose.",
+               name)
 end
 
 local context_environment = setmetatable({}, {__index = _G})
@@ -212,22 +210,22 @@ local function context_isolated_load(name)
 end
 
 local function context_loader (name, path)
-    luaotfload.log.report ("log", 3, "load",
-                           "Loading module %q from Context.",
-                           name)
+    logreport ("log", 3, "load",
+               "Loading module %q from Context.",
+               name)
     local t_0 = osgettimeofday ()
     local modname = make_loader_name (false, name)
     local modpath = modname
     if path then
         if lfs.isdir (path) then
-            luaotfload.log.report ("log", 3, "load",
-                                   "Prepending path %q.",
-                                   path)
+            logreport ("log", 3, "load",
+                       "Prepending path %q.",
+                       path)
             modpath = file.join (path, modname)
         else
-            luaotfload.log.report ("both", 0, "load",
-                                   "Non-existant path %q specified, ignoring.",
-                                   path)
+            logreport ("both", 0, "load",
+                       "Non-existant path %q specified, ignoring.",
+                       path)
         end
     end
     local ret = context_isolated_load (modpath)
@@ -239,8 +237,8 @@ local function context_loader (name, path)
         --- yields a non-zero exit code. This isn’t per se indicating that
         --- something isn’t right, but against HH’s coding practices. We’ll
         --- silently ignore this ever happening on lower log levels.
-        luaotfload.log.report ("log", 4, "load",
-                               "Module %q returned %q.", ret)
+        logreport ("log", 4, "load",
+                   "Module %q returned %q.", ret)
     end
     return ret
 end
@@ -302,10 +300,13 @@ luaotfload.main = function ()
         luaotfload.harfbuzz = harfbuzz
     end
 
+    local log       = loadmodule "log" --- Enable logging as soon as possible
+    luaotfload.log = log
+    logreport = log.report
+
     local init      = loadmodule "init" --- fontloader initialization
     init (function ()
 
-        logreport = luaotfload.log.report
         initialize "parsers"         --- fonts.conf and syntax
         initialize "configuration"   --- configuration options
     end)





More information about the latex3-commits mailing list.