blob: 7c2ef71137d374c342aafff7851022c0398fd3d3 [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
Vladimir Markob8f2f632015-01-02 14:23:26 +000019#include <limits>
Ian Rogersc7dd2952014-10-21 23:31:19 -070020#include <sstream>
21
Elliott Hughes76b61672012-12-12 17:47:30 -080022#include "base/mutex.h"
Elliott Hughes13f5a582011-09-06 13:39:14 -070023#include "runtime.h"
Brian Carlstroma3d27182013-11-05 23:22:27 -080024#include "thread-inl.h"
Elliott Hughes13f5a582011-09-06 13:39:14 -070025#include "utils.h"
26
Ian Rogersc7dd2952014-10-21 23:31:19 -070027// 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 Hughesf5a7a472011-10-07 14:31:02 -070035namespace art {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070036
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080037LogVerbosity gLogVerbosity;
38
Nicolas Geoffraydb978712014-12-09 13:33:38 +000039unsigned int gAborting = 0;
40
Elliott Hughes72395bf2012-04-24 13:45:26 -070041static LogSeverity gMinimumLogSeverity = INFO;
Ian Rogers700a4022014-05-19 16:49:03 -070042static std::unique_ptr<std::string> gCmdLine;
43static std::unique_ptr<std::string> gProgramInvocationName;
44static std::unique_ptr<std::string> gProgramInvocationShortName;
Elliott Hughes72395bf2012-04-24 13:45:26 -070045
Elliott Hughes0d39c122012-06-06 16:41:17 -070046const char* GetCmdLine() {
Ian Rogers1e363f92013-11-13 15:58:24 -080047 return (gCmdLine.get() != nullptr) ? gCmdLine->c_str() : nullptr;
Elliott Hughes0d39c122012-06-06 16:41:17 -070048}
49
50const char* ProgramInvocationName() {
Ian Rogers1e363f92013-11-13 15:58:24 -080051 return (gProgramInvocationName.get() != nullptr) ? gProgramInvocationName->c_str() : "art";
Elliott Hughes0d39c122012-06-06 16:41:17 -070052}
53
54const char* ProgramInvocationShortName() {
Ian Rogers1e363f92013-11-13 15:58:24 -080055 return (gProgramInvocationShortName.get() != nullptr) ? gProgramInvocationShortName->c_str()
56 : "art";
Elliott Hughes0d39c122012-06-06 16:41:17 -070057}
58
Elliott Hughes0d39c122012-06-06 16:41:17 -070059void InitLogging(char* argv[]) {
Ian Rogers1e363f92013-11-13 15:58:24 -080060 if (gCmdLine.get() != nullptr) {
Brian Carlstromfa42b442013-06-17 12:53:45 -070061 return;
62 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070063 // TODO: Move this to a more obvious InitART...
Ian Rogersb726dcb2012-09-05 08:57:23 -070064 Locks::Init();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070065
Elliott Hughes0d39c122012-06-06 16:41:17 -070066 // 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 Rogersc7dd2952014-10-21 23:31:19 -070069 if (argv != nullptr) {
Ian Rogers1e363f92013-11-13 15:58:24 -080070 gCmdLine.reset(new std::string(argv[0]));
Ian Rogersc7dd2952014-10-21 23:31:19 -070071 for (size_t i = 1; argv[i] != nullptr; ++i) {
Brian Carlstromfa42b442013-06-17 12:53:45 -070072 gCmdLine->append(" ");
73 gCmdLine->append(argv[i]);
74 }
Ian Rogers1e363f92013-11-13 15:58:24 -080075 gProgramInvocationName.reset(new std::string(argv[0]));
Brian Carlstromfa42b442013-06-17 12:53:45 -070076 const char* last_slash = strrchr(argv[0], '/');
Ian Rogersc7dd2952014-10-21 23:31:19 -070077 gProgramInvocationShortName.reset(new std::string((last_slash != nullptr) ? last_slash + 1
Ian Rogers1e363f92013-11-13 15:58:24 -080078 : argv[0]));
Brian Carlstromfa42b442013-06-17 12:53:45 -070079 } else {
Ian Rogersc7dd2952014-10-21 23:31:19 -070080 // TODO: fall back to /proc/self/cmdline when argv is NULL on Linux.
Ian Rogers1e363f92013-11-13 15:58:24 -080081 gCmdLine.reset(new std::string("<unset>"));
Elliott Hughes0d39c122012-06-06 16:41:17 -070082 }
Elliott Hughes72395bf2012-04-24 13:45:26 -070083 const char* tags = getenv("ANDROID_LOG_TAGS");
Ian Rogersc7dd2952014-10-21 23:31:19 -070084 if (tags == nullptr) {
Elliott Hughes72395bf2012-04-24 13:45:26 -070085 return;
86 }
87
88 std::vector<std::string> specs;
Ian Rogers6f3dbba2014-10-14 17:41:57 -070089 Split(tags, ' ', &specs);
Elliott Hughes72395bf2012-04-24 13:45:26 -070090 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 Carlstromf69863b2013-07-17 21:53:13 -070095 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 Hughes72395bf2012-04-24 13:45:26 -0700113 // liblog will even suppress FATAL if you say 's' for silent, but that's crazy!
Brian Carlstromf69863b2013-07-17 21:53:13 -0700114 case 's':
115 gMinimumLogSeverity = FATAL;
116 continue;
Elliott Hughes72395bf2012-04-24 13:45:26 -0700117 }
118 }
119 LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags << ")";
120 }
121}
122
Ian Rogersc7dd2952014-10-21 23:31:19 -0700123// This indirection greatly reduces the stack impact of having
124// lots of checks/logging in a function.
125class 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 Carlstromaf1b8922012-11-27 15:19:57 -0800135
Ian Rogersc7dd2952014-10-21 23:31:19 -0700136 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
171LogMessage::LogMessage(const char* file, unsigned int line, LogSeverity severity, int error)
172 : data_(new LogMessageData(file, line, severity, error)) {
173}
Elliott Hughes13f5a582011-09-06 13:39:14 -0700174LogMessage::~LogMessage() {
Ian Rogersc7dd2952014-10-21 23:31:19 -0700175 if (data_->GetSeverity() < gMinimumLogSeverity) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700176 return; // No need to format something we're not going to output.
Elliott Hughes72395bf2012-04-24 13:45:26 -0700177 }
178
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700179 // Finish constructing the message.
Ian Rogersc7dd2952014-10-21 23:31:19 -0700180 if (data_->GetError() != -1) {
181 data_->GetBuffer() << ": " << strerror(data_->GetError());
Elliott Hughes13f5a582011-09-06 13:39:14 -0700182 }
Ian Rogersc7dd2952014-10-21 23:31:19 -0700183 std::string msg(data_->ToString());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700184
185 // Do the actual logging with the lock held.
186 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700187 MutexLock mu(Thread::Current(), *Locks::logging_lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700188 if (msg.find('\n') == std::string::npos) {
Ian Rogersc7dd2952014-10-21 23:31:19 -0700189 LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), msg.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700190 } 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 Rogersc7dd2952014-10-21 23:31:19 -0700196 LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), &msg[i]);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700197 i = nl + 1;
198 }
Elliott Hughes13f5a582011-09-06 13:39:14 -0700199 }
200 }
201
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700202 // Abort if necessary.
Ian Rogersc7dd2952014-10-21 23:31:19 -0700203 if (data_->GetSeverity() == FATAL) {
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700204 Runtime::Abort();
Elliott Hughes13f5a582011-09-06 13:39:14 -0700205 }
206}
207
Ian Rogersc7dd2952014-10-21 23:31:19 -0700208std::ostream& LogMessage::stream() {
209 return data_->GetBuffer();
210}
211
212#ifdef HAVE_ANDROID_OS
213static 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 Gampe575e78c2014-11-03 23:41:03 -0800217static_assert(arraysize(kLogSeverityToAndroidLogPriority) == INTERNAL_FATAL + 1,
218 "Mismatch in size of kLogSeverityToAndroidLogPriority and values in LogSeverity");
Ian Rogersc7dd2952014-10-21 23:31:19 -0700219#endif
220
221void 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 Rogersf4d4da12014-11-11 16:10:33 -0800240void LogMessage::LogLineLowStack(const char* file, unsigned int line, LogSeverity log_severity,
241 const char* message) {
242#ifdef HAVE_ANDROID_OS
Vladimir Markob8f2f632015-01-02 14:23:26 +0000243 // 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 Rogersf4d4da12014-11-11 16:10:33 -0800262#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 Hughesf5a7a472011-10-07 14:31:02 -0700281} // namespace art