4.8 注释和日志 (Comments and Logging)
注释 (Comments)
Comments in Sway start with two slashes and continue until the end of the line. For comments that extend beyond a single line, you'll need to include //
on each line.
Sway 中的注释以两个斜杠开头,一直持续到行尾。对于超出单行的注释,您需要在每行中包含//
。
You can also place comments at the ends of lines containing code.
您还可以在包含代码的行的末尾放置注释。
You can also do block comments 你也可以采用块注释
日志 (Logging)
The logging
library provides a generic log
function that can be imported using use std::logging::log
and used to log variables of any type. Each call to log
appends a receipt
to the list of receipts. There are two types of receipts that a log
can generate: Log
and LogData
.
logging
库提供了一个通用的 log
函数,可以使用 use std::logging::log
导入,并日志任何类型的变量。每次调用 log
都会在收据列表中附加一个 receipt
。 log
可以生成两种类型的收据:Log
和 LogData
。
日志
收据 (Log
Receipt)
日志
收据 (Log
Receipt)The Log
receipt is generated for non-reference types, namely bool
, u8
, u16
, u32
, and u64
. For example, logging an integer variable x
that holds the value 42
using log(x)
may generate the following receipt:
Log
收据是为 non-reference 类型生成的,即 bool
、u8
、u16
、u32
和 u64
。例如,使用 log(x)
记录一个包含值 42
的整数变量 x
可能会生成以下收据:
Note that ra
will include the value being logged. The additional registers rc
and rd
will be zero when using log
while rb
may include a non-zero value representing a unique ID for the log
instance. The unique ID is not meaningful on its own but allows the Rust and the TS SDKs to know the type of the data being logged, by looking up the log ID in the JSON ABI file.
请注意,ra
将包含记录的值。使用 log
时,附加寄存器 rc
和 rd
将为零,而 rb
可能包含一个非零值,表示 log
实例的唯一 ID。唯一 ID 本身没有意义,但允许 Rust 和 TS SDK 通过在 JSON ABI 文件中查找日志 ID 来了解正在记录的数据类型。
LogData
收据 (LogData
Receipt)
LogData
收据 (LogData
Receipt)LogData
is generated for reference types which include all types except for the non_reference types mentioned above. For example, logging a b256
variable b
that holds the value 0x1111111111111111111111111111111111111111111111111111111111111111
using log(b)
may generate the following receipt:
LogData
是为 reference 类型生成的,其中包括除上述 non_reference 之外的所有类型。例如,使用 log(b)
记录一个包含值 0x111111111111111111111111111111111111111111111111111111111111111
的 b256
变量 b
可能会生成以下收据:
Note that data
in the receipt above will include the value being logged as a hexadecimal. Similarly to the Log
receipt, additional registers are written: ra
will always be zero when using log
, while rb
will contain a unique ID for the log
instance.
请注意,上面收据中的data 数据
将包含以十六进制形式记录的值。与 Log
收据类似,写入额外的寄存器:ra
在使用 log
时始终为零,而 rb
将包含 log
实例的唯一 ID。
Note The Rust SDK exposes APIs that allow you to retrieve the logged values and display them nicely based on their types as indicated in the JSON ABI file. 注意 Rust SDK 公开了 API,允许您检索记录的值,并很好地显示它们—基于 JSON ABI 文件中指示的类型。
Last updated