Skip to content

Commit

Permalink
[opt] 支持pkg层面控制日志 #62
Browse files Browse the repository at this point in the history
  • Loading branch information
q191201771 committed Feb 9, 2022
1 parent feb1be8 commit 0f21334
Show file tree
Hide file tree
Showing 86 changed files with 636 additions and 567 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ image:

.PHONY: clean
clean:
rm -rf ./bin ./release ./logs ./lal_record
rm -rf ./bin ./release ./logs ./lal_record ./pkg/base/logs ./pkg/base/lal_record ./pkg/httpts/logs ./pkg/httpts/lal_record
rm -rf ./pkg/mpegts/logs ./pkg/mpegts/lal_record ./pkg/remux/logs ./pkg/remux/lal_record ./pkg/rtprtcp/logs ./pkg/rtprtcp/lal_record
rm -rf ./pkg/rtsp/logs ./pkg/rtsp/lal_record ./pkg/sdp/logs ./pkg/sdp/lal_record

.PHONY: all
all: build test
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/q191201771/lal

go 1.14

require github.com/q191201771/naza v0.29.0
require github.com/q191201771/naza v0.30.0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/q191201771/naza v0.29.0 h1:VvvJfYVkmQjszOXqhYyf2gGOCPl69woLCSWxNF0T//I=
github.com/q191201771/naza v0.29.0/go.mod h1:n+dpJjQSh90PxBwxBNuifOwQttywvSIN5TkWSSYCeBk=
github.com/q191201771/naza v0.30.0 h1:tfy1O0QRl3O80mH8PSAd2FhpZ5eL7coQtCF0HzjEO4Y=
github.com/q191201771/naza v0.30.0/go.mod h1:n+dpJjQSh90PxBwxBNuifOwQttywvSIN5TkWSSYCeBk=
4 changes: 1 addition & 3 deletions pkg/aac/aac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (

"github.com/q191201771/lal/pkg/aac"

"github.com/q191201771/naza/pkg/nazalog"

"github.com/q191201771/naza/pkg/assert"
)

Expand Down Expand Up @@ -90,5 +88,5 @@ func TestMakeAudioDataSeqHeader(t *testing.T) {
func TestSequenceHeaderContext(t *testing.T) {
var shCtx aac.SequenceHeaderContext
shCtx.Unpack(goldenSh)
nazalog.Debugf("%+v", shCtx)
aac.Log.Debugf("%+v", shCtx)
}
13 changes: 13 additions & 0 deletions pkg/aac/var.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2022, Chef. All rights reserved.
// https://github.com/q191201771/lal
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)

package aac

import "github.com/q191201771/naza/pkg/nazalog"

var Log = nazalog.GetGlobalLogger()
10 changes: 4 additions & 6 deletions pkg/avc/avc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ package avc
import (
"io"

"github.com/q191201771/naza/pkg/nazabytes"
"github.com/q191201771/naza/pkg/nazalog"

"github.com/q191201771/lal/pkg/base"
"github.com/q191201771/naza/pkg/nazabytes"

"github.com/q191201771/naza/pkg/nazaerrors"

Expand All @@ -36,7 +34,7 @@ var (
NaluStartCode3 = []byte{0x0, 0x0, 0x1}
NaluStartCode4 = []byte{0x0, 0x0, 0x0, 0x1}

// aud nalu
// AudNalu aud nalu
AudNalu = []byte{0x00, 0x00, 0x00, 0x01, 0x09, 0xf0}
)

Expand Down Expand Up @@ -498,7 +496,7 @@ func IterateNaluAvcc(nals []byte, handler func(nal []byte)) error {

// length为0的直接过滤掉
if length == 0 {
nazalog.Warnf("avcc nalu length equal 0. nals=%s", nazabytes.Prefix(nals, 128))
Log.Warnf("avcc nalu length equal 0. nals=%s", nazabytes.Prefix(nals, 128))
continue
}
handler(nals[pos:epos])
Expand All @@ -507,7 +505,7 @@ func IterateNaluAvcc(nals []byte, handler func(nal []byte)) error {
// 最后一个

if length == 0 {
nazalog.Warnf("avcc nalu length equal 0. nals=%s", nazabytes.Prefix(nals, 128))
Log.Warnf("avcc nalu length equal 0. nals=%s", nazabytes.Prefix(nals, 128))
continue
}
handler(nals[pos:epos])
Expand Down
8 changes: 3 additions & 5 deletions pkg/avc/avc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"github.com/q191201771/naza/pkg/nazabits"
"github.com/q191201771/naza/pkg/nazaerrors"

"github.com/q191201771/naza/pkg/nazalog"

"github.com/q191201771/lal/pkg/avc"

"github.com/q191201771/naza/pkg/assert"
Expand All @@ -36,7 +34,7 @@ func TestParseNaluType(t *testing.T) {
assert.Equal(t, out, actual)

b := avc.ParseNaluTypeReadable(in)
nazalog.Debug(b)
avc.Log.Debug(b)
}
}

Expand All @@ -60,7 +58,7 @@ func TestParseSliceType(t *testing.T) {

b, err := avc.ParseSliceTypeReadable(item.in)
assert.Equal(t, nil, err)
nazalog.Debug(b)
avc.Log.Debug(b)
}
}

Expand Down Expand Up @@ -138,7 +136,7 @@ func TestParseSps(t *testing.T) {
err = avc.ParseSps(nil, &ctx)
assert.Equal(t, true, nazaerrors.Is(err, nazabits.ErrNazaBits))
assert.IsNotNil(t, err)
nazalog.Debugf("error expected not nil, actual=%+v", err)
avc.Log.Debugf("error expected not nil, actual=%+v", err)

err = avc.ParseSps(goldenSps2, &ctx)
assert.Equal(t, nil, err)
Expand Down
9 changes: 4 additions & 5 deletions pkg/avc/beta.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ import (
"github.com/q191201771/naza/pkg/bele"
"github.com/q191201771/naza/pkg/nazabits"
"github.com/q191201771/naza/pkg/nazabytes"
"github.com/q191201771/naza/pkg/nazalog"
)

func ParseSps(payload []byte, ctx *Context) error {
br := nazabits.NewBitReader(payload)
var sps Sps
if err := parseSpsBasic(&br, &sps); err != nil {
nazalog.Errorf("parseSpsBasic failed. err=%+v, payload=%s", err, hex.Dump(nazabytes.Prefix(payload, 128)))
Log.Errorf("parseSpsBasic failed. err=%+v, payload=%s", err, hex.Dump(nazabytes.Prefix(payload, 128)))
return err
}
ctx.Profile = sps.ProfileIdc
Expand All @@ -37,9 +36,9 @@ func ParseSps(payload []byte, ctx *Context) error {

if err := parseSpsGamma(&br, &sps); err != nil {
// 注意,这里不将错误返回给上层,因为可能是Beta自身解析的问题
nazalog.Errorf("parseSpsGamma failed. err=%+v, payload=%s", err, hex.Dump(nazabytes.Prefix(payload, 128)))
Log.Errorf("parseSpsGamma failed. err=%+v, payload=%s", err, hex.Dump(nazabytes.Prefix(payload, 128)))
}
nazalog.Debugf("sps=%+v", sps)
Log.Debugf("sps=%+v", sps)

ctx.Width = (sps.PicWidthInMbsMinusOne+1)*16 - (sps.FrameCropLeftOffset+sps.FrameCropRightOffset)*2
ctx.Height = (2-uint32(sps.FrameMbsOnlyFlag))*(sps.PicHeightInMapUnitsMinusOne+1)*16 - (sps.FrameCropTopOffset+sps.FrameCropBottomOffset)*2
Expand Down Expand Up @@ -94,7 +93,7 @@ func TryParseSeqHeader(payload []byte) error {
b, err = br.ReadBytes(2)
dcr.PpsLength = bele.BeUint16(b)

nazalog.Debugf("%+v", dcr)
Log.Debugf("%+v", dcr)

// 5 + 5 + 1 + 2
var ctx Context
Expand Down
13 changes: 13 additions & 0 deletions pkg/avc/var.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2022, Chef. All rights reserved.
// https://github.com/q191201771/lal
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)

package avc

import "github.com/q191201771/naza/pkg/nazalog"

var Log = nazalog.GetGlobalLogger()
15 changes: 7 additions & 8 deletions pkg/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"time"

"github.com/q191201771/naza/pkg/bininfo"
"github.com/q191201771/naza/pkg/nazalog"
)

// base包提供被其他多个package依赖的基础内容,自身不依赖任何package
Expand All @@ -33,13 +32,13 @@ func ReadableNowTime() string {

func LogoutStartInfo() {
dir, _ := os.Getwd()
nazalog.Infof(" start: %s", startTime)
nazalog.Infof(" wd: %s", dir)
nazalog.Infof(" args: %s", strings.Join(os.Args, " "))
nazalog.Infof(" bininfo: %s", bininfo.StringifySingleLine())
nazalog.Infof(" version: %s", LalFullInfo)
nazalog.Infof(" github: %s", LalGithubSite)
nazalog.Infof(" doc: %s", LalDocSite)
Log.Infof(" start: %s", startTime)
Log.Infof(" wd: %s", dir)
Log.Infof(" args: %s", strings.Join(os.Args, " "))
Log.Infof(" bininfo: %s", bininfo.StringifySingleLine())
Log.Infof(" version: %s", LalFullInfo)
Log.Infof(" github: %s", LalGithubSite)
Log.Infof(" doc: %s", LalDocSite)
}

func init() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/base/http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ func TestHttpServerManager(t *testing.T) {
//var err error
//
//var fnFlv = func(writer http.ResponseWriter, request *http.Request) {
// nazalog.Debugf("> fnFlv")
// Log.Debugf("> fnFlv")
//}
//
//var fnTs = func(writer http.ResponseWriter, request *http.Request) {
// nazalog.Debugf("> fnTs")
// Log.Debugf("> fnTs")
//}
//
//sm := NewHttpServerManager()
Expand Down
2 changes: 1 addition & 1 deletion pkg/base/http_sub_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (session *HttpSubSession) StreamName() string {
case ProtocolHttpts:
suffix = ".ts"
default:
Logger.Warnf("[%s] acquire stream name but protocol unknown.", session.Uk)
Log.Warnf("[%s] acquire stream name but protocol unknown.", session.Uk)
}
return strings.TrimSuffix(session.UrlCtx.LastItemOfPath, suffix)
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/base/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

type LogDump struct {
log nazalog.Logger
debugMaxNum int

debugCount int
Expand All @@ -24,14 +25,15 @@ type LogDump struct {
//
// @param debugMaxNum: 日志最小级别为debug时,使用debug打印日志次数的阈值
//
func NewLogDump(debugMaxNum int) LogDump {
func NewLogDump(log nazalog.Logger, debugMaxNum int) LogDump {
return LogDump{
log: log,
debugMaxNum: debugMaxNum,
}
}

func (ld *LogDump) ShouldDump() bool {
switch nazalog.GetOption().Level {
switch ld.log.GetOption().Level {
case nazalog.LevelTrace:
return true
case nazalog.LevelDebug:
Expand All @@ -52,5 +54,5 @@ func (ld *LogDump) ShouldDump() bool {
// 这个hex.Dump调用
//
func (ld *LogDump) Outf(format string, v ...interface{}) {
nazalog.Out(nazalog.GetOption().Level, 3, fmt.Sprintf(format, v...))
ld.log.Out(ld.log.GetOption().Level, 3, fmt.Sprintf(format, v...))
}
8 changes: 3 additions & 5 deletions pkg/base/merge_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ package base

import (
"net"

"github.com/q191201771/naza/pkg/nazalog"
)

// TODO(chef): feat 通过时间戳(目前是数据大小)来设定合并阈值
Expand Down Expand Up @@ -47,7 +45,7 @@ func NewMergeWriter(onWritev OnWritev, size int) *MergeWriter {
// 注意,函数调用结束后,`b`内存块会被内部持有
//
func (w *MergeWriter) Write(b []byte) {
nazalog.Debugf("[%p] MergeWriter::Write. len=%d", w, len(b))
Log.Debugf("[%p] MergeWriter::Write. len=%d", w, len(b))
w.bs = append(w.bs, b)
w.currSize += len(b)
if w.currSize >= w.size {
Expand All @@ -58,7 +56,7 @@ func (w *MergeWriter) Write(b []byte) {
// Flush 强制将内部缓冲的数据全部回调排空
//
func (w *MergeWriter) Flush() {
nazalog.Debugf("[%p] MergeWriter::Flush.", w)
Log.Debugf("[%p] MergeWriter::Flush.", w)
if w.currSize > 0 {
w.flush()
}
Expand All @@ -74,7 +72,7 @@ func (w *MergeWriter) flush() {
n += len(v)
ns = append(ns, len(v))
}
nazalog.Debugf("[%p] MergeWriter::flush. len=%d(%v)", w, n, ns)
Log.Debugf("[%p] MergeWriter::flush. len=%d(%v)", w, n, ns)
w.onWritev(w.bs)
w.currSize = 0
w.bs = nil
Expand Down
4 changes: 1 addition & 3 deletions pkg/base/signal_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"os"
"os/signal"
"syscall"

log "github.com/q191201771/naza/pkg/nazalog"
)

// RunSignalHandler 监听SIGUSR1和SIGUSR2信号并回调
Expand All @@ -26,6 +24,6 @@ func RunSignalHandler(cb func()) {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGUSR1, syscall.SIGUSR2)
s := <-c
log.Infof("recv signal. s=%+v", s)
Log.Infof("recv signal. s=%+v", s)
cb()
}
4 changes: 1 addition & 3 deletions pkg/base/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ package base

import "github.com/q191201771/naza/pkg/nazalog"

var (
Logger = nazalog.GetGlobalLogger()
)
var Log = nazalog.GetGlobalLogger()
2 changes: 1 addition & 1 deletion pkg/hevc/hevc.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func ParseVpsSpsPpsFromSeqHeaderWithoutMalloc(payload []byte) (vps, sps, pps []b
if payload[0] != 0x1c || payload[1] != 0x00 || payload[2] != 0 || payload[3] != 0 || payload[4] != 0 {
return nil, nil, nil, nazaerrors.Wrap(base.ErrHevc)
}
//nazalog.Debugf("%s", hex.Dump(payload))
//Log.Debugf("%s", hex.Dump(payload))

if len(payload) < 33 {
return nil, nil, nil, nazaerrors.Wrap(base.ErrHevc)
Expand Down
13 changes: 13 additions & 0 deletions pkg/hevc/var.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2022, Chef. All rights reserved.
// https://github.com/q191201771/lal
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)

package hevc

import "github.com/q191201771/naza/pkg/nazalog"

var Log = nazalog.GetGlobalLogger()
Loading

0 comments on commit 0f21334

Please sign in to comment.