Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "logging.h" |
| 18 | |
Vladimir Marko | b8f2f63 | 2015-01-02 14:23:26 +0000 | [diff] [blame] | 19 | #include <limits> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 20 | #include <sstream> |
| 21 | |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 22 | #include "base/mutex.h" |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 23 | #include "runtime.h" |
Brian Carlstrom | a3d2718 | 2013-11-05 23:22:27 -0800 | [diff] [blame] | 24 | #include "thread-inl.h" |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 25 | #include "utils.h" |
| 26 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 27 | // Headers for LogMessage::LogLine. |
| 28 | #ifdef HAVE_ANDROID_OS |
| 29 | #include "cutils/log.h" |
| 30 | #else |
| 31 | #include <sys/types.h> |
| 32 | #include <unistd.h> |
| 33 | #endif |
| 34 | |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 35 | namespace art { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 36 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 37 | LogVerbosity gLogVerbosity; |
| 38 | |
Nicolas Geoffray | db97871 | 2014-12-09 13:33:38 +0000 | [diff] [blame] | 39 | unsigned int gAborting = 0; |
| 40 | |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 41 | static LogSeverity gMinimumLogSeverity = INFO; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 42 | static std::unique_ptr<std::string> gCmdLine; |
| 43 | static std::unique_ptr<std::string> gProgramInvocationName; |
| 44 | static std::unique_ptr<std::string> gProgramInvocationShortName; |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 45 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 46 | const char* GetCmdLine() { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 47 | return (gCmdLine.get() != nullptr) ? gCmdLine->c_str() : nullptr; |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | const char* ProgramInvocationName() { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 51 | return (gProgramInvocationName.get() != nullptr) ? gProgramInvocationName->c_str() : "art"; |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | const char* ProgramInvocationShortName() { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 55 | return (gProgramInvocationShortName.get() != nullptr) ? gProgramInvocationShortName->c_str() |
| 56 | : "art"; |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 59 | void InitLogging(char* argv[]) { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 60 | if (gCmdLine.get() != nullptr) { |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame] | 61 | return; |
| 62 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 63 | // TODO: Move this to a more obvious InitART... |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 64 | Locks::Init(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 65 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 66 | // Stash the command line for later use. We can use /proc/self/cmdline on Linux to recover this, |
| 67 | // but we don't have that luxury on the Mac, and there are a couple of argv[0] variants that are |
| 68 | // commonly used. |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 69 | if (argv != nullptr) { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 70 | gCmdLine.reset(new std::string(argv[0])); |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 71 | for (size_t i = 1; argv[i] != nullptr; ++i) { |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame] | 72 | gCmdLine->append(" "); |
| 73 | gCmdLine->append(argv[i]); |
| 74 | } |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 75 | gProgramInvocationName.reset(new std::string(argv[0])); |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame] | 76 | const char* last_slash = strrchr(argv[0], '/'); |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 77 | gProgramInvocationShortName.reset(new std::string((last_slash != nullptr) ? last_slash + 1 |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 78 | : argv[0])); |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame] | 79 | } else { |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 80 | // TODO: fall back to /proc/self/cmdline when argv is NULL on Linux. |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 81 | gCmdLine.reset(new std::string("<unset>")); |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 82 | } |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 83 | const char* tags = getenv("ANDROID_LOG_TAGS"); |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 84 | if (tags == nullptr) { |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 85 | return; |
| 86 | } |
| 87 | |
| 88 | std::vector<std::string> specs; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 89 | Split(tags, ' ', &specs); |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 90 | for (size_t i = 0; i < specs.size(); ++i) { |
| 91 | // "tag-pattern:[vdiwefs]" |
| 92 | std::string spec(specs[i]); |
| 93 | if (spec.size() == 3 && StartsWith(spec, "*:")) { |
| 94 | switch (spec[2]) { |
Brian Carlstrom | f69863b | 2013-07-17 21:53:13 -0700 | [diff] [blame] | 95 | case 'v': |
| 96 | gMinimumLogSeverity = VERBOSE; |
| 97 | continue; |
| 98 | case 'd': |
| 99 | gMinimumLogSeverity = DEBUG; |
| 100 | continue; |
| 101 | case 'i': |
| 102 | gMinimumLogSeverity = INFO; |
| 103 | continue; |
| 104 | case 'w': |
| 105 | gMinimumLogSeverity = WARNING; |
| 106 | continue; |
| 107 | case 'e': |
| 108 | gMinimumLogSeverity = ERROR; |
| 109 | continue; |
| 110 | case 'f': |
| 111 | gMinimumLogSeverity = FATAL; |
| 112 | continue; |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 113 | // liblog will even suppress FATAL if you say 's' for silent, but that's crazy! |
Brian Carlstrom | f69863b | 2013-07-17 21:53:13 -0700 | [diff] [blame] | 114 | case 's': |
| 115 | gMinimumLogSeverity = FATAL; |
| 116 | continue; |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags << ")"; |
| 120 | } |
| 121 | } |
| 122 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 123 | // This indirection greatly reduces the stack impact of having |
| 124 | // lots of checks/logging in a function. |
| 125 | class LogMessageData { |
| 126 | public: |
| 127 | LogMessageData(const char* file, unsigned int line, LogSeverity severity, int error) |
| 128 | : file_(file), |
| 129 | line_number_(line), |
| 130 | severity_(severity), |
| 131 | error_(error) { |
| 132 | const char* last_slash = strrchr(file, '/'); |
| 133 | file = (last_slash == nullptr) ? file : last_slash + 1; |
| 134 | } |
Brian Carlstrom | af1b892 | 2012-11-27 15:19:57 -0800 | [diff] [blame] | 135 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 136 | const char * GetFile() const { |
| 137 | return file_; |
| 138 | } |
| 139 | |
| 140 | unsigned int GetLineNumber() const { |
| 141 | return line_number_; |
| 142 | } |
| 143 | |
| 144 | LogSeverity GetSeverity() const { |
| 145 | return severity_; |
| 146 | } |
| 147 | |
| 148 | int GetError() const { |
| 149 | return error_; |
| 150 | } |
| 151 | |
| 152 | std::ostream& GetBuffer() { |
| 153 | return buffer_; |
| 154 | } |
| 155 | |
| 156 | std::string ToString() const { |
| 157 | return buffer_.str(); |
| 158 | } |
| 159 | |
| 160 | private: |
| 161 | std::ostringstream buffer_; |
| 162 | const char* const file_; |
| 163 | const unsigned int line_number_; |
| 164 | const LogSeverity severity_; |
| 165 | const int error_; |
| 166 | |
| 167 | DISALLOW_COPY_AND_ASSIGN(LogMessageData); |
| 168 | }; |
| 169 | |
| 170 | |
| 171 | LogMessage::LogMessage(const char* file, unsigned int line, LogSeverity severity, int error) |
| 172 | : data_(new LogMessageData(file, line, severity, error)) { |
| 173 | } |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 174 | LogMessage::~LogMessage() { |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 175 | if (data_->GetSeverity() < gMinimumLogSeverity) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 176 | return; // No need to format something we're not going to output. |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 179 | // Finish constructing the message. |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 180 | if (data_->GetError() != -1) { |
| 181 | data_->GetBuffer() << ": " << strerror(data_->GetError()); |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 182 | } |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 183 | std::string msg(data_->ToString()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 184 | |
| 185 | // Do the actual logging with the lock held. |
| 186 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 187 | MutexLock mu(Thread::Current(), *Locks::logging_lock_); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 188 | if (msg.find('\n') == std::string::npos) { |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 189 | LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), msg.c_str()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 190 | } else { |
| 191 | msg += '\n'; |
| 192 | size_t i = 0; |
| 193 | while (i < msg.size()) { |
| 194 | size_t nl = msg.find('\n', i); |
| 195 | msg[nl] = '\0'; |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 196 | LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), &msg[i]); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 197 | i = nl + 1; |
| 198 | } |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 202 | // Abort if necessary. |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 203 | if (data_->GetSeverity() == FATAL) { |
Elliott Hughes | 8593fdb | 2012-04-21 20:53:44 -0700 | [diff] [blame] | 204 | Runtime::Abort(); |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 208 | std::ostream& LogMessage::stream() { |
| 209 | return data_->GetBuffer(); |
| 210 | } |
| 211 | |
| 212 | #ifdef HAVE_ANDROID_OS |
| 213 | static const android_LogPriority kLogSeverityToAndroidLogPriority[] = { |
| 214 | ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN, |
| 215 | ANDROID_LOG_ERROR, ANDROID_LOG_FATAL, ANDROID_LOG_FATAL |
| 216 | }; |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 217 | static_assert(arraysize(kLogSeverityToAndroidLogPriority) == INTERNAL_FATAL + 1, |
| 218 | "Mismatch in size of kLogSeverityToAndroidLogPriority and values in LogSeverity"); |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 219 | #endif |
| 220 | |
| 221 | void LogMessage::LogLine(const char* file, unsigned int line, LogSeverity log_severity, |
| 222 | const char* message) { |
| 223 | #ifdef HAVE_ANDROID_OS |
| 224 | const char* tag = ProgramInvocationShortName(); |
| 225 | int priority = kLogSeverityToAndroidLogPriority[log_severity]; |
| 226 | if (priority == ANDROID_LOG_FATAL) { |
| 227 | LOG_PRI(priority, tag, "%s:%u] %s", file, line, message); |
| 228 | } else { |
| 229 | LOG_PRI(priority, tag, "%s", message); |
| 230 | } |
| 231 | #else |
| 232 | static const char* log_characters = "VDIWEFF"; |
| 233 | CHECK_EQ(strlen(log_characters), INTERNAL_FATAL + 1U); |
| 234 | char severity = log_characters[log_severity]; |
| 235 | fprintf(stderr, "%s %c %5d %5d %s:%u] %s\n", |
| 236 | ProgramInvocationShortName(), severity, getpid(), ::art::GetTid(), file, line, message); |
| 237 | #endif |
| 238 | } |
| 239 | |
Ian Rogers | f4d4da1 | 2014-11-11 16:10:33 -0800 | [diff] [blame] | 240 | void LogMessage::LogLineLowStack(const char* file, unsigned int line, LogSeverity log_severity, |
| 241 | const char* message) { |
| 242 | #ifdef HAVE_ANDROID_OS |
Vladimir Marko | b8f2f63 | 2015-01-02 14:23:26 +0000 | [diff] [blame] | 243 | // Use android_writeLog() to avoid stack-based buffers used by android_printLog(). |
| 244 | const char* tag = ProgramInvocationShortName(); |
| 245 | int priority = kLogSeverityToAndroidLogPriority[log_severity]; |
| 246 | char* buf = nullptr; |
| 247 | size_t buf_size = 0u; |
| 248 | if (priority == ANDROID_LOG_FATAL) { |
| 249 | // Allocate buffer for snprintf(buf, buf_size, "%s:%u] %s", file, line, message) below. |
| 250 | // If allocation fails, fall back to printing only the message. |
| 251 | buf_size = strlen(file) + 1 /* ':' */ + std::numeric_limits<typeof(line)>::max_digits10 + |
| 252 | 2 /* "] " */ + strlen(message) + 1 /* terminating 0 */; |
| 253 | buf = reinterpret_cast<char*>(malloc(buf_size)); |
| 254 | } |
| 255 | if (buf != nullptr) { |
| 256 | snprintf(buf, buf_size, "%s:%u] %s", file, line, message); |
| 257 | android_writeLog(priority, tag, buf); |
| 258 | free(buf); |
| 259 | } else { |
| 260 | android_writeLog(priority, tag, message); |
| 261 | } |
Ian Rogers | f4d4da1 | 2014-11-11 16:10:33 -0800 | [diff] [blame] | 262 | #else |
| 263 | static const char* log_characters = "VDIWEFF"; |
| 264 | CHECK_EQ(strlen(log_characters), INTERNAL_FATAL + 1U); |
| 265 | |
| 266 | const char* program_name = ProgramInvocationShortName(); |
| 267 | write(STDERR_FILENO, program_name, strlen(program_name)); |
| 268 | write(STDERR_FILENO, " ", 1); |
| 269 | write(STDERR_FILENO, &log_characters[log_severity], 1); |
| 270 | write(STDERR_FILENO, " ", 1); |
| 271 | // TODO: pid and tid. |
| 272 | write(STDERR_FILENO, file, strlen(file)); |
| 273 | // TODO: line. |
| 274 | UNUSED(line); |
| 275 | write(STDERR_FILENO, "] ", 2); |
| 276 | write(STDERR_FILENO, message, strlen(message)); |
| 277 | write(STDERR_FILENO, "\n", 1); |
| 278 | #endif |
| 279 | } |
| 280 | |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 281 | } // namespace art |