Skip to content

Commit

Permalink
Removed log output on stdout
Browse files Browse the repository at this point in the history
* Removed log output on stdout 
* Corrected golangci-lint warnings
  • Loading branch information
bgn42 authored Mar 20, 2024
1 parent 2f94f33 commit 660fe34
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
30 changes: 12 additions & 18 deletions cmd/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,19 @@ func configureGlobalCmd(cmd *cobra.Command, args []string) error {
}

log.SetLevel(log.InfoLevel)
f, err := os.OpenFile("cbridge.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
log.SetOutput(cmd.OutOrStdout())
if flags.logFile == "" {
log.SetOutput(io.Discard)
} else {
log.SetOutput(f)
f, err := os.OpenFile(flags.logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err != nil || f == nil {
log.SetOutput(io.Discard)
} else {
defer func() {
_ = f.Close()
log.SetOutput(io.Discard)
}()
log.SetOutput(f)
}
}

if quiet {
Expand Down Expand Up @@ -107,20 +115,6 @@ func NewCli() *cobra.Command {
SilenceErrors: true,
PersistentPreRunE: configureGlobalCmd,
RunE: func(cmd *cobra.Command, args []string) error {
if flags.logFile == "" {
log.SetOutput(os.Stdout)
} else {
f, err := os.OpenFile(flags.logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err != nil || f == nil {
log.SetOutput(os.Stdout)
} else {
defer func() {
_ = f.Close()
log.SetOutput(os.Stdout)
}()
log.SetOutput(f)
}
}
log.Println("Command line:", args)
if flags.version {
printVersionAndLicense(cmd.OutOrStdout())
Expand Down
1 change: 0 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (

func main() {
log.SetFormatter(new(LogFormatter))
log.SetOutput(os.Stdout)

utils.StartSignalWatcher()
start := time.Now()
Expand Down
8 changes: 4 additions & 4 deletions internal/stm32CubeMX/stm32CubeMX.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var watcher *fsnotify.Watcher

func procWait(proc *os.Process) {
if proc != nil {
proc.Wait()
_, _ = proc.Wait()
log.Println("CubeMX ended")
watcher.Close()
log.Println("Watcher closed")
Expand Down Expand Up @@ -189,7 +189,7 @@ func Process(cbuildYmlPath, outPath, cubeMxPath string, runCubeMx bool, pid int)
cubeIocPath = path.Join(cubeIocPath, "STM32CubeMX.ioc")

var err error
pid := -1
var pid int
if utils.FileExists(cubeIocPath) {
log.Printf("CubeMX with: %s", cubeIocPath)
pid, err = Launch(cubeIocPath, "")
Expand All @@ -209,9 +209,9 @@ func Process(cbuildYmlPath, outPath, cubeMxPath string, runCubeMx bool, pid int)
}
}
log.Printf("pid of CubeMX in main: %d", pid)

// here cubeMX runs
cmd := exec.Command(os.Args[0])
ownPath := path.Base(os.Args[0]) //nolint
cmd := exec.Command(ownPath) //nolint
cmd.Args = os.Args
cmd.Args = append(cmd.Args, "-p", fmt.Sprint(pid)) // pid of cubeMX
if err := cmd.Start(); err != nil { // start myself as a daemon
Expand Down

0 comments on commit 660fe34

Please sign in to comment.