blob: c305c4a2d109731204c5b6b5a64796b71f19d50e [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
Andreas Gampeed957542015-01-07 18:01:29 -080019#include <iostream>
Vladimir Markob8f2f632015-01-02 14:23:26 +000020#include <limits>
Ian Rogersc7dd2952014-10-21 23:31:19 -070021#include <sstream>
22
Elliott Hughes76b61672012-12-12 17:47:30 -080023#include "base/mutex.h"
Elliott Hughes13f5a582011-09-06 13:39:14 -070024#include "runtime.h"
Brian Carlstroma3d27182013-11-05 23:22:27 -080025#include "thread-inl.h"
Elliott Hughes13f5a582011-09-06 13:39:14 -070026#include "utils.h"
27
Ian Rogersc7dd2952014-10-21 23:31:19 -070028// Headers for LogMessage::LogLine.
Bilyan Borisovbb661c02016-04-04 16:27:32 +010029#ifdef ART_TARGET_ANDROID
Ian Rogersc7dd2952014-10-21 23:31:19 -070030#include "cutils/log.h"
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070031#include <android/set_abort_message.h>
Ian Rogersc7dd2952014-10-21 23:31:19 -070032#else
33#include <sys/types.h>
34#include <unistd.h>
35#endif
36
Elliott Hughesf5a7a472011-10-07 14:31:02 -070037namespace art {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070038
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080039LogVerbosity gLogVerbosity;
40
Nicolas Geoffraydb978712014-12-09 13:33:38 +000041unsigned int gAborting = 0;
42
Ian Rogers700a4022014-05-19 16:49:03 -070043static std::unique_ptr<std::string> gCmdLine;
44static std::unique_ptr<std::string> gProgramInvocationName;
45static std::unique_ptr<std::string> gProgramInvocationShortName;
Elliott Hughes72395bf2012-04-24 13:45:26 -070046
Elliott Hughes0d39c122012-06-06 16:41:17 -070047const char* GetCmdLine() {
Ian Rogers1e363f92013-11-13 15:58:24 -080048 return (gCmdLine.get() != nullptr) ? gCmdLine->c_str() : nullptr;
Elliott Hughes0d39c122012-06-06 16:41:17 -070049}
50
51const char* ProgramInvocationName() {
Ian Rogers1e363f92013-11-13 15:58:24 -080052 return (gProgramInvocationName.get() != nullptr) ? gProgramInvocationName->c_str() : "art";
Elliott Hughes0d39c122012-06-06 16:41:17 -070053}
54
55const char* ProgramInvocationShortName() {
Ian Rogers1e363f92013-11-13 15:58:24 -080056 return (gProgramInvocationShortName.get() != nullptr) ? gProgramInvocationShortName->c_str()
57 : "art";
Elliott Hughes0d39c122012-06-06 16:41:17 -070058}
59
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070060NO_RETURN
61static void RuntimeAborter(const char* abort_message) {
62#ifdef __ANDROID__
63 android_set_abort_message(abort_message);
64#else
65 UNUSED(abort_message);
66#endif
Andreas Gampe90a32b12016-10-03 19:47:08 -070067 Runtime::Abort(abort_message);
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070068}
69
Elliott Hughes0d39c122012-06-06 16:41:17 -070070void InitLogging(char* argv[]) {
Ian Rogers1e363f92013-11-13 15:58:24 -080071 if (gCmdLine.get() != nullptr) {
Brian Carlstromfa42b442013-06-17 12:53:45 -070072 return;
73 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070074 // TODO: Move this to a more obvious InitART...
Ian Rogersb726dcb2012-09-05 08:57:23 -070075 Locks::Init();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070076
Elliott Hughes0d39c122012-06-06 16:41:17 -070077 // Stash the command line for later use. We can use /proc/self/cmdline on Linux to recover this,
78 // but we don't have that luxury on the Mac, and there are a couple of argv[0] variants that are
79 // commonly used.
Ian Rogersc7dd2952014-10-21 23:31:19 -070080 if (argv != nullptr) {
Ian Rogers1e363f92013-11-13 15:58:24 -080081 gCmdLine.reset(new std::string(argv[0]));
Ian Rogersc7dd2952014-10-21 23:31:19 -070082 for (size_t i = 1; argv[i] != nullptr; ++i) {
Brian Carlstromfa42b442013-06-17 12:53:45 -070083 gCmdLine->append(" ");
84 gCmdLine->append(argv[i]);
85 }
Ian Rogers1e363f92013-11-13 15:58:24 -080086 gProgramInvocationName.reset(new std::string(argv[0]));
Brian Carlstromfa42b442013-06-17 12:53:45 -070087 const char* last_slash = strrchr(argv[0], '/');
Ian Rogersc7dd2952014-10-21 23:31:19 -070088 gProgramInvocationShortName.reset(new std::string((last_slash != nullptr) ? last_slash + 1
Ian Rogers1e363f92013-11-13 15:58:24 -080089 : argv[0]));
Brian Carlstromfa42b442013-06-17 12:53:45 -070090 } else {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070091 // TODO: fall back to /proc/self/cmdline when argv is null on Linux.
Ian Rogers1e363f92013-11-13 15:58:24 -080092 gCmdLine.reset(new std::string("<unset>"));
Elliott Hughes0d39c122012-06-06 16:41:17 -070093 }
Elliott Hughes72395bf2012-04-24 13:45:26 -070094
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070095#ifdef __ANDROID__
96#define INIT_LOGGING_DEFAULT_LOGGER android::base::LogdLogger()
97#else
98#define INIT_LOGGING_DEFAULT_LOGGER android::base::StderrLogger
99#endif
100 android::base::InitLogging(argv, INIT_LOGGING_DEFAULT_LOGGER, RuntimeAborter);
101#undef INIT_LOGGING_DEFAULT_LOGGER
Ian Rogersc7dd2952014-10-21 23:31:19 -0700102}
103
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100104#ifdef ART_TARGET_ANDROID
Ian Rogersc7dd2952014-10-21 23:31:19 -0700105static const android_LogPriority kLogSeverityToAndroidLogPriority[] = {
106 ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN,
107 ANDROID_LOG_ERROR, ANDROID_LOG_FATAL, ANDROID_LOG_FATAL
108};
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700109static_assert(arraysize(kLogSeverityToAndroidLogPriority) == ::android::base::FATAL + 1,
Andreas Gampe575e78c2014-11-03 23:41:03 -0800110 "Mismatch in size of kLogSeverityToAndroidLogPriority and values in LogSeverity");
Ian Rogersc7dd2952014-10-21 23:31:19 -0700111#endif
112
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700113void LogHelper::LogLineLowStack(const char* file,
114 unsigned int line,
115 LogSeverity log_severity,
116 const char* message) {
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100117#ifdef ART_TARGET_ANDROID
Vladimir Markob8f2f632015-01-02 14:23:26 +0000118 // Use android_writeLog() to avoid stack-based buffers used by android_printLog().
119 const char* tag = ProgramInvocationShortName();
Andreas Gampe7fe30232016-03-25 16:58:00 -0700120 int priority = kLogSeverityToAndroidLogPriority[static_cast<size_t>(log_severity)];
Vladimir Markob8f2f632015-01-02 14:23:26 +0000121 char* buf = nullptr;
122 size_t buf_size = 0u;
123 if (priority == ANDROID_LOG_FATAL) {
124 // Allocate buffer for snprintf(buf, buf_size, "%s:%u] %s", file, line, message) below.
125 // If allocation fails, fall back to printing only the message.
126 buf_size = strlen(file) + 1 /* ':' */ + std::numeric_limits<typeof(line)>::max_digits10 +
127 2 /* "] " */ + strlen(message) + 1 /* terminating 0 */;
128 buf = reinterpret_cast<char*>(malloc(buf_size));
129 }
130 if (buf != nullptr) {
131 snprintf(buf, buf_size, "%s:%u] %s", file, line, message);
132 android_writeLog(priority, tag, buf);
133 free(buf);
134 } else {
135 android_writeLog(priority, tag, message);
136 }
Ian Rogersf4d4da12014-11-11 16:10:33 -0800137#else
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700138 static constexpr char kLogCharacters[] = { 'V', 'D', 'I', 'W', 'E', 'F', 'F' };
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700139 static_assert(
140 arraysize(kLogCharacters) == static_cast<size_t>(::android::base::FATAL) + 1,
141 "Wrong character array size");
Ian Rogersf4d4da12014-11-11 16:10:33 -0800142
143 const char* program_name = ProgramInvocationShortName();
Elliott Hughes06f08e42015-05-12 21:25:36 -0700144 TEMP_FAILURE_RETRY(write(STDERR_FILENO, program_name, strlen(program_name)));
145 TEMP_FAILURE_RETRY(write(STDERR_FILENO, " ", 1));
Andreas Gampe7fe30232016-03-25 16:58:00 -0700146 TEMP_FAILURE_RETRY(write(STDERR_FILENO, &kLogCharacters[static_cast<size_t>(log_severity)], 1));
Elliott Hughes06f08e42015-05-12 21:25:36 -0700147 TEMP_FAILURE_RETRY(write(STDERR_FILENO, " ", 1));
Ian Rogersf4d4da12014-11-11 16:10:33 -0800148 // TODO: pid and tid.
Elliott Hughes06f08e42015-05-12 21:25:36 -0700149 TEMP_FAILURE_RETRY(write(STDERR_FILENO, file, strlen(file)));
Ian Rogersf4d4da12014-11-11 16:10:33 -0800150 // TODO: line.
151 UNUSED(line);
Elliott Hughes06f08e42015-05-12 21:25:36 -0700152 TEMP_FAILURE_RETRY(write(STDERR_FILENO, "] ", 2));
153 TEMP_FAILURE_RETRY(write(STDERR_FILENO, message, strlen(message)));
154 TEMP_FAILURE_RETRY(write(STDERR_FILENO, "\n", 1));
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100155#endif // ART_TARGET_ANDROID
Ian Rogersf4d4da12014-11-11 16:10:33 -0800156}
157
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700158} // namespace art