Web Receiver SDK 提供 CastDebugLogger API 供开发者轻松调试其 Web Receiver 应用,并提供配套的命令和控制 (CaC) 工具以捕获日志。
初始化
如需使用 CastDebugLogger API,请在 Web Receiver SDK 脚本之后紧挨着 Web 接收器应用添加以下脚本:
<!-- Web Receiver SDK -->
<script src="//www.gstatic.com/cast/sdk/libs/caf_receiver/v3/cast_receiver_framework.js"></script>
<!-- Cast Debug Logger -->
<script src="//www.gstatic.com/cast/sdk/libs/devtools/debug_layer/caf_receiver_logger.js"></script>
创建 CastDebugLogger
对象并启用日志记录器:
const castDebugLogger = cast.debug.CastDebugLogger.getInstance();
const context = cast.framework.CastReceiverContext.getInstance();
context.addEventListener(cast.framework.system.EventType.READY, () => {
if (!castDebugLogger.debugOverlayElement_) {
// Enable debug logger and show a 'DEBUG MODE' overlay at top left corner.
castDebugLogger.setEnabled(true);
}
});
启用调试日志记录器后,接收器上会显示一个用于显示调试模式的叠加层。
记录播放器事件
使用 CastDebugLogger
,您可以轻松记录 Web Receiver SDK 触发的播放器事件,并使用不同的日志记录器级别来记录事件数据。loggerLevelByEvents
配置使用 cast.framework.events.EventType
和 cast.framework.events.category
来指定要记录的事件。
例如,如果您想了解播放器 CORE
事件何时触发或何时广播了 mediaStatus
更改,请使用以下配置来记录事件:
castDebugLogger.loggerLevelByEvents = {
'cast.framework.events.category.CORE': cast.framework.LoggerLevel.INFO,
'cast.framework.events.EventType.MEDIA_STATUS': cast.framework.LoggerLevel.DEBUG
}
使用自定义代码记录自定义消息
借助 CastDebugLogger API,您可以创建不同颜色的日志消息,显示在 Web 接收器调试叠加层上。使用以下日志方法(按优先级从高到低排序):
castDebugLogger.error(custom_tag, message);
castDebugLogger.warn(custom_tag, message);
castDebugLogger.info(custom_tag, message);
castDebugLogger.debug(custom_tag, message);
对于每种日志方法,第一个参数应为自定义标记,第二个参数为日志消息。标记可以是您认为有用的任何字符串。
以下示例展示了如何在 LOAD
拦截器中使用调试日志记录器。
const LOG_TAG = 'MyReceiverApp';
playerManager.setMessageInterceptor(
cast.framework.messages.MessageType.LOAD,
request => {
castDebugLogger.debug(LOG_TAG, 'Intercepting LOAD request');
return new Promise((resolve, reject) => {
fetchMediaAsset(request.media.contentId).then(
data => {
let item = data[request.media.contentId];
if (!item) {
castDebugLogger.error(LOG_TAG, 'Content not found');
reject();
} else {
request.media.contentUrl = item.stream.hls;
castDebugLogger.info(LOG_TAG,
'Playable URL:', request.media.contentUrl);
resolve(request);
}
}
);
});
}
);
通过在 loggerLevelByTags
中为每个自定义标记设置日志级别,您可以控制在调试叠加层上显示哪些消息。例如,启用日志级别为 cast.framework.LoggerLevel.DEBUG
的自定义标记将显示所有添加了错误、警告、信息和调试日志消息的消息。另一个示例是,启用具有 WARNING
级别的自定义标记只会显示错误和警告日志消息。
loggerLevelByTags
配置是可选的。如果未针对其日志记录器级别配置自定义标记,则所有日志消息都会显示在调试叠加层上。
const LOG_TAG1 = 'Tag1';
const LOG_TAG2 = 'Tag2';
// Set verbosity level for custom tags
castDebugLogger.loggerLevelByTags = {
[LOG_TAG1]: cast.framework.LoggerLevel.WARNING,
[LOG_TAG2]: cast.framework.LoggerLevel.DEBUG,
};
castDebugLogger.debug(LOG_TAG1, 'debug log from tag1');
castDebugLogger.info(LOG_TAG1, 'info log from tag1');
castDebugLogger.warn(LOG_TAG1, 'warn log from tag1');
castDebugLogger.error(LOG_TAG1, 'error log from tag1');
castDebugLogger.debug(LOG_TAG2, 'debug log from tag2');
castDebugLogger.info(LOG_TAG2, 'info log from tag2');
castDebugLogger.warn(LOG_TAG2, 'warn log from tag2');
castDebugLogger.error(LOG_TAG2, 'error log from tag2');
// example outputs:
// [Tag1] [WARN] warn log from tag1
// [Tag1] [ERROR] error log from tag1
// [Tag2] [DEBUG] debug log from tag2
// [Tag2] [INFO] info log from tag2
// [Tag2] [WARN] warn log from tag2
// [Tag2] [ERROR] error log from tag2
调试叠加层
Cast 调试日志记录器在网络接收器上提供调试叠加层,以便显示您的自定义日志消息。使用 showDebugLogs
可切换调试叠加层,使用 clearDebugLogs
可清除叠加层上的日志消息。
提醒:启用 castDebugLogger 后,请使用 showDebugLogs
和 clearDebugLogs
。
const context = cast.framework.CastReceiverContext.getInstance();
context.addEventListener(cast.framework.system.EventType.READY, () => {
if (!castDebugLogger.debugOverlayElement_) {
// Enable debug logger and show a 'DEBUG MODE' overlay at top left corner.
castDebugLogger.setEnabled(true);
// Show debug overlay
castDebugLogger.showDebugLogs(true);
// Clear log messages on debug overlay
castDebugLogger.clearDebugLogs();
}
});