Logger Debug Cast

Web Receiver SDK menyediakan CastDebugLogger API bagi developer untuk memudahkan proses debug aplikasi Web Receiver dan pendamping Command and Control (CaC) Tool untuk mengambil log.

Inisialisasi

Untuk menggunakan CastDebugLogger API, sertakan skrip berikut di aplikasi Web Receiver tepat setelah skrip 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>

Buat objek CastDebugLogger dan aktifkan pencatat log:

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);
  }
});

Saat logger debug diaktifkan, overlay yang menampilkan MODE DEBUG akan ditampilkan di penerima.

Membuat Log Peristiwa Pemain

Dengan CastDebugLogger, Anda dapat dengan mudah mencatat peristiwa pemutar yang diaktifkan oleh Web Receiver SDK dan menggunakan berbagai level logger untuk mencatat data peristiwa ke dalam log. Konfigurasi loggerLevelByEvents memerlukan cast.framework.events.EventType dan cast.framework.events.category untuk menentukan peristiwa yang akan dicatat ke dalam log.

Misalnya, jika Anda ingin mengetahui kapan peristiwa pemain CORE dipicu atau perubahan mediaStatus disiarkan, gunakan konfigurasi berikut untuk mencatat peristiwa ke dalam log:

castDebugLogger.loggerLevelByEvents = {
  'cast.framework.events.category.CORE': cast.framework.LoggerLevel.INFO,
  'cast.framework.events.EventType.MEDIA_STATUS': cast.framework.LoggerLevel.DEBUG
}

Mencatat Pesan Kustom dengan Tag Kustom

API CastDebugLogger memungkinkan Anda membuat pesan log yang muncul di overlay debug Penerima Web dengan warna yang berbeda. Gunakan metode log berikut, yang diurutkan dari prioritas tertinggi ke terendah:

  • castDebugLogger.error(custom_tag, message);
  • castDebugLogger.warn(custom_tag, message);
  • castDebugLogger.info(custom_tag, message);
  • castDebugLogger.debug(custom_tag, message);

Untuk setiap metode log, parameter pertama harus berupa tag kustom dan parameter kedua adalah pesan log. Tag dapat berupa string apa pun yang menurut Anda bermanfaat.

Berikut adalah contoh cara menggunakan logger debug di interseptor 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);
                    }
                }
            );
        });
    }
);

Anda dapat mengontrol pesan yang akan muncul di overlay debug dengan menetapkan level log di loggerLevelByTags untuk setiap tag kustom. Misalnya, mengaktifkan tag kustom dengan level log cast.framework.LoggerLevel.DEBUG akan menampilkan semua pesan yang ditambahkan dengan pesan log error, peringatan, info, dan debug. Contoh lainnya adalah mengaktifkan tag kustom dengan level WARNING hanya akan menampilkan pesan log error dan peringatan.

Konfigurasi loggerLevelByTags bersifat opsional. Jika tag kustom tidak dikonfigurasi untuk level loggernya, semua pesan log akan ditampilkan di overlay debug.

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

Overlay Debug

Logger Debug Cast menyediakan overlay debug di Penerima Web untuk menampilkan pesan log kustom. Gunakan showDebugLogs untuk beralih overlay debug dan clearDebugLogs untuk menghapus pesan log di overlay.

Pengingat: gunakan showDebugLogs dan clearDebugLogs setelah castDebugLogger diaktifkan.

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();
  }
});