summaryrefslogtreecommitdiff
path: root/bilibili
diff options
context:
space:
mode:
authorKeuin <[email protected]>2022-09-10 18:28:19 +0800
committerKeuin <[email protected]>2022-09-10 18:28:19 +0800
commit73b2b41e777614d7c372b9d92a90b5fbb68d05c0 (patch)
tree39bf028dbe68347444dc25abdfd29d1cad7d2989 /bilibili
parent5664705557ad5e4a3bd1234b12e4054a1a9579c1 (diff)
Replace all logger usages to custom logging.Logger.
Diffstat (limited to 'bilibili')
-rw-r--r--bilibili/client.go36
-rw-r--r--bilibili/request.go12
-rw-r--r--bilibili/request_test.go4
-rw-r--r--bilibili/streaming.go10
4 files changed, 28 insertions, 34 deletions
diff --git a/bilibili/client.go b/bilibili/client.go
index 8097115..f72432a 100644
--- a/bilibili/client.go
+++ b/bilibili/client.go
@@ -5,11 +5,10 @@ This struct is a facade of all necessary Bilibili HTTP API wrappers.
package bilibili
import (
+ "bilibili-livestream-archiver/logging"
"context"
- "log"
"net"
"net/http"
- "os"
)
const (
@@ -21,19 +20,12 @@ const (
type Bilibili struct {
userAgent string
http *http.Client
- loggerCommon
- ctx context.Context
- netTypes []IpNetType
+ ctx context.Context
+ netTypes []IpNetType
+ logger logging.Logger
}
-func NewBilibiliWithContext(ctx context.Context, netTypes []IpNetType) Bilibili {
- logger := loggerCommon{
- debug: log.New(os.Stderr, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile),
- info: log.New(os.Stderr, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile),
- warn: log.New(os.Stderr, "WARNING: ", log.Ldate|log.Ltime|log.Lshortfile),
- error: log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile),
- }
-
+func NewBilibiliWithContext(ctx context.Context, netTypes []IpNetType, logger logging.Logger) Bilibili {
var nets []IpNetType
nets = append(nets, netTypes...)
if len(nets) == 0 {
@@ -47,19 +39,19 @@ func NewBilibiliWithContext(ctx context.Context, netTypes []IpNetType) Bilibili
transport.DialContext, _ = np.NextNetworkType(dialer)
return Bilibili{
- loggerCommon: logger,
- userAgent: kUserAgent,
- http: http.DefaultClient,
- ctx: ctx,
- netTypes: nets,
+ logger: logger,
+ userAgent: kUserAgent,
+ http: http.DefaultClient,
+ ctx: ctx,
+ netTypes: nets,
}
}
-func NewBilibiliWithNetType(netTypes []IpNetType) Bilibili {
+func NewBilibiliWithNetType(netTypes []IpNetType, logger logging.Logger) Bilibili {
ctx := context.Background()
- return NewBilibiliWithContext(ctx, netTypes)
+ return NewBilibiliWithContext(ctx, netTypes, logger)
}
-func NewBilibili() Bilibili {
- return NewBilibiliWithNetType(nil)
+func NewBilibili(logger logging.Logger) Bilibili {
+ return NewBilibiliWithNetType(nil, logger)
}
diff --git a/bilibili/request.go b/bilibili/request.go
index f9e2088..7de35d3 100644
--- a/bilibili/request.go
+++ b/bilibili/request.go
@@ -18,7 +18,7 @@ func (b Bilibili) newRequest(
) (req *http.Request, err error) {
req, err = http.NewRequestWithContext(b.ctx, method, url, body)
if err != nil {
- b.error.Printf("Cannot create HTTP request instance: %v. Method: %v, URL: %v", err, method, url)
+ b.logger.Error("Cannot create HTTP request instance: %v. Method: %v, URL: %v", err, method, url)
return
}
req.Header.Set("User-Agent", b.userAgent)
@@ -48,24 +48,24 @@ func callGet[T BaseResponse[V], V any](b Bilibili, url string) (resp T, err erro
err = validateHttpStatus(r)
if err != nil {
- b.error.Printf("%v", err)
+ b.logger.Error("%v", err)
return
}
data, err := io.ReadAll(r.Body)
if err != nil {
- b.error.Printf("Error when reading HTTP response on API %v: %v", url, err)
+ b.logger.Error("Error when reading HTTP response on API %v: %v", url, err)
return
}
err = json.Unmarshal(data, &resp)
if err != nil {
- b.error.Printf("Invalid JSON body of HTTP response on API %v: %v. Text: \"%v\"",
+ b.logger.Error("Invalid JSON body of HTTP response on API %v: %v. Text: \"%v\"",
url, err, string(data))
return
}
- b.debug.Printf("HTTP %v, len: %v bytes, url: %v", r.StatusCode, len(data), url)
+ b.logger.Debug("HTTP %v, len: %v bytes, url: %v", r.StatusCode, len(data), url)
return
}
@@ -84,7 +84,7 @@ func (b Bilibili) Do(req *http.Request) (resp *http.Response, err error) {
isAddrErr := common.IsErrorOfType(err, &net.AddrError{})
if err == nil || !isOpErr || !isAddrErr {
// return the first success request
- b.loggerCommon.info.Printf("Request success with network %v.", typeName)
+ b.logger.Info("Request success with network %v.", typeName)
return
}
}
diff --git a/bilibili/request_test.go b/bilibili/request_test.go
index 68e5c7a..58b7127 100644
--- a/bilibili/request_test.go
+++ b/bilibili/request_test.go
@@ -1,12 +1,14 @@
package bilibili
import (
+ "bilibili-livestream-archiver/logging"
+ "log"
"testing"
)
func Test_callGet(t *testing.T) {
// an always-fail request should not panic
- bi := NewBilibili()
+ bi := NewBilibili(logging.NewWrappedLogger(log.Default(), "main"))
_, err := callGet[BaseResponse[struct{}]](bi, "https://256.256.256.256")
if err == nil {
t.Fatalf("the artificial request should fail, but it haven't")
diff --git a/bilibili/streaming.go b/bilibili/streaming.go
index 9a26c2d..7ad852c 100644
--- a/bilibili/streaming.go
+++ b/bilibili/streaming.go
@@ -27,7 +27,7 @@ func (b Bilibili) CopyLiveStream(
r, err := b.newGet(url)
if err != nil {
- b.error.Printf("Cannot create HTTP GET instance on %v: %v", url, err)
+ b.logger.Error("Cannot create HTTP GET instance on %v: %v", url, err)
return err
}
@@ -36,7 +36,7 @@ func (b Bilibili) CopyLiveStream(
resp, err := b.Do(r)
if err != nil {
- b.error.Printf("Cannot make HTTP GET request on %v: %v\n", url, err)
+ b.logger.Error("Cannot make HTTP GET request on %v: %v\n", url, err)
return
}
@@ -56,13 +56,13 @@ func (b Bilibili) CopyLiveStream(
n, err := common.CopyToFileWithBuffer(ctx, out, resp.Body, buffer, readChunkSize, false)
if err != nil && !errors.Is(err, context.Canceled) {
- b.error.Printf("Stream copying was interrupted unexpectedly: %v", err)
+ b.logger.Error("Stream copying was interrupted unexpectedly: %v", err)
}
if err == nil {
- b.info.Printf("The live is ended. (room %v)", roomId)
+ b.logger.Info("The live is ended. (room %v)", roomId)
}
- b.info.Printf("Total downloaded: %v", common.PrettyBytes(uint64(n)))
+ b.logger.Info("Total downloaded: %v", common.PrettyBytes(uint64(n)))
return err
}