Skip to content

Commit c0ccf93

Browse files
committed
Made builder logger verbosity an explicit type
1 parent e9092cc commit c0ccf93

File tree

13 files changed

+68
-44
lines changed

13 files changed

+68
-44
lines changed

Diff for: commands/service_compile.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/arduino/arduino-cli/commands/cmderrors"
2929
"github.com/arduino/arduino-cli/commands/internal/instances"
3030
"github.com/arduino/arduino-cli/internal/arduino/builder"
31+
"github.com/arduino/arduino-cli/internal/arduino/builder/logger"
3132
"github.com/arduino/arduino-cli/internal/arduino/libraries/librariesmanager"
3233
"github.com/arduino/arduino-cli/internal/arduino/sketch"
3334
"github.com/arduino/arduino-cli/internal/arduino/utils"
@@ -244,6 +245,10 @@ func (s *arduinoCoreServerImpl) Compile(req *rpc.CompileRequest, stream rpc.Ardu
244245
Message: &rpc.CompileResponse_Progress{Progress: p},
245246
})
246247
}
248+
var verbosity logger.Verbosity = logger.VerbosityNormal
249+
if req.GetVerbose() {
250+
verbosity = logger.VerbosityVerbose
251+
}
247252
sketchBuilder, err := builder.NewBuilder(
248253
ctx,
249254
sk,
@@ -265,7 +270,7 @@ func (s *arduinoCoreServerImpl) Compile(req *rpc.CompileRequest, stream rpc.Ardu
265270
req.GetSkipLibrariesDiscovery(),
266271
libsManager,
267272
paths.NewPathList(req.GetLibrary()...),
268-
outStream, errStream, req.GetVerbose(), req.GetWarnings(),
273+
outStream, errStream, verbosity, req.GetWarnings(),
269274
progressCB,
270275
pme.GetEnvVarsForSpawnedProcess(),
271276
)

Diff for: internal/arduino/builder/archive_compiled_files.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
package builder
1717

1818
import (
19+
"github.com/arduino/arduino-cli/internal/arduino/builder/logger"
1920
"github.com/arduino/arduino-cli/internal/i18n"
2021
"github.com/arduino/go-paths-helper"
2122
)
2223

2324
// ArchiveCompiledFiles fixdoc
2425
func (b *Builder) archiveCompiledFiles(archiveFilePath *paths.Path, objectFilesToArchive paths.PathList) (*paths.Path, error) {
2526
if b.onlyUpdateCompilationDatabase {
26-
if b.logger.Verbose() {
27+
if b.logger.VerbosityLevel() == logger.VerbosityVerbose {
2728
b.logger.Info(i18n.Tr("Skipping archive creation of: %[1]s", archiveFilePath))
2829
}
2930
return archiveFilePath, nil
@@ -46,7 +47,7 @@ func (b *Builder) archiveCompiledFiles(archiveFilePath *paths.Path, objectFilesT
4647
return nil, err
4748
}
4849
} else {
49-
if b.logger.Verbose() {
50+
if b.logger.VerbosityLevel() == logger.VerbosityVerbose {
5051
b.logger.Info(i18n.Tr("Using previously compiled file: %[1]s", archiveFilePath))
5152
}
5253
return archiveFilePath, nil

Diff for: internal/arduino/builder/builder.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import (
2727
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/compilation"
2828
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/detector"
2929
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/diagnostics"
30-
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/logger"
3130
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/progress"
3231
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/utils"
32+
"github.com/arduino/arduino-cli/internal/arduino/builder/logger"
3333
"github.com/arduino/arduino-cli/internal/arduino/cores"
3434
"github.com/arduino/arduino-cli/internal/arduino/libraries"
3535
"github.com/arduino/arduino-cli/internal/arduino/libraries/librariesmanager"
@@ -136,7 +136,7 @@ func NewBuilder(
136136
useCachedLibrariesResolution bool,
137137
librariesManager *librariesmanager.LibrariesManager,
138138
libraryDirs paths.PathList,
139-
stdout, stderr io.Writer, verbose bool, warningsLevel string,
139+
stdout, stderr io.Writer, verbosity logger.Verbosity, warningsLevel string,
140140
progresCB rpc.TaskProgressCB,
141141
toolEnv []string,
142142
) (*Builder, error) {
@@ -189,7 +189,7 @@ func NewBuilder(
189189
return nil, ErrSketchCannotBeLocatedInBuildPath
190190
}
191191

192-
logger := logger.New(stdout, stderr, verbose, warningsLevel)
192+
log := logger.New(stdout, stderr, verbosity, warningsLevel)
193193
libsManager, libsResolver, verboseOut, err := detector.LibrariesLoader(
194194
useCachedLibrariesResolution, librariesManager,
195195
builtInLibrariesDirs, libraryDirs, otherLibrariesDirs,
@@ -198,8 +198,8 @@ func NewBuilder(
198198
if err != nil {
199199
return nil, err
200200
}
201-
if logger.Verbose() {
202-
logger.Warn(string(verboseOut))
201+
if log.VerbosityLevel() == logger.VerbosityVerbose {
202+
log.Warn(string(verboseOut))
203203
}
204204

205205
diagnosticStore := diagnostics.NewStore()
@@ -215,7 +215,7 @@ func NewBuilder(
215215
customBuildProperties: customBuildPropertiesArgs,
216216
coreBuildCachePath: coreBuildCachePath,
217217
extraCoreBuildCachePaths: extraCoreBuildCachePaths,
218-
logger: logger,
218+
logger: log,
219219
clean: clean,
220220
sourceOverrides: sourceOverrides,
221221
onlyUpdateCompilationDatabase: onlyUpdateCompilationDatabase,
@@ -242,7 +242,7 @@ func NewBuilder(
242242
libsManager, libsResolver,
243243
useCachedLibrariesResolution,
244244
onlyUpdateCompilationDatabase,
245-
logger,
245+
log,
246246
diagnosticStore,
247247
),
248248
}
@@ -341,7 +341,7 @@ func (b *Builder) preprocess() error {
341341
}
342342

343343
func (b *Builder) logIfVerbose(warn bool, msg string) {
344-
if !b.logger.Verbose() {
344+
if b.logger.VerbosityLevel() != logger.VerbosityVerbose {
345345
return
346346
}
347347
if warn {
@@ -526,7 +526,7 @@ func (b *Builder) prepareCommandForRecipe(buildProperties *properties.Map, recip
526526
}
527527

528528
func (b *Builder) execCommand(command *paths.Process) error {
529-
if b.logger.Verbose() {
529+
if b.logger.VerbosityLevel() == logger.VerbosityVerbose {
530530
b.logger.Info(utils.PrintableCommand(command.GetArgs()))
531531
command.RedirectStdoutTo(b.logger.Stdout())
532532
}

Diff for: internal/arduino/builder/compilation.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"sync"
2424

2525
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/utils"
26+
"github.com/arduino/arduino-cli/internal/arduino/builder/logger"
2627
"github.com/arduino/arduino-cli/internal/arduino/globals"
2728
"github.com/arduino/arduino-cli/internal/i18n"
2829
"github.com/arduino/go-paths-helper"
@@ -152,7 +153,7 @@ func (b *Builder) compileFileWithRecipe(
152153
command.RedirectStdoutTo(commandStdout)
153154
command.RedirectStderrTo(commandStderr)
154155

155-
if b.logger.Verbose() {
156+
if b.logger.VerbosityLevel() == logger.VerbosityVerbose {
156157
b.logger.Info(utils.PrintableCommand(command.GetArgs()))
157158
}
158159
// Since this compile could be multithreaded, we first capture the command output
@@ -161,7 +162,7 @@ func (b *Builder) compileFileWithRecipe(
161162
}
162163
err := command.Wait()
163164
// and transfer all at once at the end...
164-
if b.logger.Verbose() {
165+
if b.logger.VerbosityLevel() == logger.VerbosityVerbose {
165166
b.logger.WriteStdout(commandStdout.Bytes())
166167
}
167168
b.logger.WriteStderr(commandStderr.Bytes())
@@ -176,7 +177,7 @@ func (b *Builder) compileFileWithRecipe(
176177
if err != nil {
177178
return nil, err
178179
}
179-
} else if b.logger.Verbose() {
180+
} else if b.logger.VerbosityLevel() == logger.VerbosityVerbose {
180181
if objIsUpToDate {
181182
b.logger.Info(i18n.Tr("Using previously compiled file: %[1]s", objectFile))
182183
} else {

Diff for: internal/arduino/builder/core.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/arduino/arduino-cli/internal/arduino/builder/cpp"
2626
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/utils"
27+
"github.com/arduino/arduino-cli/internal/arduino/builder/logger"
2728
"github.com/arduino/arduino-cli/internal/buildcache"
2829
"github.com/arduino/arduino-cli/internal/i18n"
2930
"github.com/arduino/go-paths-helper"
@@ -116,7 +117,7 @@ func (b *Builder) compileCore() (*paths.Path, paths.PathList, error) {
116117
return nil, nil, errors.New(i18n.Tr("creating core cache folder: %s", err))
117118
}
118119
// use archived core
119-
if b.logger.Verbose() {
120+
if b.logger.VerbosityLevel() == logger.VerbosityVerbose {
120121
b.logger.Info(i18n.Tr("Using precompiled core: %[1]s", targetArchivedCore))
121122
}
122123
return targetArchivedCore, variantObjectFiles, nil
@@ -128,7 +129,7 @@ func (b *Builder) compileCore() (*paths.Path, paths.PathList, error) {
128129
extraTargetArchivedCore := extraCoreBuildCachePath.Join(archivedCoreName, "core.a")
129130
if canUseArchivedCore(extraTargetArchivedCore) {
130131
// use archived core
131-
if b.logger.Verbose() {
132+
if b.logger.VerbosityLevel() == logger.VerbosityVerbose {
132133
b.logger.Info(i18n.Tr("Using precompiled core: %[1]s", extraTargetArchivedCore))
133134
}
134135
return extraTargetArchivedCore, variantObjectFiles, nil
@@ -158,7 +159,7 @@ func (b *Builder) compileCore() (*paths.Path, paths.PathList, error) {
158159
// archive core.a
159160
if targetArchivedCore != nil && !b.onlyUpdateCompilationDatabase {
160161
err := archiveFile.CopyTo(targetArchivedCore)
161-
if b.logger.Verbose() {
162+
if b.logger.VerbosityLevel() == logger.VerbosityVerbose {
162163
if err == nil {
163164
b.logger.Info(i18n.Tr("Archiving built core (caching) in: %[1]s", targetArchivedCore))
164165
} else if os.IsNotExist(err) {

Diff for: internal/arduino/builder/internal/detector/detector.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import (
2828
"time"
2929

3030
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/diagnostics"
31-
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/logger"
3231
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/preprocessor"
3332
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/utils"
33+
"github.com/arduino/arduino-cli/internal/arduino/builder/logger"
3434
"github.com/arduino/arduino-cli/internal/arduino/cores"
3535
"github.com/arduino/arduino-cli/internal/arduino/globals"
3636
"github.com/arduino/arduino-cli/internal/arduino/libraries"
@@ -87,7 +87,7 @@ func (l *SketchLibrariesDetector) resolveLibrary(header, platformArch string) *l
8787
importedLibraries := l.importedLibraries
8888
candidates := l.librariesResolver.AlternativesFor(header)
8989

90-
if l.logger.Verbose() {
90+
if l.logger.VerbosityLevel() == logger.VerbosityVerbose {
9191
l.logger.Info(i18n.Tr("Alternatives for %[1]s: %[2]s", header, candidates))
9292
l.logger.Info(fmt.Sprintf("ResolveLibrary(%s)", header))
9393
l.logger.Info(fmt.Sprintf(" -> %s: %s", i18n.Tr("candidates"), candidates))
@@ -144,7 +144,7 @@ func (l *SketchLibrariesDetector) PrintUsedAndNotUsedLibraries(sketchError bool)
144144
// - as warning, when the sketch didn't compile
145145
// - as info, when verbose is on
146146
// - otherwise, output nothing
147-
if !sketchError && !l.logger.Verbose() {
147+
if !sketchError && l.logger.VerbosityLevel() != logger.VerbosityVerbose {
148148
return
149149
}
150150

@@ -239,7 +239,7 @@ func (l *SketchLibrariesDetector) findIncludes(
239239
if err := json.Unmarshal(d, &l.includeFolders); err != nil {
240240
return err
241241
}
242-
if l.logger.Verbose() {
242+
if l.logger.VerbosityLevel() == logger.VerbosityVerbose {
243243
l.logger.Info("Using cached library discovery: " + librariesResolutionCache.String())
244244
}
245245
return nil
@@ -347,12 +347,12 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
347347
var missingIncludeH string
348348
if unchanged && cache.valid {
349349
missingIncludeH = cache.Next().Include
350-
if first && l.logger.Verbose() {
350+
if first && l.logger.VerbosityLevel() == logger.VerbosityVerbose {
351351
l.logger.Info(i18n.Tr("Using cached library dependencies for file: %[1]s", sourcePath))
352352
}
353353
} else {
354354
preprocFirstResult, preprocErr = preprocessor.GCC(ctx, sourcePath, targetFilePath, includeFolders, buildProperties)
355-
if l.logger.Verbose() {
355+
if l.logger.VerbosityLevel() == logger.VerbosityVerbose {
356356
l.logger.WriteStdout(preprocFirstResult.Stdout())
357357
}
358358
// Unwrap error and see if it is an ExitError.
@@ -365,7 +365,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
365365
return preprocErr
366366
} else {
367367
missingIncludeH = IncludesFinderWithRegExp(string(preprocFirstResult.Stderr()))
368-
if missingIncludeH == "" && l.logger.Verbose() {
368+
if missingIncludeH == "" && l.logger.VerbosityLevel() == logger.VerbosityVerbose {
369369
l.logger.Info(i18n.Tr("Error while detecting libraries included by %[1]s", sourcePath))
370370
}
371371
}
@@ -383,7 +383,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
383383
if preprocErr == nil || preprocFirstResult.Stderr() == nil {
384384
// Filename came from cache, so run preprocessor to obtain error to show
385385
result, err := preprocessor.GCC(ctx, sourcePath, targetFilePath, includeFolders, buildProperties)
386-
if l.logger.Verbose() {
386+
if l.logger.VerbosityLevel() == logger.VerbosityVerbose {
387387
l.logger.WriteStdout(result.Stdout())
388388
}
389389
if err == nil {
@@ -410,7 +410,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
410410

411411
if library.Precompiled && library.PrecompiledWithSources {
412412
// Fully precompiled libraries should have no dependencies to avoid ABI breakage
413-
if l.logger.Verbose() {
413+
if l.logger.VerbosityLevel() == logger.VerbosityVerbose {
414414
l.logger.Info(i18n.Tr("Skipping dependencies detection for precompiled library %[1]s", library.Name))
415415
}
416416
} else {

Diff for: internal/arduino/builder/libraries.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"time"
2222

2323
"github.com/arduino/arduino-cli/internal/arduino/builder/cpp"
24+
"github.com/arduino/arduino-cli/internal/arduino/builder/logger"
2425
"github.com/arduino/arduino-cli/internal/arduino/libraries"
2526
"github.com/arduino/arduino-cli/internal/i18n"
2627
"github.com/arduino/go-paths-helper"
@@ -129,7 +130,7 @@ func (b *Builder) compileLibraries(libraries libraries.List, includes []string)
129130
}
130131

131132
func (b *Builder) compileLibrary(library *libraries.Library, includes []string) (paths.PathList, error) {
132-
if b.logger.Verbose() {
133+
if b.logger.VerbosityLevel() == logger.VerbosityVerbose {
133134
b.logger.Info(i18n.Tr(`Compiling library "%[1]s"`, library.Name))
134135
}
135136
libraryBuildPath := b.librariesBuildPath.Join(library.DirName)
@@ -292,7 +293,7 @@ func (b *Builder) warnAboutArchIncompatibleLibraries(importedLibraries libraries
292293
// TODO here we can completly remove this part as it's duplicated in what we can
293294
// read in the gRPC response
294295
func (b *Builder) printUsedLibraries(importedLibraries libraries.List) {
295-
if !b.logger.Verbose() || len(importedLibraries) == 0 {
296+
if b.logger.VerbosityLevel() != logger.VerbosityVerbose || len(importedLibraries) == 0 {
296297
return
297298
}
298299

Diff for: internal/arduino/builder/linker.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package builder
1818
import (
1919
"strings"
2020

21+
"github.com/arduino/arduino-cli/internal/arduino/builder/logger"
2122
"github.com/arduino/arduino-cli/internal/i18n"
2223
"github.com/arduino/go-paths-helper"
2324
"go.bug.st/f"
@@ -26,7 +27,7 @@ import (
2627
// link fixdoc
2728
func (b *Builder) link() error {
2829
if b.onlyUpdateCompilationDatabase {
29-
if b.logger.Verbose() {
30+
if b.logger.VerbosityLevel() == logger.VerbosityVerbose {
3031
b.logger.Info(i18n.Tr("Skip linking of final executable."))
3132
}
3233
return nil

Diff for: internal/arduino/builder/internal/logger/logger.go renamed to internal/arduino/builder/logger/logger.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,27 @@ import (
2222
"sync"
2323
)
2424

25+
// Verbosity is the verbosity level of the Logger
26+
type Verbosity int
27+
28+
const (
29+
VerbosityQuiet Verbosity = -1
30+
VerbosityNormal Verbosity = 0
31+
VerbosityVerbose Verbosity = 1
32+
)
33+
2534
// BuilderLogger fixdoc
2635
type BuilderLogger struct {
2736
stdLock sync.Mutex
2837
stdout io.Writer
2938
stderr io.Writer
3039

31-
verbose bool
40+
verbosity Verbosity
3241
warningsLevel string
3342
}
3443

35-
// New fixdoc
36-
func New(stdout, stderr io.Writer, verbose bool, warningsLevel string) *BuilderLogger {
44+
// New creates a new Logger to the given output streams with the specified verbosity and warnings level
45+
func New(stdout, stderr io.Writer, verbosity Verbosity, warningsLevel string) *BuilderLogger {
3746
if stdout == nil {
3847
stdout = os.Stdout
3948
}
@@ -46,7 +55,7 @@ func New(stdout, stderr io.Writer, verbose bool, warningsLevel string) *BuilderL
4655
return &BuilderLogger{
4756
stdout: stdout,
4857
stderr: stderr,
49-
verbose: verbose,
58+
verbosity: verbosity,
5059
warningsLevel: warningsLevel,
5160
}
5261
}
@@ -79,9 +88,9 @@ func (l *BuilderLogger) WriteStderr(data []byte) (int, error) {
7988
return l.stderr.Write(data)
8089
}
8190

82-
// Verbose fixdoc
83-
func (l *BuilderLogger) Verbose() bool {
84-
return l.verbose
91+
// VerbosityLevel returns the verbosity level of the logger
92+
func (l *BuilderLogger) VerbosityLevel() Verbosity {
93+
return l.verbosity
8594
}
8695

8796
// WarningsLevel fixdoc

Diff for: internal/arduino/builder/preprocess_sketch.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package builder
1717

1818
import (
1919
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/preprocessor"
20+
"github.com/arduino/arduino-cli/internal/arduino/builder/logger"
2021
"github.com/arduino/go-paths-helper"
2122
)
2223

@@ -26,10 +27,11 @@ func (b *Builder) preprocessSketch(includes paths.PathList) error {
2627
result, err := preprocessor.PreprocessSketchWithCtags(
2728
b.ctx,
2829
b.sketch, b.buildPath, includes, b.lineOffset,
29-
b.buildProperties, b.onlyUpdateCompilationDatabase, b.logger.Verbose(),
30+
b.buildProperties, b.onlyUpdateCompilationDatabase,
31+
b.logger.VerbosityLevel() == logger.VerbosityVerbose,
3032
)
3133
if result != nil {
32-
if b.logger.Verbose() {
34+
if b.logger.VerbosityLevel() == logger.VerbosityVerbose {
3335
b.logger.WriteStdout(result.Stdout())
3436
}
3537
b.logger.WriteStderr(result.Stderr())

0 commit comments

Comments
 (0)