Use syslog for logging on Fuchsia (flutter/engine#23429)

Make fml logging use syslog on Fuchsia. This allows logs
to be attributed to flutter and not pollute kernel logs.
This commit is contained in:
s-sami
2021-01-06 17:02:07 -05:00
committed by GitHub
parent b9e6475fee
commit 5619043974
4 changed files with 146 additions and 0 deletions

View File

@@ -187,6 +187,7 @@ source_set("fml") {
"$fuchsia_sdk_root/pkg:async-cpp",
"$fuchsia_sdk_root/pkg:async-loop-cpp",
"$fuchsia_sdk_root/pkg:async-loop-default",
"$fuchsia_sdk_root/pkg:syslog",
"$fuchsia_sdk_root/pkg:trace",
"$fuchsia_sdk_root/pkg:trace-engine",
"$fuchsia_sdk_root/pkg:zx",

View File

@@ -10,8 +10,13 @@
#include <cstring>
#include <iostream>
#include "flutter/fml/build_config.h"
#include "flutter/fml/logging.h"
#if defined(OS_FUCHSIA)
#include <lib/syslog/global.h>
#endif
namespace fml {
namespace state {
@@ -24,6 +29,10 @@ void SetLogSettings(const LogSettings& settings) {
// Validate the new settings as we set them.
state::g_log_settings.min_log_level =
std::min(LOG_FATAL, settings.min_log_level);
#if defined(OS_FUCHSIA)
// Syslog should accept all logs, since filtering by severity is done by fml.
FX_LOG_SET_SEVERITY(ALL);
#endif
}
LogSettings GetLogSettings() {

View File

@@ -13,11 +13,15 @@
#include <android/log.h>
#elif defined(OS_IOS)
#include <syslog.h>
#elif defined(OS_FUCHSIA)
#include <lib/syslog/global.h>
#endif
namespace fml {
namespace {
#if !defined(OS_FUCHSIA)
const char* const kLogSeverityNames[LOG_NUM_SEVERITIES] = {"INFO", "WARNING",
"ERROR", "FATAL"};
@@ -42,6 +46,7 @@ const char* StripPath(const char* path) {
}
return path;
}
#endif
} // namespace
@@ -50,6 +55,7 @@ LogMessage::LogMessage(LogSeverity severity,
int line,
const char* condition)
: severity_(severity), file_(file), line_(line) {
#if !defined(OS_FUCHSIA)
stream_ << "[";
if (severity >= LOG_INFO) {
stream_ << GetNameForLogSeverity(severity);
@@ -58,6 +64,7 @@ LogMessage::LogMessage(LogSeverity severity,
}
stream_ << ":" << (severity > LOG_INFO ? StripDots(file_) : StripPath(file_))
<< "(" << line_ << ")] ";
#endif
if (condition) {
stream_ << "Check failed: " << condition << ". ";
@@ -65,7 +72,9 @@ LogMessage::LogMessage(LogSeverity severity,
}
LogMessage::~LogMessage() {
#if !defined(OS_FUCHSIA)
stream_ << std::endl;
#endif
#if defined(OS_ANDROID)
android_LogPriority priority =
@@ -87,6 +96,31 @@ LogMessage::~LogMessage() {
__android_log_write(priority, "flutter", stream_.str().c_str());
#elif defined(OS_IOS)
syslog(LOG_ALERT, "%s", stream_.str().c_str());
#elif defined(OS_FUCHSIA)
fx_log_severity_t fx_severity;
switch (severity_) {
case LOG_INFO:
fx_severity = FX_LOG_INFO;
break;
case LOG_WARNING:
fx_severity = FX_LOG_WARNING;
break;
case LOG_ERROR:
fx_severity = FX_LOG_ERROR;
break;
case LOG_FATAL:
fx_severity = FX_LOG_FATAL;
break;
default:
if (severity_ < 0) {
fx_severity = fx_log_severity_from_verbosity(-severity_);
} else {
// Unknown severity. Use INFO.
fx_severity = FX_LOG_INFO;
}
}
fx_logger_log_with_source(fx_log_get_logger(), fx_severity, nullptr, file_,
line_, stream_.str().c_str());
#else
std::cerr << stream_.str();
std::cerr.flush();

View File

@@ -2,9 +2,20 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/fml/build_config.h"
#include "flutter/fml/log_settings.h"
#include "flutter/fml/logging.h"
#include "gtest/gtest.h"
#if defined(OS_FUCHSIA)
#include <lib/syslog/global.h>
#include <lib/syslog/logger.h>
#include <lib/syslog/wire_format.h>
#include <lib/zx/socket.h>
#include "gmock/gmock.h"
#endif
namespace fml {
namespace testing {
@@ -28,5 +39,96 @@ TEST(LoggingTest, UnreachableKillProcessWithMacro) {
ASSERT_DEATH({ FML_UNREACHABLE(); }, "");
}
#if defined(OS_FUCHSIA)
struct LogPacket {
fx_log_metadata_t metadata;
std::vector<std::string> tags;
std::string message;
};
class LoggingSocketTest : public ::testing::Test {
protected:
void SetUp() override {
zx::socket local;
ASSERT_EQ(ZX_OK, zx::socket::create(ZX_SOCKET_DATAGRAM, &local, &socket_));
fx_logger_config_t config = {.min_severity = FX_LOG_INFO,
.console_fd = -1,
.log_service_channel = local.release(),
.tags = nullptr,
.num_tags = 0};
fx_log_reconfigure(&config);
}
LogPacket ReadPacket() {
LogPacket result;
fx_log_packet_t packet;
zx_status_t res = socket_.read(0, &packet, sizeof(packet), nullptr);
EXPECT_EQ(ZX_OK, res);
result.metadata = packet.metadata;
int pos = 0;
while (packet.data[pos]) {
int tag_len = packet.data[pos++];
result.tags.emplace_back(packet.data + pos, tag_len);
pos += tag_len;
}
result.message.append(packet.data + pos + 1);
return result;
}
void ReadPacketAndCompare(fx_log_severity_t severity,
const std::string& message,
const std::vector<std::string>& tags = {}) {
LogPacket packet = ReadPacket();
EXPECT_EQ(severity, packet.metadata.severity);
EXPECT_THAT(packet.message, ::testing::EndsWith(message));
EXPECT_EQ(tags, packet.tags);
}
void CheckSocketEmpty() {
zx_info_socket_t info = {};
zx_status_t status =
socket_.get_info(ZX_INFO_SOCKET, &info, sizeof(info), nullptr, nullptr);
ASSERT_EQ(ZX_OK, status);
EXPECT_EQ(0u, info.rx_buf_available);
}
zx::socket socket_;
};
TEST_F(LoggingSocketTest, UseSyslogOnFuchsia) {
const char* msg1 = "test message";
const char* msg2 = "hello";
const char* msg3 = "logging";
const char* msg4 = "Another message";
const char* msg5 = "Foo";
fml::SetLogSettings({.min_log_level = -1});
FML_LOG(INFO) << msg1;
ReadPacketAndCompare(FX_LOG_INFO, msg1);
CheckSocketEmpty();
FML_LOG(WARNING) << msg2;
ReadPacketAndCompare(FX_LOG_WARNING, msg2);
CheckSocketEmpty();
FML_LOG(ERROR) << msg3;
ReadPacketAndCompare(FX_LOG_ERROR, msg3);
CheckSocketEmpty();
FML_VLOG(1) << msg4;
ReadPacketAndCompare(fx_log_severity_from_verbosity(1), msg4);
CheckSocketEmpty();
// VLOG(2) is not enabled so the log gets dropped.
FML_VLOG(2) << msg5;
CheckSocketEmpty();
}
#endif
} // namespace testing
} // namespace fml