mirror of
https://github.com/Monibuca/engine.git
synced 2025-10-19 23:16:27 +08:00
增加彩色日志输出功能
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
package engine
|
package engine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Monibuca/engine/util"
|
"github.com/Monibuca/engine/util"
|
||||||
|
. "github.com/logrusorgru/aurora"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -32,7 +32,6 @@ type PluginConfig struct {
|
|||||||
|
|
||||||
// InstallPlugin 安装插件
|
// InstallPlugin 安装插件
|
||||||
func InstallPlugin(opt *PluginConfig) {
|
func InstallPlugin(opt *PluginConfig) {
|
||||||
log.Printf("install plugin %s version: %s", opt.Name, opt.Version)
|
|
||||||
Plugins[opt.Name] = opt
|
Plugins[opt.Name] = opt
|
||||||
_, pluginFilePath, _, _ := runtime.Caller(1)
|
_, pluginFilePath, _, _ := runtime.Caller(1)
|
||||||
opt.Dir = filepath.Dir(pluginFilePath)
|
opt.Dir = filepath.Dir(pluginFilePath)
|
||||||
@@ -40,6 +39,10 @@ func InstallPlugin(opt *PluginConfig) {
|
|||||||
if util.Exist(ui) {
|
if util.Exist(ui) {
|
||||||
opt.UI = ui
|
opt.UI = ui
|
||||||
}
|
}
|
||||||
|
if parts := strings.Split(opt.Dir, "@"); len(parts) > 1 {
|
||||||
|
opt.Version = parts[len(parts)-1]
|
||||||
|
}
|
||||||
|
Print(Green("install plugin"), BrightCyan(opt.Name), BrightBlue(opt.Version))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenerConfig 带有监听地址端口的插件配置类型
|
// ListenerConfig 带有监听地址端口的插件配置类型
|
||||||
|
4
go.mod
4
go.mod
@@ -6,9 +6,11 @@ require (
|
|||||||
github.com/BurntSushi/toml v0.3.1
|
github.com/BurntSushi/toml v0.3.1
|
||||||
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
|
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
|
||||||
github.com/funny/slab v0.0.0-20180511031532-b1fad5e5d478
|
github.com/funny/slab v0.0.0-20180511031532-b1fad5e5d478
|
||||||
|
github.com/funny/utest v0.0.0-20161029064919-43870a374500 // indirect
|
||||||
github.com/go-ole/go-ole v1.2.4 // indirect
|
github.com/go-ole/go-ole v1.2.4 // indirect
|
||||||
|
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381
|
||||||
|
github.com/mattn/go-colorable v0.1.6
|
||||||
github.com/pkg/errors v0.9.1
|
github.com/pkg/errors v0.9.1
|
||||||
github.com/shirou/gopsutil v2.20.1+incompatible
|
github.com/shirou/gopsutil v2.20.1+incompatible
|
||||||
github.com/stretchr/testify v1.5.1 // indirect
|
github.com/stretchr/testify v1.5.1 // indirect
|
||||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect
|
|
||||||
)
|
)
|
||||||
|
11
go.sum
11
go.sum
@@ -8,14 +8,19 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
|
|||||||
github.com/elgs/gostrgen v0.0.0-20161222160715-9d61ae07eeae/go.mod h1:wruC5r2gHdr/JIUs5Rr1V45YtsAzKXZxAnn/5rPC97g=
|
github.com/elgs/gostrgen v0.0.0-20161222160715-9d61ae07eeae/go.mod h1:wruC5r2gHdr/JIUs5Rr1V45YtsAzKXZxAnn/5rPC97g=
|
||||||
github.com/funny/slab v0.0.0-20180511031532-b1fad5e5d478 h1:Db9StoJ6RZN3YttC0Pm0I4Y5izITRYch3RMbT59BYN0=
|
github.com/funny/slab v0.0.0-20180511031532-b1fad5e5d478 h1:Db9StoJ6RZN3YttC0Pm0I4Y5izITRYch3RMbT59BYN0=
|
||||||
github.com/funny/slab v0.0.0-20180511031532-b1fad5e5d478/go.mod h1:0j1+svBH8ABEIPdUP0AIg4qedsybnXGJBakCEw8cfoo=
|
github.com/funny/slab v0.0.0-20180511031532-b1fad5e5d478/go.mod h1:0j1+svBH8ABEIPdUP0AIg4qedsybnXGJBakCEw8cfoo=
|
||||||
|
github.com/funny/utest v0.0.0-20161029064919-43870a374500 h1:Z0r1CZnoIWFB/Uiwh1BU5FYmuFe6L5NPi6XWQEmsTRg=
|
||||||
github.com/funny/utest v0.0.0-20161029064919-43870a374500/go.mod h1:mUn39tBov9jKnTWV1RlOYoNzxdBFHiSzXWdY1FoNGGg=
|
github.com/funny/utest v0.0.0-20161029064919-43870a374500/go.mod h1:mUn39tBov9jKnTWV1RlOYoNzxdBFHiSzXWdY1FoNGGg=
|
||||||
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
|
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
|
||||||
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
|
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
|
||||||
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
|
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
|
||||||
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
|
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
|
||||||
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
|
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
|
||||||
github.com/langhuihui/monibuca v0.4.1 h1:hR5xiVtYJM272ChQUrKdNd+AQyY98SNxVZEx2WAuNmA=
|
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381 h1:bqDmpDG49ZRnB5PcgP0RXtQvnMSgIF14M7CBd2shtXs=
|
||||||
github.com/langhuihui/monibuca v0.4.1/go.mod h1:S4rqYUQ+bCB3WdwuXTJ92FqVRZz5Sh7zAXOJc94JqMI=
|
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
|
||||||
|
github.com/mattn/go-colorable v0.1.6 h1:6Su7aK7lXmJ/U79bYtBjLNaha4Fs1Rg9plHpcH+vvnE=
|
||||||
|
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||||
|
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||||
|
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
@@ -35,9 +40,11 @@ github.com/yakovlevdmv/gosoap v0.0.0-20180512142237-299a954b1c6d/go.mod h1:NhCpq
|
|||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/net v0.0.0-20200226051749-491c5fce7268/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
golang.org/x/net v0.0.0-20200226051749-491c5fce7268/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
|
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
|
||||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
74
logger.go
74
logger.go
@@ -1,35 +1,87 @@
|
|||||||
package engine
|
package engine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/logrusorgru/aurora"
|
||||||
|
"github.com/mattn/go-colorable"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LogWriter 多端写日志类
|
// LogWriter 多端写日志类
|
||||||
type LogWriter struct {
|
type LogWriter struct {
|
||||||
io.Writer
|
*MultiLogWriter
|
||||||
origin io.Writer
|
}
|
||||||
|
type MultiLogWriter struct {
|
||||||
|
sync.Map
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *LogWriter) Write(data []byte) (n int, err error) {
|
var logWriter = &LogWriter{new(MultiLogWriter)}
|
||||||
if n, err = w.Writer.Write(data); err != nil {
|
var multiLogger = log.New(logWriter.MultiLogWriter, "", log.LstdFlags)
|
||||||
go log.SetOutput(w.origin)
|
var colorLogger = log.New(colorable.NewColorableStdout(), "", log.LstdFlags)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
log.SetOutput(logWriter)
|
||||||
}
|
}
|
||||||
return w.origin.Write(data)
|
func (w *LogWriter) Write(data []byte) (n int, err error) {
|
||||||
|
os.Stdout.Write(data)
|
||||||
|
return w.MultiLogWriter.Write(data)
|
||||||
|
}
|
||||||
|
func (w *MultiLogWriter) Write(data []byte) (n int, err error) {
|
||||||
|
w.Range(func(k, v interface{}) bool {
|
||||||
|
n, err = k.(io.Writer).Write(data)
|
||||||
|
if err != nil {
|
||||||
|
w.Delete(k)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddWriter 添加日志输出端
|
// AddWriter 添加日志输出端
|
||||||
func AddWriter(wn io.Writer) {
|
func AddWriter(wn io.Writer) {
|
||||||
log.SetOutput(&LogWriter{
|
logWriter.Store(wn, wn)
|
||||||
Writer: wn,
|
|
||||||
origin: log.Writer(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MayBeError 优雅错误判断加日志辅助函数
|
// MayBeError 优雅错误判断加日志辅助函数
|
||||||
func MayBeError(info error) (hasError bool) {
|
func MayBeError(info error) (hasError bool) {
|
||||||
if hasError = info != nil; hasError {
|
if hasError = info != nil; hasError {
|
||||||
log.Print(info)
|
Print(aurora.Red(info))
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
func getNoColor(v ...interface{}) (noColor []interface{}) {
|
||||||
|
noColor = append(noColor, v...)
|
||||||
|
for i, value := range v {
|
||||||
|
if vv, ok := value.(aurora.Value); ok {
|
||||||
|
noColor[i] = vv.Value()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print 带颜色识别
|
||||||
|
func Print(v ...interface{}) {
|
||||||
|
noColor := getNoColor(v...)
|
||||||
|
colorLogger.Output(2, fmt.Sprint(v...))
|
||||||
|
multiLogger.Output(2, fmt.Sprint(noColor...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Printf calls Output to print to the standard logger.
|
||||||
|
// Arguments are handled in the manner of fmt.Printf.
|
||||||
|
func Printf(format string, v ...interface{}) {
|
||||||
|
noColor := getNoColor(v...)
|
||||||
|
colorLogger.Output(2, fmt.Sprintf(format, v...))
|
||||||
|
multiLogger.Output(2, fmt.Sprintf(format, noColor...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Println calls Output to print to the standard logger.
|
||||||
|
// Arguments are handled in the manner of fmt.Println.
|
||||||
|
func Println(v ...interface{}) {
|
||||||
|
noColor := getNoColor(v...)
|
||||||
|
colorLogger.Output(2, fmt.Sprintln(v...))
|
||||||
|
multiLogger.Output(2, fmt.Sprintln(noColor...))
|
||||||
|
}
|
||||||
|
21
main.go
21
main.go
@@ -6,22 +6,25 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"strings"
|
||||||
|
"time" // colorable
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
|
. "github.com/logrusorgru/aurora"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ConfigRaw 配置信息的原始数据
|
// ConfigRaw 配置信息的原始数据
|
||||||
ConfigRaw []byte
|
ConfigRaw []byte
|
||||||
// Version 引擎版本号
|
// Version 引擎版本号
|
||||||
Version = "1.1.3"
|
Version string
|
||||||
// EngineInfo 引擎信息
|
// EngineInfo 引擎信息
|
||||||
EngineInfo = &struct {
|
EngineInfo = &struct {
|
||||||
Version string
|
Version *string
|
||||||
StartTime time.Time //启动时间
|
StartTime time.Time //启动时间
|
||||||
}{Version, time.Now()}
|
}{&Version, time.Now()}
|
||||||
)
|
)
|
||||||
|
|
||||||
// Run 启动Monibuca引擎
|
// Run 启动Monibuca引擎
|
||||||
@@ -31,11 +34,15 @@ func Run(configFile string) (err error) {
|
|||||||
} else {
|
} else {
|
||||||
ioutil.WriteFile("shutdown.sh", []byte(fmt.Sprintf("kill -9 %d", os.Getpid())), 0777)
|
ioutil.WriteFile("shutdown.sh", []byte(fmt.Sprintf("kill -9 %d", os.Getpid())), 0777)
|
||||||
}
|
}
|
||||||
log.Printf("start monibuca version:%s", Version)
|
_, enginePath, _, _ := runtime.Caller(0)
|
||||||
|
if parts := strings.Split(filepath.Dir(enginePath), "@"); len(parts) > 1 {
|
||||||
|
Version = parts[len(parts)-1]
|
||||||
|
}
|
||||||
if ConfigRaw, err = ioutil.ReadFile(configFile); err != nil {
|
if ConfigRaw, err = ioutil.ReadFile(configFile); err != nil {
|
||||||
log.Printf("read config file error: %v", err)
|
Print(Red("read config file error:"), err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Print(Green("start monibuca"), BrightBlue(Version))
|
||||||
go Summary.StartSummary()
|
go Summary.StartSummary()
|
||||||
var cg map[string]interface{}
|
var cg map[string]interface{}
|
||||||
if _, err = toml.Decode(string(ConfigRaw), &cg); err == nil {
|
if _, err = toml.Decode(string(ConfigRaw), &cg); err == nil {
|
||||||
@@ -60,7 +67,7 @@ func Run(configFile string) (err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Printf("decode config file error: %v", err)
|
Print(Red("decode config file error:"), err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
5
room.go
5
room.go
@@ -7,6 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Monibuca/engine/avformat"
|
"github.com/Monibuca/engine/avformat"
|
||||||
|
. "github.com/logrusorgru/aurora"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -89,7 +90,7 @@ type ChangeRoomCmd struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Room) onClosed() {
|
func (r *Room) onClosed() {
|
||||||
log.Printf("room destoryed :%s", r.StreamPath)
|
Print(Yellow("room destoryed :"), BgBrightCyan(r.StreamPath))
|
||||||
AllRoom.Delete(r.StreamPath)
|
AllRoom.Delete(r.StreamPath)
|
||||||
OnRoomClosedHooks.Trigger(r)
|
OnRoomClosedHooks.Trigger(r)
|
||||||
if r.Publisher != nil {
|
if r.Publisher != nil {
|
||||||
@@ -118,7 +119,7 @@ func (r *Room) UnSubscribe(s *OutputStream) {
|
|||||||
|
|
||||||
// Run 房间运行,转发逻辑
|
// Run 房间运行,转发逻辑
|
||||||
func (r *Room) Run() {
|
func (r *Room) Run() {
|
||||||
log.Printf("room create :%s", r.StreamPath)
|
Print(Green("room create:"), BgBrightCyan(r.StreamPath))
|
||||||
defer r.onClosed()
|
defer r.onClosed()
|
||||||
update := time.NewTicker(time.Second)
|
update := time.NewTicker(time.Second)
|
||||||
defer update.Stop()
|
defer update.Stop()
|
||||||
|
Reference in New Issue
Block a user