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