Skip to content

Commit 9d6c1cb

Browse files
committed
Improve handling stderr/stdout with "no verbose output"
Grouped helpful info to print at the end. Added missing return value.
1 parent 2c45e71 commit 9d6c1cb

File tree

1 file changed

+55
-14
lines changed

1 file changed

+55
-14
lines changed

tools/mkbuildoptglobals.py

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -193,20 +193,58 @@
193193

194194
docs_url = "https://arduino-esp8266.readthedocs.io/en/latest/faq/a06-global-build-options.html"
195195

196+
197+
err_print_flag = False
198+
msg_print_buf = ""
199+
200+
# Issues trying to address through buffered printing
201+
# 1. Arduino IDE 2.0 RC5 does not show stderr text in color. Text printed does
202+
# not stand out from stdout messages.
203+
# 2. Separate pipes, buffering, and multiple threads with output can create
204+
# mixed-up messages. "flush" helped but did not resolve. The Arduino IDE 2.0
205+
# somehow makes the problem worse.
206+
# 3. With Arduino IDE preferences set for "no verbose output", you only see
207+
# stderr messages. Prior related prints are missing.
208+
#
209+
# Locally buffer and merge both stdout and stderr prints. This allows us to
210+
# print a complete context when there is an error. When any buffered prints
211+
# are targeted to stderr, print the whole buffer to stderr.
212+
196213
def print_msg(*args, **kwargs):
197-
print(*args, **kwargs)
214+
global msg_print_buf
215+
# At this time, we can only handle one args value.
216+
msg_print_buf += args[0]
217+
if 'end' in kwargs:
218+
msg_print_buf += kwargs['end']
219+
else:
220+
msg_print_buf += '\n'
198221

199222

200-
# I prefer error messages to stand out; however, using stderr for a different
201-
# color does not work on the new Arduino IDE 2.0 RC4. Also, separate pipes,
202-
# buffering, and multiple threads with output can create mixed-up messages.
203223
# Bring attention to errors with a blank line and lines starting with "*** ".
204-
# Let multiple prints buffer to aid them in staying together.
205224
def print_err(*args, **kwargs):
225+
global err_print_flag
206226
if (args[0])[0] != ' ':
207-
print("")
208-
print("*** ", end='')
209-
print(*args, **kwargs)
227+
print_msg("")
228+
print_msg("*** ", end='')
229+
print_msg(*args, **kwargs)
230+
err_print_flag = True
231+
232+
233+
def handle_error(err_no):
234+
# on err_no 0, commit print buffer to stderr or stdout
235+
# on err_no != 0, commit print buffer to stderr and sys exist with err_no
236+
global msg_print_buf
237+
global err_print_flag
238+
if len(msg_print_buf):
239+
if err_no or err_print_flag:
240+
fd = sys.stderr
241+
else:
242+
fd = sys.stdout
243+
print(msg_print_buf, file=fd, end='', flush=True)
244+
msg_print_buf = ""
245+
err_print_flag = False
246+
if err_no:
247+
sys.exit(err_no)
210248

211249

212250
def copy_create_build_file(source_fqfn, build_target_fqfn):
@@ -258,7 +296,7 @@ def extract_create_build_opt_file(globals_h_fqfn, file_name, build_opt_fqfn):
258296
build_opt = open(build_opt_fqfn, 'w')
259297
if not os.path.exists(globals_h_fqfn) or (0 == os.path.getsize(globals_h_fqfn)):
260298
build_opt.close()
261-
return
299+
return False
262300

263301
complete_comment = False
264302
build_opt_error = False
@@ -315,8 +353,8 @@ def extract_create_build_opt_file(globals_h_fqfn, file_name, build_opt_fqfn):
315353
print_err(" Extraction failed")
316354
# Don't let the failure get hidden by a spew of nonsensical error
317355
# messages that will follow. Bring things to a halt.
318-
sys.exit(1)
319-
return False
356+
handle_error(1)
357+
return False # not reached
320358
elif complete_comment:
321359
print_msg(" Created compiler command-line options file " + build_opt_fqfn)
322360
build_opt.close()
@@ -475,9 +513,6 @@ def main():
475513

476514
if os.path.exists(source_globals_h_fqfn):
477515
print_msg("Using global include from " + source_globals_h_fqfn)
478-
else:
479-
print_msg("Note: optional global include file '" + source_globals_h_fqfn + "' does not exist.")
480-
print_msg(" Read more at " + docs_url)
481516

482517
copy_create_build_file(source_globals_h_fqfn, globals_h_fqfn)
483518

@@ -535,10 +570,16 @@ def main():
535570
if not embedded_options:
536571
print_msg("Tip: Embedd compiler command-line options in a block comment starting with '" + build_opt_signature + "'.")
537572
print_msg(" Read more at " + docs_url)
573+
else:
574+
print_msg("Note: optional global include file '" + source_globals_h_fqfn + "' does not exist.")
575+
print_msg(" Read more at " + docs_url)
538576

539577
else:
540578
print_err("Too few arguments. Add arguments:")
541579
print_err(" Runtime IDE path, Build path, Build FQFN build.opt, Source FQFN Sketch.ino.globals.h, Core Source FQFN CommonHFile.h")
580+
handle_error(1)
581+
582+
handle_error(0) # commit print buffer
542583

543584
if __name__ == '__main__':
544585
sys.exit(main())

0 commit comments

Comments
 (0)