Don't log to a file anymore. Log to stdout if called with -log

Additionally refactor the CLI a bit more to make that possible
pull/128/head
lawl 3 years ago
parent 0ef8229e6f
commit 8b11b649ee

@ -8,24 +8,42 @@ import (
"github.com/lawl/pulseaudio" "github.com/lawl/pulseaudio"
) )
func doCLI(config *config, librnnoise string) { type CLIOpts struct {
var setcap bool doLog bool
var sinkName string setcap bool
var unload bool sinkName string
var loadInput bool unload bool
var loadOutput bool loadInput bool
var threshold int loadOutput bool
var list bool threshold int
list bool
flag.BoolVar(&setcap, "setcap", false, "for internal use only") }
flag.StringVar(&sinkName, "s", "", "Use the specified source/sink device ID")
flag.BoolVar(&loadInput, "i", false, "Load supressor for input. If no source device ID is specified the default pulse audio source is used.") func parseCLIOpts() CLIOpts {
flag.BoolVar(&loadOutput, "o", false, "Load supressor for output. If no source device ID is specified the default pulse audio source is used.") var opt CLIOpts
flag.BoolVar(&unload, "u", false, "Unload supressor") flag.BoolVar(&opt.doLog, "log", false, "Print debugging output to stdout")
flag.IntVar(&threshold, "t", -1, "Voice activation threshold") flag.BoolVar(&opt.setcap, "setcap", false, "for internal use only")
flag.BoolVar(&list, "l", false, "List available PulseAudio devices") flag.StringVar(&opt.sinkName, "s", "", "Use the specified source/sink device ID")
flag.BoolVar(&opt.loadInput, "i", false, "Load supressor for input. If no source device ID is specified the default pulse audio source is used.")
flag.BoolVar(&opt.loadOutput, "o", false, "Load supressor for output. If no source device ID is specified the default pulse audio source is used.")
flag.BoolVar(&opt.unload, "u", false, "Unload supressor")
flag.IntVar(&opt.threshold, "t", -1, "Voice activation threshold")
flag.BoolVar(&opt.list, "l", false, "List available PulseAudio devices")
flag.Parse() flag.Parse()
return opt
}
func doCLI(opt CLIOpts, config *config, librnnoise string) {
if opt.setcap {
err := makeBinarySetcapped()
if err != nil {
os.Exit(1)
}
os.Exit(0)
}
paClient, err := pulseaudio.NewClient() paClient, err := pulseaudio.NewClient()
if err != nil { if err != nil {
@ -40,15 +58,7 @@ func doCLI(config *config, librnnoise string) {
ctx.paClient = paClient ctx.paClient = paClient
if setcap { if opt.list {
err := makeBinarySetcapped()
if err != nil {
os.Exit(1)
}
os.Exit(0)
}
if list {
fmt.Println("Sources:") fmt.Println("Sources:")
sources := getSources(paClient) sources := getSources(paClient)
for i := range sources { for i := range sources {
@ -64,16 +74,16 @@ func doCLI(config *config, librnnoise string) {
os.Exit(0) os.Exit(0)
} }
if threshold > 0 { if opt.threshold > 0 {
if threshold > 95 { if opt.threshold > 95 {
fmt.Fprintf(os.Stderr, "Threshold of '%d' too high, setting to maximum of 95.\n", threshold) fmt.Fprintf(os.Stderr, "Threshold of '%d' too high, setting to maximum of 95.\n", opt.threshold)
ctx.config.Threshold = 95 ctx.config.Threshold = 95
} else { } else {
ctx.config.Threshold = threshold ctx.config.Threshold = opt.threshold
} }
} }
if unload { if opt.unload {
err := unloadSupressor(&ctx) err := unloadSupressor(&ctx)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error unloading PulseAudio Module: %+v\n", err) fmt.Fprintf(os.Stderr, "Error unloading PulseAudio Module: %+v\n", err)
@ -82,19 +92,19 @@ func doCLI(config *config, librnnoise string) {
os.Exit(0) os.Exit(0)
} }
if loadInput { if opt.loadInput {
sources := getSources(paClient) sources := getSources(paClient)
if sinkName == "" { if opt.sinkName == "" {
defaultSource, err := getDefaultSourceID(paClient) defaultSource, err := getDefaultSourceID(paClient)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "No source specified to load and failed to load default source: %+v\n", err) fmt.Fprintf(os.Stderr, "No source specified to load and failed to load default source: %+v\n", err)
os.Exit(1) os.Exit(1)
} }
sinkName = defaultSource opt.sinkName = defaultSource
} }
for i := range sources { for i := range sources {
if sources[i].ID == sinkName { if sources[i].ID == opt.sinkName {
sources[i].checked = true sources[i].checked = true
err := loadSupressor(&ctx, &sources[i], &device{}) err := loadSupressor(&ctx, &sources[i], &device{})
if err != nil { if err != nil {
@ -104,23 +114,23 @@ func doCLI(config *config, librnnoise string) {
os.Exit(0) os.Exit(0)
} }
} }
fmt.Fprintf(os.Stderr, "PulseAudio source not found: %s\n", sinkName) fmt.Fprintf(os.Stderr, "PulseAudio source not found: %s\n", opt.sinkName)
os.Exit(1) os.Exit(1)
} }
if loadOutput { if opt.loadOutput {
sinks := getSinks(paClient) sinks := getSinks(paClient)
if sinkName == "" { if opt.sinkName == "" {
defaultSink, err := getDefaultSinkID(paClient) defaultSink, err := getDefaultSinkID(paClient)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "No sink specified to load and failed to load default sink: %+v\n", err) fmt.Fprintf(os.Stderr, "No sink specified to load and failed to load default sink: %+v\n", err)
os.Exit(1) os.Exit(1)
} }
sinkName = defaultSink opt.sinkName = defaultSink
} }
for i := range sinks { for i := range sinks {
if sinks[i].ID == sinkName { if sinks[i].ID == opt.sinkName {
sinks[i].checked = true sinks[i].checked = true
err := loadSupressor(&ctx, &device{}, &sinks[i]) err := loadSupressor(&ctx, &device{}, &sinks[i])
if err != nil { if err != nil {
@ -130,7 +140,7 @@ func doCLI(config *config, librnnoise string) {
os.Exit(0) os.Exit(0)
} }
} }
fmt.Fprintf(os.Stderr, "PulseAudio sink not found: %s\n", sinkName) fmt.Fprintf(os.Stderr, "PulseAudio sink not found: %s\n", opt.sinkName)
os.Exit(1) os.Exit(1)
} }

@ -6,7 +6,6 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"path/filepath"
"strings" "strings"
"time" "time"
@ -44,16 +43,13 @@ type device struct {
const appName = "NoiseTorch" const appName = "NoiseTorch"
func main() { func main() {
opt := parseCLIOpts()
date := time.Now().Format("2006_01_02_15_04_05") if opt.doLog {
tmpdir := os.TempDir() log.SetOutput(os.Stdout)
f, err := os.OpenFile(filepath.Join(tmpdir, fmt.Sprintf("noisetorch-%s.log", date)), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) } else {
if err != nil { log.SetOutput(ioutil.Discard)
log.Fatalf("error opening file: %v\n", err)
} }
defer f.Close()
log.SetOutput(f)
log.Printf("Application starting. Version: %s\n", version) log.Printf("Application starting. Version: %s\n", version)
log.Printf("CAP_SYS_RESOURCE: %t\n", hasCapSysResource(getCurrentCaps())) log.Printf("CAP_SYS_RESOURCE: %t\n", hasCapSysResource(getCurrentCaps()))
@ -65,7 +61,7 @@ func main() {
ctx.config = readConfig() ctx.config = readConfig()
ctx.librnnoise = rnnoisefile ctx.librnnoise = rnnoisefile
doCLI(ctx.config, ctx.librnnoise) doCLI(opt, ctx.config, ctx.librnnoise)
if ctx.config.EnableUpdates { if ctx.config.EnableUpdates {
go updateCheck(&ctx) go updateCheck(&ctx)

Loading…
Cancel
Save