GoLang 拖尾 UTF16LE windows 日志文件

我如何结合这两个 golang 脚本来跟踪 UTF16LEBOM 的活动日志?

我正在为游戏(Eve Online)开发日志解析器。游戏中的聊天记录可以保存,我想用GoLang读取运行日志,并在“准备去死渣男!”中标记关键字“去死”。

我发现了两个单独工作的例子。将它们结合起来是一个我一直无法弄清楚的挑战。这里的第一个尾巴等效项: https ://medium.com/@arunprabhu.1/tailing-a-file-in-golang-72944204f22b

第二次阅读文件,使其清晰易读并且没有多余的字符: https ://github.com/TomOnTime/utfutil/

我一直在尝试用“等效”替换 utfutil 中的代码,但它似乎缺少几个功能。

// EDIT Almost working. It doesn't look like utfutil is defering the close.


package main


import (

    "bufio"

    "fmt"

    "io"

    "time"

    

    "github.com/TomOnTime/utfutil"

)


func main() {

    logfilefile := "C:/Users/user/Documents/EVE/logs/Chatlogs/chat_20220709_022129_1006197774.txt"

    file, err := utfutil.OpenFile(logfilefile, utfutil.WINDOWS)

    if err != nil {

        return

    }


    defer file.Close()


    reader := bufio.NewReader(file)

    for {

        line, err := reader.ReadString('\n')

        if err != nil {

            if err == io.EOF {

                // without this sleep you would hogg the CPU

                time.Sleep(500 * time.Millisecond)

                continue

            }


            break

        }


        fmt.Printf(string(line))

    }

}

<cut white space>

        ---------------------------------------------------------------


          Channel ID:      local

          Channel Name:    Local

          Listener:        Steve

          Session started: 2022.07.07 16:18:21

        ---------------------------------------------------------------


[ 2022.07.07 17:11:44 ] Steve > hello world

[ 2022.07.07 17:11:48 ] John > hello world

[ 2022.07.07 17:11:51 ] James > hello world

[ 2022.07.07 19:36:53 ] Bob > hello world


慕盖茨4494581
浏览 137回答 1
1回答

慕无忌1623718

您可以通过删除来简化您的代码github.com/TomOnTime/utfutil(这是对 的一个非常薄的包装golang.org/x/text/encoding/unicode)。链接任务通常也比尝试一次完成所有事情更简单;在这里,我从这个答案tailReader中借用(稍作改动)。注意:我只是非常快速地测试了下面的内容(并且手头没有“Eve Online”日志文件)。package mainimport (&nbsp; &nbsp; "bufio"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "io"&nbsp; &nbsp; "os"&nbsp; &nbsp; "time"&nbsp; &nbsp; "golang.org/x/text/encoding/unicode")func main() {&nbsp; &nbsp; file, err := newTailReader("./Local_20220707_170827_1006197774.txt")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; defer file.Close()&nbsp; &nbsp; utf := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)&nbsp; &nbsp; reader := bufio.NewReader(utf.NewDecoder().Reader(file))&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; line, err := reader.ReadString('\n')&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf(string(line))&nbsp; &nbsp; }}// Code copied from https://stackoverflow.com/a/31122253/11810946// and modified to output contents of file before beginning to 'tail'type tailReader struct {&nbsp; &nbsp; io.ReadCloser}func (t tailReader) Read(b []byte) (int, error) {&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; n, err := t.ReadCloser.Read(b)&nbsp; &nbsp; &nbsp; &nbsp; if n > 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return n, nil&nbsp; &nbsp; &nbsp; &nbsp; } else if err != io.EOF {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return n, err&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(10 * time.Millisecond)&nbsp; &nbsp; }}func newTailReader(fileName string) (tailReader, error) {&nbsp; &nbsp; f, err := os.Open(fileName)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return tailReader{}, err&nbsp; &nbsp; }&nbsp; &nbsp; return tailReader{f}, nil}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go