blob: b6c6b9b2df6bc1b9b3cf5b3b32bae129d38c1ff2 [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
Elliott Hughes76b61672012-12-12 17:47:30 -080019#include "base/mutex.h"
Elliott Hughes13f5a582011-09-06 13:39:14 -070020#include "runtime.h"
Brian Carlstroma3d27182013-11-05 23:22:27 -080021#include "thread-inl.h"
Ian Rogers507dfdd2014-05-15 16:42:40 -070022#include "UniquePtrCompat.h"
Elliott Hughes13f5a582011-09-06 13:39:14 -070023#include "utils.h"
24
Elliott Hughesf5a7a472011-10-07 14:31:02 -070025namespace art {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070026
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080027LogVerbosity gLogVerbosity;
28
Mingyao Yang42d65c52014-04-18 16:49:39 -070029std::vector<std::string> gVerboseMethods;
30
Ian Rogersf08e4732013-04-09 09:45:49 -070031unsigned int gAborting = 0;
Brian Carlstrom81b88712012-11-05 19:21:30 -080032
Elliott Hughes72395bf2012-04-24 13:45:26 -070033static LogSeverity gMinimumLogSeverity = INFO;
Ian Rogers1e363f92013-11-13 15:58:24 -080034static UniquePtr<std::string> gCmdLine;
35static UniquePtr<std::string> gProgramInvocationName;
36static UniquePtr<std::string> gProgramInvocationShortName;
Elliott Hughes72395bf2012-04-24 13:45:26 -070037
Elliott Hughes0d39c122012-06-06 16:41:17 -070038const char* GetCmdLine() {
Ian Rogers1e363f92013-11-13 15:58:24 -080039 return (gCmdLine.get() != nullptr) ? gCmdLine->c_str() : nullptr;
Elliott Hughes0d39c122012-06-06 16:41:17 -070040}
41
42const char* ProgramInvocationName() {
Ian Rogers1e363f92013-11-13 15:58:24 -080043 return (gProgramInvocationName.get() != nullptr) ? gProgramInvocationName->c_str() : "art";
Elliott Hughes0d39c122012-06-06 16:41:17 -070044}
45
46const char* ProgramInvocationShortName() {
Ian Rogers1e363f92013-11-13 15:58:24 -080047 return (gProgramInvocationShortName.get() != nullptr) ? gProgramInvocationShortName->c_str()
48 : "art";
Elliott Hughes0d39c122012-06-06 16:41:17 -070049}
50
Elliott Hughes72395bf2012-04-24 13:45:26 -070051// Configure logging based on ANDROID_LOG_TAGS environment variable.
52// We need to parse a string that looks like
53//
54// *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i
55//
56// The tag (or '*' for the global level) comes first, followed by a colon
57// and a letter indicating the minimum priority level we're expected to log.
58// This can be used to reveal or conceal logs with specific tags.
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.
Brian Carlstromfa42b442013-06-17 12:53:45 -070069 if (argv != NULL) {
Ian Rogers1e363f92013-11-13 15:58:24 -080070 gCmdLine.reset(new std::string(argv[0]));
Brian Carlstromfa42b442013-06-17 12:53:45 -070071 for (size_t i = 1; argv[i] != NULL; ++i) {
72 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 Rogers1e363f92013-11-13 15:58:24 -080077 gProgramInvocationShortName.reset(new std::string((last_slash != NULL) ? last_slash + 1
78 : argv[0]));
Brian Carlstromfa42b442013-06-17 12:53:45 -070079 } else {
80 // 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");
84 if (tags == NULL) {
85 return;
86 }
87
88 std::vector<std::string> specs;
89 Split(tags, ' ', specs);
90 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
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800123LogMessageData::LogMessageData(const char* file, int line, LogSeverity severity, int error)
124 : file(file),
125 line_number(line),
126 severity(severity),
127 error(error) {
128 const char* last_slash = strrchr(file, '/');
129 file = (last_slash == NULL) ? file : last_slash + 1;
130}
131
Elliott Hughes13f5a582011-09-06 13:39:14 -0700132LogMessage::~LogMessage() {
Elliott Hughes72395bf2012-04-24 13:45:26 -0700133 if (data_->severity < gMinimumLogSeverity) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700134 return; // No need to format something we're not going to output.
Elliott Hughes72395bf2012-04-24 13:45:26 -0700135 }
136
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700137 // Finish constructing the message.
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700138 if (data_->error != -1) {
139 data_->buffer << ": " << strerror(data_->error);
Elliott Hughes13f5a582011-09-06 13:39:14 -0700140 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700141 std::string msg(data_->buffer.str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700142
143 // Do the actual logging with the lock held.
144 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700145 MutexLock mu(Thread::Current(), *Locks::logging_lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700146 if (msg.find('\n') == std::string::npos) {
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800147 LogLine(*data_, msg.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700148 } else {
149 msg += '\n';
150 size_t i = 0;
151 while (i < msg.size()) {
152 size_t nl = msg.find('\n', i);
153 msg[nl] = '\0';
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800154 LogLine(*data_, &msg[i]);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700155 i = nl + 1;
156 }
Elliott Hughes13f5a582011-09-06 13:39:14 -0700157 }
158 }
159
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700160 // Abort if necessary.
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700161 if (data_->severity == FATAL) {
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700162 Runtime::Abort();
Elliott Hughes13f5a582011-09-06 13:39:14 -0700163 }
164}
165
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700166} // namespace art