@@ -17,10 +17,10 @@ package cli
1717
1818import (
1919 "fmt"
20- "io/ioutil"
2120 "os"
2221 "strings"
2322
23+ "github.com/arduino/arduino-cli/cli/arguments"
2424 "github.com/arduino/arduino-cli/cli/board"
2525 "github.com/arduino/arduino-cli/cli/burnbootloader"
2626 "github.com/arduino/arduino-cli/cli/cache"
@@ -47,17 +47,15 @@ import (
4747 "github.com/arduino/arduino-cli/configuration"
4848 "github.com/arduino/arduino-cli/i18n"
4949 "github.com/arduino/arduino-cli/inventory"
50+ "github.com/arduino/arduino-cli/logging"
5051 "github.com/fatih/color"
5152 "github.com/mattn/go-colorable"
52- "github.com/rifflock/lfshook"
5353 "github.com/sirupsen/logrus"
5454 "github.com/spf13/cobra"
5555 semver "go.bug.st/relaxed-semver"
5656)
5757
5858var (
59- verbose bool
60- outputFormat string
6159 configFile string
6260 updaterMessageChan chan * semver.Version = make (chan * semver.Version )
6361)
@@ -104,45 +102,15 @@ func createCliCommandTree(cmd *cobra.Command) {
104102 cmd .AddCommand (burnbootloader .NewCommand ())
105103 cmd .AddCommand (version .NewCommand ())
106104
107- cmd .PersistentFlags ().BoolVarP (& verbose , "verbose" , "v" , false , tr ("Print the logs on the standard output." ))
108- 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 , ", " )))
110- cmd .RegisterFlagCompletionFunc ("log-level" , func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
111- return validLogLevels , cobra .ShellCompDirectiveDefault
112- })
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 , ", " )))
116- cmd .RegisterFlagCompletionFunc ("log-format" , func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
117- return validLogFormats , cobra .ShellCompDirectiveDefault
118- })
119- 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 , ", " )))
121- cmd .RegisterFlagCompletionFunc ("format" , func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
122- return validOutputFormats , cobra .ShellCompDirectiveDefault
123- })
105+ arguments .AddLoggingFlags (cmd .PersistentFlags ())
106+ arguments .AddLoggingCompletion (cmd )
107+ arguments .AddOutputFlags (cmd .PersistentFlags ())
108+ arguments .AddOutputCompletion (cmd )
124109 cmd .PersistentFlags ().StringVar (& configFile , "config-file" , "" , tr ("The custom config file (if not specified the default will be used)." ))
125110 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." )
127111 configuration .BindFlags (cmd , configuration .Settings )
128112}
129113
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-
146114func parseFormatString (arg string ) (feedback.OutputFormat , bool ) {
147115 f , found := map [string ]feedback.OutputFormat {
148116 "json" : feedback .JSON ,
@@ -155,10 +123,33 @@ func parseFormatString(arg string) (feedback.OutputFormat, bool) {
155123}
156124
157125func preRun (cmd * cobra.Command , args []string ) {
126+ // Set feedback format output
127+ outputFormat , err := cmd .Flags ().GetString ("format" )
128+ if err != nil {
129+ feedback .Errorf (tr ("Error getting flag value: %s" , err ))
130+ os .Exit (errorcodes .ErrBadCall )
131+ }
132+ outputFormat = strings .ToLower (outputFormat )
133+ output .OutputFormat = outputFormat
134+ format , found := parseFormatString (outputFormat )
135+ if ! found {
136+ feedback .Errorf (tr ("Invalid output format: %s" ), outputFormat )
137+ os .Exit (errorcodes .ErrBadCall )
138+ }
139+ feedback .SetFormat (format )
140+
141+ // Set default feedback output to colorable
142+ feedback .SetOut (colorable .NewColorableStdout ())
143+ feedback .SetErr (colorable .NewColorableStderr ())
144+
145+ if cmd .Name () == "daemon" {
146+ return
147+ }
148+
158149 configFile := configuration .Settings .ConfigFileUsed ()
159150
160151 // initialize inventory
161- err : = inventory .Init (configuration .Settings .GetString ("directories.Data" ))
152+ err = inventory .Init (configuration .Settings .GetString ("directories.Data" ))
162153 if err != nil {
163154 feedback .Errorf ("Error: %v" , err )
164155 os .Exit (errorcodes .ErrBadArgument )
@@ -167,10 +158,6 @@ func preRun(cmd *cobra.Command, args []string) {
167158 // https://no-color.org/
168159 color .NoColor = configuration .Settings .GetBool ("output.no_color" ) || os .Getenv ("NO_COLOR" ) != ""
169160
170- // Set default feedback output to colorable
171- feedback .SetOut (colorable .NewColorableStdout ())
172- feedback .SetErr (colorable .NewColorableStderr ())
173-
174161 updaterMessageChan = make (chan * semver.Version )
175162 go func () {
176163 if cmd .Name () == "version" {
@@ -185,70 +172,19 @@ func preRun(cmd *cobra.Command, args []string) {
185172 updaterMessageChan <- updater .CheckForUpdate (currentVersion )
186173 }()
187174
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 )
175+ // Setups logging if necessary
176+ verbose , err := cmd .Flags ().GetBool ("verbose" )
177+ if err != nil {
178+ feedback .Errorf (tr ("Error getting flag value: %s" , err ))
247179 os .Exit (errorcodes .ErrBadCall )
248180 }
249-
250- // use the output format to configure the Feedback
251- feedback .SetFormat (format )
181+ logging .Setup (
182+ verbose ,
183+ color .NoColor ,
184+ configuration .Settings .GetString ("logging.level" ),
185+ configuration .Settings .GetString ("logging.file" ),
186+ configuration .Settings .GetString ("logging.format" ),
187+ )
252188
253189 //
254190 // Print some status info and check command is consistent
0 commit comments