blob: 87e873d644adc51d110292a73c942de8a0b583fc [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
2# BitBake (No)TTY UI Implementation
3#
4# Handling output to TTYs or files (no TTY)
5#
6# Copyright (C) 2006-2012 Richard Purdie
7#
Brad Bishopc342db32019-05-15 21:57:59 -04008# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009#
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010
11from __future__ import division
12
13import os
14import sys
Patrick Williamsc124f4f2015-09-15 14:41:29 -050015import logging
16import progressbar
17import signal
18import bb.msg
19import time
20import fcntl
21import struct
22import copy
23import atexit
Patrick Williamsc0f7c042017-02-23 20:41:17 -060024
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025from bb.ui import uihelper
26
27featureSet = [bb.cooker.CookerFeatures.SEND_SANITYEVENTS]
28
29logger = logging.getLogger("BitBake")
30interactive = sys.stdout.isatty()
31
32class BBProgress(progressbar.ProgressBar):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060033 def __init__(self, msg, maxval, widgets=None, extrapos=-1, resize_handler=None):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050034 self.msg = msg
Patrick Williamsc0f7c042017-02-23 20:41:17 -060035 self.extrapos = extrapos
36 if not widgets:
Andrew Geissler82c905d2020-04-13 13:39:40 -050037 widgets = [': ', progressbar.Percentage(), ' ', progressbar.Bar(),
38 ' ', progressbar.ETA()]
39 self.extrapos = 5
Patrick Williamsc124f4f2015-09-15 14:41:29 -050040
Patrick Williamsc0f7c042017-02-23 20:41:17 -060041 if resize_handler:
42 self._resize_default = resize_handler
43 else:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044 self._resize_default = signal.getsignal(signal.SIGWINCH)
Andrew Geissler82c905d2020-04-13 13:39:40 -050045 progressbar.ProgressBar.__init__(self, maxval, [self.msg] + widgets, fd=sys.stdout)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046
Patrick Williamsc0f7c042017-02-23 20:41:17 -060047 def _handle_resize(self, signum=None, frame=None):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048 progressbar.ProgressBar._handle_resize(self, signum, frame)
49 if self._resize_default:
50 self._resize_default(signum, frame)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060051
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052 def finish(self):
53 progressbar.ProgressBar.finish(self)
54 if self._resize_default:
55 signal.signal(signal.SIGWINCH, self._resize_default)
56
Patrick Williamsc0f7c042017-02-23 20:41:17 -060057 def setmessage(self, msg):
58 self.msg = msg
59 self.widgets[0] = msg
60
61 def setextra(self, extra):
62 if self.extrapos > -1:
63 if extra:
64 extrastr = str(extra)
65 if extrastr[0] != ' ':
66 extrastr = ' ' + extrastr
Patrick Williamsc0f7c042017-02-23 20:41:17 -060067 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050068 extrastr = ''
Patrick Williamsc0f7c042017-02-23 20:41:17 -060069 self.widgets[self.extrapos] = extrastr
70
71 def _need_update(self):
72 # We always want the bar to print when update() is called
73 return True
74
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075class NonInteractiveProgress(object):
76 fobj = sys.stdout
77
78 def __init__(self, msg, maxval):
79 self.msg = msg
80 self.maxval = maxval
Patrick Williamsc0f7c042017-02-23 20:41:17 -060081 self.finished = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082
Patrick Williamsc0f7c042017-02-23 20:41:17 -060083 def start(self, update=True):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050084 self.fobj.write("%s..." % self.msg)
85 self.fobj.flush()
86 return self
87
88 def update(self, value):
89 pass
90
91 def finish(self):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060092 if self.finished:
93 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094 self.fobj.write("done.\n")
95 self.fobj.flush()
Patrick Williamsc0f7c042017-02-23 20:41:17 -060096 self.finished = True
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097
98def new_progress(msg, maxval):
99 if interactive:
100 return BBProgress(msg, maxval)
101 else:
102 return NonInteractiveProgress(msg, maxval)
103
104def pluralise(singular, plural, qty):
105 if(qty == 1):
106 return singular % qty
107 else:
108 return plural % qty
109
110
111class InteractConsoleLogFilter(logging.Filter):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500112 def __init__(self, tf):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113 self.tf = tf
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114
115 def filter(self, record):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500116 if record.levelno == bb.msg.BBLogFormatter.NOTE and (record.msg.startswith("Running") or record.msg.startswith("recipe ")):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500117 return False
118 self.tf.clearFooter()
119 return True
120
121class TerminalFilter(object):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500122 rows = 25
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500123 columns = 80
124
125 def sigwinch_handle(self, signum, frame):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500126 self.rows, self.columns = self.getTerminalColumns()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500127 if self._sigwinch_default:
128 self._sigwinch_default(signum, frame)
129
130 def getTerminalColumns(self):
131 def ioctl_GWINSZ(fd):
132 try:
133 cr = struct.unpack('hh', fcntl.ioctl(fd, self.termios.TIOCGWINSZ, '1234'))
134 except:
135 return None
136 return cr
137 cr = ioctl_GWINSZ(sys.stdout.fileno())
138 if not cr:
139 try:
140 fd = os.open(os.ctermid(), os.O_RDONLY)
141 cr = ioctl_GWINSZ(fd)
142 os.close(fd)
143 except:
144 pass
145 if not cr:
146 try:
147 cr = (env['LINES'], env['COLUMNS'])
148 except:
149 cr = (25, 80)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500150 return cr
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500151
Andrew Geissler82c905d2020-04-13 13:39:40 -0500152 def __init__(self, main, helper, handlers, quiet):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500153 self.main = main
154 self.helper = helper
155 self.cuu = None
156 self.stdinbackup = None
157 self.interactive = sys.stdout.isatty()
158 self.footer_present = False
159 self.lastpids = []
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600160 self.lasttime = None
161 self.quiet = quiet
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500162
163 if not self.interactive:
164 return
165
166 try:
167 import curses
168 except ImportError:
169 sys.exit("FATAL: The knotty ui could not load the required curses python module.")
170
171 import termios
172 self.curses = curses
173 self.termios = termios
174 try:
175 fd = sys.stdin.fileno()
176 self.stdinbackup = termios.tcgetattr(fd)
177 new = copy.deepcopy(self.stdinbackup)
178 new[3] = new[3] & ~termios.ECHO
179 termios.tcsetattr(fd, termios.TCSADRAIN, new)
180 curses.setupterm()
181 if curses.tigetnum("colors") > 2:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500182 for h in handlers:
183 try:
184 h.formatter.enable_color()
185 except AttributeError:
186 pass
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500187 self.ed = curses.tigetstr("ed")
188 if self.ed:
189 self.cuu = curses.tigetstr("cuu")
190 try:
191 self._sigwinch_default = signal.getsignal(signal.SIGWINCH)
192 signal.signal(signal.SIGWINCH, self.sigwinch_handle)
193 except:
194 pass
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500195 self.rows, self.columns = self.getTerminalColumns()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500196 except:
197 self.cuu = None
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500198 if not self.cuu:
199 self.interactive = False
200 bb.note("Unable to use interactive mode for this terminal, using fallback")
201 return
Andrew Geissler82c905d2020-04-13 13:39:40 -0500202
203 for h in handlers:
204 h.addFilter(InteractConsoleLogFilter(self))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500205
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600206 self.main_progress = None
207
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500208 def clearFooter(self):
209 if self.footer_present:
210 lines = self.footer_present
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600211 sys.stdout.buffer.write(self.curses.tparm(self.cuu, lines))
212 sys.stdout.buffer.write(self.curses.tparm(self.ed))
213 sys.stdout.flush()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500214 self.footer_present = False
215
Brad Bishopc342db32019-05-15 21:57:59 -0400216 def elapsed(self, sec):
217 hrs = int(sec / 3600.0)
218 sec -= hrs * 3600
219 min = int(sec / 60.0)
220 sec -= min * 60
221 if hrs > 0:
222 return "%dh%dm%ds" % (hrs, min, sec)
223 elif min > 0:
224 return "%dm%ds" % (min, sec)
225 else:
226 return "%ds" % (sec)
227
228 def keepAlive(self, t):
229 if not self.cuu:
230 print("Bitbake still alive (%ds)" % t)
231 sys.stdout.flush()
232
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500233 def updateFooter(self):
234 if not self.cuu:
235 return
236 activetasks = self.helper.running_tasks
237 failedtasks = self.helper.failed_tasks
238 runningpids = self.helper.running_pids
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600239 currenttime = time.time()
240 if not self.lasttime or (currenttime - self.lasttime > 5):
241 self.helper.needUpdate = True
242 self.lasttime = currenttime
243 if self.footer_present and not self.helper.needUpdate:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500244 return
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600245 self.helper.needUpdate = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500246 if self.footer_present:
247 self.clearFooter()
248 if (not self.helper.tasknumber_total or self.helper.tasknumber_current == self.helper.tasknumber_total) and not len(activetasks):
249 return
250 tasks = []
251 for t in runningpids:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600252 progress = activetasks[t].get("progress", None)
253 if progress is not None:
254 pbar = activetasks[t].get("progressbar", None)
255 rate = activetasks[t].get("rate", None)
256 start_time = activetasks[t].get("starttime", None)
257 if not pbar or pbar.bouncing != (progress < 0):
258 if progress < 0:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500259 pbar = BBProgress("0: %s (pid %s)" % (activetasks[t]["title"], activetasks[t]["pid"]), 100, widgets=[' ', progressbar.BouncingSlider(), ''], extrapos=3, resize_handler=self.sigwinch_handle)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600260 pbar.bouncing = True
261 else:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500262 pbar = BBProgress("0: %s (pid %s)" % (activetasks[t]["title"], activetasks[t]["pid"]), 100, widgets=[' ', progressbar.Percentage(), ' ', progressbar.Bar(), ''], extrapos=5, resize_handler=self.sigwinch_handle)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600263 pbar.bouncing = False
264 activetasks[t]["progressbar"] = pbar
265 tasks.append((pbar, progress, rate, start_time))
266 else:
267 start_time = activetasks[t].get("starttime", None)
268 if start_time:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500269 tasks.append("%s - %s (pid %s)" % (activetasks[t]["title"], self.elapsed(currenttime - start_time), activetasks[t]["pid"]))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600270 else:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500271 tasks.append("%s (pid %s)" % (activetasks[t]["title"], activetasks[t]["pid"]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500272
273 if self.main.shutdown:
274 content = "Waiting for %s running tasks to finish:" % len(activetasks)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500275 print(content)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600276 else:
277 if self.quiet:
278 content = "Running tasks (%s of %s)" % (self.helper.tasknumber_current, self.helper.tasknumber_total)
279 elif not len(activetasks):
280 content = "No currently running tasks (%s of %s)" % (self.helper.tasknumber_current, self.helper.tasknumber_total)
281 else:
282 content = "Currently %2s running tasks (%s of %s)" % (len(activetasks), self.helper.tasknumber_current, self.helper.tasknumber_total)
283 maxtask = self.helper.tasknumber_total
284 if not self.main_progress or self.main_progress.maxval != maxtask:
285 widgets = [' ', progressbar.Percentage(), ' ', progressbar.Bar()]
286 self.main_progress = BBProgress("Running tasks", maxtask, widgets=widgets, resize_handler=self.sigwinch_handle)
287 self.main_progress.start(False)
288 self.main_progress.setmessage(content)
289 progress = self.helper.tasknumber_current - 1
290 if progress < 0:
291 progress = 0
292 content = self.main_progress.update(progress)
293 print('')
294 lines = 1 + int(len(content) / (self.columns + 1))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500295 if self.quiet == 0:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600296 for tasknum, task in enumerate(tasks[:(self.rows - 2)]):
297 if isinstance(task, tuple):
298 pbar, progress, rate, start_time = task
299 if not pbar.start_time:
300 pbar.start(False)
301 if start_time:
302 pbar.start_time = start_time
303 pbar.setmessage('%s:%s' % (tasknum, pbar.msg.split(':', 1)[1]))
Brad Bishop15ae2502019-06-18 21:44:24 -0400304 pbar.setextra(rate)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600305 if progress > -1:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600306 content = pbar.update(progress)
307 else:
308 content = pbar.update(1)
309 print('')
310 else:
311 content = "%s: %s" % (tasknum, task)
312 print(content)
313 lines = lines + 1 + int(len(content) / (self.columns + 1))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500314 self.footer_present = lines
315 self.lastpids = runningpids[:]
316 self.lastcount = self.helper.tasknumber_current
317
318 def finish(self):
319 if self.stdinbackup:
320 fd = sys.stdin.fileno()
321 self.termios.tcsetattr(fd, self.termios.TCSADRAIN, self.stdinbackup)
322
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500323def print_event_log(event, includelogs, loglines, termfilter):
324 # FIXME refactor this out further
325 logfile = event.logfile
326 if logfile and os.path.exists(logfile):
327 termfilter.clearFooter()
328 bb.error("Logfile of failure stored in: %s" % logfile)
329 if includelogs and not event.errprinted:
330 print("Log data follows:")
331 f = open(logfile, "r")
332 lines = []
333 while True:
334 l = f.readline()
335 if l == '':
336 break
337 l = l.rstrip()
338 if loglines:
339 lines.append(' | %s' % l)
340 if len(lines) > int(loglines):
341 lines.pop(0)
342 else:
343 print('| %s' % l)
344 f.close()
345 if lines:
346 for line in lines:
347 print(line)
348
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500349def _log_settings_from_server(server, observe_only):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500350 # Get values of variables which control our output
351 includelogs, error = server.runCommand(["getVariable", "BBINCLUDELOGS"])
352 if error:
353 logger.error("Unable to get the value of BBINCLUDELOGS variable: %s" % error)
354 raise BaseException(error)
355 loglines, error = server.runCommand(["getVariable", "BBINCLUDELOGS_LINES"])
356 if error:
357 logger.error("Unable to get the value of BBINCLUDELOGS_LINES variable: %s" % error)
358 raise BaseException(error)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500359 if observe_only:
360 cmd = 'getVariable'
361 else:
362 cmd = 'getSetVariable'
363 consolelogfile, error = server.runCommand([cmd, "BB_CONSOLELOG"])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500364 if error:
365 logger.error("Unable to get the value of BB_CONSOLELOG variable: %s" % error)
366 raise BaseException(error)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500367 logconfigfile, error = server.runCommand([cmd, "BB_LOGCONFIG"])
368 if error:
369 logger.error("Unable to get the value of BB_LOGCONFIG variable: %s" % error)
370 raise BaseException(error)
371 return includelogs, loglines, consolelogfile, logconfigfile
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500372
373_evt_list = [ "bb.runqueue.runQueueExitWait", "bb.event.LogExecTTY", "logging.LogRecord",
374 "bb.build.TaskFailed", "bb.build.TaskBase", "bb.event.ParseStarted",
375 "bb.event.ParseProgress", "bb.event.ParseCompleted", "bb.event.CacheLoadStarted",
376 "bb.event.CacheLoadProgress", "bb.event.CacheLoadCompleted", "bb.command.CommandFailed",
377 "bb.command.CommandExit", "bb.command.CommandCompleted", "bb.cooker.CookerExit",
378 "bb.event.MultipleProviders", "bb.event.NoProvider", "bb.runqueue.sceneQueueTaskStarted",
379 "bb.runqueue.runQueueTaskStarted", "bb.runqueue.runQueueTaskFailed", "bb.runqueue.sceneQueueTaskFailed",
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600380 "bb.event.BuildBase", "bb.build.TaskStarted", "bb.build.TaskSucceeded", "bb.build.TaskFailedSilent",
381 "bb.build.TaskProgress", "bb.event.ProcessStarted", "bb.event.ProcessProgress", "bb.event.ProcessFinished"]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500382
383def main(server, eventHandler, params, tf = TerminalFilter):
384
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500385 if not params.observe_only:
386 params.updateToServer(server, os.environ.copy())
387
Andrew Geissler82c905d2020-04-13 13:39:40 -0500388 includelogs, loglines, consolelogfile, logconfigfile = _log_settings_from_server(server, params.observe_only)
389
390 loglevel, _ = bb.msg.constructLogOptions()
391
392 if params.options.quiet == 0:
393 console_loglevel = loglevel
394 elif params.options.quiet > 2:
395 console_loglevel = bb.msg.BBLogFormatter.ERROR
396 else:
397 console_loglevel = bb.msg.BBLogFormatter.WARNING
398
399 logconfig = {
400 "version": 1,
401 "handlers": {
402 "BitBake.console": {
403 "class": "logging.StreamHandler",
404 "formatter": "BitBake.consoleFormatter",
405 "level": console_loglevel,
406 "stream": "ext://sys.stdout",
407 "filters": ["BitBake.stdoutFilter"],
408 ".": {
409 "is_console": True,
410 },
411 },
412 "BitBake.errconsole": {
413 "class": "logging.StreamHandler",
414 "formatter": "BitBake.consoleFormatter",
415 "level": loglevel,
416 "stream": "ext://sys.stderr",
417 "filters": ["BitBake.stderrFilter"],
418 ".": {
419 "is_console": True,
420 },
421 },
422 # This handler can be used if specific loggers should print on
423 # the console at a lower severity than the default. It will
424 # display any messages sent to it that are lower than then
425 # BitBake.console logging level (so as to prevent duplication of
426 # messages). Nothing is attached to this handler by default
427 "BitBake.verbconsole": {
428 "class": "logging.StreamHandler",
429 "formatter": "BitBake.consoleFormatter",
430 "level": 1,
431 "stream": "ext://sys.stdout",
432 "filters": ["BitBake.verbconsoleFilter"],
433 ".": {
434 "is_console": True,
435 },
436 },
437 },
438 "formatters": {
439 # This format instance will get color output enabled by the
440 # terminal
441 "BitBake.consoleFormatter" : {
442 "()": "bb.msg.BBLogFormatter",
443 "format": "%(levelname)s: %(message)s"
444 },
445 # The file log requires a separate instance so that it doesn't get
446 # color enabled
447 "BitBake.logfileFormatter": {
448 "()": "bb.msg.BBLogFormatter",
449 "format": "%(levelname)s: %(message)s"
450 }
451 },
452 "filters": {
453 "BitBake.stdoutFilter": {
454 "()": "bb.msg.LogFilterLTLevel",
455 "level": "ERROR"
456 },
457 "BitBake.stderrFilter": {
458 "()": "bb.msg.LogFilterGEQLevel",
459 "level": "ERROR"
460 },
461 "BitBake.verbconsoleFilter": {
462 "()": "bb.msg.LogFilterLTLevel",
463 "level": console_loglevel
464 },
465 },
466 "loggers": {
467 "BitBake": {
468 "level": loglevel,
469 "handlers": ["BitBake.console", "BitBake.errconsole"],
470 }
471 },
472 "disable_existing_loggers": False
473 }
474
475 # Enable the console log file if enabled
476 if consolelogfile and not params.options.show_environment and not params.options.show_versions:
477 logconfig = bb.msg.mergeLoggingConfig(logconfig, {
478 "version": 1,
479 "handlers" : {
480 "BitBake.consolelog": {
481 "class": "logging.FileHandler",
482 "formatter": "BitBake.logfileFormatter",
483 "level": loglevel,
484 "filename": consolelogfile,
485 },
486 # Just like verbconsole, anything sent here will go to the
487 # log file, unless it would go to BitBake.consolelog
488 "BitBake.verbconsolelog" : {
489 "class": "logging.FileHandler",
490 "formatter": "BitBake.logfileFormatter",
491 "level": 1,
492 "filename": consolelogfile,
493 "filters": ["BitBake.verbconsolelogFilter"],
494 },
495 },
496 "filters": {
497 "BitBake.verbconsolelogFilter": {
498 "()": "bb.msg.LogFilterLTLevel",
499 "level": loglevel,
500 },
501 },
502 "loggers": {
503 "BitBake": {
504 "handlers": ["BitBake.consolelog"],
505 },
506
507 # Other interesting things that we want to keep an eye on
508 # in the log files in case someone has an issue, but not
509 # necessarily show to the user on the console
510 "BitBake.SigGen.HashEquiv": {
511 "level": "VERBOSE",
512 "handlers": ["BitBake.verbconsolelog"],
513 },
514 "BitBake.RunQueue.HashEquiv": {
515 "level": "VERBOSE",
516 "handlers": ["BitBake.verbconsolelog"],
517 }
518 }
519 })
520
521 bb.utils.mkdirhier(os.path.dirname(consolelogfile))
522 loglink = os.path.join(os.path.dirname(consolelogfile), 'console-latest.log')
523 bb.utils.remove(loglink)
524 try:
525 os.symlink(os.path.basename(consolelogfile), loglink)
526 except OSError:
527 pass
528
529 conf = bb.msg.setLoggingConfig(logconfig, logconfigfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500530
531 if sys.stdin.isatty() and sys.stdout.isatty():
532 log_exec_tty = True
533 else:
534 log_exec_tty = False
535
536 helper = uihelper.BBUIHelper()
537
Andrew Geissler82c905d2020-04-13 13:39:40 -0500538 # Look for the specially designated handlers which need to be passed to the
539 # terminal handler
540 console_handlers = [h for h in conf.config['handlers'].values() if getattr(h, 'is_console', False)]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500541
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500542 bb.utils.set_process_name("KnottyUI")
543
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500544 if params.options.remote_server and params.options.kill_server:
545 server.terminateServer()
546 return
547
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500548 llevel, debug_domains = bb.msg.constructLogOptions()
549 server.runCommand(["setEventMask", server.getEventHandle(), llevel, debug_domains, _evt_list])
550
Andrew Geissler82c905d2020-04-13 13:39:40 -0500551 # The logging_tree module is *extremely* helpful in debugging logging
552 # domains. Uncomment here to dump the logging tree when bitbake starts
553 #import logging_tree
554 #logging_tree.printout()
555
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500556 universe = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500557 if not params.observe_only:
558 params.updateFromServer(server)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500559 cmdline = params.parseActions()
560 if not cmdline:
561 print("Nothing to do. Use 'bitbake world' to build everything, or run 'bitbake --help' for usage information.")
562 return 1
563 if 'msg' in cmdline and cmdline['msg']:
564 logger.error(cmdline['msg'])
565 return 1
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500566 if cmdline['action'][0] == "buildTargets" and "universe" in cmdline['action'][1]:
567 universe = True
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500568
569 ret, error = server.runCommand(cmdline['action'])
570 if error:
571 logger.error("Command '%s' failed: %s" % (cmdline, error))
572 return 1
Andrew Geissler82c905d2020-04-13 13:39:40 -0500573 elif not ret:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500574 logger.error("Command '%s' failed: returned %s" % (cmdline, ret))
575 return 1
576
577
578 parseprogress = None
579 cacheprogress = None
580 main.shutdown = 0
581 interrupted = False
582 return_value = 0
583 errors = 0
584 warnings = 0
585 taskfailures = []
586
Brad Bishopc342db32019-05-15 21:57:59 -0400587 printinterval = 5000
588 lastprint = time.time()
589
Andrew Geissler82c905d2020-04-13 13:39:40 -0500590 termfilter = tf(main, helper, console_handlers, params.options.quiet)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500591 atexit.register(termfilter.finish)
592
593 while True:
594 try:
Brad Bishopc342db32019-05-15 21:57:59 -0400595 if (lastprint + printinterval) <= time.time():
596 termfilter.keepAlive(printinterval)
597 printinterval += 5000
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500598 event = eventHandler.waitEvent(0)
599 if event is None:
600 if main.shutdown > 1:
601 break
Andrew Geissler82c905d2020-04-13 13:39:40 -0500602 if not parseprogress:
603 termfilter.updateFooter()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500604 event = eventHandler.waitEvent(0.25)
605 if event is None:
606 continue
607 helper.eventHandler(event)
608 if isinstance(event, bb.runqueue.runQueueExitWait):
609 if not main.shutdown:
610 main.shutdown = 1
611 continue
612 if isinstance(event, bb.event.LogExecTTY):
613 if log_exec_tty:
614 tries = event.retries
615 while tries:
616 print("Trying to run: %s" % event.prog)
617 if os.system(event.prog) == 0:
618 break
619 time.sleep(event.sleep_delay)
620 tries -= 1
621 if tries:
622 continue
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600623 logger.warning(event.msg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500624 continue
625
626 if isinstance(event, logging.LogRecord):
Brad Bishopc342db32019-05-15 21:57:59 -0400627 lastprint = time.time()
628 printinterval = 5000
Andrew Geissler82c905d2020-04-13 13:39:40 -0500629 if event.levelno >= bb.msg.BBLogFormatter.ERROR:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500630 errors = errors + 1
631 return_value = 1
Andrew Geissler82c905d2020-04-13 13:39:40 -0500632 elif event.levelno == bb.msg.BBLogFormatter.WARNING:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500633 warnings = warnings + 1
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500634
635 if event.taskpid != 0:
636 # For "normal" logging conditions, don't show note logs from tasks
637 # but do show them if the user has changed the default log level to
638 # include verbose/debug messages
Andrew Geissler82c905d2020-04-13 13:39:40 -0500639 if event.levelno <= bb.msg.BBLogFormatter.NOTE and (event.levelno < llevel or (event.levelno == bb.msg.BBLogFormatter.NOTE and llevel != bb.msg.BBLogFormatter.VERBOSE)):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500640 continue
641
642 # Prefix task messages with recipe/task
Andrew Geissler82c905d2020-04-13 13:39:40 -0500643 if event.taskpid in helper.pidmap and event.levelno != bb.msg.BBLogFormatter.PLAIN:
644 taskinfo = helper.running_tasks[helper.pidmap[event.taskpid]]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500645 event.msg = taskinfo['title'] + ': ' + event.msg
646 if hasattr(event, 'fn'):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500647 event.msg = event.fn + ': ' + event.msg
Andrew Geissler82c905d2020-04-13 13:39:40 -0500648 logging.getLogger(event.name).handle(event)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500649 continue
650
651 if isinstance(event, bb.build.TaskFailedSilent):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600652 logger.warning("Logfile for failed setscene task is %s" % event.logfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500653 continue
654 if isinstance(event, bb.build.TaskFailed):
655 return_value = 1
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500656 print_event_log(event, includelogs, loglines, termfilter)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500657 if isinstance(event, bb.build.TaskBase):
658 logger.info(event._message)
659 continue
660 if isinstance(event, bb.event.ParseStarted):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500661 if params.options.quiet > 1:
662 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500663 if event.total == 0:
664 continue
Andrew Geissler82c905d2020-04-13 13:39:40 -0500665 termfilter.clearFooter()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500666 parseprogress = new_progress("Parsing recipes", event.total).start()
667 continue
668 if isinstance(event, bb.event.ParseProgress):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500669 if params.options.quiet > 1:
670 continue
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600671 if parseprogress:
672 parseprogress.update(event.current)
673 else:
674 bb.warn("Got ParseProgress event for parsing that never started?")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500675 continue
676 if isinstance(event, bb.event.ParseCompleted):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500677 if params.options.quiet > 1:
678 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500679 if not parseprogress:
680 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500681 parseprogress.finish()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600682 pasreprogress = None
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500683 if params.options.quiet == 0:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600684 print(("Parsing of %d .bb files complete (%d cached, %d parsed). %d targets, %d skipped, %d masked, %d errors."
685 % ( event.total, event.cached, event.parsed, event.virtuals, event.skipped, event.masked, event.errors)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500686 continue
687
688 if isinstance(event, bb.event.CacheLoadStarted):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500689 if params.options.quiet > 1:
690 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500691 cacheprogress = new_progress("Loading cache", event.total).start()
692 continue
693 if isinstance(event, bb.event.CacheLoadProgress):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500694 if params.options.quiet > 1:
695 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500696 cacheprogress.update(event.current)
697 continue
698 if isinstance(event, bb.event.CacheLoadCompleted):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500699 if params.options.quiet > 1:
700 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500701 cacheprogress.finish()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500702 if params.options.quiet == 0:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600703 print("Loaded %d entries from dependency cache." % event.num_entries)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500704 continue
705
706 if isinstance(event, bb.command.CommandFailed):
707 return_value = event.exitcode
708 if event.error:
709 errors = errors + 1
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500710 logger.error(str(event))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500711 main.shutdown = 2
712 continue
713 if isinstance(event, bb.command.CommandExit):
714 if not return_value:
715 return_value = event.exitcode
Andrew Geissler82c905d2020-04-13 13:39:40 -0500716 main.shutdown = 2
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500717 continue
718 if isinstance(event, (bb.command.CommandCompleted, bb.cooker.CookerExit)):
719 main.shutdown = 2
720 continue
721 if isinstance(event, bb.event.MultipleProviders):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500722 logger.info(str(event))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500723 continue
724 if isinstance(event, bb.event.NoProvider):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500725 # For universe builds, only show these as warnings, not errors
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500726 if not universe:
727 return_value = 1
728 errors = errors + 1
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500729 logger.error(str(event))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500730 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500731 logger.warning(str(event))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500732 continue
733
734 if isinstance(event, bb.runqueue.sceneQueueTaskStarted):
735 logger.info("Running setscene task %d of %d (%s)" % (event.stats.completed + event.stats.active + event.stats.failed + 1, event.stats.total, event.taskstring))
736 continue
737
738 if isinstance(event, bb.runqueue.runQueueTaskStarted):
739 if event.noexec:
740 tasktype = 'noexec task'
741 else:
742 tasktype = 'task'
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600743 logger.info("Running %s %d of %d (%s)",
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500744 tasktype,
745 event.stats.completed + event.stats.active +
746 event.stats.failed + 1,
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600747 event.stats.total, event.taskstring)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500748 continue
749
750 if isinstance(event, bb.runqueue.runQueueTaskFailed):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500751 return_value = 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500752 taskfailures.append(event.taskstring)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500753 logger.error(str(event))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500754 continue
755
756 if isinstance(event, bb.runqueue.sceneQueueTaskFailed):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500757 logger.warning(str(event))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500758 continue
759
760 if isinstance(event, bb.event.DepTreeGenerated):
761 continue
762
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600763 if isinstance(event, bb.event.ProcessStarted):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500764 if params.options.quiet > 1:
765 continue
Andrew Geissler82c905d2020-04-13 13:39:40 -0500766 termfilter.clearFooter()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600767 parseprogress = new_progress(event.processname, event.total)
768 parseprogress.start(False)
769 continue
770 if isinstance(event, bb.event.ProcessProgress):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500771 if params.options.quiet > 1:
772 continue
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600773 if parseprogress:
774 parseprogress.update(event.progress)
775 else:
776 bb.warn("Got ProcessProgress event for someting that never started?")
777 continue
778 if isinstance(event, bb.event.ProcessFinished):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500779 if params.options.quiet > 1:
780 continue
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600781 if parseprogress:
782 parseprogress.finish()
783 parseprogress = None
784 continue
785
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500786 # ignore
787 if isinstance(event, (bb.event.BuildBase,
788 bb.event.MetadataEvent,
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500789 bb.event.ConfigParsed,
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500790 bb.event.MultiConfigParsed,
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500791 bb.event.RecipeParsed,
792 bb.event.RecipePreFinalise,
793 bb.runqueue.runQueueEvent,
794 bb.event.OperationStarted,
795 bb.event.OperationCompleted,
796 bb.event.OperationProgress,
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600797 bb.event.DiskFull,
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500798 bb.event.HeartbeatEvent,
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600799 bb.build.TaskProgress)):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500800 continue
801
802 logger.error("Unknown event: %s", event)
803
804 except EnvironmentError as ioerror:
805 termfilter.clearFooter()
806 # ignore interrupted io
807 if ioerror.args[0] == 4:
808 continue
809 sys.stderr.write(str(ioerror))
810 if not params.observe_only:
811 _, error = server.runCommand(["stateForceShutdown"])
812 main.shutdown = 2
813 except KeyboardInterrupt:
814 termfilter.clearFooter()
815 if params.observe_only:
816 print("\nKeyboard Interrupt, exiting observer...")
817 main.shutdown = 2
Brad Bishop08902b02019-08-20 09:16:51 -0400818
819 def state_force_shutdown():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500820 print("\nSecond Keyboard Interrupt, stopping...\n")
821 _, error = server.runCommand(["stateForceShutdown"])
822 if error:
823 logger.error("Unable to cleanly stop: %s" % error)
Brad Bishop08902b02019-08-20 09:16:51 -0400824
825 if not params.observe_only and main.shutdown == 1:
826 state_force_shutdown()
827
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500828 if not params.observe_only and main.shutdown == 0:
829 print("\nKeyboard Interrupt, closing down...\n")
830 interrupted = True
Brad Bishop08902b02019-08-20 09:16:51 -0400831 # Capture the second KeyboardInterrupt during stateShutdown is running
832 try:
833 _, error = server.runCommand(["stateShutdown"])
834 if error:
835 logger.error("Unable to cleanly shutdown: %s" % error)
836 except KeyboardInterrupt:
837 state_force_shutdown()
838
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500839 main.shutdown = main.shutdown + 1
840 pass
841 except Exception as e:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500842 import traceback
843 sys.stderr.write(traceback.format_exc())
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500844 if not params.observe_only:
845 _, error = server.runCommand(["stateForceShutdown"])
846 main.shutdown = 2
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500847 return_value = 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500848 try:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600849 termfilter.clearFooter()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500850 summary = ""
851 if taskfailures:
852 summary += pluralise("\nSummary: %s task failed:",
853 "\nSummary: %s tasks failed:", len(taskfailures))
854 for failure in taskfailures:
855 summary += "\n %s" % failure
856 if warnings:
857 summary += pluralise("\nSummary: There was %s WARNING message shown.",
858 "\nSummary: There were %s WARNING messages shown.", warnings)
859 if return_value and errors:
860 summary += pluralise("\nSummary: There was %s ERROR message shown, returning a non-zero exit code.",
861 "\nSummary: There were %s ERROR messages shown, returning a non-zero exit code.", errors)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500862 if summary and params.options.quiet == 0:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500863 print(summary)
864
865 if interrupted:
866 print("Execution was interrupted, returning a non-zero exit code.")
867 if return_value == 0:
868 return_value = 1
869 except IOError as e:
870 import errno
871 if e.errno == errno.EPIPE:
872 pass
873
Andrew Geissler82c905d2020-04-13 13:39:40 -0500874 logging.shutdown()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600875
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500876 return return_value