Skip to content

Commit

Permalink
feat(cmd): clean up logging
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Sep 25, 2023
1 parent af4f0aa commit 316b357
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 42 deletions.
9 changes: 0 additions & 9 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,10 @@ import (
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(infoCmd)
}

var infoCmd = &cobra.Command{
Use: "info",
Short: "Print details of this device",
Long: "This will show the information that was used to register this device with Home Assistant",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
setLogfileLogging()
setLoggingLevel()
setProfiling()
},
Run: func(cmd *cobra.Command, args []string) {
agent.ShowInfo(agent.AgentOptions{ID: appID})
},
Expand Down
1 change: 0 additions & 1 deletion cmd/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,4 @@ func init() {
registerCmd.PersistentFlags().BoolVar(&forcedFlag,
"force", false,
"Ignore any previous registration and re-register the agent.")
rootCmd.AddCommand(registerCmd)
}
9 changes: 6 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
_ "net/http/pprof"

"github.com/joshuar/go-hass-agent/internal/agent"
"github.com/joshuar/go-hass-agent/internal/logging"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
Expand All @@ -31,9 +32,7 @@ var rootCmd = &cobra.Command{
It can also receive notifications from Home Assistant.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
setLogfileLogging()
setLoggingLevel()
setProfiling()
logging.SetLogging(traceFlag, debugFlag, profileFlag)
},
Run: func(cmd *cobra.Command, args []string) {
agent.Run(agent.AgentOptions{
Expand All @@ -60,4 +59,8 @@ func init() {
"specify a custom app ID (for debugging)")
rootCmd.PersistentFlags().BoolVar(&headlessFlag, "terminal", false,
"run in terminal (without a GUI)")

rootCmd.AddCommand(infoCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(registerCmd)
}
6 changes: 0 additions & 6 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,11 @@ import (
)

func init() {
rootCmd.AddCommand(versionCmd)
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
setLogfileLogging()
setLoggingLevel()
setProfiling()
},
Run: func(cmd *cobra.Command, args []string) {
agent.ShowVersion(agent.AgentOptions{ID: appID})
},
Expand Down
47 changes: 24 additions & 23 deletions cmd/common.go → internal/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,52 @@
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

package cmd
package logging

import (
"fmt"
"net/http"
"os"

_ "net/http/pprof"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/rs/zerolog/pkgerrors"
)

func setLogfileLogging() {
func init() {
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}

func setLoggingLevel() {
func setProfiling() {
go func() {
for i := 6060; i < 6070; i++ {
log.Debug().
Msgf("Starting profiler web interface on localhost:" + fmt.Sprint(i))
err := http.ListenAndServe("localhost:"+fmt.Sprint(i), nil)
if err != nil {
log.Debug().Err(err).
Msg("Trouble starting profiler, trying again.")
}
}
}()
}

// SetLogging sets an appropriate log level and enables profiling if requested.
func SetLogging(trace, debug, profile bool) {
switch {
case traceFlag:
case trace:
zerolog.SetGlobalLevel(zerolog.TraceLevel)
log.Debug().Msg("Trace logging enabled.")
case debugFlag:
case debug:
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Debug().Msg("Debug logging enabled.")
default:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
if debugID != "" {
appID = debugID
}
}

func setProfiling() {
if profileFlag {
go func() {
for i := 6060; i < 6070; i++ {
log.Debug().
Msgf("Starting profiler web interface on localhost:" + fmt.Sprint(i))
err := http.ListenAndServe("localhost:"+fmt.Sprint(i), nil)
if err != nil {
log.Debug().Err(err).
Msg("Trouble starting profiler, trying again.")
}
}
}()
if profile {
setProfiling()
}
}

0 comments on commit 316b357

Please sign in to comment.