Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions fml/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ LogMessage::~LogMessage() {
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();
// Don't use std::cerr here, because it may not be initialized properly yet.
fprintf(stderr, "%s", stream_.str().c_str());
fflush(stderr);
#endif

if (severity_ >= LOG_FATAL) {
Expand Down
35 changes: 35 additions & 0 deletions fml/logging_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <signal.h>

#include "flutter/fml/build_config.h"
#include "flutter/fml/log_settings.h"
#include "flutter/fml/logging.h"
Expand All @@ -19,6 +21,39 @@
namespace fml {
namespace testing {

class MakeSureFmlLogDoesNotSegfaultWhenStaticallyCalled {
public:
MakeSureFmlLogDoesNotSegfaultWhenStaticallyCalled() {
SegfaultCatcher catcher;
// If this line causes a segfault, FML is using a method of logging that is
// not safe from static initialization on your platform.
FML_LOG(INFO)
<< "This log exists to verify that static logging from FML works.";
}

private:
struct SegfaultCatcher {
typedef void (*sighandler_t)(int);

SegfaultCatcher() {
handler = ::signal(SIGSEGV, SegfaultHandler);
FML_CHECK(handler != SIG_ERR);
}

~SegfaultCatcher() { FML_CHECK(::signal(SIGSEGV, handler) != SIG_ERR); }

static void SegfaultHandler(int signal) {
fprintf(stderr,
"FML failed to handle logging from static initialization.\n");
exit(signal);
}

sighandler_t handler;
};
};

static MakeSureFmlLogDoesNotSegfaultWhenStaticallyCalled fml_log_static_check_;

int UnreachableScopeWithoutReturnDoesNotMakeCompilerMad() {
KillProcess();
// return 0; <--- Missing but compiler is fine.
Expand Down