Skip to content

Commit

Permalink
chore: Add comments and update readme.
Browse files Browse the repository at this point in the history
Signed-off-by: J.J. Martzki <mars14850@gmail.com>
  • Loading branch information
Martzki committed Apr 14, 2024
1 parent 4b4a000 commit 1a192e4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,47 @@
# dmesg
Golang package to get message from Linux kernel ring buffer.
This package gets message by reading from `/dev/kmsg` like cmd util `dmesg`.

# types
## Msg
```go
type Msg struct {
Level uint64 // SYSLOG lvel
Facility uint64 // SYSLOG facility
Seq uint64 // Message sequence number
TsUsec int64 // Timestamp in microsecond
Caller string // Message caller
IsFragment bool // This message is a fragment of an early message which is not a fragment
Text string // Log text
DeviceInfo map[string]string // Device info
}
```
`Msg` is a serialized message structure by parsing native message. It returned by `Dmesg` or `DmesgWithBufSize`.

# functions
## Dmesg
```go
func Dmesg() ([]Msg, error)
```
Dmesg gets all messages from kernel ring buffer with default buf size 16KB for each message.
It returns serialized message structure and the error while getting messages.
The error `syscall.EINVAL` means the buf size is not enough, consider to use `DmesgWithBufSize` instead.
## RawDmesg
```go
func RawDmesg() ([][]byte, error)
```
RawDmesg gets all messages from kernel ring buffer with default buf size 16KB for each message.
It returns native message from kernel without parsing and the error while getting messages.
The error `syscall.EINVAL` means the buf size is not enough, consider to use `RawDmesgWithBufSize` instead.
## DmesgWithBufSize
```go
func DmesgWithBufSize(bufSize uint32) ([]Msg, error)
```
DmesgWithBufSize gets all messages from kernel ring buffer with specific buf size for each message.
It returns serialized message structure and the error while getting messages.
## RawDmesgWithBufSize
```go
func RawDmesgWithBufSize(bufSize uint32) ([][]byte, error)
```
RawDmesgWithBufSize gets all messages from kernel ring buffer with specific buf size for each message.
It returns native message from kernel without parsing and the error while getting messages.
32 changes: 23 additions & 9 deletions pkg/dmesg/dmesg.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package dmesg provides interfaces to get log messages from linux kernel ring buffer like
// cmd util 'dmesg' by reading data from /dev/kmsg.
package dmesg

import (
Expand All @@ -10,19 +12,19 @@ import (
)

const (
defaultBufSize = uint32(1 << 14)
defaultBufSize = uint32(1 << 14) // 16KB by default
levelMask = uint64(1<<3 - 1)
)

type Msg struct {
Level uint64
Facility uint64
Seq uint64
TsUsec int64
Caller string
IsFragment bool
Text string
DeviceInfo map[string]string
Level uint64 // SYSLOG lvel
Facility uint64 // SYSLOG facility
Seq uint64 // Message sequence number
TsUsec int64 // Timestamp in microsecond
Caller string // Message caller
IsFragment bool // This message is a fragment of an early message which is not a fragment
Text string // Log text
DeviceInfo map[string]string // Device info
}

type dmesg struct {
Expand Down Expand Up @@ -143,22 +145,34 @@ func fetch(bufSize uint32, fetchRaw bool) (dmesg, error) {
return d, err
}

// DmesgWithBufSize gets all messages from kernel ring buffer with specific buf size for each message.
// It returns serialized message structure and the error while getting messages.
func DmesgWithBufSize(bufSize uint32) ([]Msg, error) {
d, err := fetch(bufSize, false)

return d.msg, err
}

// RawDmesgWithBufSize gets all messages from kernel ring buffer with specific buf size for each message.
// It returns native message from kernel without parsing and the error while getting messages.
func RawDmesgWithBufSize(bufSize uint32) ([][]byte, error) {
d, err := fetch(bufSize, true)

return d.raw, err
}

// Dmesg gets all messages from kernel ring buffer with default buf size 16KB for each message.
// It returns serialized message structure and the error while getting messages.
// The error syscall.EINVAL means the buf size is not enough, consider to use
// DmesgWithBufSize instead.
func Dmesg() ([]Msg, error) {
return DmesgWithBufSize(defaultBufSize)
}

// RawDmesg gets all messages from kernel ring buffer with default buf size 16KB for each message.
// It returns native message from kernel without parsing and the error while getting messages.
// The error syscall.EINVAL means the buf size is not enough, consider to use
// RawDmesgWithBufSize instead.
func RawDmesg() ([][]byte, error) {
return RawDmesgWithBufSize(defaultBufSize)
}

0 comments on commit 1a192e4

Please sign in to comment.