Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Import logging utilities from FXL. #5310

Merged
merged 2 commits into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 12 additions & 5 deletions fml/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@ source_set("fml") {
"build_config.h",
"compiler_specific.h",
"eintr_wrapper.h",
"export.h",
"file.h",
"icu_util.cc",
"icu_util.h",
"log_level.h",
"log_settings.cc",
"log_settings.h",
"log_settings_state.cc",
"logging.cc",
"logging.h",
"mapping.cc",
"mapping.h",
"memory/thread_checker.h",
"memory/weak_ptr.h",
"memory/weak_ptr_internal.cc",
"memory/weak_ptr_internal.h",
"memory/ref_counted.h",
"memory/ref_counted_internal.h",
"memory/ref_ptr.h",
"memory/ref_ptr_internal.h",
"memory/thread_checker.h",
"memory/weak_ptr.h",
"memory/weak_ptr_internal.cc",
"memory/weak_ptr_internal.h",
"message_loop.cc",
"message_loop.h",
"message_loop_impl.cc",
Expand Down Expand Up @@ -136,8 +143,8 @@ executable("fml_unittests") {
testonly = true

sources = [
"memory/weak_ptr_unittest.cc",
"memory/ref_counted_unittest.cc",
"memory/weak_ptr_unittest.cc",
"message_loop_unittests.cc",
"thread_local_unittests.cc",
"thread_unittests.cc",
Expand Down
16 changes: 16 additions & 0 deletions fml/export.h
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_
4 changes: 2 additions & 2 deletions fml/icu_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#include <mutex>

#include "flutter/fml/build_config.h"
#include "flutter/fml/logging.h"
#include "flutter/fml/mapping.h"
#include "flutter/fml/paths.h"
#include "lib/fxl/logging.h"
#include "third_party/icu/source/common/unicode/udata.h"

namespace fml {
Expand Down Expand Up @@ -92,7 +92,7 @@ class ICUContext {

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

Expand Down
28 changes: 28 additions & 0 deletions fml/log_level.h
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_
37 changes: 37 additions & 0 deletions fml/log_settings.cc
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
40 changes: 40 additions & 0 deletions fml/log_settings.h
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_
14 changes: 14 additions & 0 deletions fml/log_settings_state.cc
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
104 changes: 104 additions & 0 deletions fml/logging.cc
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
Loading