2017-07-06 05:54:38 +00:00
|
|
|
## Log Happy
|
|
|
|
## =========
|
|
|
|
##
|
|
|
|
## Little tool to extract expected information from log streams.
|
2021-06-08 02:57:46 +00:00
|
|
|
import json, ncurses, os, osproc, sequtils, streams, strutils, threadpool
|
2017-07-06 05:54:38 +00:00
|
|
|
import nre except toSeq
|
|
|
|
|
|
|
|
import private/ncurses_ext
|
|
|
|
|
|
|
|
type
|
|
|
|
Expectation* = ref object
|
|
|
|
label: string
|
|
|
|
pattern: Regex
|
|
|
|
expected, found: bool
|
|
|
|
|
|
|
|
let expPattern = re"^-([eE])([^;]+);(.+)$"
|
|
|
|
|
|
|
|
var msgChannel: Channel[string]
|
|
|
|
|
|
|
|
proc exitErr(msg: string): void =
|
|
|
|
stderr.writeLine "log_happy: " & msg
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
proc readStream(stream: Stream): void =
|
|
|
|
var line = TaintedString""
|
|
|
|
try:
|
|
|
|
while stream.readLine(line): msgChannel.send(line)
|
|
|
|
except: discard ""
|
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
var cmdProc: Process
|
2017-07-06 05:52:59 +00:00
|
|
|
var inStream: Stream
|
|
|
|
var outFile: File
|
|
|
|
|
2017-07-06 05:54:38 +00:00
|
|
|
try:
|
2021-06-08 02:57:46 +00:00
|
|
|
let VERSION = "0.2.1"
|
2017-07-06 05:54:38 +00:00
|
|
|
let usage = """
|
|
|
|
Usage:
|
|
|
|
log_happy [options]
|
|
|
|
log_happy <filename> [options]
|
|
|
|
log_happy [options] -- <cmd>
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
2017-08-23 20:17:18 +00:00
|
|
|
-h --help Print this usage information and exit.
|
|
|
|
-v --version Print version information and exit.
|
2017-07-06 05:54:38 +00:00
|
|
|
-e<label>;<pattern> Add something to expect to see in the log stream.
|
|
|
|
-E<label>;<patterh> Add something to expect not to see in the log stream.
|
2017-07-06 05:53:28 +00:00
|
|
|
-d<def-file> Read expectations from a JSON definitions file.
|
2017-07-06 05:54:38 +00:00
|
|
|
-i<in-file> Read JSON definitions from <in-file>.
|
2017-07-06 05:52:59 +00:00
|
|
|
-o<out-file> Write the log to <out-file>. Usefull for viewing
|
|
|
|
output later when executing commands.
|
2017-07-06 05:54:38 +00:00
|
|
|
-f Similar to tail, do not stop when the end of file
|
|
|
|
is reached but wait for additional modifications
|
|
|
|
to the file. -f is ignored when the input is STDIN.
|
|
|
|
|
|
|
|
Expectation definitions take the format LABEL;REGEX where the LABEL is the text
|
|
|
|
label log_happy will use to describe the event, and REGEX is the regular
|
|
|
|
expression log_happy will use to identify the event.
|
|
|
|
|
|
|
|
Expectations JSON definitions follow this format:
|
|
|
|
{
|
|
|
|
"expected": {
|
|
|
|
"<label>": "<regex>",
|
|
|
|
"<label>": "<regex>"
|
|
|
|
},
|
|
|
|
"unexpected": {
|
|
|
|
"<label>": "<regex>",
|
|
|
|
"<label>": "<regex>"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
let args = commandLineParams()
|
|
|
|
|
|
|
|
var expectations: seq[Expectation] = @[]
|
2021-06-08 02:57:46 +00:00
|
|
|
var follow = false
|
2017-07-06 05:54:38 +00:00
|
|
|
var cmd = "NOCMD"
|
|
|
|
|
|
|
|
if args.len == 0:
|
|
|
|
echo usage
|
|
|
|
quit(QuitSuccess)
|
|
|
|
|
|
|
|
for arg in args:
|
|
|
|
if cmd != "NOCMD": cmd &= " " & arg
|
|
|
|
|
|
|
|
elif arg.startsWith("-d"):
|
|
|
|
let filename = arg[2..^1]
|
|
|
|
try:
|
|
|
|
let defs = parseFile(filename)
|
|
|
|
|
|
|
|
if defs.hasKey("expected"):
|
|
|
|
for item in defs["expected"].pairs():
|
|
|
|
expectations.add(Expectation(
|
|
|
|
label: item.key,
|
|
|
|
pattern: re(item.val.getStr()),
|
|
|
|
expected: true,
|
|
|
|
found: false))
|
|
|
|
|
|
|
|
if defs.hasKey("unexpected"):
|
|
|
|
for item in defs["unexpected"].pairs():
|
|
|
|
expectations.add(Expectation(
|
|
|
|
label: item.key,
|
|
|
|
pattern: re(item.val.getStr()),
|
|
|
|
expected: false,
|
|
|
|
found: false))
|
|
|
|
except:
|
|
|
|
exitErr "could not parse definitions file (" &
|
|
|
|
filename & "):\n\t" & getCurrentExceptionMsg()
|
|
|
|
|
|
|
|
elif arg.startsWith("-i"):
|
|
|
|
let filename = arg[2..^1]
|
|
|
|
try:
|
2021-06-08 02:57:46 +00:00
|
|
|
if not fileExists(filename):
|
2017-07-06 05:54:38 +00:00
|
|
|
exitErr "no such file (" & filename & ")"
|
|
|
|
|
|
|
|
inStream = newFileStream(filename)
|
|
|
|
except:
|
|
|
|
exitErr "could not open file (" & filename & "):\t\n" &
|
|
|
|
getCurrentExceptionMsg()
|
|
|
|
|
2017-07-06 05:52:59 +00:00
|
|
|
elif arg.startsWith("-o"):
|
|
|
|
let filename = arg[2..^1]
|
|
|
|
try:
|
|
|
|
outFile = open(filename, fmWrite)
|
|
|
|
except:
|
|
|
|
exitErr "could not open output file in write mode (" & filename &
|
|
|
|
"):\n\t" & getCurrentExceptionMsg()
|
2021-06-08 02:57:46 +00:00
|
|
|
|
2017-07-06 05:54:38 +00:00
|
|
|
elif arg.match(expPattern).isSome:
|
|
|
|
var m = arg.match(expPattern).get().captures()
|
|
|
|
expectations.add(Expectation(
|
|
|
|
label: m[1],
|
|
|
|
pattern: re(m[2]),
|
|
|
|
expected: m[0] == "e",
|
|
|
|
found: false))
|
|
|
|
|
2017-08-23 20:17:18 +00:00
|
|
|
elif arg == "-v" or arg == "--version":
|
2017-07-06 05:53:28 +00:00
|
|
|
stdout.writeLine "log_happy v" & VERSION
|
|
|
|
quit(QuitSuccess)
|
|
|
|
|
2017-08-23 20:17:18 +00:00
|
|
|
elif arg == "-h" or arg == "--help":
|
|
|
|
stdout.writeLine usage
|
|
|
|
quit(QuitSuccess)
|
|
|
|
|
2017-07-06 05:54:38 +00:00
|
|
|
elif arg == "-f": follow = true
|
|
|
|
elif arg == "--": cmd = ""
|
|
|
|
else: exitErr "unrecognized argument: " & arg
|
2021-06-08 02:57:46 +00:00
|
|
|
|
2017-07-06 05:52:59 +00:00
|
|
|
if cmd == "NOCMD" and inStream.isNil:
|
2017-07-06 05:54:38 +00:00
|
|
|
exitErr "no input file or command to execute."
|
|
|
|
|
2017-07-06 05:52:59 +00:00
|
|
|
elif inStream.isNil:
|
2017-07-06 05:54:38 +00:00
|
|
|
cmdProc = startProcess(cmd, "", [], nil, {poStdErrToStdOut, poEvalCommand, poUsePath})
|
|
|
|
inStream = cmdProc.outputStream
|
|
|
|
|
|
|
|
open(msgChannel)
|
|
|
|
spawn readStream(inStream)
|
|
|
|
|
|
|
|
# Init ncurses
|
|
|
|
let stdscr = initscr()
|
2021-06-08 02:57:46 +00:00
|
|
|
var height, width: cint
|
2017-07-06 05:54:38 +00:00
|
|
|
getmaxyx(stdscr, height, width)
|
|
|
|
|
|
|
|
startColor()
|
|
|
|
noecho()
|
|
|
|
cbreak()
|
|
|
|
keypad(stdscr, true)
|
|
|
|
clear()
|
|
|
|
nonl()
|
|
|
|
|
|
|
|
init_pair(1, GREEN, BLACK)
|
|
|
|
init_pair(2, RED, BLACK)
|
|
|
|
|
2017-07-06 05:52:59 +00:00
|
|
|
# Figure out how much room we need to display our information
|
|
|
|
let labelWidth = mapIt(expectations, it.label.len).foldl(max(a, b)) + 10
|
|
|
|
let labelsPerLine = width div labelWidth
|
|
|
|
let dispHeight = max((expectations.len div labelsPerLine) + 1, 2)
|
2017-07-06 05:54:38 +00:00
|
|
|
let logwin = newwin(height - dispHeight, width, 0, 0)
|
|
|
|
let dispwin = newwin(dispHeight, width, height - dispHeight, 0)
|
|
|
|
dispwin.wborder(' ', ' ', '_', ' ', '_', '_', ' ', ' ')
|
|
|
|
dispwin.wrefresh()
|
|
|
|
|
|
|
|
logwin.scrollok(true)
|
|
|
|
logwin.nodelay(true)
|
|
|
|
|
|
|
|
# init run loop
|
|
|
|
var stop = false
|
|
|
|
var firstPass = true
|
|
|
|
var drawDisp = false
|
|
|
|
|
|
|
|
while not stop:
|
|
|
|
|
|
|
|
var lineMsg = msgChannel.tryRecv()
|
|
|
|
|
|
|
|
if lineMsg.dataAvailable:
|
|
|
|
let line = lineMsg.msg
|
2017-07-06 05:52:59 +00:00
|
|
|
if not outFile.isNil: outFile.writeLine(line)
|
2017-07-06 05:54:38 +00:00
|
|
|
for expect in expectations:
|
|
|
|
if not expect.found:
|
|
|
|
if line.find(expect.pattern).isSome:
|
|
|
|
expect.found = true
|
|
|
|
drawDisp = true
|
|
|
|
|
|
|
|
if drawDisp or firstPass:
|
|
|
|
dispwin.wmove(1, 0)
|
2017-07-06 05:52:59 +00:00
|
|
|
var labelsOnLine = 0
|
2017-07-06 05:54:38 +00:00
|
|
|
for expect in expectations:
|
2017-07-06 05:52:59 +00:00
|
|
|
var text =
|
2017-07-06 05:54:38 +00:00
|
|
|
if expect.found: " FOUND "
|
|
|
|
else: " MISSING "
|
2017-07-06 05:52:59 +00:00
|
|
|
text = align(expect.label & text, labelWidth)
|
2017-07-06 05:54:38 +00:00
|
|
|
|
|
|
|
if expect.expected == expect.found:
|
|
|
|
dispwin.wattron(COLOR_PAIR(1))
|
2017-07-06 05:52:59 +00:00
|
|
|
dispwin.wprintw(text)
|
2017-07-06 05:54:38 +00:00
|
|
|
dispwin.wattroff(COLOR_PAIR(1))
|
|
|
|
|
|
|
|
else:
|
|
|
|
dispwin.wattron(COLOR_PAIR(2))
|
2017-07-06 05:52:59 +00:00
|
|
|
dispwin.wprintw(text)
|
2017-07-06 05:54:38 +00:00
|
|
|
dispwin.wattroff(COLOR_PAIR(2))
|
|
|
|
|
2017-07-06 05:52:59 +00:00
|
|
|
if labelsOnLine == labelsPerLine - 1:
|
|
|
|
labelsOnLine = 0
|
|
|
|
dispwin.wprintw("\n")
|
|
|
|
else: labelsOnLine += 1
|
|
|
|
|
2017-07-06 05:54:38 +00:00
|
|
|
dispwin.wrefresh()
|
|
|
|
firstPass = false
|
2021-06-08 02:57:46 +00:00
|
|
|
|
2017-07-06 05:54:38 +00:00
|
|
|
logwin.wprintw("\n" & line)
|
|
|
|
logwin.wrefresh()
|
|
|
|
|
|
|
|
let ch = logwin.wgetch()
|
|
|
|
|
|
|
|
# Stop on CTRL-D
|
|
|
|
if ch == 4:
|
|
|
|
stop = true
|
|
|
|
if not cmdProc.isNil and cmdProc.running(): cmdProc.terminate()
|
|
|
|
break
|
|
|
|
|
|
|
|
# Reset on CTRL-R
|
|
|
|
elif ch == 18:
|
|
|
|
for expect in expectations: expect.found = false
|
|
|
|
drawDisp = true
|
|
|
|
|
|
|
|
sleep 1
|
|
|
|
|
|
|
|
# echo $args
|
|
|
|
# echo "\n\n\n"
|
|
|
|
#
|
|
|
|
# for line in lines fin:
|
|
|
|
# stdout.cursorUp(1)
|
|
|
|
# stdout.eraseLine()
|
|
|
|
# stdout.cursorUp(1)
|
|
|
|
# stdout.eraseLine()
|
|
|
|
#
|
|
|
|
# stdout.writeLine line
|
|
|
|
|
|
|
|
# Scan for patterns
|
|
|
|
# Print PASS/FAIL when patterns are seen.
|
|
|
|
except:
|
|
|
|
exitErr getCurrentExceptionMsg()
|
|
|
|
|
|
|
|
finally:
|
2017-07-06 05:52:59 +00:00
|
|
|
if not cmdProc.isNil:
|
|
|
|
if running(cmdProc): kill(cmdProc)
|
|
|
|
else:
|
|
|
|
if not inStream.isNil: close(inStream)
|
|
|
|
if not outFile.isNil: close(outFile)
|
|
|
|
|
2017-07-06 05:54:38 +00:00
|
|
|
close(msgChannel)
|
|
|
|
endwin()
|