diff --git a/cmd/godmesg/main.go b/cmd/godmesg/main.go index 77a1ff1..cca6735 100644 --- a/cmd/godmesg/main.go +++ b/cmd/godmesg/main.go @@ -25,12 +25,13 @@ import ( ) func main() { - kmsgParser, err := kmsgparser.NewParser() + parser, err := kmsgparser.NewParser() if err != nil { log.Fatalf("unable to create parser: %v", err) } + defer parser.Close() - kmsg := kmsgParser.Parse() + kmsg := parser.Parse() for msg := range kmsg { fmt.Printf("(%d) - %s: %s", msg.SequenceNumber, msg.Timestamp.Format(time.RFC3339Nano), msg.Message) diff --git a/kmsgparser/kmsgparser.go b/kmsgparser/kmsgparser.go index 6407e85..618fd89 100644 --- a/kmsgparser/kmsgparser.go +++ b/kmsgparser/kmsgparser.go @@ -39,6 +39,8 @@ type Parser interface { // SetLogger sets the logger that will be used to report malformed kernel // ringbuffer lines or unexpected kmsg read errors. SetLogger(Logger) + // Close closes the underlying kmsg reader for this parser + Close() error } // Message represents a given kmsg logline, including its timestamp (as @@ -90,6 +92,10 @@ func (p *parser) SetLogger(log Logger) { p.log = log } +func (p *parser) Close() error { + return p.kmsgReader.Close() +} + // Parse will read from the provided reader and provide a channel of messages // parsed. // If the provided reader *is not* a proper Linux kmsg device, Parse might not