summaryrefslogtreecommitdiff
path: root/chromium/base/fuchsia/scoped_fx_logger.cc
blob: cbc72ed492fa678f5dc92c5ea0f5af3ff01c81d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/fuchsia/scoped_fx_logger.h"

#include "base/fuchsia/fuchsia_logging.h"

namespace base {

ScopedFxLogger CreateFxLoggerFromLogSink(
    fuchsia::logger::LogSinkHandle log_sink) {
  // TODO(bugs.fuchsia.dev/63529): Use |log_sink_socket| when available.
  zx::socket client_end, request_end;
  zx_status_t status =
      zx::socket::create(ZX_SOCKET_DATAGRAM, &client_end, &request_end);
  if (status != ZX_OK) {
    return nullptr;
  }
  fuchsia::logger::LogSinkSyncPtr log_sink_ptr;
  log_sink_ptr.Bind(std::move(log_sink));
  log_sink_ptr->Connect(std::move(request_end));

  fx_logger_config_t config = {
      // Selecting based on log level is handled outside the fx_logger.
      .min_severity = FX_LOG_ALL,
      .console_fd = -1,
      .log_service_channel = client_end.release(),
      // Do not set any custom tags.
      .tags = nullptr,
      .num_tags = 0,
  };

  fx_logger_t* fx_logger = nullptr;
  status = fx_logger_create(&config, &fx_logger);
  if (status != ZX_OK) {
    ZX_LOG(ERROR, status) << "fx_logger_create";
    return nullptr;
  }

  return ScopedFxLogger(fx_logger);
}

}  // namespace base