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 中的注释以两个斜杠开头,一直持续到行尾。对于超出单行的注释,您需要在每行中包含//

// hello world
// let's make a couple of lines
// commented.

You can also place comments at the ends of lines containing code.

您还可以在包含代码的行的末尾放置注释。

fn main() {
    let baz = 8; // Eight is a lucky number
}

You can also do block comments 你也可以采用块注释

fn main() {
    /*
    You can write on multiple lines
    like this if you want
    */
    let baz = 8;
}

日志 (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 都会在收据列表中附加一个 receiptlog 可以生成两种类型的收据:LogLogData

fn log_values(){
  // Generates a Log receipt
  log(42);

  // Generates a LogData receipt
  let string = "sway";
  log(string);
}

日志收据 (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 类型生成的,即 boolu8u16u32u64。例如,使用 log(x) 记录一个包含值 42 的整数变量 x 可能会生成以下收据:

"Log": {
  "id": "0000000000000000000000000000000000000000000000000000000000000000",
  "is": 10352,
  "pc": 10404,
  "ra": 42,
  "rb": 1018205,
  "rc": 0,
  "rd": 0
}

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 时,附加寄存器 rcrd 将为零,而 rb 可能包含一个非零值,表示 log 实例的唯一 ID。唯一 ID 本身没有意义,但允许 Rust 和 TS SDK 通过在 JSON ABI 文件中查找日志 ID 来了解正在记录的数据类型。

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) 记录一个包含值 0x111111111111111111111111111111111111111111111111111111111111111b256 变量 b 可能会生成以下收据:

"LogData": {
  "data": "1111111111111111111111111111111111111111111111111111111111111111",
  "digest": "02d449a31fbb267c8f352e9968a79e3e5fc95c1bbeaa502fd6454ebde5a4bedc",
  "id": "0000000000000000000000000000000000000000000000000000000000000000",
  "is": 10352,
  "len": 32,
  "pc": 10444,
  "ptr": 10468,
  "ra": 0,
  "rb": 1018194
}

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