forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import logging utilities from FXL. (flutter#5310)
- Loading branch information
1 parent
088e1c0
commit fbf07dc
Showing
29 changed files
with
472 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2017 The Fuchsia Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_FML_EXPORT_H_ | ||
#define FLUTTER_FML_EXPORT_H_ | ||
|
||
#include "flutter/fml/build_config.h" | ||
|
||
#if OS_WIN | ||
#define FML_EXPORT __declspec(dllimport) | ||
#else | ||
#define FML_EXPORT __attribute__((visibility("default"))) | ||
#endif | ||
|
||
#endif // FLUTTER_FML_EXPORT_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2016 The Fuchsia Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_FML_LOG_LEVEL_H_ | ||
#define FLUTTER_FML_LOG_LEVEL_H_ | ||
|
||
namespace fml { | ||
|
||
typedef int LogSeverity; | ||
|
||
// Default log levels. Negative values can be used for verbose log levels. | ||
constexpr LogSeverity LOG_INFO = 0; | ||
constexpr LogSeverity LOG_WARNING = 1; | ||
constexpr LogSeverity LOG_ERROR = 2; | ||
constexpr LogSeverity LOG_FATAL = 3; | ||
constexpr LogSeverity LOG_NUM_SEVERITIES = 4; | ||
|
||
// LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode | ||
#ifdef NDEBUG | ||
const LogSeverity LOG_DFATAL = LOG_ERROR; | ||
#else | ||
const LogSeverity LOG_DFATAL = LOG_FATAL; | ||
#endif | ||
|
||
} // namespace fml | ||
|
||
#endif // FLUTTER_FML_LOG_LEVEL_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2016 The Fuchsia Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "flutter/fml/log_settings.h" | ||
|
||
#include <fcntl.h> | ||
#include <string.h> | ||
|
||
#include <algorithm> | ||
#include <iostream> | ||
|
||
#include "flutter/fml/logging.h" | ||
|
||
namespace fml { | ||
namespace state { | ||
|
||
// Defined in log_settings_state.cc. | ||
extern LogSettings g_log_settings; | ||
|
||
} // namespace state | ||
|
||
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); | ||
} | ||
|
||
LogSettings GetLogSettings() { | ||
return state::g_log_settings; | ||
} | ||
|
||
int GetMinLogLevel() { | ||
return std::min(state::g_log_settings.min_log_level, LOG_FATAL); | ||
} | ||
|
||
} // namespace fml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2016 The Fuchsia Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_FML_LOG_SETTINGS_H_ | ||
#define FLUTTER_FML_LOG_SETTINGS_H_ | ||
|
||
#include "flutter/fml/log_level.h" | ||
|
||
#include <string> | ||
|
||
namespace fml { | ||
|
||
// Settings which control the behavior of FML logging. | ||
struct LogSettings { | ||
// The minimum logging level. | ||
// Anything at or above this level will be logged (if applicable). | ||
// Anything below this level will be silently ignored. | ||
// | ||
// The log level defaults to 0 (LOG_INFO). | ||
// | ||
// Log messages for FML_VLOG(x) (from flutter/fml/logging.h) are logged | ||
// at level -x, so setting the min log level to negative values enables | ||
// verbose logging. | ||
LogSeverity min_log_level = LOG_INFO; | ||
}; | ||
|
||
// Gets the active log settings for the current process. | ||
void SetLogSettings(const LogSettings& settings); | ||
|
||
// Sets the active log settings for the current process. | ||
LogSettings GetLogSettings(); | ||
|
||
// Gets the minimum log level for the current process. Never returs a value | ||
// higher than LOG_FATAL. | ||
int GetMinLogLevel(); | ||
|
||
} // namespace fml | ||
|
||
#endif // FLUTTER_FML_LOG_SETTINGS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2016 The Fuchsia Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "flutter/fml/log_settings.h" | ||
|
||
namespace fml { | ||
namespace state { | ||
|
||
// Declared in log_settings.cc. | ||
LogSettings g_log_settings; | ||
|
||
} // namespace state | ||
} // namespace fml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2016 The Fuchsia Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include <algorithm> | ||
#include <iostream> | ||
|
||
#include "flutter/fml/build_config.h" | ||
#include "flutter/fml/log_settings.h" | ||
#include "flutter/fml/logging.h" | ||
|
||
#if defined(OS_ANDROID) | ||
#include <android/log.h> | ||
#elif defined(OS_IOS) | ||
#include <syslog.h> | ||
#endif | ||
|
||
namespace fml { | ||
namespace { | ||
|
||
const char* const kLogSeverityNames[LOG_NUM_SEVERITIES] = {"INFO", "WARNING", | ||
"ERROR", "FATAL"}; | ||
|
||
const char* GetNameForLogSeverity(LogSeverity severity) { | ||
if (severity >= LOG_INFO && severity < LOG_NUM_SEVERITIES) | ||
return kLogSeverityNames[severity]; | ||
return "UNKNOWN"; | ||
} | ||
|
||
const char* StripDots(const char* path) { | ||
while (strncmp(path, "../", 3) == 0) | ||
path += 3; | ||
return path; | ||
} | ||
|
||
const char* StripPath(const char* path) { | ||
auto p = strrchr(path, '/'); | ||
if (p) | ||
return p + 1; | ||
else | ||
return path; | ||
} | ||
|
||
} // namespace | ||
|
||
LogMessage::LogMessage(LogSeverity severity, | ||
const char* file, | ||
int line, | ||
const char* condition) | ||
: severity_(severity), file_(file), line_(line) { | ||
stream_ << "["; | ||
if (severity >= LOG_INFO) | ||
stream_ << GetNameForLogSeverity(severity); | ||
else | ||
stream_ << "VERBOSE" << -severity; | ||
stream_ << ":" << (severity > LOG_INFO ? StripDots(file_) : StripPath(file_)) | ||
<< "(" << line_ << ")] "; | ||
|
||
if (condition) | ||
stream_ << "Check failed: " << condition << ". "; | ||
} | ||
|
||
LogMessage::~LogMessage() { | ||
stream_ << std::endl; | ||
|
||
#if defined(OS_ANDROID) | ||
android_LogPriority priority = | ||
(severity_ < 0) ? ANDROID_LOG_VERBOSE : ANDROID_LOG_UNKNOWN; | ||
switch (severity_) { | ||
case LOG_INFO: | ||
priority = ANDROID_LOG_INFO; | ||
break; | ||
case LOG_WARNING: | ||
priority = ANDROID_LOG_WARN; | ||
break; | ||
case LOG_ERROR: | ||
priority = ANDROID_LOG_ERROR; | ||
break; | ||
case LOG_FATAL: | ||
priority = ANDROID_LOG_FATAL; | ||
break; | ||
} | ||
__android_log_write(priority, "flutter", stream_.str().c_str()); | ||
#elif defined(OS_IOS) | ||
syslog(LOG_ALERT, "%s", stream_.str().c_str()); | ||
#else | ||
std::cerr << stream_.str(); | ||
std::cerr.flush(); | ||
#endif | ||
|
||
if (severity_ >= LOG_FATAL) { | ||
abort(); | ||
} | ||
} | ||
|
||
int GetVlogVerbosity() { | ||
return std::max(-1, LOG_INFO - GetMinLogLevel()); | ||
} | ||
|
||
bool ShouldCreateLogMessage(LogSeverity severity) { | ||
return severity >= GetMinLogLevel(); | ||
} | ||
|
||
} // namespace fml |
Oops, something went wrong.