mirror of
https://github.com/onyx-and-iris/vbantxt.git
synced 2025-04-12 17:23:48 +01:00
log-level is now a str flag
add some validation logic update Logging section in README bps now defaults to 256000 (same as the vban-text-client) see: https://github.com/vburel2018/VBAN-Text-Client
This commit is contained in:
parent
b9d0578507
commit
b070fb615e
20
README.md
20
README.md
@ -116,8 +116,22 @@ vbantxt -s=streamname "Point(ASIO128.IN[2],ASIO128.OUT[1]).dBGain = -8"
|
|||||||
|
|
||||||
## `Logging`
|
## `Logging`
|
||||||
|
|
||||||
Log level may be set by passing the `-l` flag with a number from 0 up to 6 where
|
The `-log-level` flag allows you to control the verbosity of the application's logging output.
|
||||||
|
|
||||||
0 = Panic, 1 = Fatal, 2 = Error, 3 = Warning, 4 = Info, 5 = Debug, 6 = Trace
|
Acceptable values for this flag are:
|
||||||
|
|
||||||
Log level defaults to Warning level.
|
- `trace`
|
||||||
|
- `debug`
|
||||||
|
- `info`
|
||||||
|
- `warn`
|
||||||
|
- `error`
|
||||||
|
- `fatal`
|
||||||
|
- `panic`
|
||||||
|
|
||||||
|
For example, to set the log level to `debug`, you can use:
|
||||||
|
|
||||||
|
```
|
||||||
|
vbantxt -s=streamname -log-level=debug "bus[0].eq.on=1 bus[1].gain=-12.8"
|
||||||
|
```
|
||||||
|
|
||||||
|
The default log level is `warn` if the flag is not specified.
|
||||||
|
@ -5,13 +5,23 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/onyx-and-iris/vbantxt"
|
"github.com/onyx-and-iris/vbantxt"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type opts struct {
|
||||||
|
host string
|
||||||
|
port int
|
||||||
|
streamname string
|
||||||
|
bps int
|
||||||
|
channel int
|
||||||
|
ratelimit int
|
||||||
|
configPath string
|
||||||
|
loglevel string
|
||||||
|
}
|
||||||
|
|
||||||
func exit(err error) {
|
func exit(err error) {
|
||||||
_, _ = fmt.Fprintf(os.Stderr, "Error: %s", err)
|
_, _ = fmt.Fprintf(os.Stderr, "Error: %s", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@ -22,7 +32,7 @@ func main() {
|
|||||||
host string
|
host string
|
||||||
port int
|
port int
|
||||||
streamname string
|
streamname string
|
||||||
loglevel int
|
loglevel string
|
||||||
configPath string
|
configPath string
|
||||||
bps int
|
bps int
|
||||||
channel int
|
channel int
|
||||||
@ -36,8 +46,8 @@ func main() {
|
|||||||
flag.StringVar(&streamname, "streamname", "Command1", "stream name for text requests")
|
flag.StringVar(&streamname, "streamname", "Command1", "stream name for text requests")
|
||||||
flag.StringVar(&streamname, "s", "Command1", "stream name for text requests (shorthand)")
|
flag.StringVar(&streamname, "s", "Command1", "stream name for text requests (shorthand)")
|
||||||
|
|
||||||
flag.IntVar(&bps, "bps", 0, "vban bps")
|
flag.IntVar(&bps, "bps", 256000, "vban bps")
|
||||||
flag.IntVar(&bps, "b", 0, "vban bps (shorthand)")
|
flag.IntVar(&bps, "b", 256000, "vban bps (shorthand)")
|
||||||
flag.IntVar(&channel, "channel", 0, "vban channel")
|
flag.IntVar(&channel, "channel", 0, "vban channel")
|
||||||
flag.IntVar(&channel, "c", 0, "vban channel (shorthand)")
|
flag.IntVar(&channel, "c", 0, "vban channel (shorthand)")
|
||||||
flag.IntVar(&ratelimit, "ratelimit", 20, "request ratelimit in milliseconds")
|
flag.IntVar(&ratelimit, "ratelimit", 20, "request ratelimit in milliseconds")
|
||||||
@ -50,29 +60,60 @@ func main() {
|
|||||||
defaultConfigPath := filepath.Join(configDir, "vbantxt", "config.toml")
|
defaultConfigPath := filepath.Join(configDir, "vbantxt", "config.toml")
|
||||||
flag.StringVar(&configPath, "config", defaultConfigPath, "config path")
|
flag.StringVar(&configPath, "config", defaultConfigPath, "config path")
|
||||||
flag.StringVar(&configPath, "C", defaultConfigPath, "config path (shorthand)")
|
flag.StringVar(&configPath, "C", defaultConfigPath, "config path (shorthand)")
|
||||||
flag.IntVar(&loglevel, "loglevel", int(log.WarnLevel), "log level")
|
flag.StringVar(&loglevel, "log-level", "warn", "log level")
|
||||||
flag.IntVar(&loglevel, "l", int(log.WarnLevel), "log level (shorthand)")
|
flag.StringVar(&loglevel, "l", "warn", "log level (shorthand)")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if slices.Contains(log.AllLevels, log.Level(loglevel)) {
|
switch loglevel {
|
||||||
log.SetLevel(log.Level(loglevel))
|
case "trace":
|
||||||
|
log.SetLevel(log.TraceLevel)
|
||||||
|
case "debug":
|
||||||
|
log.SetLevel(log.DebugLevel)
|
||||||
|
case "info":
|
||||||
|
log.SetLevel(log.InfoLevel)
|
||||||
|
case "warn":
|
||||||
|
log.SetLevel(log.WarnLevel)
|
||||||
|
case "error":
|
||||||
|
log.SetLevel(log.ErrorLevel)
|
||||||
|
case "fatal":
|
||||||
|
log.SetLevel(log.FatalLevel)
|
||||||
|
case "panic":
|
||||||
|
log.SetLevel(log.PanicLevel)
|
||||||
|
default:
|
||||||
|
exit(fmt.Errorf("invalid log level: %s", loglevel))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
o := opts{
|
||||||
|
host: host,
|
||||||
|
port: port,
|
||||||
|
streamname: streamname,
|
||||||
|
bps: bps,
|
||||||
|
channel: channel,
|
||||||
|
ratelimit: ratelimit,
|
||||||
|
configPath: configPath,
|
||||||
|
loglevel: loglevel,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load the config only if the host, port, and streamname flags are not provided.
|
||||||
|
// This allows the user to override the config values with command line flags.
|
||||||
if !flagsPassed([]string{"host", "h", "port", "p", "streamname", "s"}) {
|
if !flagsPassed([]string{"host", "h", "port", "p", "streamname", "s"}) {
|
||||||
config, err := loadConfig(configPath)
|
config, err := loadConfig(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exit(err)
|
exit(err)
|
||||||
}
|
}
|
||||||
host = config.Host
|
|
||||||
port = config.Port
|
|
||||||
streamname = config.Streamname
|
|
||||||
}
|
|
||||||
|
|
||||||
client, err := createClient(host, port, streamname, bps, channel, ratelimit)
|
o.host = config.Host
|
||||||
|
o.port = config.Port
|
||||||
|
o.streamname = config.Streamname
|
||||||
|
}
|
||||||
|
log.Debugf("opts: %+v", o)
|
||||||
|
|
||||||
|
client, closer, err := createClient(o)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exit(err)
|
exit(err)
|
||||||
}
|
}
|
||||||
defer client.Close()
|
defer closer()
|
||||||
|
|
||||||
for _, arg := range flag.Args() {
|
for _, arg := range flag.Args() {
|
||||||
err := client.Send(arg)
|
err := client.Send(arg)
|
||||||
@ -82,16 +123,24 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createClient(host string, port int, streamname string, bps, channel, ratelimit int) (*vbantxt.VbanTxt, error) {
|
// createClient creates a new vban client with the provided options.
|
||||||
|
func createClient(o opts) (*vbantxt.VbanTxt, func(), error) {
|
||||||
client, err := vbantxt.New(
|
client, err := vbantxt.New(
|
||||||
host,
|
o.host,
|
||||||
port,
|
o.port,
|
||||||
streamname,
|
o.streamname,
|
||||||
vbantxt.WithBPSOpt(bps),
|
vbantxt.WithBPSOpt(o.bps),
|
||||||
vbantxt.WithChannel(channel),
|
vbantxt.WithChannel(o.channel),
|
||||||
vbantxt.WithRateLimit(time.Duration(ratelimit)*time.Millisecond))
|
vbantxt.WithRateLimit(time.Duration(o.ratelimit)*time.Millisecond))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
return client, err
|
|
||||||
|
closer := func() {
|
||||||
|
if err := client.Close(); err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return client, closer, err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user