|
| 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