Web Receiver SDK 提供 CastDebugLogger API,讓開發人員能夠輕鬆對 Web Receiver 應用程式進行偵錯,以及透過隨附指令與控制 (CaC) 工具擷取記錄。
初始化
如要使用 CastDebugLogger API,請在 Web Receiver 應用程式中加入下列指令碼,緊接在 Web Receiver SDK 指令碼之後:
<!-- 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 Receiver 偵錯重疊事件上的記錄訊息。請使用以下記錄方法,由高到低依序列出:
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 Debug Loger 在網路接收器上提供偵錯重疊元素,以顯示自訂記錄訊息。使用 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();
}
});