diff options
| author | Philipp Gesang <phg42.2a@gmail.com> | 2013-07-03 01:48:10 +0200 | 
|---|---|---|
| committer | Philipp Gesang <phg42.2a@gmail.com> | 2013-07-03 01:48:10 +0200 | 
| commit | 1a42a842639df21d4738bb8ac12f146d829a9d17 (patch) | |
| tree | 88e65914ef5e9a19ac3f635fde288725ef72f80d | |
| parent | cba3f693b07b52734aa8ef6c35eb99ac4946ba04 (diff) | |
| download | luaotfload-1a42a842639df21d4738bb8ac12f146d829a9d17.tar.gz | |
add option to redirect log output to file
| -rw-r--r-- | luaotfload-override.lua | 77 | ||||
| -rwxr-xr-x | luaotfload-tool.lua | 24 | ||||
| -rw-r--r-- | luaotfload-tool.rst | 10 | 
3 files changed, 100 insertions, 11 deletions
| diff --git a/luaotfload-override.lua b/luaotfload-override.lua index 225ac68..39cc172 100644 --- a/luaotfload-override.lua +++ b/luaotfload-override.lua @@ -15,17 +15,23 @@ because we lack a user interface to toggle per-subsystem tracing.  local module_name       = "luaotfload" +local ioopen            = io.open +local iowrite           = io.write +local lfsisdir          = lfs.isdir +local lfsisfile         = lfs.isfile +local md5sumhexa        = md5.sumhexa +local osdate            = os.date +local ostime            = os.time  local select            = select  local stringformat      = string.format +local stringsub         = string.sub  local tableconcat       = table.concat +local texio_write_nl    = texio.write_nl  local texiowrite_nl     = texio.write_nl +local texio_write       = texio.write  local texiowrite        = texio.write  local type              = type -local texio_write_nl    = texio.write_nl -local texio_write       = texio.write -local iowrite           = io.write -  --[[doc--  We recreate the verbosity levels previously implemented in font-nms: @@ -62,9 +68,67 @@ logs.getloglevel    = get_loglevel  logs.get_loglevel   = get_loglevel  logs.get_log_level  = get_loglevel -local set_logout = function (s) +local writeln --- scope so we can change it + +local log_msg = [[ +logging output redirected to %s +to monitor the progress run "tail -f %s" in another terminal +]] + +local tmppath = os.getenv "TMPDIR" or "/tmp" + +local choose_logfile = function ( ) +    if lfsisdir (tmppath) then +        local fname +        repeat --- ensure that file of that name doesn’t exist +            fname = tmppath .. "/luaotfload-log-" +                            .. stringsub (md5sumhexa (ostime ()), 1, 8) +        until not lfsisfile (fname) +        iowrite (stringformat (log_msg, fname, fname)) +        return ioopen (fname, "w") +    end +    --- missing /tmp +    return false +end + +local set_logout = function (s, finalizers)      if s == "stdout" then          logout = "term" +    elseif s == "file" then --- inject custom logger +        local chan = choose_logfile () +        chan:write (stringformat ("logging initiated at %s", +                                  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 (...) +            chan:write "\n" +            if select ("#", ...) == 2 then +                chan:write (select (2, ...)) +            else +                chan:write (select (1, ...)) +            end +        end + +        local writeln_orig = writeln + +        texiowrite    = writefile +        texiowrite_nl = writefile_nl +        writeln       = writefile_nl + +        finalizers[#finalizers+1] = function () +            chan:write (stringformat ("\nlogging finished at %s\n", +                                      osdate ("%F %T", ostime ()))) +            chan:close () +            texiowrite    = texio.write +            texiowrite_nl = texio.write_nl +            writeln       = writeln_orig +        end +        return finalizers      --else --- remains “log”      end  end @@ -92,7 +156,6 @@ end  io.stdout:setvbuf "no"  io.stderr:setvbuf "no" -local writeln  if tex and (tex.jobname or tex.formatname) then      --- TeX      writeln = texiowrite_nl @@ -155,7 +218,7 @@ local names_report = function (mode, lvl, ...)      if loglevel >= lvl then          if mode == "log" then              log (...) -        elseif mode == "both" then +        elseif mode == "both" and log ~= "stdout" then              log (...)              stdout (...)          else diff --git a/luaotfload-tool.lua b/luaotfload-tool.lua index 45a75ba..392be6e 100755 --- a/luaotfload-tool.lua +++ b/luaotfload-tool.lua @@ -902,6 +902,22 @@ actions.list = function (job)      return true, true  end +--- stuff to be carried out prior to exit + +local finalizers = { } + +--- returns false if at least one of the actions failed, mainly +--- for closing io channels +local finalize = function () +    local success = true +    for _, fun in next, finalizers do +        if type (fun) == "function" then +            if fun () == false then success = false end +        end +    end +    return success +end +  --[[--  Command-line processing.  mkluatexfontdb.lua relies on the script alt_getopt to process argv and @@ -985,7 +1001,7 @@ local process_cmdline = function ( ) -- unit -> jobspec          elseif v == "log" then              local str = optarg[n]              if str then -                logs.set_logout(str) +                finalizers = logs.set_logout(str, finalizers)              end          elseif v == "find" then              action_pending["query"] = true @@ -1065,7 +1081,11 @@ local main = function ( ) -- unit -> int          if exit then break end      end -    texiowrite_nl"" +    if finalize () == false then +        retval = -1 +    end + +    --texiowrite_nl""      return retval  end diff --git a/luaotfload-tool.rst b/luaotfload-tool.rst index 7b75367..31a1010 100644 --- a/luaotfload-tool.rst +++ b/luaotfload-tool.rst @@ -116,8 +116,14 @@ miscellaneous  --verbose=N, -v         Set verbosity level to *n* or the number of                          repetitions of ``-v``.  --quiet                 No verbose output (log level set to zero). ---log=stdout            Redirect log output to terminal (for database -                        troubleshooting). +--log=CHANNEL           Redirect log output (for database +                        troubleshooting), where *CHANNEL* can be + +                        1) ``stdout`` -> all output will be +                           dumped to the terminal; or +                        2) ``file`` -> write to a file to the temporary +                           directory (the name will be chosen +                           automatically (**experimental!**).  --version, -V           Show version number and exit.  --help, -h              Show help message and exit. | 
