@@ -17,10 +17,10 @@ package cli
17
17
18
18
import (
19
19
"fmt"
20
- "io/ioutil"
21
20
"os"
22
21
"strings"
23
22
23
+ "github.com/arduino/arduino-cli/cli/arguments"
24
24
"github.com/arduino/arduino-cli/cli/board"
25
25
"github.com/arduino/arduino-cli/cli/burnbootloader"
26
26
"github.com/arduino/arduino-cli/cli/cache"
@@ -47,17 +47,15 @@ import (
47
47
"github.com/arduino/arduino-cli/configuration"
48
48
"github.com/arduino/arduino-cli/i18n"
49
49
"github.com/arduino/arduino-cli/inventory"
50
+ "github.com/arduino/arduino-cli/logging"
50
51
"github.com/fatih/color"
51
52
"github.com/mattn/go-colorable"
52
- "github.com/rifflock/lfshook"
53
53
"github.com/sirupsen/logrus"
54
54
"github.com/spf13/cobra"
55
55
semver "go.bug.st/relaxed-semver"
56
56
)
57
57
58
58
var (
59
- verbose bool
60
- outputFormat string
61
59
configFile string
62
60
updaterMessageChan chan * semver.Version = make (chan * semver.Version )
63
61
)
@@ -104,45 +102,15 @@ func createCliCommandTree(cmd *cobra.Command) {
104
102
cmd .AddCommand (burnbootloader .NewCommand ())
105
103
cmd .AddCommand (version .NewCommand ())
106
104
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 )
124
109
cmd .PersistentFlags ().StringVar (& configFile , "config-file" , "" , tr ("The custom config file (if not specified the default will be used)." ))
125
110
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." )
127
111
configuration .BindFlags (cmd , configuration .Settings )
128
112
}
129
113
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
114
func parseFormatString (arg string ) (feedback.OutputFormat , bool ) {
147
115
f , found := map [string ]feedback.OutputFormat {
148
116
"json" : feedback .JSON ,
@@ -155,10 +123,33 @@ func parseFormatString(arg string) (feedback.OutputFormat, bool) {
155
123
}
156
124
157
125
func 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
+
158
149
configFile := configuration .Settings .ConfigFileUsed ()
159
150
160
151
// initialize inventory
161
- err : = inventory .Init (configuration .Settings .GetString ("directories.Data" ))
152
+ err = inventory .Init (configuration .Settings .GetString ("directories.Data" ))
162
153
if err != nil {
163
154
feedback .Errorf ("Error: %v" , err )
164
155
os .Exit (errorcodes .ErrBadArgument )
@@ -167,10 +158,6 @@ func preRun(cmd *cobra.Command, args []string) {
167
158
// https://no-color.org/
168
159
color .NoColor = configuration .Settings .GetBool ("output.no_color" ) || os .Getenv ("NO_COLOR" ) != ""
169
160
170
- // Set default feedback output to colorable
171
- feedback .SetOut (colorable .NewColorableStdout ())
172
- feedback .SetErr (colorable .NewColorableStderr ())
173
-
174
161
updaterMessageChan = make (chan * semver.Version )
175
162
go func () {
176
163
if cmd .Name () == "version" {
@@ -185,70 +172,19 @@ func preRun(cmd *cobra.Command, args []string) {
185
172
updaterMessageChan <- updater .CheckForUpdate (currentVersion )
186
173
}()
187
174
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 ))
247
179
os .Exit (errorcodes .ErrBadCall )
248
180
}
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
+ )
252
188
253
189
//
254
190
// Print some status info and check command is consistent
0 commit comments