Skip to content

Commit 559454c

Browse files
committed
Refactored logger in context.
I don't know if logger should be part of the Context, anyway I kept it in the structure to maximize compatibility. Default logging level has been lowered from 5 to 0, so the initialization step could be completely removed. Logging subroutines have been adjusted accordingly. Signed-off-by: Cristian Maglie <c.maglie@arduino.cc>
1 parent 691b76d commit 559454c

File tree

81 files changed

+365
-566
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+365
-566
lines changed

main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func main() {
308308
}
309309

310310
if *debugLevelFlag > -1 {
311-
context[constants.CTX_DEBUG_LEVEL] = *debugLevelFlag
311+
ctx.DebugLevel = *debugLevelFlag
312312
}
313313

314314
if *quietFlag {
@@ -335,11 +335,11 @@ func main() {
335335

336336
exitCode := 0
337337
if err != nil {
338-
err = utils.WrapError(err)
338+
err = i18n.WrapError(err)
339339

340340
fmt.Fprintln(os.Stderr, err)
341341

342-
if utils.DebugLevel(context) >= 10 {
342+
if ctx.DebugLevel >= 10 {
343343
fmt.Fprintln(os.Stderr, err.(*errors.Error).ErrorStack())
344344
}
345345

@@ -398,7 +398,7 @@ func printError(err error, printStackTrace bool) {
398398
}
399399

400400
func printCompleteError(err error) {
401-
err = utils.WrapError(err)
401+
err = i18n.WrapError(err)
402402
fmt.Fprintln(os.Stderr, err.(*errors.Error).ErrorStack())
403403
}
404404

src/arduino.cc/builder/add_additional_entries_to_context.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ package builder
3131

3232
import (
3333
"arduino.cc/builder/constants"
34+
"arduino.cc/builder/i18n"
3435
"arduino.cc/builder/types"
3536
"arduino.cc/builder/utils"
3637
"path/filepath"
@@ -43,19 +44,19 @@ func (s *AddAdditionalEntriesToContext) Run(context map[string]interface{}, ctx
4344
buildPath := context[constants.CTX_BUILD_PATH].(string)
4445
preprocPath, err := filepath.Abs(filepath.Join(buildPath, constants.FOLDER_PREPROC))
4546
if err != nil {
46-
return utils.WrapError(err)
47+
return i18n.WrapError(err)
4748
}
4849
sketchBuildPath, err := filepath.Abs(filepath.Join(buildPath, constants.FOLDER_SKETCH))
4950
if err != nil {
50-
return utils.WrapError(err)
51+
return i18n.WrapError(err)
5152
}
5253
librariesBuildPath, err := filepath.Abs(filepath.Join(buildPath, constants.FOLDER_LIBRARIES))
5354
if err != nil {
54-
return utils.WrapError(err)
55+
return i18n.WrapError(err)
5556
}
5657
coreBuildPath, err := filepath.Abs(filepath.Join(buildPath, constants.FOLDER_CORE))
5758
if err != nil {
58-
return utils.WrapError(err)
59+
return i18n.WrapError(err)
5960
}
6061

6162
context[constants.CTX_PREPROC_PATH] = preprocPath
@@ -72,10 +73,6 @@ func (s *AddAdditionalEntriesToContext) Run(context map[string]interface{}, ctx
7273
context[constants.CTX_VERBOSE] = false
7374
}
7475

75-
if !utils.MapHas(context, constants.CTX_DEBUG_LEVEL) {
76-
context[constants.CTX_DEBUG_LEVEL] = DEFAULT_DEBUG_LEVEL
77-
}
78-
7976
sourceFiles := &types.UniqueStringQueue{}
8077
context[constants.CTX_COLLECTED_SOURCE_FILES_QUEUE] = sourceFiles
8178
foldersWithSources := &types.UniqueSourceFolderQueue{}

src/arduino.cc/builder/add_build_board_property_if_missing.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ package builder
3131

3232
import (
3333
"arduino.cc/builder/constants"
34-
"arduino.cc/builder/i18n"
3534
"arduino.cc/builder/types"
3635
"os"
3736
"strings"
@@ -41,7 +40,7 @@ type AddBuildBoardPropertyIfMissing struct{}
4140

4241
func (s *AddBuildBoardPropertyIfMissing) Run(context map[string]interface{}, ctx *types.Context) error {
4342
packages := context[constants.CTX_HARDWARE].(*types.Packages)
44-
logger := context[constants.CTX_LOGGER].(i18n.Logger)
43+
logger := ctx.GetLogger()
4544

4645
for _, aPackage := range packages.Packages {
4746
for _, platform := range aPackage.Platforms {

src/arduino.cc/builder/additional_sketch_files_copier.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ package builder
3131

3232
import (
3333
"arduino.cc/builder/constants"
34+
"arduino.cc/builder/i18n"
3435
"arduino.cc/builder/types"
3536
"arduino.cc/builder/utils"
3637
"bytes"
@@ -46,32 +47,32 @@ func (s *AdditionalSketchFilesCopier) Run(context map[string]interface{}, ctx *t
4647

4748
err := utils.EnsureFolderExists(sketchBuildPath)
4849
if err != nil {
49-
return utils.WrapError(err)
50+
return i18n.WrapError(err)
5051
}
5152

5253
sketchBasePath := filepath.Dir(sketch.MainFile.Name)
5354

5455
for _, file := range sketch.AdditionalFiles {
5556
relativePath, err := filepath.Rel(sketchBasePath, file.Name)
5657
if err != nil {
57-
return utils.WrapError(err)
58+
return i18n.WrapError(err)
5859
}
5960

6061
targetFilePath := filepath.Join(sketchBuildPath, relativePath)
6162
err = utils.EnsureFolderExists(filepath.Dir(targetFilePath))
6263
if err != nil {
63-
return utils.WrapError(err)
64+
return i18n.WrapError(err)
6465
}
6566

6667
bytes, err := ioutil.ReadFile(file.Name)
6768
if err != nil {
68-
return utils.WrapError(err)
69+
return i18n.WrapError(err)
6970
}
7071

7172
if targetFileChanged(bytes, targetFilePath) {
7273
err := utils.WriteFileBytes(targetFilePath, bytes)
7374
if err != nil {
74-
return utils.WrapError(err)
75+
return i18n.WrapError(err)
7576
}
7677
}
7778
}

src/arduino.cc/builder/builder.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ package builder
3131

3232
import (
3333
"arduino.cc/builder/constants"
34+
"arduino.cc/builder/i18n"
3435
"arduino.cc/builder/phases"
3536
"arduino.cc/builder/types"
36-
"arduino.cc/builder/utils"
3737
"os"
3838
"reflect"
3939
"strconv"
@@ -68,8 +68,6 @@ type Builder struct{}
6868

6969
func (s *Builder) Run(context map[string]interface{}, ctx *types.Context) error {
7070
commands := []types.Command{
71-
&SetupHumanLoggerIfMissing{},
72-
7371
&GenerateBuildPathIfMissing{},
7472
&EnsureBuildPathExists{},
7573

@@ -135,8 +133,6 @@ type Preprocess struct{}
135133

136134
func (s *Preprocess) Run(context map[string]interface{}, ctx *types.Context) error {
137135
commands := []types.Command{
138-
&SetupHumanLoggerIfMissing{},
139-
140136
&GenerateBuildPathIfMissing{},
141137
&EnsureBuildPathExists{},
142138

@@ -164,8 +160,6 @@ type ParseHardwareAndDumpBuildProperties struct{}
164160

165161
func (s *ParseHardwareAndDumpBuildProperties) Run(context map[string]interface{}, ctx *types.Context) error {
166162
commands := []types.Command{
167-
&SetupHumanLoggerIfMissing{},
168-
169163
&GenerateBuildPathIfMissing{},
170164

171165
&ContainerSetupHardwareToolsLibsSketchAndProps{},
@@ -182,34 +176,34 @@ func runCommands(context map[string]interface{}, ctx *types.Context, commands []
182176

183177
progress := float32(0)
184178
for _, command := range commands {
185-
PrintRingNameIfDebug(context, command)
186-
printProgressIfProgressEnabledAndMachineLogger(progressEnabled, context, progress)
179+
PrintRingNameIfDebug(ctx, command)
180+
printProgressIfProgressEnabledAndMachineLogger(progressEnabled, ctx, progress)
187181
err := command.Run(context, ctx)
188182
if err != nil {
189-
return utils.WrapError(err)
183+
return i18n.WrapError(err)
190184
}
191185
progress += progressForEachCommand
192186
}
193187

194-
printProgressIfProgressEnabledAndMachineLogger(progressEnabled, context, 100)
188+
printProgressIfProgressEnabledAndMachineLogger(progressEnabled, ctx, 100)
195189

196190
return nil
197191
}
198192

199-
func printProgressIfProgressEnabledAndMachineLogger(progressEnabled bool, context map[string]interface{}, progress float32) {
193+
func printProgressIfProgressEnabledAndMachineLogger(progressEnabled bool, ctx *types.Context, progress float32) {
200194
if !progressEnabled {
201195
return
202196
}
203197

204-
log := utils.Logger(context)
198+
log := ctx.GetLogger()
205199
if log.Name() == "machine" {
206200
log.Println(constants.LOG_LEVEL_INFO, constants.MSG_PROGRESS, strconv.FormatFloat(float64(progress), 'f', 2, 32))
207201
}
208202
}
209203

210-
func PrintRingNameIfDebug(context map[string]interface{}, command types.Command) {
211-
if utils.DebugLevel(context) >= 10 {
212-
utils.Logger(context).Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_RUNNING_COMMAND, strconv.FormatInt(time.Now().Unix(), 10), reflect.Indirect(reflect.ValueOf(command)).Type().Name())
204+
func PrintRingNameIfDebug(ctx *types.Context, command types.Command) {
205+
if ctx.DebugLevel >= 10 {
206+
ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_RUNNING_COMMAND, strconv.FormatInt(time.Now().Unix(), 10), reflect.Indirect(reflect.ValueOf(command)).Type().Name())
213207
}
214208
}
215209

0 commit comments

Comments
 (0)