3 Commits

Author SHA1 Message Date
65a5d026e1 WIP: Refactor for Nim 2.x 2025-12-16 15:13:00 -06:00
9be03f61ea Updates to work under Nim 1.x 2025-12-16 15:12:49 -06:00
3a745ac98f Add -h, --version, and --help options. 2017-08-23 15:19:11 -05:00
4 changed files with 43 additions and 26 deletions

View File

@@ -2,8 +2,9 @@
## =========
##
## Little tool to extract expected information from log streams.
import json, logging, ncurses, os, osproc, sequtils, streams, strutils, threadpool
import nre except toSeq
import std/[json, logging, os, osproc, sequtils, streams, strutils]
import std/nre except toSeq
import docopt, malebolgia, ncurses
import private/ncurses_ext
@@ -15,6 +16,16 @@ type
pattern: Regex
expected, found: bool
SourceKind = enum skStdIn, skFile, skCmd
InSource = object
case kind: SourceKind
of skStdIn:
discard
of skFile:
filename: string
of skCmd:
cmdInvocation: string
let expPattern = re"^-([eE])([^;]+);(.+)$"
var msgChannel: Channel[string]
@@ -24,18 +35,19 @@ proc exitErr(msg: string): void =
quit(QuitFailure)
proc readStream(stream: Stream): void =
var line = TaintedString""
var line: string
try:
while stream.readLine(line): msgChannel.send(line)
except: discard ""
when isMainModule:
var cmdProc: Process
var inStream: Stream
var source = InSource(kind: skStdIn)
var outFile: File
var threadMaster = createMaster()
try:
let VERSION = "0.1.0"
let VERSION = "0.2.0"
let usage = """
Usage:
log_happy [options]
@@ -44,7 +56,8 @@ Usage:
Options:
-v Print version information and exit.
-h --help Print this usage information and exit.
-v --version Print version information and exit.
-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.
-d<def-file> Read expectations from a JSON definitions file.
@@ -112,10 +125,10 @@ Expectations JSON definitions follow this format:
elif arg.startsWith("-i"):
let filename = arg[2..^1]
try:
if not existsFile(filename):
if not fileExists(filename):
exitErr "no such file (" & filename & ")"
inStream = newFileStream(filename)
source = InSource(kind: skFile, filename: filename)
except:
exitErr "could not open file (" & filename & "):\t\n" &
getCurrentExceptionMsg()
@@ -136,10 +149,14 @@ Expectations JSON definitions follow this format:
expected: m[0] == "e",
found: false))
elif arg == "-v":
elif arg == "-v" or arg == "--version":
stdout.writeLine "log_happy v" & VERSION
quit(QuitSuccess)
elif arg == "-h" or arg == "--help":
stdout.writeLine usage
quit(QuitSuccess)
elif arg == "-f": follow = true
elif arg == "--": cmd = ""
else: exitErr "unrecognized argument: " & arg
@@ -152,11 +169,11 @@ Expectations JSON definitions follow this format:
inStream = cmdProc.outputStream
open(msgChannel)
spawn readStream(inStream)
threadMaster.spawn readStream(inStream)
# Init ncurses
let stdscr = initscr()
var height, width: int
var height, width: cint
getmaxyx(stdscr, height, width)
startColor()
@@ -267,5 +284,6 @@ Expectations JSON definitions follow this format:
if not inStream.isNil: close(inStream)
if not outFile.isNil: close(outFile)
threadMaster.cancel()
close(msgChannel)
endwin()

View File

@@ -1,6 +1,6 @@
# Package
version = "0.1.0"
version = "0.2.0"
author = "Jonathan Bernard"
description = "Scan logs for regex-defined events and report on what was found."
license = "MIT"
@@ -8,5 +8,4 @@ bin = @["log_happy"]
# Dependencies
requires @["nim >= 0.16.1", "docopt >= 0.6.4", "ncurses"]
requires @["nim >= 0.16.1", "docopt >= 0.6.4", "ncurses", "malebolgia >= 1.3.2"]

2
mise.toml Normal file
View File

@@ -0,0 +1,2 @@
[tools]
nim = "1"

View File

@@ -1,21 +1,21 @@
import ncurses
export ncurses
proc keypad*(win: ptr window, enable: bool): int {.cdecl, discardable, importc: "keypad", dynlib: libncurses.}
proc scrollok*(win: ptr window, enable: bool): int {.cdecl, discardable, importc: "scrollok", dynlib: libncurses.}
proc nodelay*(win: ptr window, enable: bool): int {.cdecl, discardable, importc: "nodelay", dynlib: libncurses.}
proc nonl*(): int {.cdecl, discardable, importc: "nonl", dynlib: libncurses.}
#proc keypad*(win: ptr window, enable: bool): int {.cdecl, discardable, importc: "keypad", dynlib: libncurses.}
#proc scrollok*(win: ptr window, enable: bool): int {.cdecl, discardable, importc: "scrollok", dynlib: libncurses.}
#proc nodelay*(win: ptr window, enable: bool): int {.cdecl, discardable, importc: "nodelay", dynlib: libncurses.}
#proc nonl*(): int {.cdecl, discardable, importc: "nonl", dynlib: libncurses.}
proc newwin*(num_rows, num_cols, begin_x, begin_y: int): ptr window {.cdecl, discardable, importc: "newwin", dynlib: libncurses.}
proc delwin*(win: ptr window): int {.cdecl, discardable, importc: "delwin", dynlib: libncurses.}
#proc newwin(num_rows, num_cols, begin_x, begin_y: int): ptr window {.cdecl, discardable, importc: "newwin", dynlib: libncurses.}
#proc wgetch*(win: ptr window): int {.cdecl, discardable, importc: "wgetch", dynlib: libncurses.}
proc wrefresh*(win: ptr window): int {.cdecl, discardable, importc: "wrefresh", dynlib: libncurses.}
#proc wrefresh*(win: ptr window): int {.cdecl, discardable, importc: "wrefresh", dynlib: libncurses.}
proc wclear*(win: ptr window): int {.cdecl, discardable, importc: "wclear", dynlib: libncurses.}
proc mvwhline*(win: ptr window, rol, col: int, chType: char, n: int): int {.cdecl, discardable, importc: "whline", dynlib: libncurses.}
proc wmove*(win: ptr window, rol, col: int): int {.cdecl, discardable, importc: "wmove", dynlib: libncurses.}
proc wprintw*(win: ptr window, str: cstring): int {.cdecl, discardable, importc: "wprintw", dynlib: libncurses.}
#proc wprintw*(win: ptr window, str: cstring): int {.cdecl, discardable, importc: "wprintw", dynlib: libncurses.}
#proc mvwprintw*(win: ptr window, row, col: int, str: cstring): int {.cdecl, discardable, importc: "mvwprintw", dynlib: libncurses.}
proc box*(win: ptr window, vert_border, horiz_border: char): int {.cdecl, discardable, importc: "box", dynlib: libncurses.}
@@ -35,5 +35,3 @@ const BLUE* = 4
const MAGENTA* = 5
const CYAN* = 6
const WHITE* = 7