Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 19 | #include "base/mutex.h" |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 20 | #include "runtime.h" |
Brian Carlstrom | a3d2718 | 2013-11-05 23:22:27 -0800 | [diff] [blame] | 21 | #include "thread-inl.h" |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 22 | #include "utils.h" |
| 23 | |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 24 | namespace art { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 26 | LogVerbosity gLogVerbosity; |
| 27 | |
Mingyao Yang | 42d65c5 | 2014-04-18 16:49:39 -0700 | [diff] [blame] | 28 | std::vector<std::string> gVerboseMethods; |
| 29 | |
Ian Rogers | f08e473 | 2013-04-09 09:45:49 -0700 | [diff] [blame] | 30 | unsigned int gAborting = 0; |
Brian Carlstrom | 81b8871 | 2012-11-05 19:21:30 -0800 | [diff] [blame] | 31 | |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 32 | static LogSeverity gMinimumLogSeverity = INFO; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame^] | 33 | static std::unique_ptr<std::string> gCmdLine; |
| 34 | static std::unique_ptr<std::string> gProgramInvocationName; |
| 35 | static std::unique_ptr<std::string> gProgramInvocationShortName; |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 36 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 37 | const char* GetCmdLine() { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 38 | return (gCmdLine.get() != nullptr) ? gCmdLine->c_str() : nullptr; |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | const char* ProgramInvocationName() { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 42 | return (gProgramInvocationName.get() != nullptr) ? gProgramInvocationName->c_str() : "art"; |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | const char* ProgramInvocationShortName() { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 46 | return (gProgramInvocationShortName.get() != nullptr) ? gProgramInvocationShortName->c_str() |
| 47 | : "art"; |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 50 | // Configure logging based on ANDROID_LOG_TAGS environment variable. |
| 51 | // We need to parse a string that looks like |
| 52 | // |
| 53 | // *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i |
| 54 | // |
| 55 | // The tag (or '*' for the global level) comes first, followed by a colon |
| 56 | // and a letter indicating the minimum priority level we're expected to log. |
| 57 | // This can be used to reveal or conceal logs with specific tags. |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 58 | void InitLogging(char* argv[]) { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 59 | if (gCmdLine.get() != nullptr) { |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame] | 60 | return; |
| 61 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 62 | // TODO: Move this to a more obvious InitART... |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 63 | Locks::Init(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 64 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 65 | // 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. |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame] | 68 | if (argv != NULL) { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 69 | gCmdLine.reset(new std::string(argv[0])); |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame] | 70 | for (size_t i = 1; argv[i] != NULL; ++i) { |
| 71 | gCmdLine->append(" "); |
| 72 | gCmdLine->append(argv[i]); |
| 73 | } |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 74 | gProgramInvocationName.reset(new std::string(argv[0])); |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame] | 75 | const char* last_slash = strrchr(argv[0], '/'); |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 76 | gProgramInvocationShortName.reset(new std::string((last_slash != NULL) ? last_slash + 1 |
| 77 | : argv[0])); |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame] | 78 | } else { |
| 79 | // TODO: fall back to /proc/self/cmdline when argv is NULL on Linux |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 80 | gCmdLine.reset(new std::string("<unset>")); |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 81 | } |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 82 | const char* tags = getenv("ANDROID_LOG_TAGS"); |
| 83 | if (tags == NULL) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | std::vector<std::string> specs; |
| 88 | Split(tags, ' ', specs); |
| 89 | 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 Carlstrom | f69863b | 2013-07-17 21:53:13 -0700 | [diff] [blame] | 94 | 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 Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 112 | // liblog will even suppress FATAL if you say 's' for silent, but that's crazy! |
Brian Carlstrom | f69863b | 2013-07-17 21:53:13 -0700 | [diff] [blame] | 113 | case 's': |
| 114 | gMinimumLogSeverity = FATAL; |
| 115 | continue; |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags << ")"; |
| 119 | } |
| 120 | } |
| 121 | |
Brian Carlstrom | af1b892 | 2012-11-27 15:19:57 -0800 | [diff] [blame] | 122 | LogMessageData::LogMessageData(const char* file, int line, LogSeverity severity, int error) |
| 123 | : file(file), |
| 124 | line_number(line), |
| 125 | severity(severity), |
| 126 | error(error) { |
| 127 | const char* last_slash = strrchr(file, '/'); |
| 128 | file = (last_slash == NULL) ? file : last_slash + 1; |
| 129 | } |
| 130 | |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 131 | LogMessage::~LogMessage() { |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 132 | if (data_->severity < gMinimumLogSeverity) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 133 | return; // No need to format something we're not going to output. |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 136 | // Finish constructing the message. |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 137 | if (data_->error != -1) { |
| 138 | data_->buffer << ": " << strerror(data_->error); |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 139 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 140 | std::string msg(data_->buffer.str()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 141 | |
| 142 | // Do the actual logging with the lock held. |
| 143 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 144 | MutexLock mu(Thread::Current(), *Locks::logging_lock_); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 145 | if (msg.find('\n') == std::string::npos) { |
Brian Carlstrom | af1b892 | 2012-11-27 15:19:57 -0800 | [diff] [blame] | 146 | LogLine(*data_, msg.c_str()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 147 | } else { |
| 148 | msg += '\n'; |
| 149 | size_t i = 0; |
| 150 | while (i < msg.size()) { |
| 151 | size_t nl = msg.find('\n', i); |
| 152 | msg[nl] = '\0'; |
Brian Carlstrom | af1b892 | 2012-11-27 15:19:57 -0800 | [diff] [blame] | 153 | LogLine(*data_, &msg[i]); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 154 | i = nl + 1; |
| 155 | } |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 159 | // Abort if necessary. |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 160 | if (data_->severity == FATAL) { |
Elliott Hughes | 8593fdb | 2012-04-21 20:53:44 -0700 | [diff] [blame] | 161 | Runtime::Abort(); |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 165 | } // namespace art |