blob: 0764b877a135fd1da6b1f8b9810345df9b1132fd [file] [log] [blame]
Elliott Hughes13f5a582011-09-06 13:39:14 -07001/*
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
Andreas Gampeed957542015-01-07 18:01:29 -080019#include <iostream>
Vladimir Markob8f2f632015-01-02 14:23:26 +000020#include <limits>
Ian Rogersc7dd2952014-10-21 23:31:19 -070021#include <sstream>
22
Elliott Hughes76b61672012-12-12 17:47:30 -080023#include "base/mutex.h"
Elliott Hughes13f5a582011-09-06 13:39:14 -070024#include "runtime.h"
Brian Carlstroma3d27182013-11-05 23:22:27 -080025#include "thread-inl.h"
Elliott Hughes13f5a582011-09-06 13:39:14 -070026#include "utils.h"
27
Ian Rogersc7dd2952014-10-21 23:31:19 -070028// Headers for LogMessage::LogLine.
29#ifdef HAVE_ANDROID_OS
30#include "cutils/log.h"
31#else
32#include <sys/types.h>
33#include <unistd.h>
34#endif
35
Elliott Hughesf5a7a472011-10-07 14:31:02 -070036namespace art {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070037
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080038LogVerbosity gLogVerbosity;
39
Nicolas Geoffraydb978712014-12-09 13:33:38 +000040unsigned int gAborting = 0;
41
Elliott Hughes72395bf2012-04-24 13:45:26 -070042static LogSeverity gMinimumLogSeverity = INFO;
Ian Rogers700a4022014-05-19 16:49:03 -070043static std::unique_ptr<std::string> gCmdLine;
44static std::unique_ptr<std::string> gProgramInvocationName;
45static std::unique_ptr<std::string> gProgramInvocationShortName;
Elliott Hughes72395bf2012-04-24 13:45:26 -070046
Andreas Gampeed957542015-01-07 18:01:29 -080047// Print INTERNAL_FATAL messages directly instead of at destruction time. This only works on the
48// host right now: for the device, a stream buf collating output into lines and calling LogLine or
49// lower-level logging is necessary.
50#ifdef HAVE_ANDROID_OS
51static constexpr bool kPrintInternalFatalDirectly = false;
52#else
53static constexpr bool kPrintInternalFatalDirectly = !kIsTargetBuild;
54#endif
55
56static bool PrintDirectly(LogSeverity severity) {
57 return kPrintInternalFatalDirectly && severity == INTERNAL_FATAL;
58}
59
Elliott Hughes0d39c122012-06-06 16:41:17 -070060const char* GetCmdLine() {
Ian Rogers1e363f92013-11-13 15:58:24 -080061 return (gCmdLine.get() != nullptr) ? gCmdLine->c_str() : nullptr;
Elliott Hughes0d39c122012-06-06 16:41:17 -070062}
63
64const char* ProgramInvocationName() {
Ian Rogers1e363f92013-11-13 15:58:24 -080065 return (gProgramInvocationName.get() != nullptr) ? gProgramInvocationName->c_str() : "art";
Elliott Hughes0d39c122012-06-06 16:41:17 -070066}
67
68const char* ProgramInvocationShortName() {
Ian Rogers1e363f92013-11-13 15:58:24 -080069 return (gProgramInvocationShortName.get() != nullptr) ? gProgramInvocationShortName->c_str()
70 : "art";
Elliott Hughes0d39c122012-06-06 16:41:17 -070071}
72
Elliott Hughes0d39c122012-06-06 16:41:17 -070073void InitLogging(char* argv[]) {
Ian Rogers1e363f92013-11-13 15:58:24 -080074 if (gCmdLine.get() != nullptr) {
Brian Carlstromfa42b442013-06-17 12:53:45 -070075 return;
76 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070077 // TODO: Move this to a more obvious InitART...
Ian Rogersb726dcb2012-09-05 08:57:23 -070078 Locks::Init();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070079
Elliott Hughes0d39c122012-06-06 16:41:17 -070080 // Stash the command line for later use. We can use /proc/self/cmdline on Linux to recover this,
81 // but we don't have that luxury on the Mac, and there are a couple of argv[0] variants that are
82 // commonly used.
Ian Rogersc7dd2952014-10-21 23:31:19 -070083 if (argv != nullptr) {
Ian Rogers1e363f92013-11-13 15:58:24 -080084 gCmdLine.reset(new std::string(argv[0]));
Ian Rogersc7dd2952014-10-21 23:31:19 -070085 for (size_t i = 1; argv[i] != nullptr; ++i) {
Brian Carlstromfa42b442013-06-17 12:53:45 -070086 gCmdLine->append(" ");
87 gCmdLine->append(argv[i]);
88 }
Ian Rogers1e363f92013-11-13 15:58:24 -080089 gProgramInvocationName.reset(new std::string(argv[0]));
Brian Carlstromfa42b442013-06-17 12:53:45 -070090 const char* last_slash = strrchr(argv[0], '/');
Ian Rogersc7dd2952014-10-21 23:31:19 -070091 gProgramInvocationShortName.reset(new std::string((last_slash != nullptr) ? last_slash + 1
Ian Rogers1e363f92013-11-13 15:58:24 -080092 : argv[0]));
Brian Carlstromfa42b442013-06-17 12:53:45 -070093 } else {
Ian Rogersc7dd2952014-10-21 23:31:19 -070094 // TODO: fall back to /proc/self/cmdline when argv is NULL on Linux.
Ian Rogers1e363f92013-11-13 15:58:24 -080095 gCmdLine.reset(new std::string("<unset>"));
Elliott Hughes0d39c122012-06-06 16:41:17 -070096 }
Elliott Hughes72395bf2012-04-24 13:45:26 -070097 const char* tags = getenv("ANDROID_LOG_TAGS");
Ian Rogersc7dd2952014-10-21 23:31:19 -070098 if (tags == nullptr) {
Elliott Hughes72395bf2012-04-24 13:45:26 -070099 return;
100 }
101
102 std::vector<std::string> specs;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700103 Split(tags, ' ', &specs);
Elliott Hughes72395bf2012-04-24 13:45:26 -0700104 for (size_t i = 0; i < specs.size(); ++i) {
105 // "tag-pattern:[vdiwefs]"
106 std::string spec(specs[i]);
107 if (spec.size() == 3 && StartsWith(spec, "*:")) {
108 switch (spec[2]) {
Brian Carlstromf69863b2013-07-17 21:53:13 -0700109 case 'v':
110 gMinimumLogSeverity = VERBOSE;
111 continue;
112 case 'd':
113 gMinimumLogSeverity = DEBUG;
114 continue;
115 case 'i':
116 gMinimumLogSeverity = INFO;
117 continue;
118 case 'w':
119 gMinimumLogSeverity = WARNING;
120 continue;
121 case 'e':
122 gMinimumLogSeverity = ERROR;
123 continue;
124 case 'f':
125 gMinimumLogSeverity = FATAL;
126 continue;
Elliott Hughes72395bf2012-04-24 13:45:26 -0700127 // liblog will even suppress FATAL if you say 's' for silent, but that's crazy!
Brian Carlstromf69863b2013-07-17 21:53:13 -0700128 case 's':
129 gMinimumLogSeverity = FATAL;
130 continue;
Elliott Hughes72395bf2012-04-24 13:45:26 -0700131 }
132 }
133 LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags << ")";
134 }
135}
136
Ian Rogersc7dd2952014-10-21 23:31:19 -0700137// This indirection greatly reduces the stack impact of having
138// lots of checks/logging in a function.
139class LogMessageData {
140 public:
141 LogMessageData(const char* file, unsigned int line, LogSeverity severity, int error)
142 : file_(file),
143 line_number_(line),
144 severity_(severity),
145 error_(error) {
146 const char* last_slash = strrchr(file, '/');
147 file = (last_slash == nullptr) ? file : last_slash + 1;
148 }
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800149
Ian Rogersc7dd2952014-10-21 23:31:19 -0700150 const char * GetFile() const {
151 return file_;
152 }
153
154 unsigned int GetLineNumber() const {
155 return line_number_;
156 }
157
158 LogSeverity GetSeverity() const {
159 return severity_;
160 }
161
162 int GetError() const {
163 return error_;
164 }
165
166 std::ostream& GetBuffer() {
167 return buffer_;
168 }
169
170 std::string ToString() const {
171 return buffer_.str();
172 }
173
174 private:
175 std::ostringstream buffer_;
176 const char* const file_;
177 const unsigned int line_number_;
178 const LogSeverity severity_;
179 const int error_;
180
181 DISALLOW_COPY_AND_ASSIGN(LogMessageData);
182};
183
184
185LogMessage::LogMessage(const char* file, unsigned int line, LogSeverity severity, int error)
186 : data_(new LogMessageData(file, line, severity, error)) {
Andreas Gampeed957542015-01-07 18:01:29 -0800187 if (PrintDirectly(severity)) {
188 static const char* log_characters = "VDIWEFF";
189 CHECK_EQ(strlen(log_characters), INTERNAL_FATAL + 1U);
190 stream() << ProgramInvocationShortName() << " " << log_characters[static_cast<size_t>(severity)]
191 << " " << getpid() << " " << ::art::GetTid() << " " << file << ":" << line << "]";
192 }
Ian Rogersc7dd2952014-10-21 23:31:19 -0700193}
Elliott Hughes13f5a582011-09-06 13:39:14 -0700194LogMessage::~LogMessage() {
Andreas Gampeed957542015-01-07 18:01:29 -0800195 if (!PrintDirectly(data_->GetSeverity())) {
196 if (data_->GetSeverity() < gMinimumLogSeverity) {
197 return; // No need to format something we're not going to output.
198 }
Elliott Hughes72395bf2012-04-24 13:45:26 -0700199
Andreas Gampeed957542015-01-07 18:01:29 -0800200 // Finish constructing the message.
201 if (data_->GetError() != -1) {
202 data_->GetBuffer() << ": " << strerror(data_->GetError());
203 }
204 std::string msg(data_->ToString());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700205
Andreas Gampeed957542015-01-07 18:01:29 -0800206 // Do the actual logging with the lock held.
207 {
208 MutexLock mu(Thread::Current(), *Locks::logging_lock_);
209 if (msg.find('\n') == std::string::npos) {
210 LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), msg.c_str());
211 } else {
212 msg += '\n';
213 size_t i = 0;
214 while (i < msg.size()) {
215 size_t nl = msg.find('\n', i);
216 msg[nl] = '\0';
217 LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), &msg[i]);
218 i = nl + 1;
219 }
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700220 }
Elliott Hughes13f5a582011-09-06 13:39:14 -0700221 }
222 }
223
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700224 // Abort if necessary.
Ian Rogersc7dd2952014-10-21 23:31:19 -0700225 if (data_->GetSeverity() == FATAL) {
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700226 Runtime::Abort();
Elliott Hughes13f5a582011-09-06 13:39:14 -0700227 }
228}
229
Ian Rogersc7dd2952014-10-21 23:31:19 -0700230std::ostream& LogMessage::stream() {
Andreas Gampeed957542015-01-07 18:01:29 -0800231 if (PrintDirectly(data_->GetSeverity())) {
232 return std::cerr;
233 }
Ian Rogersc7dd2952014-10-21 23:31:19 -0700234 return data_->GetBuffer();
235}
236
237#ifdef HAVE_ANDROID_OS
238static const android_LogPriority kLogSeverityToAndroidLogPriority[] = {
239 ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN,
240 ANDROID_LOG_ERROR, ANDROID_LOG_FATAL, ANDROID_LOG_FATAL
241};
Andreas Gampe575e78c2014-11-03 23:41:03 -0800242static_assert(arraysize(kLogSeverityToAndroidLogPriority) == INTERNAL_FATAL + 1,
243 "Mismatch in size of kLogSeverityToAndroidLogPriority and values in LogSeverity");
Ian Rogersc7dd2952014-10-21 23:31:19 -0700244#endif
245
246void LogMessage::LogLine(const char* file, unsigned int line, LogSeverity log_severity,
247 const char* message) {
248#ifdef HAVE_ANDROID_OS
249 const char* tag = ProgramInvocationShortName();
250 int priority = kLogSeverityToAndroidLogPriority[log_severity];
251 if (priority == ANDROID_LOG_FATAL) {
252 LOG_PRI(priority, tag, "%s:%u] %s", file, line, message);
253 } else {
254 LOG_PRI(priority, tag, "%s", message);
255 }
256#else
257 static const char* log_characters = "VDIWEFF";
258 CHECK_EQ(strlen(log_characters), INTERNAL_FATAL + 1U);
259 char severity = log_characters[log_severity];
260 fprintf(stderr, "%s %c %5d %5d %s:%u] %s\n",
261 ProgramInvocationShortName(), severity, getpid(), ::art::GetTid(), file, line, message);
262#endif
263}
264
Ian Rogersf4d4da12014-11-11 16:10:33 -0800265void LogMessage::LogLineLowStack(const char* file, unsigned int line, LogSeverity log_severity,
266 const char* message) {
267#ifdef HAVE_ANDROID_OS
Vladimir Markob8f2f632015-01-02 14:23:26 +0000268 // Use android_writeLog() to avoid stack-based buffers used by android_printLog().
269 const char* tag = ProgramInvocationShortName();
270 int priority = kLogSeverityToAndroidLogPriority[log_severity];
271 char* buf = nullptr;
272 size_t buf_size = 0u;
273 if (priority == ANDROID_LOG_FATAL) {
274 // Allocate buffer for snprintf(buf, buf_size, "%s:%u] %s", file, line, message) below.
275 // If allocation fails, fall back to printing only the message.
276 buf_size = strlen(file) + 1 /* ':' */ + std::numeric_limits<typeof(line)>::max_digits10 +
277 2 /* "] " */ + strlen(message) + 1 /* terminating 0 */;
278 buf = reinterpret_cast<char*>(malloc(buf_size));
279 }
280 if (buf != nullptr) {
281 snprintf(buf, buf_size, "%s:%u] %s", file, line, message);
282 android_writeLog(priority, tag, buf);
283 free(buf);
284 } else {
285 android_writeLog(priority, tag, message);
286 }
Ian Rogersf4d4da12014-11-11 16:10:33 -0800287#else
288 static const char* log_characters = "VDIWEFF";
289 CHECK_EQ(strlen(log_characters), INTERNAL_FATAL + 1U);
290
291 const char* program_name = ProgramInvocationShortName();
292 write(STDERR_FILENO, program_name, strlen(program_name));
293 write(STDERR_FILENO, " ", 1);
294 write(STDERR_FILENO, &log_characters[log_severity], 1);
295 write(STDERR_FILENO, " ", 1);
296 // TODO: pid and tid.
297 write(STDERR_FILENO, file, strlen(file));
298 // TODO: line.
299 UNUSED(line);
300 write(STDERR_FILENO, "] ", 2);
301 write(STDERR_FILENO, message, strlen(message));
302 write(STDERR_FILENO, "\n", 1);
303#endif
304}
305
Andreas Gampe369810a2015-01-14 19:53:31 -0800306ScopedLogSeverity::ScopedLogSeverity(LogSeverity level) {
307 old_ = gMinimumLogSeverity;
308 gMinimumLogSeverity = level;
309}
310
311ScopedLogSeverity::~ScopedLogSeverity() {
312 gMinimumLogSeverity = old_;
313}
314
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700315} // namespace art