Flutter Engine
logging.cc
Go to the documentation of this file.
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 <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  }
28  return "UNKNOWN";
29 }
30 
31 const char* StripDots(const char* path) {
32  while (strncmp(path, "../", 3) == 0) {
33  path += 3;
34  }
35  return path;
36 }
37 
38 const char* StripPath(const char* path) {
39  auto* p = strrchr(path, '/');
40  if (p) {
41  return p + 1;
42  }
43  return path;
44 }
45 
46 } // namespace
47 
49  const char* file,
50  int line,
51  const char* condition)
52  : severity_(severity), file_(file), line_(line) {
53  stream_ << "[";
54  if (severity >= LOG_INFO) {
55  stream_ << GetNameForLogSeverity(severity);
56  } else {
57  stream_ << "VERBOSE" << -severity;
58  }
59  stream_ << ":" << (severity > LOG_INFO ? StripDots(file_) : StripPath(file_))
60  << "(" << line_ << ")] ";
61 
62  if (condition) {
63  stream_ << "Check failed: " << condition << ". ";
64  }
65 }
66 
68  stream_ << std::endl;
69 
70 #if defined(OS_ANDROID)
71  android_LogPriority priority =
72  (severity_ < 0) ? ANDROID_LOG_VERBOSE : ANDROID_LOG_UNKNOWN;
73  switch (severity_) {
74  case LOG_INFO:
75  priority = ANDROID_LOG_INFO;
76  break;
77  case LOG_WARNING:
78  priority = ANDROID_LOG_WARN;
79  break;
80  case LOG_ERROR:
81  priority = ANDROID_LOG_ERROR;
82  break;
83  case LOG_FATAL:
84  priority = ANDROID_LOG_FATAL;
85  break;
86  }
87  __android_log_write(priority, "flutter", stream_.str().c_str());
88 #elif defined(OS_IOS)
89  syslog(LOG_ALERT, "%s", stream_.str().c_str());
90 #else
91  std::cerr << stream_.str();
92  std::cerr.flush();
93 #endif
94 
95  if (severity_ >= LOG_FATAL) {
96  KillProcess();
97  }
98 }
99 
101  return std::max(-1, LOG_INFO - GetMinLogLevel());
102 }
103 
105  return severity >= GetMinLogLevel();
106 }
107 
108 void KillProcess() {
109  abort();
110 }
111 
112 } // namespace fml
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
LogMessage(LogSeverity severity, const char *file, int line, const char *condition)
Definition: logging.cc:48
int GetMinLogLevel()
Definition: log_settings.cc:33
Definition: ascii_trie.cc:9
int LogSeverity
Definition: log_level.h:10
int GetVlogVerbosity()
Definition: logging.cc:100
constexpr LogSeverity LOG_WARNING
Definition: log_level.h:14
bool ShouldCreateLogMessage(LogSeverity severity)
Definition: logging.cc:104
constexpr LogSeverity LOG_ERROR
Definition: log_level.h:15
void KillProcess()
Definition: logging.cc:108
constexpr LogSeverity LOG_INFO
Definition: log_level.h:13
constexpr LogSeverity LOG_NUM_SEVERITIES
Definition: log_level.h:17
constexpr LogSeverity LOG_FATAL
Definition: log_level.h:16