Skip to content

Commit fbf07dc

Browse files
authored
Import logging utilities from FXL. (flutter#5310)
1 parent 088e1c0 commit fbf07dc

29 files changed

+472
-84
lines changed

fml/BUILD.gn

+12-5
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,26 @@ source_set("fml") {
77
"build_config.h",
88
"compiler_specific.h",
99
"eintr_wrapper.h",
10+
"export.h",
1011
"file.h",
1112
"icu_util.cc",
1213
"icu_util.h",
14+
"log_level.h",
15+
"log_settings.cc",
16+
"log_settings.h",
17+
"log_settings_state.cc",
18+
"logging.cc",
19+
"logging.h",
1320
"mapping.cc",
1421
"mapping.h",
15-
"memory/thread_checker.h",
16-
"memory/weak_ptr.h",
17-
"memory/weak_ptr_internal.cc",
18-
"memory/weak_ptr_internal.h",
1922
"memory/ref_counted.h",
2023
"memory/ref_counted_internal.h",
2124
"memory/ref_ptr.h",
2225
"memory/ref_ptr_internal.h",
26+
"memory/thread_checker.h",
27+
"memory/weak_ptr.h",
28+
"memory/weak_ptr_internal.cc",
29+
"memory/weak_ptr_internal.h",
2330
"message_loop.cc",
2431
"message_loop.h",
2532
"message_loop_impl.cc",
@@ -136,8 +143,8 @@ executable("fml_unittests") {
136143
testonly = true
137144

138145
sources = [
139-
"memory/weak_ptr_unittest.cc",
140146
"memory/ref_counted_unittest.cc",
147+
"memory/weak_ptr_unittest.cc",
141148
"message_loop_unittests.cc",
142149
"thread_local_unittests.cc",
143150
"thread_unittests.cc",

fml/export.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2017 The Fuchsia 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+
#ifndef FLUTTER_FML_EXPORT_H_
6+
#define FLUTTER_FML_EXPORT_H_
7+
8+
#include "flutter/fml/build_config.h"
9+
10+
#if OS_WIN
11+
#define FML_EXPORT __declspec(dllimport)
12+
#else
13+
#define FML_EXPORT __attribute__((visibility("default")))
14+
#endif
15+
16+
#endif // FLUTTER_FML_EXPORT_H_

fml/icu_util.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
#include <mutex>
99

1010
#include "flutter/fml/build_config.h"
11+
#include "flutter/fml/logging.h"
1112
#include "flutter/fml/mapping.h"
1213
#include "flutter/fml/paths.h"
13-
#include "lib/fxl/logging.h"
1414
#include "third_party/icu/source/common/unicode/udata.h"
1515

1616
namespace fml {
@@ -92,7 +92,7 @@ class ICUContext {
9292

9393
void InitializeICUOnce(const std::string& icu_data_path) {
9494
static ICUContext* context = new ICUContext(icu_data_path);
95-
FXL_CHECK(context->IsValid())
95+
FML_CHECK(context->IsValid())
9696
<< "Must be able to initialize the ICU context. Tried: " << icu_data_path;
9797
}
9898

fml/log_level.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2016 The Fuchsia 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+
#ifndef FLUTTER_FML_LOG_LEVEL_H_
6+
#define FLUTTER_FML_LOG_LEVEL_H_
7+
8+
namespace fml {
9+
10+
typedef int LogSeverity;
11+
12+
// Default log levels. Negative values can be used for verbose log levels.
13+
constexpr LogSeverity LOG_INFO = 0;
14+
constexpr LogSeverity LOG_WARNING = 1;
15+
constexpr LogSeverity LOG_ERROR = 2;
16+
constexpr LogSeverity LOG_FATAL = 3;
17+
constexpr LogSeverity LOG_NUM_SEVERITIES = 4;
18+
19+
// LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode
20+
#ifdef NDEBUG
21+
const LogSeverity LOG_DFATAL = LOG_ERROR;
22+
#else
23+
const LogSeverity LOG_DFATAL = LOG_FATAL;
24+
#endif
25+
26+
} // namespace fml
27+
28+
#endif // FLUTTER_FML_LOG_LEVEL_H_

fml/log_settings.cc

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2016 The Fuchsia 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 "flutter/fml/log_settings.h"
6+
7+
#include <fcntl.h>
8+
#include <string.h>
9+
10+
#include <algorithm>
11+
#include <iostream>
12+
13+
#include "flutter/fml/logging.h"
14+
15+
namespace fml {
16+
namespace state {
17+
18+
// Defined in log_settings_state.cc.
19+
extern LogSettings g_log_settings;
20+
21+
} // namespace state
22+
23+
void SetLogSettings(const LogSettings& settings) {
24+
// Validate the new settings as we set them.
25+
state::g_log_settings.min_log_level =
26+
std::min(LOG_FATAL, settings.min_log_level);
27+
}
28+
29+
LogSettings GetLogSettings() {
30+
return state::g_log_settings;
31+
}
32+
33+
int GetMinLogLevel() {
34+
return std::min(state::g_log_settings.min_log_level, LOG_FATAL);
35+
}
36+
37+
} // namespace fml

fml/log_settings.h

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2016 The Fuchsia 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+
#ifndef FLUTTER_FML_LOG_SETTINGS_H_
6+
#define FLUTTER_FML_LOG_SETTINGS_H_
7+
8+
#include "flutter/fml/log_level.h"
9+
10+
#include <string>
11+
12+
namespace fml {
13+
14+
// Settings which control the behavior of FML logging.
15+
struct LogSettings {
16+
// The minimum logging level.
17+
// Anything at or above this level will be logged (if applicable).
18+
// Anything below this level will be silently ignored.
19+
//
20+
// The log level defaults to 0 (LOG_INFO).
21+
//
22+
// Log messages for FML_VLOG(x) (from flutter/fml/logging.h) are logged
23+
// at level -x, so setting the min log level to negative values enables
24+
// verbose logging.
25+
LogSeverity min_log_level = LOG_INFO;
26+
};
27+
28+
// Gets the active log settings for the current process.
29+
void SetLogSettings(const LogSettings& settings);
30+
31+
// Sets the active log settings for the current process.
32+
LogSettings GetLogSettings();
33+
34+
// Gets the minimum log level for the current process. Never returs a value
35+
// higher than LOG_FATAL.
36+
int GetMinLogLevel();
37+
38+
} // namespace fml
39+
40+
#endif // FLUTTER_FML_LOG_SETTINGS_H_

fml/log_settings_state.cc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2016 The Fuchsia 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 "flutter/fml/log_settings.h"
6+
7+
namespace fml {
8+
namespace state {
9+
10+
// Declared in log_settings.cc.
11+
LogSettings g_log_settings;
12+
13+
} // namespace state
14+
} // namespace fml

fml/logging.cc

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright 2016 The Fuchsia 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 <iostream>
7+
8+
#include "flutter/fml/build_config.h"
9+
#include "flutter/fml/log_settings.h"
10+
#include "flutter/fml/logging.h"
11+
12+
#if defined(OS_ANDROID)
13+
#include <android/log.h>
14+
#elif defined(OS_IOS)
15+
#include <syslog.h>
16+
#endif
17+
18+
namespace fml {
19+
namespace {
20+
21+
const char* const kLogSeverityNames[LOG_NUM_SEVERITIES] = {"INFO", "WARNING",
22+
"ERROR", "FATAL"};
23+
24+
const char* GetNameForLogSeverity(LogSeverity severity) {
25+
if (severity >= LOG_INFO && severity < LOG_NUM_SEVERITIES)
26+
return kLogSeverityNames[severity];
27+
return "UNKNOWN";
28+
}
29+
30+
const char* StripDots(const char* path) {
31+
while (strncmp(path, "../", 3) == 0)
32+
path += 3;
33+
return path;
34+
}
35+
36+
const char* StripPath(const char* path) {
37+
auto p = strrchr(path, '/');
38+
if (p)
39+
return p + 1;
40+
else
41+
return path;
42+
}
43+
44+
} // namespace
45+
46+
LogMessage::LogMessage(LogSeverity severity,
47+
const char* file,
48+
int line,
49+
const char* condition)
50+
: severity_(severity), file_(file), line_(line) {
51+
stream_ << "[";
52+
if (severity >= LOG_INFO)
53+
stream_ << GetNameForLogSeverity(severity);
54+
else
55+
stream_ << "VERBOSE" << -severity;
56+
stream_ << ":" << (severity > LOG_INFO ? StripDots(file_) : StripPath(file_))
57+
<< "(" << line_ << ")] ";
58+
59+
if (condition)
60+
stream_ << "Check failed: " << condition << ". ";
61+
}
62+
63+
LogMessage::~LogMessage() {
64+
stream_ << std::endl;
65+
66+
#if defined(OS_ANDROID)
67+
android_LogPriority priority =
68+
(severity_ < 0) ? ANDROID_LOG_VERBOSE : ANDROID_LOG_UNKNOWN;
69+
switch (severity_) {
70+
case LOG_INFO:
71+
priority = ANDROID_LOG_INFO;
72+
break;
73+
case LOG_WARNING:
74+
priority = ANDROID_LOG_WARN;
75+
break;
76+
case LOG_ERROR:
77+
priority = ANDROID_LOG_ERROR;
78+
break;
79+
case LOG_FATAL:
80+
priority = ANDROID_LOG_FATAL;
81+
break;
82+
}
83+
__android_log_write(priority, "flutter", stream_.str().c_str());
84+
#elif defined(OS_IOS)
85+
syslog(LOG_ALERT, "%s", stream_.str().c_str());
86+
#else
87+
std::cerr << stream_.str();
88+
std::cerr.flush();
89+
#endif
90+
91+
if (severity_ >= LOG_FATAL) {
92+
abort();
93+
}
94+
}
95+
96+
int GetVlogVerbosity() {
97+
return std::max(-1, LOG_INFO - GetMinLogLevel());
98+
}
99+
100+
bool ShouldCreateLogMessage(LogSeverity severity) {
101+
return severity >= GetMinLogLevel();
102+
}
103+
104+
} // namespace fml

0 commit comments

Comments
 (0)