blob: bdc4cf63999af737eb99e3e157837024d809a718 [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
Ian Rogersc7dd2952014-10-21 23:31:19 -070019#include <sstream>
20
Elliott Hughes76b61672012-12-12 17:47:30 -080021#include "base/mutex.h"
Elliott Hughes13f5a582011-09-06 13:39:14 -070022#include "runtime.h"
Brian Carlstroma3d27182013-11-05 23:22:27 -080023#include "thread-inl.h"
Elliott Hughes13f5a582011-09-06 13:39:14 -070024#include "utils.h"
25
Ian Rogersc7dd2952014-10-21 23:31:19 -070026// 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 Hughesf5a7a472011-10-07 14:31:02 -070034namespace art {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070035
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080036LogVerbosity gLogVerbosity;
37
Elliott Hughes72395bf2012-04-24 13:45:26 -070038static LogSeverity gMinimumLogSeverity = INFO;
Ian Rogers700a4022014-05-19 16:49:03 -070039static std::unique_ptr<std::string> gCmdLine;
40static std::unique_ptr<std::string> gProgramInvocationName;
41static std::unique_ptr<std::string> gProgramInvocationShortName;
Elliott Hughes72395bf2012-04-24 13:45:26 -070042
Elliott Hughes0d39c122012-06-06 16:41:17 -070043const char* GetCmdLine() {
Ian Rogers1e363f92013-11-13 15:58:24 -080044 return (gCmdLine.get() != nullptr) ? gCmdLine->c_str() : nullptr;
Elliott Hughes0d39c122012-06-06 16:41:17 -070045}
46
47const char* ProgramInvocationName() {
Ian Rogers1e363f92013-11-13 15:58:24 -080048 return (gProgramInvocationName.get() != nullptr) ? gProgramInvocationName->c_str() : "art";
Elliott Hughes0d39c122012-06-06 16:41:17 -070049}
50
51const char* ProgramInvocationShortName() {
Ian Rogers1e363f92013-11-13 15:58:24 -080052 return (gProgramInvocationShortName.get() != nullptr) ? gProgramInvocationShortName->c_str()
53 : "art";
Elliott Hughes0d39c122012-06-06 16:41:17 -070054}
55
Elliott Hughes0d39c122012-06-06 16:41:17 -070056void InitLogging(char* argv[]) {
Ian Rogers1e363f92013-11-13 15:58:24 -080057 if (gCmdLine.get() != nullptr) {
Brian Carlstromfa42b442013-06-17 12:53:45 -070058 return;
59 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070060 // TODO: Move this to a more obvious InitART...
Ian Rogersb726dcb2012-09-05 08:57:23 -070061 Locks::Init();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070062
Elliott Hughes0d39c122012-06-06 16:41:17 -070063 // 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 Rogersc7dd2952014-10-21 23:31:19 -070066 if (argv != nullptr) {
Ian Rogers1e363f92013-11-13 15:58:24 -080067 gCmdLine.reset(new std::string(argv[0]));
Ian Rogersc7dd2952014-10-21 23:31:19 -070068 for (size_t i = 1; argv[i] != nullptr; ++i) {
Brian Carlstromfa42b442013-06-17 12:53:45 -070069 gCmdLine->append(" ");
70 gCmdLine->append(argv[i]);
71 }
Ian Rogers1e363f92013-11-13 15:58:24 -080072 gProgramInvocationName.reset(new std::string(argv[0]));
Brian Carlstromfa42b442013-06-17 12:53:45 -070073 const char* last_slash = strrchr(argv[0], '/');
Ian Rogersc7dd2952014-10-21 23:31:19 -070074 gProgramInvocationShortName.reset(new std::string((last_slash != nullptr) ? last_slash + 1
Ian Rogers1e363f92013-11-13 15:58:24 -080075 : argv[0]));
Brian Carlstromfa42b442013-06-17 12:53:45 -070076 } else {
Ian Rogersc7dd2952014-10-21 23:31:19 -070077 // TODO: fall back to /proc/self/cmdline when argv is NULL on Linux.
Ian Rogers1e363f92013-11-13 15:58:24 -080078 gCmdLine.reset(new std::string("<unset>"));
Elliott Hughes0d39c122012-06-06 16:41:17 -070079 }
Elliott Hughes72395bf2012-04-24 13:45:26 -070080 const char* tags = getenv("ANDROID_LOG_TAGS");
Ian Rogersc7dd2952014-10-21 23:31:19 -070081 if (tags == nullptr) {
Elliott Hughes72395bf2012-04-24 13:45:26 -070082 return;
83 }
84
85 std::vector<std::string> specs;
Ian Rogers6f3dbba2014-10-14 17:41:57 -070086 Split(tags, ' ', &specs);
Elliott Hughes72395bf2012-04-24 13:45:26 -070087 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 Carlstromf69863b2013-07-17 21:53:13 -070092 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 Hughes72395bf2012-04-24 13:45:26 -0700110 // liblog will even suppress FATAL if you say 's' for silent, but that's crazy!
Brian Carlstromf69863b2013-07-17 21:53:13 -0700111 case 's':
112 gMinimumLogSeverity = FATAL;
113 continue;
Elliott Hughes72395bf2012-04-24 13:45:26 -0700114 }
115 }
116 LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags << ")";
117 }
118}
119
Ian Rogersc7dd2952014-10-21 23:31:19 -0700120// This indirection greatly reduces the stack impact of having
121// lots of checks/logging in a function.
122class 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 Carlstromaf1b8922012-11-27 15:19:57 -0800132
Ian Rogersc7dd2952014-10-21 23:31:19 -0700133 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
168LogMessage::LogMessage(const char* file, unsigned int line, LogSeverity severity, int error)
169 : data_(new LogMessageData(file, line, severity, error)) {
170}
Elliott Hughes13f5a582011-09-06 13:39:14 -0700171LogMessage::~LogMessage() {
Ian Rogersc7dd2952014-10-21 23:31:19 -0700172 if (data_->GetSeverity() < gMinimumLogSeverity) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700173 return; // No need to format something we're not going to output.
Elliott Hughes72395bf2012-04-24 13:45:26 -0700174 }
175
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700176 // Finish constructing the message.
Ian Rogersc7dd2952014-10-21 23:31:19 -0700177 if (data_->GetError() != -1) {
178 data_->GetBuffer() << ": " << strerror(data_->GetError());
Elliott Hughes13f5a582011-09-06 13:39:14 -0700179 }
Ian Rogersc7dd2952014-10-21 23:31:19 -0700180 std::string msg(data_->ToString());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700181
182 // Do the actual logging with the lock held.
183 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700184 MutexLock mu(Thread::Current(), *Locks::logging_lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700185 if (msg.find('\n') == std::string::npos) {
Ian Rogersc7dd2952014-10-21 23:31:19 -0700186 LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), msg.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700187 } 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 Rogersc7dd2952014-10-21 23:31:19 -0700193 LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), &msg[i]);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700194 i = nl + 1;
195 }
Elliott Hughes13f5a582011-09-06 13:39:14 -0700196 }
197 }
198
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700199 // Abort if necessary.
Ian Rogersc7dd2952014-10-21 23:31:19 -0700200 if (data_->GetSeverity() == FATAL) {
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700201 Runtime::Abort();
Elliott Hughes13f5a582011-09-06 13:39:14 -0700202 }
203}
204
Ian Rogersc7dd2952014-10-21 23:31:19 -0700205std::ostream& LogMessage::stream() {
206 return data_->GetBuffer();
207}
208
209#ifdef HAVE_ANDROID_OS
210static 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 Gampe575e78c2014-11-03 23:41:03 -0800214static_assert(arraysize(kLogSeverityToAndroidLogPriority) == INTERNAL_FATAL + 1,
215 "Mismatch in size of kLogSeverityToAndroidLogPriority and values in LogSeverity");
Ian Rogersc7dd2952014-10-21 23:31:19 -0700216#endif
217
218void 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 Rogersf4d4da12014-11-11 16:10:33 -0800237void 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 Hughesf5a7a472011-10-07 14:31:02 -0700261} // namespace art