@@ -17,7 +17,6 @@ package cli
1717
1818import (
1919 "fmt"
20- "io/ioutil"
2120 "os"
2221 "strings"
2322
@@ -37,7 +36,6 @@ import (
3736 "github.com/arduino/arduino-cli/cli/lib"
3837 "github.com/arduino/arduino-cli/cli/monitor"
3938 "github.com/arduino/arduino-cli/cli/outdated"
40- "github.com/arduino/arduino-cli/cli/output"
4139 "github.com/arduino/arduino-cli/cli/sketch"
4240 "github.com/arduino/arduino-cli/cli/update"
4341 "github.com/arduino/arduino-cli/cli/updater"
@@ -47,17 +45,14 @@ import (
4745 "github.com/arduino/arduino-cli/configuration"
4846 "github.com/arduino/arduino-cli/i18n"
4947 "github.com/arduino/arduino-cli/inventory"
50- "github.com/fatih/color"
51- "github.com/mattn/go-colorable"
52- "github.com/rifflock/lfshook"
48+ "github.com/arduino/arduino-cli/logging"
49+ "github.com/arduino/arduino-cli/output"
5350 "github.com/sirupsen/logrus"
5451 "github.com/spf13/cobra"
5552 semver "go.bug.st/relaxed-semver"
5653)
5754
5855var (
59- verbose bool
60- outputFormat string
6156 configFile string
6257 updaterMessageChan chan * semver.Version = make (chan * semver.Version )
6358)
@@ -104,57 +99,34 @@ func createCliCommandTree(cmd *cobra.Command) {
10499 cmd .AddCommand (burnbootloader .NewCommand ())
105100 cmd .AddCommand (version .NewCommand ())
106101
107- cmd .PersistentFlags ().BoolVarP (& verbose , "verbose" , "v" , false , tr ("Print the logs on the standard output." ))
108102 validLogLevels := []string {"trace" , "debug" , "info" , "warn" , "error" , "fatal" , "panic" }
109- cmd .PersistentFlags ().String ("log-level" , "" , tr ("Messages with this level and above will be logged. Valid levels are: %s" , strings .Join (validLogLevels , ", " )))
103+ validLogFormats := []string {"text" , "json" }
104+ cmd .PersistentFlags ().String ("log-level" , "info" , tr ("Messages with this level and above will be logged. Valid levels are: %s" , strings .Join (validLogLevels , ", " )))
105+ cmd .PersistentFlags ().String ("log-file" , "" , tr ("Path to the file where logs will be written." ))
106+ cmd .PersistentFlags ().String ("log-format" , "text" , tr ("The output format for the logs, can be: %s" , strings .Join (validLogFormats , ", " )))
110107 cmd .RegisterFlagCompletionFunc ("log-level" , func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
111108 return validLogLevels , cobra .ShellCompDirectiveDefault
112109 })
113- cmd .PersistentFlags ().String ("log-file" , "" , tr ("Path to the file where logs will be written." ))
114- validLogFormats := []string {"text" , "json" }
115- cmd .PersistentFlags ().String ("log-format" , "" , tr ("The output format for the logs, can be: %s" , strings .Join (validLogFormats , ", " )))
116110 cmd .RegisterFlagCompletionFunc ("log-format" , func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
117111 return validLogFormats , cobra .ShellCompDirectiveDefault
118112 })
119113 validOutputFormats := []string {"text" , "json" , "jsonmini" , "yaml" }
120- cmd .PersistentFlags ().StringVar (& outputFormat , "format" , "text" , tr ("The output format for the logs, can be: %s" , strings .Join (validOutputFormats , ", " )))
114+ cmd .PersistentFlags ().BoolP ("verbose" , "v" , false , tr ("Print the logs on the standard output." ))
115+ cmd .PersistentFlags ().String ("format" , "text" , tr ("The output format for the logs, can be: %s" , strings .Join (validOutputFormats , ", " )))
116+ cmd .PersistentFlags ().Bool ("no-color" , false , "Disable colored output." )
121117 cmd .RegisterFlagCompletionFunc ("format" , func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
122118 return validOutputFormats , cobra .ShellCompDirectiveDefault
123119 })
124120 cmd .PersistentFlags ().StringVar (& configFile , "config-file" , "" , tr ("The custom config file (if not specified the default will be used)." ))
125121 cmd .PersistentFlags ().StringSlice ("additional-urls" , []string {}, tr ("Comma-separated list of additional URLs for the Boards Manager." ))
126- cmd .PersistentFlags ().Bool ("no-color" , false , "Disable colored output." )
127122 configuration .BindFlags (cmd , configuration .Settings )
128123}
129124
130- // convert the string passed to the `--log-level` option to the corresponding
131- // logrus formal level.
132- func toLogLevel (s string ) (t logrus.Level , found bool ) {
133- t , found = map [string ]logrus.Level {
134- "trace" : logrus .TraceLevel ,
135- "debug" : logrus .DebugLevel ,
136- "info" : logrus .InfoLevel ,
137- "warn" : logrus .WarnLevel ,
138- "error" : logrus .ErrorLevel ,
139- "fatal" : logrus .FatalLevel ,
140- "panic" : logrus .PanicLevel ,
141- }[s ]
142-
143- return
144- }
145-
146- func parseFormatString (arg string ) (feedback.OutputFormat , bool ) {
147- f , found := map [string ]feedback.OutputFormat {
148- "json" : feedback .JSON ,
149- "jsonmini" : feedback .JSONMini ,
150- "text" : feedback .Text ,
151- "yaml" : feedback .YAML ,
152- }[strings .ToLower (arg )]
153-
154- return f , found
155- }
156-
157125func preRun (cmd * cobra.Command , args []string ) {
126+ if cmd .Name () == "daemon" {
127+ return
128+ }
129+
158130 configFile := configuration .Settings .ConfigFileUsed ()
159131
160132 // initialize inventory
@@ -164,12 +136,13 @@ func preRun(cmd *cobra.Command, args []string) {
164136 os .Exit (errorcodes .ErrBadArgument )
165137 }
166138
167- // https://no-color.org/
168- color .NoColor = configuration .Settings .GetBool ("output.no_color" ) || os .Getenv ("NO_COLOR" ) != ""
169-
170- // Set default feedback output to colorable
171- feedback .SetOut (colorable .NewColorableStdout ())
172- feedback .SetErr (colorable .NewColorableStderr ())
139+ outputFormat , err := cmd .Flags ().GetString ("format" )
140+ if err != nil {
141+ feedback .Errorf (globals .Tr ("Error getting flag value: %s" , err ))
142+ os .Exit (errorcodes .ErrBadCall )
143+ }
144+ noColor := configuration .Settings .GetBool ("output.no_color" ) || os .Getenv ("NO_COLOR" ) != ""
145+ output .Setup (outputFormat , noColor )
173146
174147 updaterMessageChan = make (chan * semver.Version )
175148 go func () {
@@ -185,70 +158,19 @@ func preRun(cmd *cobra.Command, args []string) {
185158 updaterMessageChan <- updater .CheckForUpdate (currentVersion )
186159 }()
187160
188- //
189- // Prepare logging
190- //
191-
192- // decide whether we should log to stdout
193- if verbose {
194- // if we print on stdout, do it in full colors
195- logrus .SetOutput (colorable .NewColorableStdout ())
196- logrus .SetFormatter (& logrus.TextFormatter {
197- ForceColors : true ,
198- DisableColors : color .NoColor ,
199- })
200- } else {
201- logrus .SetOutput (ioutil .Discard )
202- }
203-
204- // set the Logger format
205- logFormat := strings .ToLower (configuration .Settings .GetString ("logging.format" ))
206- if logFormat == "json" {
207- logrus .SetFormatter (& logrus.JSONFormatter {})
208- }
209-
210- // should we log to file?
211- logFile := configuration .Settings .GetString ("logging.file" )
212- if logFile != "" {
213- file , err := os .OpenFile (logFile , os .O_CREATE | os .O_WRONLY | os .O_APPEND , 0666 )
214- if err != nil {
215- fmt .Println (tr ("Unable to open file for logging: %s" , logFile ))
216- os .Exit (errorcodes .ErrBadCall )
217- }
218-
219- // we use a hook so we don't get color codes in the log file
220- if logFormat == "json" {
221- logrus .AddHook (lfshook .NewHook (file , & logrus.JSONFormatter {}))
222- } else {
223- logrus .AddHook (lfshook .NewHook (file , & logrus.TextFormatter {}))
224- }
225- }
226-
227- // configure logging filter
228- if lvl , found := toLogLevel (configuration .Settings .GetString ("logging.level" )); ! found {
229- feedback .Errorf (tr ("Invalid option for --log-level: %s" ), configuration .Settings .GetString ("logging.level" ))
230- os .Exit (errorcodes .ErrBadArgument )
231- } else {
232- logrus .SetLevel (lvl )
233- }
234-
235- //
236- // Prepare the Feedback system
237- //
238-
239- // normalize the format strings
240- outputFormat = strings .ToLower (outputFormat )
241- // configure the output package
242- output .OutputFormat = outputFormat
243- // check the right output format was passed
244- format , found := parseFormatString (outputFormat )
245- if ! found {
246- feedback .Errorf (tr ("Invalid output format: %s" ), outputFormat )
161+ // Setups logging if necessary
162+ verbose , err := cmd .Flags ().GetBool ("verbose" )
163+ if err != nil {
164+ feedback .Errorf (tr ("Error getting flag value: %s" , err ))
247165 os .Exit (errorcodes .ErrBadCall )
248166 }
249-
250- // use the output format to configure the Feedback
251- feedback .SetFormat (format )
167+ logging .Setup (
168+ verbose ,
169+ noColor ,
170+ configuration .Settings .GetString ("logging.level" ),
171+ configuration .Settings .GetString ("logging.file" ),
172+ configuration .Settings .GetString ("logging.format" ),
173+ )
252174
253175 //
254176 // Print some status info and check command is consistent
0 commit comments