blob: d3a2655c941d783e8d7801bd20236b101a6cb6ba [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
Ian Rogersf08e4732013-04-09 09:45:49 -070038unsigned int gAborting = 0;
Brian Carlstrom81b88712012-11-05 19:21:30 -080039
Elliott Hughes72395bf2012-04-24 13:45:26 -070040static LogSeverity gMinimumLogSeverity = INFO;
Ian Rogers700a4022014-05-19 16:49:03 -070041static std::unique_ptr<std::string> gCmdLine;
42static std::unique_ptr<std::string> gProgramInvocationName;
43static std::unique_ptr<std::string> gProgramInvocationShortName;
Elliott Hughes72395bf2012-04-24 13:45:26 -070044
Elliott Hughes0d39c122012-06-06 16:41:17 -070045const char* GetCmdLine() {
Ian Rogers1e363f92013-11-13 15:58:24 -080046 return (gCmdLine.get() != nullptr) ? gCmdLine->c_str() : nullptr;
Elliott Hughes0d39c122012-06-06 16:41:17 -070047}
48
49const char* ProgramInvocationName() {
Ian Rogers1e363f92013-11-13 15:58:24 -080050 return (gProgramInvocationName.get() != nullptr) ? gProgramInvocationName->c_str() : "art";
Elliott Hughes0d39c122012-06-06 16:41:17 -070051}
52
53const char* ProgramInvocationShortName() {
Ian Rogers1e363f92013-11-13 15:58:24 -080054 return (gProgramInvocationShortName.get() != nullptr) ? gProgramInvocationShortName->c_str()
55 : "art";
Elliott Hughes0d39c122012-06-06 16:41:17 -070056}
57
Elliott Hughes0d39c122012-06-06 16:41:17 -070058void InitLogging(char* argv[]) {
Ian Rogers1e363f92013-11-13 15:58:24 -080059 if (gCmdLine.get() != nullptr) {
Brian Carlstromfa42b442013-06-17 12:53:45 -070060 return;
61 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070062 // TODO: Move this to a more obvious InitART...
Ian Rogersb726dcb2012-09-05 08:57:23 -070063 Locks::Init();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070064
Elliott Hughes0d39c122012-06-06 16:41:17 -070065 // Stash the command line for later use. We can use /proc/self/cmdline on Linux to recover this,
66 // but we don't have that luxury on the Mac, and there are a couple of argv[0] variants that are
67 // commonly used.
Ian Rogersc7dd2952014-10-21 23:31:19 -070068 if (argv != nullptr) {
Ian Rogers1e363f92013-11-13 15:58:24 -080069 gCmdLine.reset(new std::string(argv[0]));
Ian Rogersc7dd2952014-10-21 23:31:19 -070070 for (size_t i = 1; argv[i] != nullptr; ++i) {
Brian Carlstromfa42b442013-06-17 12:53:45 -070071 gCmdLine->append(" ");
72 gCmdLine->append(argv[i]);
73 }
Ian Rogers1e363f92013-11-13 15:58:24 -080074 gProgramInvocationName.reset(new std::string(argv[0]));
Brian Carlstromfa42b442013-06-17 12:53:45 -070075 const char* last_slash = strrchr(argv[0], '/');
Ian Rogersc7dd2952014-10-21 23:31:19 -070076 gProgramInvocationShortName.reset(new std::string((last_slash != nullptr) ? last_slash + 1
Ian Rogers1e363f92013-11-13 15:58:24 -080077 : argv[0]));
Brian Carlstromfa42b442013-06-17 12:53:45 -070078 } else {
Ian Rogersc7dd2952014-10-21 23:31:19 -070079 // TODO: fall back to /proc/self/cmdline when argv is NULL on Linux.
Ian Rogers1e363f92013-11-13 15:58:24 -080080 gCmdLine.reset(new std::string("<unset>"));
Elliott Hughes0d39c122012-06-06 16:41:17 -070081 }
Elliott Hughes72395bf2012-04-24 13:45:26 -070082 const char* tags = getenv("ANDROID_LOG_TAGS");
Ian Rogersc7dd2952014-10-21 23:31:19 -070083 if (tags == nullptr) {
Elliott Hughes72395bf2012-04-24 13:45:26 -070084 return;
85 }
86
87 std::vector<std::string> specs;
Ian Rogers6f3dbba2014-10-14 17:41:57 -070088 Split(tags, ' ', &specs);
Elliott Hughes72395bf2012-04-24 13:45:26 -070089 for (size_t i = 0; i < specs.size(); ++i) {
90 // "tag-pattern:[vdiwefs]"
91 std::string spec(specs[i]);
92 if (spec.size() == 3 && StartsWith(spec, "*:")) {
93 switch (spec[2]) {
Brian Carlstromf69863b2013-07-17 21:53:13 -070094 case 'v':
95 gMinimumLogSeverity = VERBOSE;
96 continue;
97 case 'd':
98 gMinimumLogSeverity = DEBUG;
99 continue;
100 case 'i':
101 gMinimumLogSeverity = INFO;
102 continue;
103 case 'w':
104 gMinimumLogSeverity = WARNING;
105 continue;
106 case 'e':
107 gMinimumLogSeverity = ERROR;
108 continue;
109 case 'f':
110 gMinimumLogSeverity = FATAL;
111 continue;
Elliott Hughes72395bf2012-04-24 13:45:26 -0700112 // liblog will even suppress FATAL if you say 's' for silent, but that's crazy!
Brian Carlstromf69863b2013-07-17 21:53:13 -0700113 case 's':
114 gMinimumLogSeverity = FATAL;
115 continue;
Elliott Hughes72395bf2012-04-24 13:45:26 -0700116 }
117 }
118 LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags << ")";
119 }
120}
121
Ian Rogersc7dd2952014-10-21 23:31:19 -0700122// This indirection greatly reduces the stack impact of having
123// lots of checks/logging in a function.
124class LogMessageData {
125 public:
126 LogMessageData(const char* file, unsigned int line, LogSeverity severity, int error)
127 : file_(file),
128 line_number_(line),
129 severity_(severity),
130 error_(error) {
131 const char* last_slash = strrchr(file, '/');
132 file = (last_slash == nullptr) ? file : last_slash + 1;
133 }
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800134
Ian Rogersc7dd2952014-10-21 23:31:19 -0700135 const char * GetFile() const {
136 return file_;
137 }
138
139 unsigned int GetLineNumber() const {
140 return line_number_;
141 }
142
143 LogSeverity GetSeverity() const {
144 return severity_;
145 }
146
147 int GetError() const {
148 return error_;
149 }
150
151 std::ostream& GetBuffer() {
152 return buffer_;
153 }
154
155 std::string ToString() const {
156 return buffer_.str();
157 }
158
159 private:
160 std::ostringstream buffer_;
161 const char* const file_;
162 const unsigned int line_number_;
163 const LogSeverity severity_;
164 const int error_;
165
166 DISALLOW_COPY_AND_ASSIGN(LogMessageData);
167};
168
169
170LogMessage::LogMessage(const char* file, unsigned int line, LogSeverity severity, int error)
171 : data_(new LogMessageData(file, line, severity, error)) {
172}
Elliott Hughes13f5a582011-09-06 13:39:14 -0700173LogMessage::~LogMessage() {
Ian Rogersc7dd2952014-10-21 23:31:19 -0700174 if (data_->GetSeverity() < gMinimumLogSeverity) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700175 return; // No need to format something we're not going to output.
Elliott Hughes72395bf2012-04-24 13:45:26 -0700176 }
177
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700178 // Finish constructing the message.
Ian Rogersc7dd2952014-10-21 23:31:19 -0700179 if (data_->GetError() != -1) {
180 data_->GetBuffer() << ": " << strerror(data_->GetError());
Elliott Hughes13f5a582011-09-06 13:39:14 -0700181 }
Ian Rogersc7dd2952014-10-21 23:31:19 -0700182 std::string msg(data_->ToString());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700183
184 // Do the actual logging with the lock held.
185 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700186 MutexLock mu(Thread::Current(), *Locks::logging_lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700187 if (msg.find('\n') == std::string::npos) {
Ian Rogersc7dd2952014-10-21 23:31:19 -0700188 LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), msg.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700189 } else {
190 msg += '\n';
191 size_t i = 0;
192 while (i < msg.size()) {
193 size_t nl = msg.find('\n', i);
194 msg[nl] = '\0';
Ian Rogersc7dd2952014-10-21 23:31:19 -0700195 LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), &msg[i]);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700196 i = nl + 1;
197 }
Elliott Hughes13f5a582011-09-06 13:39:14 -0700198 }
199 }
200
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700201 // Abort if necessary.
Ian Rogersc7dd2952014-10-21 23:31:19 -0700202 if (data_->GetSeverity() == FATAL) {
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700203 Runtime::Abort();
Elliott Hughes13f5a582011-09-06 13:39:14 -0700204 }
205}
206
Ian Rogersc7dd2952014-10-21 23:31:19 -0700207std::ostream& LogMessage::stream() {
208 return data_->GetBuffer();
209}
210
211#ifdef HAVE_ANDROID_OS
212static const android_LogPriority kLogSeverityToAndroidLogPriority[] = {
213 ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN,
214 ANDROID_LOG_ERROR, ANDROID_LOG_FATAL, ANDROID_LOG_FATAL
215};
Andreas Gampe575e78c2014-11-03 23:41:03 -0800216static_assert(arraysize(kLogSeverityToAndroidLogPriority) == INTERNAL_FATAL + 1,
217 "Mismatch in size of kLogSeverityToAndroidLogPriority and values in LogSeverity");
Ian Rogersc7dd2952014-10-21 23:31:19 -0700218#endif
219
220void LogMessage::LogLine(const char* file, unsigned int line, LogSeverity log_severity,
221 const char* message) {
222#ifdef HAVE_ANDROID_OS
223 const char* tag = ProgramInvocationShortName();
224 int priority = kLogSeverityToAndroidLogPriority[log_severity];
225 if (priority == ANDROID_LOG_FATAL) {
226 LOG_PRI(priority, tag, "%s:%u] %s", file, line, message);
227 } else {
228 LOG_PRI(priority, tag, "%s", message);
229 }
230#else
231 static const char* log_characters = "VDIWEFF";
232 CHECK_EQ(strlen(log_characters), INTERNAL_FATAL + 1U);
233 char severity = log_characters[log_severity];
234 fprintf(stderr, "%s %c %5d %5d %s:%u] %s\n",
235 ProgramInvocationShortName(), severity, getpid(), ::art::GetTid(), file, line, message);
236#endif
237}
238
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700239} // namespace art