|
193 | 193 |
|
194 | 194 | docs_url = "https://arduino-esp8266.readthedocs.io/en/latest/faq/a06-global-build-options.html"
|
195 | 195 |
|
| 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 | + |
196 | 213 | 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' |
198 | 221 |
|
199 | 222 |
|
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. |
203 | 223 | # Bring attention to errors with a blank line and lines starting with "*** ".
|
204 |
| -# Let multiple prints buffer to aid them in staying together. |
205 | 224 | def print_err(*args, **kwargs):
|
| 225 | + global err_print_flag |
206 | 226 | 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) |
210 | 248 |
|
211 | 249 |
|
212 | 250 | 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):
|
258 | 296 | build_opt = open(build_opt_fqfn, 'w')
|
259 | 297 | if not os.path.exists(globals_h_fqfn) or (0 == os.path.getsize(globals_h_fqfn)):
|
260 | 298 | build_opt.close()
|
261 |
| - return |
| 299 | + return False |
262 | 300 |
|
263 | 301 | complete_comment = False
|
264 | 302 | build_opt_error = False
|
@@ -315,8 +353,8 @@ def extract_create_build_opt_file(globals_h_fqfn, file_name, build_opt_fqfn):
|
315 | 353 | print_err(" Extraction failed")
|
316 | 354 | # Don't let the failure get hidden by a spew of nonsensical error
|
317 | 355 | # 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 |
320 | 358 | elif complete_comment:
|
321 | 359 | print_msg(" Created compiler command-line options file " + build_opt_fqfn)
|
322 | 360 | build_opt.close()
|
@@ -475,9 +513,6 @@ def main():
|
475 | 513 |
|
476 | 514 | if os.path.exists(source_globals_h_fqfn):
|
477 | 515 | 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) |
481 | 516 |
|
482 | 517 | copy_create_build_file(source_globals_h_fqfn, globals_h_fqfn)
|
483 | 518 |
|
@@ -535,10 +570,16 @@ def main():
|
535 | 570 | if not embedded_options:
|
536 | 571 | print_msg("Tip: Embedd compiler command-line options in a block comment starting with '" + build_opt_signature + "'.")
|
537 | 572 | 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) |
538 | 576 |
|
539 | 577 | else:
|
540 | 578 | print_err("Too few arguments. Add arguments:")
|
541 | 579 | 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 |
542 | 583 |
|
543 | 584 | if __name__ == '__main__':
|
544 | 585 | sys.exit(main())
|
0 commit comments