Skip to content

Commit

Permalink
Beta479 (#483)
Browse files Browse the repository at this point in the history
* beta447

* beta448

* beta449

* beta450

* beta451

* beta452

* beta453

* beta454

* beta455

* btea455

* beta456

* beta457

* beta458

* beta460

* beta460

* beta461

* beta462

* beta463

* beta464

* beta465

* beta467

* beta468

* beta469

* beta470

* beta471

* beta472

* beta473

* beta473

* beta475

* beta476

* beta478

* beta479

* beta479
  • Loading branch information
Hoshinonyaruko authored Aug 17, 2024
1 parent f741539 commit 6c88176
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 16 deletions.
1 change: 1 addition & 0 deletions Processor/ProcessC2CMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (p *Processors) ProcessC2CMessage(data *dto.WSC2CMessageData) error {

if data.Author.ID == "" {
mylog.Printf("出现ID为空未知错误.%v\n", data)
return nil
}

//获取当前的s值 当前ws连接所收到的信息条数
Expand Down
1 change: 1 addition & 0 deletions Processor/ProcessGroupMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (p *Processors) ProcessGroupMessage(data *dto.WSGroupATMessageData) error {

if data.Author.ID == "" {
mylog.Printf("出现ID为空未知错误.%v\n", data)
return nil
}

if !config.GetStringOb11() {
Expand Down
16 changes: 15 additions & 1 deletion botgo/sessions/multi/multi.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package multi

import (
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -93,6 +94,19 @@ func (sm *ShardManager) newConnect(session dto.Session, shardID uint32) {
}
if err := wsClient.Listening(); err != nil {
log.Errorf("[ws/session] Listening error: %+v", err)
sm.SessionChans[shardID] <- session // Reconnect
currentSession := wsClient.Session()
// 对于不能够进行重连的session,需要清空 session id 与 seq
if manager.CanNotResume(err) {
currentSession.ID = ""
currentSession.LastSeq = 0
}
// 一些错误不能够鉴权,比如机器人被封禁,这里就直接退出了
if manager.CanNotIdentify(err) {
msg := fmt.Sprintf("can not identify because server return %+v, so process exit", err)
log.Errorf(msg)
panic(msg) // 当机器人被下架,或者封禁,将不能再连接,所以 panic
}
// 将 session 放到 session chan 中,用于启动新的连接,当前连接退出
sm.SessionChans[shardID] <- *currentSession // Reconnect
}
}
5 changes: 4 additions & 1 deletion handlers/send_private_msg_sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func HandleSendPrivateMsgSSE(client callapi.Client, api openapi.OpenAPI, apiv2 o

// 输出反序列化后的对象,确认是否成功转换
fmt.Printf("Recovered InterfaceBody: %+v\n", messageBody)

// 使用 echo 获取消息ID
var messageID string
if config.GetLazyMessageId() {
Expand All @@ -135,7 +136,9 @@ func HandleSendPrivateMsgSSE(client callapi.Client, api openapi.OpenAPI, apiv2 o

// 获取并打印相关ID
relatedID := GetRelatedID(messageID)
fmt.Println("相关ID:", relatedID)

//fmt.Println("相关ID:", relatedID)

dtoSSE := generateMessageSSE(messageBody, messageID, relatedID)

mylog.Printf("私聊发信息sse:%v", dtoSSE)
Expand Down
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"syscall"
"time"

// _ "net/http/pprof"

"github.com/fatih/color"
"github.com/fsnotify/fsnotify"
"github.com/hoshinonyaruko/gensokyo/Processor"
Expand Down Expand Up @@ -125,6 +127,7 @@ func main() {
//logger
logLevel := mylog.GetLogLevelFromConfig(config.GetLogLevel())
loggerAdapter := mylog.NewMyLogAdapter(logLevel, config.GetSaveLogs())
mylog.SetLogLevel(logLevel)
botgo.SetLogger(loggerAdapter)

if *m {
Expand Down Expand Up @@ -577,6 +580,14 @@ func main() {
}()
}

// // 启动一个用于 pprof 的 HTTP 服务器
// go func() {
// log.Println("pprof server running on :6060")
// if err := http.ListenAndServe("localhost:6060", nil); err != nil {
// log.Fatalf("pprof server failed: %s", err)
// }
// }()

//杂七杂八的地方
if conf.Settings.MemoryMsgid {
echo.StartCleanupRoutine()
Expand Down
54 changes: 40 additions & 14 deletions mylog/mylog.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const (
LogLevelError
)

var currentLevel = LogLevelInfo // 默认日志级别为 INFO

type Client struct {
conn *websocket.Conn
send chan EnhancedLogEntry
Expand Down Expand Up @@ -249,32 +251,56 @@ type EnhancedLogEntry struct {
// 日志频道,所有的 WebSocket 客户端都会在此监听日志事件
var logChannel = make(chan EnhancedLogEntry, 1000)

func SetLogLevel(level LogLevel) {
if level >= LogLevelDebug && level <= LogLevelError {
currentLevel = level
} else {
log.Printf("Invalid log level: %d", level)
}
}

func Println(v ...interface{}) {
log.Println(v...)
message := fmt.Sprint(v...)
emitLog("INFO", message)
LogToFile("INFO", message)
if currentLevel <= LogLevelInfo {
log.Println(v...)
message := fmt.Sprint(v...)
emitLog("INFO", message)
LogToFile("INFO", message)
}
}

func Printf(format string, v ...interface{}) {
log.Printf(format, v...)
message := fmt.Sprintf(format, v...)
emitLog("INFO", message)
LogToFile("INFO", message)
if currentLevel <= LogLevelInfo {
log.Printf(format, v...)
message := fmt.Sprintf(format, v...)
emitLog("INFO", message)
LogToFile("INFO", message)
}
}

func Warnf(format string, v ...interface{}) {
if currentLevel <= LogLevelWarn {
log.Printf(format, v...)
message := fmt.Sprintf(format, v...)
emitLog("WARN", message)
LogToFile("WARN", message)
}
}

func Errorf(format string, v ...interface{}) {
log.Printf(format, v...)
message := fmt.Sprintf(format, v...)
emitLog("ERROR", message)
LogToFile("ERROR", message)
if currentLevel <= LogLevelError {
log.Printf(format, v...)
message := fmt.Sprintf(format, v...)
emitLog("ERROR", message)
LogToFile("ERROR", message)
}
}

func Fatalf(format string, v ...interface{}) {
log.Printf(format, v...)
message := fmt.Sprintf(format, v...)
emitLog("Fatal", message)
LogToFile("Fatal", message)
emitLog("FATAL", message)
LogToFile("FATAL", message)
os.Exit(1) // Fatal logs usually terminate the program
}

func emitLog(level, message string) {
Expand Down

0 comments on commit 6c88176

Please sign in to comment.