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
59 changes: 50 additions & 9 deletions cndocs/debugging-native-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,70 @@ import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import con
<p>以下部分仅适用于包含原生代码的项目。如果你使用的是托管型 Expo 工作流,请参阅 <a href="https://docs.expo.dev/workflow/prebuild/" target="_blank">prebuild</a> 指南以使用此 API。</p>
</div>

## 打印日志
## 查看日志

当应用正在运行时,你可以在终端中使用以下命令打印 iOS 或 Android 应用的日志
当应用正在运行时,你可以在终端中使用以下命令查看 iOS 或 Android 应用的原生日志

```shell
# For Android:
# Android
npx react-native log-android
# Or, for iOS:
# iOS
npx react-native log-ios
```

你也可以通过 iOS 模拟器中的 "Debug > Open System Log..." 菜单,或者在运行 Android 应用的设备或模拟器中运行 `adb logcat "*:S" ReactNative:V ReactNativeJS:V` 来访问这些日志。
你也可以通过 iOS 模拟器中的 Debug > Open System Log… 菜单,或者在运行 Android 应用的设备或模拟器中执行 `adb logcat "*:S" ReactNative:V ReactNativeJS:V` 来查看这些日志。

<details>
<summary>**💡 自定义原生日志**</summary>

如果你正在编写原生模块,并且想为你的模块添加自定义日志用于调试,可以使用以下方法:

#### Android(Java/Kotlin)

在原生模块中,使用 `Log` 类添加可在 Logcat 中查看的日志:

```java
import android.util.Log;

private void log(String message) {
Log.d("YourModuleName", message);
}
```

要在 Logcat 中查看这些日志,请使用以下命令,将 `YourModuleName` 替换为你的自定义标签:

```shell
adb logcat "*:S" ReactNative:V ReactNativeJS:V YourModuleName:D
```

#### iOS(Objective-C/Swift)

在原生模块中,使用 `NSLog` 添加自定义日志:

```objective-c
NSLog(@"YourModuleName: %@", message);
```

或者在 Swift 中:

```swift
print("YourModuleName: \(message)")
```
Comment on lines +31 to +61
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Heading levels skip h3 inside the <details> block (MD001).

Lines 31 and 49 jump directly from h2 (## 查看日志) to h4, skipping h3. The markdownlint tool flags this. Downgrading to h3 keeps the hierarchy valid and fixes the warning.

🔧 Proposed fix
-#### Android(Java/Kotlin)
+### Android(Java/Kotlin)
-#### iOS(Objective-C/Swift)
+### iOS(Objective-C/Swift)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#### Android(Java/Kotlin)
在原生模块中,使用 `Log` 类添加可在 Logcat 中查看的日志:
```java
import android.util.Log;
private void log(String message) {
Log.d("YourModuleName", message);
}
```
要在 Logcat 中查看这些日志,请使用以下命令,将 `YourModuleName` 替换为你的自定义标签:
```shell
adb logcat "*:S" ReactNative:V ReactNativeJS:V YourModuleName:D
```
#### iOS(Objective-C/Swift)
在原生模块中,使用 `NSLog` 添加自定义日志:
```objective-c
NSLog(@"YourModuleName: %@", message);
```
或者在 Swift 中:
```swift
print("YourModuleName: \(message)")
```
### Android(Java/Kotlin)
在原生模块中,使用 `Log` 类添加可在 Logcat 中查看的日志:
🧰 Tools
🪛 markdownlint-cli2 (0.21.0)

[warning] 31-31: Heading levels should only increment by one level at a time
Expected: h3; Actual: h4

(MD001, heading-increment)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cndocs/debugging-native-code.md` around lines 31 - 61, The section headings
"#### Android(Java/Kotlin)" and "#### iOS(Objective-C/Swift)" under the main "##
查看日志" heading should be demoted to h3 to maintain proper heading hierarchy;
change those two "####" headings to "###" so they no longer skip h3 and the
markdownlint MD001 warning is resolved, leaving the contained code blocks and
commands unchanged.


运行应用时,这些日志将出现在 Xcode 控制台中。

</details>

## 在原生 IDE 中调试

当你编写原生模块时,你可以使用 Android Studio 或 Xcode 来启动应用,并使用其原生调试功能(设置断点等),就像在构建标准原生应用时一样
当你编写原生代码(如原生模块)时,可以从 Android Studio 或 Xcode 启动应用,并利用其原生调试功能(设置断点等),就像构建标准原生应用一样

另一种选择是使用 React Native CLI 运行应用,并从原生 IDE(Android Studio 或 Xcode)中附加到进程
另一种选择是使用 React Native CLI 运行应用,然后将原生 IDE(Android Studio 或 Xcode)的原生调试器附加到进程上

### Android Studio

在 Android Studio 中,你可以通过点击菜单栏中的 "Run" 选项,然后选择 "Attach to Process...",并选择正在运行的 React Native 应用。
在 Android Studio 中,点击菜单栏上的"Run"选项,选择"Attach to Process...",然后选择正在运行的 React Native 应用。

### Xcode

在 Xcode 中,点击菜单栏中的 "Debug",选择 "Attach to Process...",然后从 "Likely Targets" 列表中选择正在运行的应用
在 Xcode 中,点击顶部菜单栏的"Debug",选择"Attach to process"选项,然后从"Likely Targets"列表中选择你的应用
56 changes: 52 additions & 4 deletions cndocs/global-PerformanceObserver.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,56 @@ id: global-PerformanceObserver
title: PerformanceObserver
---

:::warning
🚧 本页面仍在完善中,请参考 [MDN 文档](https://developer.mozilla.org/zh-CN/docs/Web/API/PerformanceObserver) 获取更多信息。
:::
全局 [`PerformanceObserver`](https://developer.mozilla.org/zh-CN/docs/Web/API/PerformanceObserver) 类,按照 Web 规范定义。

全局 `PerformanceObserver` 类,按照 Web 规范定义。
## 示例

```ts
const observer = new PerformanceObserver(
(list, observer, options) => {
for (const entry of list.getEntries()) {
console.log(
'Received entry with type',
entry.entryType,
'and name',
entry.name,
'that started at',
entry.startTime,
'and took',
entry.duration,
'ms',
);
}
},
);

observer.observe({entryTypes: ['mark', 'measure']});
```

---

# 参考

## 构造函数

### `PerformanceObserver()`

参见 [MDN 文档](https://developer.mozilla.org/zh-CN/docs/Web/API/PerformanceObserver/PerformanceObserver)。

## 静态属性

### `supportedEntryTypes`

参见 [MDN 文档](https://developer.mozilla.org/zh-CN/docs/Web/API/PerformanceObserver/supportedEntryTypes)。

返回 `['mark', 'measure', 'event', 'longtask', 'resource']`。

## 实例方法

### `observe()`

参见 [MDN 文档](https://developer.mozilla.org/zh-CN/docs/Web/API/PerformanceObserver/observe)。

### `disconnect()`

参见 [MDN 文档](https://developer.mozilla.org/zh-CN/docs/Web/API/PerformanceObserver/disconnect)。
82 changes: 79 additions & 3 deletions cndocs/global-performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,84 @@ id: global-performance
title: performance
---

:::warning
🚧 本页面仍在完善中,请参考 [MDN 文档](https://developer.mozilla.org/zh-CN/docs/Web/API/Performance) 获取更多信息。
全局 [`performance`](https://developer.mozilla.org/zh-CN/docs/Web/API/Window/performance) 对象,按照 Web 规范定义。

---

# 参考

## 实例属性

### `eventCounts`

参见 [MDN 文档](https://developer.mozilla.org/zh-CN/docs/Web/API/Performance/eventCounts)。

### `memory`

参见 [MDN 文档](https://developer.mozilla.org/zh-CN/docs/Web/API/Performance/memory)。

### `rnStartupTiming` ⚠️

:::warning 非标准
这是 React Native 特有的扩展。
:::

提供应用启动时间的相关信息。

```ts
get rnStartupTiming(): ReactNativeStartupTiming;
```

`ReactNativeStartupTiming` 接口提供以下字段:

| 名称 | 类型 | 描述 |
| ---------------------------------------- | -------------- | ---------------------------------------------- |
| `startTime` | number \| void | React Native 运行时初始化开始的时间。 |
| `executeJavaScriptBundleEntryPointStart` | number \| void | 应用 bundle 开始执行的时间。 |
| `endTime` | number \| void | React Native 运行时完全初始化完成的时间。 |

### `timeOrigin`

:::warning 部分支持
提供的是从 UNIX 纪元到系统启动的毫秒数,而非从 UNIX 纪元到应用启动的毫秒数。
:::

参见 [MDN 文档](https://developer.mozilla.org/zh-CN/docs/Web/API/Performance/timeOrigin)。

## 实例方法

### `clearMarks()`

参见 [MDN 文档](https://developer.mozilla.org/zh-CN/docs/Web/API/Performance/clearMarks)。

### `clearMeasures()`

参见 [MDN 文档](https://developer.mozilla.org/zh-CN/docs/Web/API/Performance/clearMeasures)。

### `getEntries()`

参见 [MDN 文档](https://developer.mozilla.org/zh-CN/docs/Web/API/Performance/getEntries)。

### `getEntriesByName()`

参见 [MDN 文档](https://developer.mozilla.org/zh-CN/docs/Web/API/Performance/getEntriesByName)。

### `getEntriesByType()`

参见 [MDN 文档](https://developer.mozilla.org/zh-CN/docs/Web/API/Performance/getEntriesByType)。

### `mark()`

参见 [MDN 文档](https://developer.mozilla.org/zh-CN/docs/Web/API/Performance/mark)。

### `measure()`

参见 [MDN 文档](https://developer.mozilla.org/zh-CN/docs/Web/API/Performance/measure)。

### `now()`

:::warning 部分支持
提供的是从系统启动开始的毫秒数,而非从应用启动开始的毫秒数。
:::

全局 `performance` 对象,按照 Web 规范定义
参见 [MDN 文档](https://developer.mozilla.org/zh-CN/docs/Web/API/Performance/now)
Loading