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