Skip to content

Commit

Permalink
lib: add process logging
Browse files Browse the repository at this point in the history
log() may be called to enable logging; log(nil) would turn it back off.  The
log will be opened in write-append mode, preserving existing contents.

Fixes #13

Signed-off-by: Kyle Evans <[email protected]>
  • Loading branch information
kevans91 committed Feb 10, 2024
1 parent 7c6c6e8 commit 25400a9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
20 changes: 20 additions & 0 deletions lib/orch/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ actions.defined = {
return true
end,
},
log = {
init = function(action, args)
local file = args[1]
if type(file) == "string" then
file = io.open(file, "a+")
end

action.file = file
end,
execute = function(action)
local current_process = action.ctx.process

if not current_process then
error("execute() called before process spawned.")
end

current_process:logfile(action.file)
return true
end,
},
raw = {
init = function(action, args)
action.value = args[1]
Expand Down
22 changes: 20 additions & 2 deletions lib/orch/process.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ function MatchBuffer:refill(action, timeout)
return true
end

if self.process.log then
self.process.log:write(input)
end

self.buffer = self.buffer .. input
if type(action) == "table" then
return self:_matches(action)
Expand Down Expand Up @@ -111,20 +115,34 @@ function Process:read(func, timeout)
return self._process:read(func)
end
end
function Process:raw(text)
return self._process:raw(text)
function Process:raw(is_raw)
self.is_raw = is_raw
return self._process:raw(is_raw)
end
function Process:write(data)
if self.log then
self.log:write(data)
end
return self._process:write(data)
end
function Process:close()
assert(self._process:close())

-- Flush output, close everything out
self:logfile(nil)
self._process = nil
self.term = nil
return true
end
-- Our own special salt
function Process:logfile(file)
if self.log then
self.log:flush()
self.log:close()
end

self.log = file
end
function Process:match(action)
local buffer = self.buffer
if not buffer:match(action) then
Expand Down
10 changes: 9 additions & 1 deletion man/orch.5
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
.\"
.\" SPDX-License-Identifier: BSD-2-Clause
.\"
.Dd January 27, 2024
.Dd February 10, 2024
.Dt ORCH 5
.Os
.Sh NAME
Expand Down Expand Up @@ -135,6 +135,14 @@ style hexdump output to stderr of the input
.Pp
This directive is always processed immediately, and is intended to be used
within a failure context to aide in analysis of why a match failed.
.It Fn log "logfile"
Sets a logfile for subsequent input and output to the process.
The
.Fa logfile
may be a string specifying a filename or an open file (for lib users).
If a filename is specified, then
.Nm
will open it in write-append mode and preserve existing contents.
.It Fn matcher "type"
Changes the default matcher for subsequent match blocks to the type described
by
Expand Down

0 comments on commit 25400a9

Please sign in to comment.