代码拉取完成,页面将自动刷新
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,
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。