Skip to content

Commit c0d5aa5

Browse files
authored
[skip changelog] Add logging support (#44)
1 parent a75ca10 commit c0d5aa5

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

cli/cli.go

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package cli
2222
import (
2323
"encoding/json"
2424
"fmt"
25+
"io/ioutil"
2526
"log"
2627
"os"
2728
"strings"
@@ -33,16 +34,23 @@ import (
3334
"github.com/arduino/FirmwareUploader/modules/winc"
3435
"github.com/arduino/FirmwareUploader/utils"
3536
"github.com/arduino/FirmwareUploader/utils/context"
37+
v "github.com/arduino/FirmwareUploader/version"
3638
"github.com/arduino/arduino-cli/cli/errorcodes"
3739
"github.com/arduino/arduino-cli/cli/feedback"
3840
"github.com/arduino/go-paths-helper"
41+
"github.com/mattn/go-colorable"
42+
"github.com/rifflock/lfshook"
3943
"github.com/sirupsen/logrus"
4044
"github.com/spf13/cobra"
4145
)
4246

4347
var (
4448
ctx = &context.Context{}
4549
outputFormat string
50+
verbose bool
51+
logFile string
52+
logFormat string
53+
logLevel string
4654
)
4755

4856
func NewCommand() *cobra.Command {
@@ -70,8 +78,14 @@ func NewCommand() *cobra.Command {
7078
firmwareUploaderCli.Flags().StringVar(&ctx.Model, "model", "", "module model (winc, nina or sara)")
7179
firmwareUploaderCli.Flags().StringVar(&ctx.BoardName, "get_available_for", "", "Ask for available firmwares matching a given board")
7280
firmwareUploaderCli.Flags().IntVar(&ctx.Retries, "retries", 9, "Number of retries in case of upload failure")
81+
7382
firmwareUploaderCli.PersistentFlags().StringVar(&outputFormat, "format", "text", "The output format, can be {text|json}.")
7483

84+
firmwareUploaderCli.PersistentFlags().StringVar(&logFile, "log-file", "", "Path to the file where logs will be written")
85+
firmwareUploaderCli.PersistentFlags().StringVar(&logFormat, "log-format", "", "The output format for the logs, can be {text|json}.")
86+
firmwareUploaderCli.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Messages with this level and above will be logged. Valid levels are: trace, debug, info, warn, error, fatal, panic")
87+
firmwareUploaderCli.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Print the logs on the standard output.")
88+
7589
return firmwareUploaderCli
7690
}
7791

@@ -129,6 +143,22 @@ func run(cmd *cobra.Command, args []string) {
129143
}
130144
}
131145

146+
// Convert the string passed to the `--log-level` option to the corresponding
147+
// logrus formal level.
148+
func toLogLevel(s string) (t logrus.Level, found bool) {
149+
t, found = map[string]logrus.Level{
150+
"trace": logrus.TraceLevel,
151+
"debug": logrus.DebugLevel,
152+
"info": logrus.InfoLevel,
153+
"warn": logrus.WarnLevel,
154+
"error": logrus.ErrorLevel,
155+
"fatal": logrus.FatalLevel,
156+
"panic": logrus.PanicLevel,
157+
}[s]
158+
159+
return
160+
}
161+
132162
func parseFormatString(arg string) (feedback.OutputFormat, bool) {
133163
f, found := map[string]feedback.OutputFormat{
134164
"json": feedback.JSON,
@@ -139,6 +169,47 @@ func parseFormatString(arg string) (feedback.OutputFormat, bool) {
139169
}
140170

141171
func preRun(cmd *cobra.Command, args []string) {
172+
// Prepare logging
173+
if verbose {
174+
// if we print on stdout, do it in full colors
175+
logrus.SetOutput(colorable.NewColorableStdout())
176+
logrus.SetFormatter(&logrus.TextFormatter{
177+
ForceColors: true,
178+
})
179+
} else {
180+
logrus.SetOutput(ioutil.Discard)
181+
}
182+
183+
// Normalize the format strings
184+
logFormat = strings.ToLower(logFormat)
185+
if logFormat == "json" {
186+
logrus.SetFormatter(&logrus.JSONFormatter{})
187+
}
188+
189+
logFile := ""
190+
if logFile != "" {
191+
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
192+
if err != nil {
193+
fmt.Printf("Unable to open file for logging: %s", logFile)
194+
os.Exit(errorcodes.ErrBadCall)
195+
}
196+
197+
// Use a hook so we don't get color codes in the log file
198+
if outputFormat == "json" {
199+
logrus.AddHook(lfshook.NewHook(file, &logrus.JSONFormatter{}))
200+
} else {
201+
logrus.AddHook(lfshook.NewHook(file, &logrus.TextFormatter{}))
202+
}
203+
}
204+
205+
// Configure logging filter
206+
if lvl, found := toLogLevel(logLevel); !found {
207+
feedback.Errorf("Invalid option for --log-level: %s", logLevel)
208+
os.Exit(errorcodes.ErrBadArgument)
209+
} else {
210+
logrus.SetLevel(lvl)
211+
}
212+
142213
//
143214
// Prepare the Feedback system
144215
//
@@ -148,13 +219,15 @@ func preRun(cmd *cobra.Command, args []string) {
148219
// check the right output format was passed
149220
format, found := parseFormatString(outputFormat)
150221
if !found {
151-
feedback.Error("Invalid output format: " + outputFormat)
222+
feedback.Errorf("Invalid output format: %s", outputFormat)
152223
os.Exit(errorcodes.ErrBadCall)
153224
}
154225

155226
// use the output format to configure the Feedback
156227
feedback.SetFormat(format)
157228

229+
logrus.Info(v.VersionInfo)
230+
158231
if outputFormat != "text" {
159232
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
160233
logrus.Warn("Calling help on JSON format")

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ replace go.bug.st/serial => github.com/cmaglie/go-serial v0.0.0-20200923162623-b
88
require (
99
github.com/arduino/arduino-cli v0.0.0-20210422154105-5aa424818026
1010
github.com/arduino/go-paths-helper v1.4.0
11+
github.com/mattn/go-colorable v0.1.8 // indirect
1112
github.com/pkg/errors v0.9.1
13+
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 // indirect
1214
github.com/sirupsen/logrus v1.8.1
1315
github.com/spf13/cobra v1.1.3
1416
github.com/stretchr/testify v1.6.1

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,12 @@ github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP
179179
github.com/marcinbor85/gohex v0.0.0-20210308104911-55fb1c624d84/go.mod h1:Pb6XcsXyropB9LNHhnqaknG/vEwYztLkQzVCHv8sQ3M=
180180
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
181181
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
182+
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
183+
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
182184
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
183185
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
186+
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
187+
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
184188
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
185189
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
186190
github.com/mdlayher/genetlink v0.0.0-20190313224034-60417448a851/go.mod h1:EsbsAEUEs15qC1cosAwxgCWV0Qhd8TmkxnA9Kw1Vhl4=
@@ -225,6 +229,7 @@ github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8
225229
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
226230
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
227231
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
232+
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 h1:mZHayPoR0lNmnHyvtYjDeq0zlVHn9K/ZXoy17ylucdo=
228233
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5/go.mod h1:GEXHk5HgEKCvEIIrSpFI3ozzG5xOKA2DVlEX/gGnewM=
229234
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
230235
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
@@ -359,6 +364,8 @@ golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7w
359364
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
360365
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
361366
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
367+
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
368+
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
362369
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
363370
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 h1:W0lCpv29Hv0UaM1LXb9QlBHLNP8UFfcKjblhVCWftOM=
364371
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

0 commit comments

Comments
 (0)