Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/cli.toit
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by an MIT-style license that can be
// found in the package's LICENSE file.

import log
import uuid show Uuid
import system

Expand Down Expand Up @@ -296,9 +297,13 @@ class Command:
The $add-ui-help flag is used to determine whether to include help for `--verbose`, ...
in the help output. By default it is active if no $cli is provided.
*/
run arguments/List --invoked-command=system.program-name --cli/Cli?=null --add-ui-help/bool=(not cli) -> none:
run arguments/List -> none
--invoked-command=system.program-name
--cli/Cli?=null
--add-ui-help/bool=(not cli):
if not cli:
ui := create-ui-from-args_ arguments
log.set-default (ui.logger --name=name)
if add-ui-help:
add-ui-options_
cli = Cli_ name --ui=ui --cache=null --config=null
Expand Down
68 changes: 67 additions & 1 deletion src/ui.toit
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
// found in the package's LICENSE file.

import encoding.json
import io
import log
import log as log-lib

create-ui-from-args_ args/List:
create-ui-from-args_ args/List -> Ui:
verbose-level/string? := null
output-format/string? := null

Expand Down Expand Up @@ -288,6 +291,23 @@ class Ui:
constructor.from-args args/List:
return create-ui-from-args_ args

/**
Returns the log-level (like $log.DEBUG-LEVEL) of this instance.
*/
log-level -> int:
if level == Ui.SILENT-LEVEL: return log.FATAL-LEVEL
else if level == Ui.QUIET-LEVEL: return log.ERROR-LEVEL
else if level == Ui.NORMAL-LEVEL: return log.WARN-LEVEL
else if level == Ui.VERBOSE-LEVEL: return log.INFO-LEVEL
else: return log.DEBUG-LEVEL

/**
Creates a logger that logs to this UI instance.
*/
logger --name/string?=null -> log.Logger:
target := UiLogTarget this
return log.Logger log-level target --name=name

/**
Emits the given $object using the $INFO kind.

Expand Down Expand Up @@ -740,3 +760,49 @@ class JsonPrinter extends HumanPrinterBase:

emit-structured --kind/int object/any:
print (json.stringify object)

/**
A log target that emits log messages to a Ui instance.
*/
class UiLogTarget implements log.Target:
ui_/Ui
log-level_/int

constructor .ui_:
log-level_ = ui_.log-level

log level/int message/string names/List? keys/List? values/List? -> none:
full-message := message
buffer := io.Buffer.with-capacity 64

if names and names.size > 0:
buffer.write "["
names.size.repeat:
if it > 0: buffer.write "."
buffer.write names[it]
buffer.write "] "

buffer.write ": "
buffer.write message

if keys and keys.size > 0:
buffer.write " {"
keys.size.repeat:
if it > 0: buffer.write ", "
buffer.write keys[it]
buffer.write ": "
buffer.write values[it]
buffer.write "}"

constructed ::= buffer.to-string

if level == log-lib.FATAL-LEVEL:
ui_.emit --error constructed
else if level == log-lib.ERROR-LEVEL:
ui_.emit --error constructed
else if level == log-lib.WARN-LEVEL:
ui_.emit --warning constructed
else if level == log-lib.INFO-LEVEL:
ui_.emit --info constructed
else:
ui_.emit --debug constructed