Flutter Engine
The Flutter Engine
logging_unittests.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <signal.h>
6
7#include "flutter/fml/build_config.h"
8#include "flutter/fml/log_settings.h"
9#include "flutter/fml/logging.h"
10#include "fml/log_level.h"
11#include "gtest/gtest.h"
12
13namespace fml {
14namespace testing {
15
16static_assert(fml::kLogFatal == fml::kLogNumSeverities - 1);
17static_assert(fml::kLogImportant < fml::kLogFatal);
18static_assert(fml::kLogImportant > fml::kLogError);
19
20#ifndef OS_FUCHSIA
22 public:
24 SegfaultCatcher catcher;
25 // If this line causes a segfault, FML is using a method of logging that is
26 // not safe from static initialization on your platform.
27 FML_LOG(INFO)
28 << "This log exists to verify that static logging from FML works.";
29 }
30
31 private:
32 struct SegfaultCatcher {
33 typedef void (*sighandler_t)(int);
34
35 SegfaultCatcher() {
36 handler = ::signal(SIGSEGV, SegfaultHandler);
37 FML_CHECK(handler != SIG_ERR);
38 }
39
40 ~SegfaultCatcher() { FML_CHECK(::signal(SIGSEGV, handler) != SIG_ERR); }
41
42 static void SegfaultHandler(int signal) {
43 fprintf(stderr,
44 "FML failed to handle logging from static initialization.\n");
45 exit(signal);
46 }
47
48 sighandler_t handler;
49 };
50};
51
53#endif // !defined(OS_FUCHSIA)
54
57 // return 0; <--- Missing but compiler is fine.
58}
59
62 // return 0; <--- Missing but compiler is fine.
63}
64
65TEST(LoggingTest, UnreachableKillProcess) {
66 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
67 ASSERT_DEATH(KillProcess(), "");
68}
69
70TEST(LoggingTest, UnreachableKillProcessWithMacro) {
71 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
72 ASSERT_DEATH({ FML_UNREACHABLE(); }, "");
73}
74
75#ifndef OS_FUCHSIA
76TEST(LoggingTest, SanityTests) {
77 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
78 std::vector<std::string> severities = {"INFO", "WARNING", "ERROR",
79 "IMPORTANT"};
80
81 for (size_t i = 0; i < severities.size(); i++) {
82 LogCapture capture;
83 {
84 LogMessage log(i, "path/to/file.cc", 4, nullptr);
85 log.stream() << "Hello!";
86 }
87 EXPECT_EQ(capture.str(), std::string("[" + severities[i] +
88 ":path/to/file.cc(4)] Hello!\n"));
89 }
90
91 ASSERT_DEATH(
92 {
93 LogMessage log(kLogFatal, "path/to/file.cc", 5, nullptr);
94 log.stream() << "Goodbye";
95 },
96 R"(\[FATAL:path/to/file.cc\‍(5\)\] Goodbye)");
97}
98#endif // !OS_FUCHSIA
99
100} // namespace testing
101} // namespace fml
#define FML_LOG(severity)
Definition: logging.h:82
#define FML_CHECK(condition)
Definition: logging.h:85
#define FML_UNREACHABLE()
Definition: logging.h:109
exit(kErrorExitCode)
int UnreachableScopeWithoutReturnDoesNotMakeCompilerMad()
static MakeSureFmlLogDoesNotSegfaultWhenStaticallyCalled fml_log_static_check_
int UnreachableScopeWithMacroWithoutReturnDoesNotMakeCompilerMad()
TEST(BacktraceTest, CanGatherBacktrace)
Definition: ascii_trie.cc:9
void KillProcess()
Definition: logging.cc:220
constexpr LogSeverity kLogFatal
Definition: log_level.h:19
constexpr LogSeverity kLogNumSeverities
Definition: log_level.h:20
constexpr LogSeverity kLogImportant
Definition: log_level.h:18
constexpr LogSeverity kLogError
Definition: log_level.h:15
std::string str() const
Definition: logging.cc:122