2 Star 1 Fork 0

李玮/trireme-lib

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
dmesgparser.go 1.54 KB
一键复制 编辑 原始数据 按行查看 历史
李玮 提交于 2020-01-29 13:23 +08:00 . v1
package dmesgparser
import (
"fmt"
"os/exec"
"strconv"
"strings"
"sync"
)
// Dmesg struct handle for the dmesg parser
type Dmesg struct {
chanSize int
lastProcessedTime float64
sync.Mutex
}
func getEntryTime(line string) float64 {
leftindex := strings.Index(line, "[")
rightIndex := strings.Index(line, "]")
val, _ := strconv.ParseFloat(line[leftindex+1:rightIndex], 64)
return val
}
// TODOD move to Dmesg -w mode later
// func (r *Dmesg) runDmesgCommandFollowMode(outputChan chan string, interval time.Duration) {
// cmdCtx,cancel := context.WithTimeout(ctx, interval)
// defer cancel()
// cmd := exec.CommandContext(, "Dmesg", "-w", "-l", "warn")
// }
// RunDmesgCommand runs the Dmesg command to capture raw Dmesg output
func (d *Dmesg) RunDmesgCommand() ([]string, error) {
output, err := exec.Command("Dmesg").CombinedOutput()
if err != nil {
return nil, fmt.Errorf("Cannot run Dmesg cmd %s", err)
}
lines := strings.Split(strings.TrimSuffix(string(output), "\n"), "\n")
outputslice := make([]string, len(lines))
elementsadded := 0
for _, line := range lines {
if !isTraceOutput(line) {
continue
}
if d.lastProcessedTime < getEntryTime(line) {
outputslice[elementsadded] = line
}
}
return outputslice[elementsadded:], nil
}
func isTraceOutput(line string) bool {
substring := string(line[strings.Index(line, "]")])
return strings.HasPrefix(strings.TrimSpace(substring), "TRACE:")
}
// New return an initialized Dmesg
func New() *Dmesg {
return &Dmesg{
chanSize: 10000,
lastProcessedTime: 0,
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/emmoblin/trireme-lib.git
git@gitee.com:emmoblin/trireme-lib.git
emmoblin
trireme-lib
trireme-lib
7726874a2b9a

搜索帮助