-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteowebrtc_log.go
145 lines (123 loc) · 4 KB
/
teowebrtc_log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2021-2024 Kirill Scherba <kirill@scherba.ru>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Webrtc log contains log definition used in Teonet webrts packages
package teowebrtc_log
import (
"fmt"
"io"
"log"
"os"
)
// Teolog
type teolog struct {
logFlags int
packages packagesMap
}
type packagesMap map[string]*packageData
type packageData struct {
show bool
prefix string
log *log.Logger
}
// Teonet webrtc modules name
const (
Package_main = "main"
Package_teowebrtc_log = "teowebrtc_log"
Package_teowebrtc_server = "teowebrtc_server"
Package_teowebrtc_client = "teowebrtc_client"
Package_teowebrtc_signal = "teowebrtc_signal"
Package_teowebrtc_signal_client = "teowebrtc_signal_client"
)
var logt *log.Logger
// Initialize Teonet Logger and set default parameters
var teologPtr *teolog = func() (t *teolog) {
t = &teolog{
// Logger flags
logFlags: log.LstdFlags | log.Lmicroseconds | log.Lmsgprefix,
// Application packages
packages: packagesMap{
Package_main: &packageData{show: true, prefix: "[MAIN ] "},
Package_teowebrtc_log: &packageData{show: true, prefix: "[TEOLOG] "},
Package_teowebrtc_server: &packageData{show: true, prefix: "[WEBRTC] "},
Package_teowebrtc_client: &packageData{show: true, prefix: "[WEBCLI] "},
Package_teowebrtc_signal: &packageData{show: true, prefix: "[SIGNAL] "},
Package_teowebrtc_signal_client: &packageData{show: true, prefix: "[SIGCLI] "},
},
}
for packageName, pac := range t.packages {
pac.log, _ = t.getLog(packageName)
}
logt = t.packages[Package_teowebrtc_log].log
logt.Println("teolog created")
return
}()
// getLog returns teowebrtc logger depend of package name
//
// The method creates new logger and sets its parameters:
// - Output: os.Stdout if the package is visible, io.Discard if the package is not visible
// - Prefix: the prefix of the log messages
// - Flags: the log flags
func (teolog *teolog) getLog(packageName string) (logt *log.Logger, err error) {
pac, ok := teolog.packages[packageName]
if !ok {
err = fmt.Errorf("wrong package %s", packageName)
}
// Log output
var out io.Writer = os.Stdout
if !pac.show {
out = io.Discard
}
// Create new loger
logt = log.New(out, pac.prefix, teolog.logFlags)
pac.log = logt
return
}
// GetLog returns teowebrtc logger depend of package name
func GetLog(packageName string) (logger *log.Logger) {
logt.Printf("get log %s", packageName)
pac, ok := teologPtr.packages[packageName]
if !ok {
logt.Printf("error: wrong package %s", packageName)
return
}
logger = pac.log
return
}
// SetVisibility sets the visibility of package log
//
// SetVisibility sets the visibility of the log messages generated by the
// specified package. If the visibility is set to true, the log messages will
// be written to the standard output. If the visibility is set to false, the
// log messages will be discarded.
//
// The visibility is set to false by default.
//
// The visibility can be set for any package, but it is mainly used to control
// the visibility of the log messages generated by the main package and the
// child packages.
func SetVisibility(packageName string, show bool) (err error) {
pac, ok := teologPtr.packages[packageName]
if !ok {
err = fmt.Errorf("wrong package %s", packageName)
return
}
pac.show = show
var out io.Writer = os.Stdout
if !pac.show {
out = io.Discard
}
pac.log.SetOutput(out)
return
}
// SetMainPackagePrefix sets main package prefix
//
// The prefix is a string that will be used as a prefix for all log messages
// generated by the main package. The prefix should be no longer than 6
// characters.
//
// The prefix is of the form "[<prefix>] " and is used to make it easier to
// identify which package is generating the log messages.
func SetMainPackagePrefix(prefix string) {
teologPtr.packages[Package_main].prefix = fmt.Sprintf("[%6s] ", prefix[:6])
}