Skip to content

Commit 79660a5

Browse files
authored
Integrate Ui with logger. (#82)
1 parent 1090845 commit 79660a5

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

src/cli.toit

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by an MIT-style license that can be
33
// found in the package's LICENSE file.
44
5+
import log
56
import uuid show Uuid
67
import system
78

@@ -296,9 +297,13 @@ class Command:
296297
The $add-ui-help flag is used to determine whether to include help for `--verbose`, ...
297298
in the help output. By default it is active if no $cli is provided.
298299
*/
299-
run arguments/List --invoked-command=system.program-name --cli/Cli?=null --add-ui-help/bool=(not cli) -> none:
300+
run arguments/List -> none
301+
--invoked-command=system.program-name
302+
--cli/Cli?=null
303+
--add-ui-help/bool=(not cli):
300304
if not cli:
301305
ui := create-ui-from-args_ arguments
306+
log.set-default (ui.logger --name=name)
302307
if add-ui-help:
303308
add-ui-options_
304309
cli = Cli_ name --ui=ui --cache=null --config=null

src/ui.toit

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
// found in the package's LICENSE file.
44
55
import encoding.json
6+
import io
7+
import log
8+
import log as log-lib
69

7-
create-ui-from-args_ args/List:
10+
create-ui-from-args_ args/List -> Ui:
811
verbose-level/string? := null
912
output-format/string? := null
1013

@@ -288,6 +291,23 @@ class Ui:
288291
constructor.from-args args/List:
289292
return create-ui-from-args_ args
290293

294+
/**
295+
Returns the log-level (like $log.DEBUG-LEVEL) of this instance.
296+
*/
297+
log-level -> int:
298+
if level == Ui.SILENT-LEVEL: return log.FATAL-LEVEL
299+
else if level == Ui.QUIET-LEVEL: return log.ERROR-LEVEL
300+
else if level == Ui.NORMAL-LEVEL: return log.WARN-LEVEL
301+
else if level == Ui.VERBOSE-LEVEL: return log.INFO-LEVEL
302+
else: return log.DEBUG-LEVEL
303+
304+
/**
305+
Creates a logger that logs to this UI instance.
306+
*/
307+
logger --name/string?=null -> log.Logger:
308+
target := UiLogTarget this
309+
return log.Logger log-level target --name=name
310+
291311
/**
292312
Emits the given $object using the $INFO kind.
293313
@@ -740,3 +760,49 @@ class JsonPrinter extends HumanPrinterBase:
740760

741761
emit-structured --kind/int object/any:
742762
print (json.stringify object)
763+
764+
/**
765+
A log target that emits log messages to a Ui instance.
766+
*/
767+
class UiLogTarget implements log.Target:
768+
ui_/Ui
769+
log-level_/int
770+
771+
constructor .ui_:
772+
log-level_ = ui_.log-level
773+
774+
log level/int message/string names/List? keys/List? values/List? -> none:
775+
full-message := message
776+
buffer := io.Buffer.with-capacity 64
777+
778+
if names and names.size > 0:
779+
buffer.write "["
780+
names.size.repeat:
781+
if it > 0: buffer.write "."
782+
buffer.write names[it]
783+
buffer.write "] "
784+
785+
buffer.write ": "
786+
buffer.write message
787+
788+
if keys and keys.size > 0:
789+
buffer.write " {"
790+
keys.size.repeat:
791+
if it > 0: buffer.write ", "
792+
buffer.write keys[it]
793+
buffer.write ": "
794+
buffer.write values[it]
795+
buffer.write "}"
796+
797+
constructed ::= buffer.to-string
798+
799+
if level == log-lib.FATAL-LEVEL:
800+
ui_.emit --error constructed
801+
else if level == log-lib.ERROR-LEVEL:
802+
ui_.emit --error constructed
803+
else if level == log-lib.WARN-LEVEL:
804+
ui_.emit --warning constructed
805+
else if level == log-lib.INFO-LEVEL:
806+
ui_.emit --info constructed
807+
else:
808+
ui_.emit --debug constructed

0 commit comments

Comments
 (0)