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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
// Copyright 2020 The Chromium Authors
// 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 <lib/component/incoming/cpp/protocol.h>
#include <lib/fdio/directory.h>
#include <stdio.h>
#include <string_view>
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/fuchsia/fuchsia_component_connect.h"
#include "base/fuchsia/fuchsia_logging.h"
#include "base/process/process.h"
#include "base/threading/platform_thread.h"
namespace base {
namespace {
inline FuchsiaLogSeverity LogSeverityToFuchsiaLogSeverity(
logging::LogSeverity severity) {
switch (severity) {
case logging::LOGGING_INFO:
return FUCHSIA_LOG_INFO;
case logging::LOGGING_WARNING:
return FUCHSIA_LOG_WARNING;
case logging::LOGGING_ERROR:
return FUCHSIA_LOG_ERROR;
case logging::LOGGING_FATAL:
// Don't use FX_LOG_FATAL, otherwise fx_logger_log() will abort().
return FUCHSIA_LOG_ERROR;
}
if (severity > -3) {
// LOGGING_VERBOSE levels 1 and 2.
return FUCHSIA_LOG_DEBUG;
}
// LOGGING_VERBOSE levels 3 and higher, or incorrect levels.
return FUCHSIA_LOG_TRACE;
}
} // namespace
ScopedFxLogger::ScopedFxLogger() = default;
ScopedFxLogger::~ScopedFxLogger() = default;
ScopedFxLogger::ScopedFxLogger(ScopedFxLogger&& other) = default;
ScopedFxLogger& ScopedFxLogger::operator=(ScopedFxLogger&& other) = default;
// static
ScopedFxLogger ScopedFxLogger::CreateForProcess(
std::vector<std::string_view> tags) {
// CHECK()ing or LOG()ing inside this function is safe, since it is only
// called to initialize logging, not during individual logging operations.
auto log_sink_client_end = component::Connect<fuchsia_logger::LogSink>();
if (log_sink_client_end.is_error()) {
LOG(ERROR) << FidlConnectionErrorMessage(log_sink_client_end);
return {};
}
// Rather than relying on automatic LogSink attribution via COMPONENT_NAME,
// prepend a tag based on the calling process' name. COMPONENT_NAME may be
// mis-attributed, in some Component configurations, to a parent or caller
// component, from which the process' LogSink service is routed.
std::string program_name = base::CommandLine::ForCurrentProcess()
->GetProgram()
.BaseName()
.AsUTF8Unsafe();
tags.insert(tags.begin(), program_name);
return CreateFromLogSink(std::move(log_sink_client_end.value()),
std::move(tags));
}
// static
ScopedFxLogger ScopedFxLogger::CreateFromLogSink(
fidl::ClientEnd<fuchsia_logger::LogSink> log_sink_client_end,
std::vector<std::string_view> tags) {
// CHECK()ing or LOG()ing inside this function is safe, since it is only
// called to initialize logging, not during individual logging operations.
// Attempts to create a kernel socket object should never fail.
zx::socket local, remote;
zx_status_t socket_status =
zx::socket::create(ZX_SOCKET_DATAGRAM, &local, &remote);
ZX_CHECK(socket_status == ZX_OK, socket_status)
<< "zx_socket_create() failed";
// ConnectStructured() may fail if e.g. the LogSink has disconnected already.
fidl::SyncClient log_sink(std::move(log_sink_client_end));
auto connect_structured_result =
log_sink->ConnectStructured(std::move(remote));
if (connect_structured_result.is_error()) {
ZX_LOG(ERROR, connect_structured_result.error_value().status())
<< "ConnectStructured() failed";
return {};
}
return ScopedFxLogger(std::move(tags), std::move(local));
}
void ScopedFxLogger::LogMessage(std::string_view file,
uint32_t line_number,
std::string_view msg,
logging::LogSeverity severity) {
if (!socket_.is_valid()) {
return;
}
auto fuchsia_severity = LogSeverityToFuchsiaLogSeverity(severity);
// It is not safe to use e.g. CHECK() or LOG() macros here, since those
// may result in reentrancy if this instance is used for routing process-
// global logs to the system logger.
fuchsia_syslog::LogBuffer buffer;
buffer.BeginRecord(fuchsia_severity,
std::string_view(file.data(), file.size()), line_number,
std::string_view(msg.data(), msg.size()),
socket_.borrow(), 0, base::Process::Current().Pid(),
base::PlatformThread::CurrentId().raw());
for (const auto& tag : tags_) {
buffer.WriteKeyValue("tag", tag);
}
if (!buffer.FlushRecord()) {
fprintf(stderr, "fuchsia_syslog.LogBuffer.FlushRecord() failed\n");
}
}
ScopedFxLogger::ScopedFxLogger(std::vector<std::string_view> tags,
zx::socket socket)
: tags_(tags.begin(), tags.end()), socket_(std::move(socket)) {}
} // namespace base
|