亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

GoLang 拖尾 UTF16LE windows 日志文件

GoLang 拖尾 UTF16LE windows 日志文件

Go
慕蓋茨4494581 2023-01-03 11:21:09
我如何結合這兩個 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 mainimport (    "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
查看完整描述

1 回答

?
慕無忌1623718

TA貢獻1744條經驗 獲得超4個贊

您可以通過刪除來簡化您的代碼github.com/TomOnTime/utfutil(這是對 的一個非常薄的包裝golang.org/x/text/encoding/unicode)。鏈接任務通常也比嘗試一次完成所有事情更簡單;在這里,我從這個答案tailReader中借用(稍作改動)。

注意:我只是非??焖俚販y試了下面的內容(并且手頭沒有“Eve Online”日志文件)。

package main


import (

    "bufio"

    "fmt"

    "io"

    "os"

    "time"


    "golang.org/x/text/encoding/unicode"

)


func main() {

    file, err := newTailReader("./Local_20220707_170827_1006197774.txt")

    if err != nil {

        return

    }

    defer file.Close()


    utf := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)

    reader := bufio.NewReader(utf.NewDecoder().Reader(file))


    for {

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

        if err != nil {

            fmt.Println(err)

            break

        }


        fmt.Printf(string(line))

    }


}


// Code copied from https://stackoverflow.com/a/31122253/11810946

// and modified to output contents of file before beginning to 'tail'

type tailReader struct {

    io.ReadCloser

}


func (t tailReader) Read(b []byte) (int, error) {

    for {

        n, err := t.ReadCloser.Read(b)

        if n > 0 {

            return n, nil

        } else if err != io.EOF {

            return n, err

        }

        time.Sleep(10 * time.Millisecond)

    }

}


func newTailReader(fileName string) (tailReader, error) {

    f, err := os.Open(fileName)

    if err != nil {

        return tailReader{}, err

    }

    return tailReader{f}, nil

}


查看完整回答
反對 回復 2023-01-03
  • 1 回答
  • 0 關注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號