diff options
author | Keuin <[email protected]> | 2022-09-10 18:28:19 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-09-10 18:28:19 +0800 |
commit | 73b2b41e777614d7c372b9d92a90b5fbb68d05c0 (patch) | |
tree | 39bf028dbe68347444dc25abdfd29d1cad7d2989 /recording | |
parent | 5664705557ad5e4a3bd1234b12e4054a1a9579c1 (diff) |
Replace all logger usages to custom logging.Logger.
Diffstat (limited to 'recording')
-rw-r--r-- | recording/runner.go | 27 | ||||
-rw-r--r-- | recording/watcher.go | 36 |
2 files changed, 30 insertions, 33 deletions
diff --git a/recording/runner.go b/recording/runner.go index 98d4432..4f3b1f5 100644 --- a/recording/runner.go +++ b/recording/runner.go @@ -12,7 +12,6 @@ import ( "encoding/json" "errors" "fmt" - "log" "os" "path" "time" @@ -57,7 +56,7 @@ func (t *RunningTask) runTaskWithAutoRestart() error { func tryRunTask(t *RunningTask) error { netTypes := t.Transport.AllowedNetworkTypes t.logger.Info("Network types: %v", netTypes) - bi := bilibili.NewBilibiliWithNetType(netTypes) + bi := bilibili.NewBilibiliWithNetType(netTypes, t.logger) t.logger.Info("Start task: room %v", t.RoomId) t.logger.Info("Getting notification server info...") @@ -135,8 +134,7 @@ func record( bi bilibili.Bilibili, task *RunningTask, ) (cancelled bool, err error) { - logger := log.Default() - logger.Printf("INFO: Getting room profile...") + task.logger.Info("Getting room profile...") profile, err := common.AutoRetry( ctx, @@ -145,7 +143,7 @@ func record( }, task.Transport.MaxRetryTimes, time.Duration(task.Transport.RetryIntervalSeconds)*time.Second, - logger, + &task.logger, ) if errors.Is(err, context.Canceled) { cancelled = true @@ -153,12 +151,12 @@ func record( } if err != nil { // still error, abort - logger.Printf("ERROR: Cannot get room information: %v. Stopping current task.", err) + task.logger.Error("Cannot get room information: %v. Stopping current task.", err) cancelled = true return } - logger.Printf("INFO: Getting stream url...") + task.logger.Info("Getting stream url...") urlInfo, err := common.AutoRetry( ctx, func() (bilibili.RoomUrlInfoResponse, error) { @@ -166,14 +164,14 @@ func record( }, task.Transport.MaxRetryTimes, time.Duration(task.Transport.RetryIntervalSeconds)*time.Second, - logger, + &task.logger, ) if errors.Is(err, context.Canceled) { cancelled = true return } if err != nil { - logger.Printf("ERROR: Cannot get streaming info: %v", err) + task.logger.Error("Cannot get streaming info: %v", err) cancelled = true return } @@ -182,7 +180,7 @@ func record( if err2 != nil { j = []byte("(not available)") } - logger.Printf("ERROR: No stream returned from API. Response: %v", string(j)) + task.logger.Error("No stream returned from API. Response: %v", string(j)) cancelled = true return } @@ -196,7 +194,7 @@ func record( filePath := path.Join(task.Download.SaveDirectory, fileName) file, err := os.OpenFile(filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) if err != nil { - logger.Printf("ERROR: Cannot open file for writing: %v", err) + task.logger.Error("Cannot open file for writing: %v", err) cancelled = true return } @@ -210,13 +208,13 @@ func record( writeBufferSize += kReadChunkSize - mod } writeBuffer := make([]byte, writeBufferSize) - logger.Printf("Write buffer size: %v byte", writeBufferSize) - logger.Printf("Recording live stream to file \"%v\"...", filePath) + task.logger.Info("Write buffer size: %v byte", writeBufferSize) + task.logger.Info("Recording live stream to file \"%v\"...", filePath) err = bi.CopyLiveStream(ctx, task.RoomId, streamSource, file, writeBuffer, kReadChunkSize) cancelled = err == nil || errors.Is(err, context.Canceled) if !cancelled { // real error happens - logger.Printf("Error when copying live stream: %v", err) + task.logger.Error("Error when copying live stream: %v", err) } return } @@ -248,6 +246,7 @@ func watcherRecoverableLoop( return resp.Data.LiveStatus.IsStreaming(), nil }, chWatcherEvent, + task.logger, ) // the context is cancelled, stop watching diff --git a/recording/watcher.go b/recording/watcher.go index afc7ab5..0e5d087 100644 --- a/recording/watcher.go +++ b/recording/watcher.go @@ -5,10 +5,10 @@ import ( "bilibili-livestream-archiver/danmaku" "bilibili-livestream-archiver/danmaku/dmmsg" "bilibili-livestream-archiver/danmaku/dmpkg" + "bilibili-livestream-archiver/logging" "context" "encoding/json" "fmt" - "log" "time" ) @@ -53,10 +53,8 @@ func watch( roomId common.RoomId, liveStatusChecker func() (bool, error), chEvent chan<- WatcherEvent, + logger logging.Logger, ) (error, ErrorReason) { - - logger := log.Default() - var err error dm := danmaku.NewDanmakuClient() @@ -70,7 +68,7 @@ func watch( defer func() { _ = dm.Disconnect() }() // the danmaku server requires an auth token and room id when connected - logger.Println("ws connected. Authenticating...") + logger.Info("ws connected. Authenticating...") err = dm.Authenticate(roomId, authKey) if err != nil { return fmt.Errorf("auth failed: %w", err), ErrProtocol @@ -92,17 +90,17 @@ func watch( heartBeatTimer := time.NewTicker(kHeartBeatInterval) defer func() { heartBeatTimer.Stop() }() - logger.Println("Checking initial live status...") + logger.Info("Checking initial live status...") isLiving, err := liveStatusChecker() if err != nil { return fmt.Errorf("check initial live status failed: %w", err), ErrTransport } if isLiving { - logger.Println("The live is already started. Start recording immediately.") + logger.Info("The live is already started. Start recording immediately.") chEvent <- WatcherLiveStart } else { - logger.Println("The live is not started yet. Waiting...") + logger.Info("The live is not started yet. Waiting...") } for { @@ -132,7 +130,7 @@ func watch( var info liveInfo err := json.Unmarshal(msg.Body, &info) if err != nil { - logger.Printf("ERROR: invalid JSON: \"%v\", exchange: %v", string(msg.Body), msg) + logger.Error("Invalid JSON: \"%v\", exchange: %v", string(msg.Body), msg) return fmt.Errorf("decode server message body JSON failed: %w", err), ErrProtocol } switch info.Command { @@ -158,7 +156,7 @@ func watch( fallthrough case "HOT_RANK_CHANGED_V2": // useless message - logger.Printf("Ignore message: %v", info.Command) + logger.Info("Ignore message: %v", info.Command) case "WATCHED_CHANGE": // number of watched people changed obj, exists := info.Data["num"] @@ -167,39 +165,39 @@ func watch( } viewersNum, ok := obj.(float64) if !ok { - logger.Printf("Cannot parse watched people number: %v", obj) + logger.Error("Cannot parse watched people number: %v", obj) continue } - logger.Printf("The number of viewers (room: %v): %v", roomId, viewersNum) + logger.Info("The number of viewers (room: %v): %v", roomId, viewersNum) case "INTERACT_WORD": var raw dmmsg.RawInteractWordMessage err = json.Unmarshal(msg.Body, &raw) if err != nil { - logger.Printf("Cannot parse RawInteractWordMessage JSON: %v", err) + logger.Error("Cannot parse RawInteractWordMessage JSON: %v", err) continue } - logger.Printf("Interact word message: user: %v medal: %v", + logger.Info("Interact word message: user: %v medal: %v", raw.Data.UserName, raw.Data.FansMedal.Name) case "DANMU_MSG": var raw dmmsg.RawDanMuMessage err = json.Unmarshal(msg.Body, &raw) if err != nil { - logger.Printf("Cannot parse Dan Mu message as JSON: %v", err) + logger.Error("Cannot parse Dan Mu message as JSON: %v", err) continue } dmm, err := dmmsg.ParseDanmakuMessage(raw) if err != nil { - logger.Printf("Cannot parse Dan Mu message JSON: %v", err) + logger.Error("Cannot parse Dan Mu message JSON: %v", err) continue } - logger.Printf("Dan Mu: %v", dmm.String()) + logger.Info("Dan Mu: %v", dmm.String()) default: - logger.Printf("Ignore unhandled server message %v %v %v", + logger.Info("Ignore unhandled server message %v %v %v", info.Command, msg.Operation, string(msg.Body)) } } default: - logger.Printf("Server message: %v", msg.String()) + logger.Info("Server message: %v", msg.String()) } } |