Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
logging.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 <algorithm>
6#include <cstring>
7#include <iostream>
8
12#include "flutter/fml/logging.h"
13
14#if defined(FML_OS_ANDROID)
15#include <android/log.h>
16#elif defined(FML_OS_IOS)
17#include <syslog.h>
18#elif defined(OS_FUCHSIA)
19#include <lib/syslog/cpp/log_message_impl.h>
20#include <lib/syslog/structured_backend/cpp/log_buffer.h>
21#include <lib/syslog/structured_backend/fuchsia_syslog.h>
22#include <zircon/process.h>
23#include <zircon/syscalls.h>
24#include <zircon/syscalls/object.h>
25#endif
26
27namespace fml {
28
29namespace {
30
31#if !defined(OS_FUCHSIA)
32const char* const kLogSeverityNames[kLogNumSeverities] = {
33 "INFO", "WARNING", "ERROR", "IMPORTANT", "FATAL"};
34
35const char* GetNameForLogSeverity(LogSeverity severity) {
36 if (severity >= kLogInfo && severity < kLogNumSeverities) {
37 return kLogSeverityNames[severity];
38 }
39 return "UNKNOWN";
40}
41#endif
42
43const char* StripDots(const char* path) {
44 while (strncmp(path, "../", 3) == 0) {
45 path += 3;
46 }
47 return path;
48}
49
50#if defined(OS_FUCHSIA)
51
52const std::string* GetProcessName() {
53 static const std::string* process_name = []() -> const std::string* {
54 char name[ZX_MAX_NAME_LEN];
55 zx_status_t status = zx_object_get_property(zx_process_self(), ZX_PROP_NAME,
56 name, sizeof(name));
57 if (status != ZX_OK) {
58 return nullptr;
59 }
60 return new std::string(name);
61 }();
62 return process_name;
63}
64
65#endif
66
67} // namespace
68
70 const char* file,
71 int line,
72 const char* condition)
73 : severity_(severity), file_(StripDots(file)), line_(line) {
74#if !defined(OS_FUCHSIA)
75 stream_ << "[";
76 if (severity >= kLogInfo) {
77 stream_ << GetNameForLogSeverity(severity);
78 } else {
79 stream_ << "VERBOSE" << -severity;
80 }
81 stream_ << ":" << file_ << "(" << line_ << ")] ";
82#endif
83
84 if (condition) {
85 stream_ << "Check failed: " << condition << ". ";
86 }
87}
88
89// static
90thread_local std::ostringstream* LogMessage::capture_next_log_stream_ = nullptr;
91
92namespace testing {
93
97
101
102std::string LogCapture::str() const {
103 return stream_.str();
104}
105
106} // namespace testing
107
108// static
109void LogMessage::CaptureNextLog(std::ostringstream* stream) {
110 LogMessage::capture_next_log_stream_ = stream;
111}
112
114#if !defined(OS_FUCHSIA)
115 stream_ << std::endl;
116#endif
117 if (capture_next_log_stream_) {
118 *capture_next_log_stream_ << stream_.str();
119 capture_next_log_stream_ = nullptr;
120 } else {
121#if defined(FML_OS_ANDROID)
122 android_LogPriority priority =
123 (severity_ < 0) ? ANDROID_LOG_VERBOSE : ANDROID_LOG_UNKNOWN;
124 switch (severity_) {
125 case kLogImportant:
126 case kLogInfo:
127 priority = ANDROID_LOG_INFO;
128 break;
129 case kLogWarning:
130 priority = ANDROID_LOG_WARN;
131 break;
132 case kLogError:
133 priority = ANDROID_LOG_ERROR;
134 break;
135 case kLogFatal:
136 priority = ANDROID_LOG_FATAL;
137 break;
138 }
139 __android_log_write(priority, "flutter", stream_.str().c_str());
140#elif defined(FML_OS_IOS)
141 syslog(LOG_ALERT, "%s", stream_.str().c_str());
142#elif defined(OS_FUCHSIA)
143 FuchsiaLogSeverity severity;
144 switch (severity_) {
145 case kLogImportant:
146 case kLogInfo:
147 severity = FUCHSIA_LOG_INFO;
148 break;
149 case kLogWarning:
150 severity = FUCHSIA_LOG_WARNING;
151 break;
152 case kLogError:
153 severity = FUCHSIA_LOG_ERROR;
154 break;
155 case kLogFatal:
156 severity = FUCHSIA_LOG_FATAL;
157 break;
158 default:
159 if (severity_ < 0) {
160 severity = FUCHSIA_LOG_DEBUG;
161 } else {
162 // Unknown severity. Use INFO.
163 severity = FUCHSIA_LOG_INFO;
164 }
165 break;
166 }
167 fuchsia_logging::LogBuffer buffer =
168 fuchsia_logging::LogBufferBuilder(severity)
169 .WithFile(file_, line_)
170 .WithMsg(stream_.str())
171 .Build();
172 const std::string* process_name = GetProcessName();
173 if (process_name) {
174 buffer.WriteKeyValue("tag", *process_name);
175 }
176 [[maybe_unused]] zx::result result =
177 fuchsia_logging::FlushToGlobalLogger(buffer);
178#else
179 // Don't use std::cerr here, because it may not be initialized properly yet.
180 fprintf(stderr, "%s", stream_.str().c_str());
181 fflush(stderr);
182#endif
183 }
184
185 if (severity_ >= kLogFatal) {
186 KillProcess();
187 }
188}
189
191 return std::max(-1, kLogInfo - GetMinLogLevel());
192}
193
195 return severity >= GetMinLogLevel();
196}
197
199 abort();
200}
201
202} // namespace fml
LogMessage(LogSeverity severity, const char *file, int line, const char *condition)
Definition logging.cc:69
std::ostream & stream()
Definition logging.h:40
static void CaptureNextLog(std::ostringstream *stream)
Definition logging.cc:109
const char * name
Definition fuchsia.cc:49
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition switch_defs.h:52
void KillProcess()
Definition logging.cc:198
constexpr LogSeverity kLogFatal
Definition log_level.h:19
int LogSeverity
Definition log_level.h:11
constexpr LogSeverity kLogNumSeverities
Definition log_level.h:20
constexpr LogSeverity kLogImportant
Definition log_level.h:18
bool ShouldCreateLogMessage(LogSeverity severity)
Definition logging.cc:194
int GetMinLogLevel()
constexpr LogSeverity kLogError
Definition log_level.h:15
constexpr LogSeverity kLogInfo
Definition log_level.h:13
int GetVlogVerbosity()
Definition logging.cc:190
constexpr LogSeverity kLogWarning
Definition log_level.h:14
std::string str() const
Definition logging.cc:102