blob: f962e41ccc0ad0fc8a7731fa9333f8d4f1e8d259 [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
19#include "runtime.h"
Elliott Hughes5fe594f2011-09-08 12:33:17 -070020#include "thread.h"
Elliott Hughes13f5a582011-09-06 13:39:14 -070021#include "utils.h"
22
Elliott Hughesf5a7a472011-10-07 14:31:02 -070023namespace art {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070024
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080025LogVerbosity gLogVerbosity;
26
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080027static Mutex& GetLoggingLock() {
28 static Mutex logging_lock("LogMessage lock");
29 return logging_lock;
Elliott Hughes5fe594f2011-09-08 12:33:17 -070030}
31
Elliott Hughes13f5a582011-09-06 13:39:14 -070032LogMessage::~LogMessage() {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070033 // Finish constructing the message.
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070034 if (data_->error != -1) {
35 data_->buffer << ": " << strerror(data_->error);
Elliott Hughes13f5a582011-09-06 13:39:14 -070036 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070037 std::string msg(data_->buffer.str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070038
39 // Do the actual logging with the lock held.
40 {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080041 MutexLock mu(GetLoggingLock());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070042 if (msg.find('\n') == std::string::npos) {
43 LogLine(msg.c_str());
44 } else {
45 msg += '\n';
46 size_t i = 0;
47 while (i < msg.size()) {
48 size_t nl = msg.find('\n', i);
49 msg[nl] = '\0';
50 LogLine(&msg[i]);
51 i = nl + 1;
52 }
Elliott Hughes13f5a582011-09-06 13:39:14 -070053 }
54 }
55
Elliott Hughes5fe594f2011-09-08 12:33:17 -070056 // Abort if necessary.
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070057 if (data_->severity == FATAL) {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080058 Runtime::Abort(data_->file, data_->line_number);
Elliott Hughes13f5a582011-09-06 13:39:14 -070059 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070060
61 delete data_;
Elliott Hughes13f5a582011-09-06 13:39:14 -070062}
63
64std::ostream& LogMessage::stream() {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070065 return data_->buffer;
Elliott Hughes13f5a582011-09-06 13:39:14 -070066}
Elliott Hughesf5a7a472011-10-07 14:31:02 -070067
Elliott Hughesbfbf0e22012-03-29 18:09:19 -070068HexDump::HexDump(const void* address, size_t byte_count, bool show_actual_addresses)
69 : address_(address), byte_count_(byte_count), show_actual_addresses_(show_actual_addresses) {
70}
71
72void HexDump::Dump(std::ostream& os) const {
73 if (byte_count_ == 0) {
74 return;
75 }
76
Elliott Hughes872d4ec2011-10-21 17:07:15 -070077 static const char gHexDigit[] = "0123456789abcdef";
Elliott Hughesbfbf0e22012-03-29 18:09:19 -070078 const unsigned char* addr = reinterpret_cast<const unsigned char*>(address_);
Elliott Hughes21f32d72011-11-09 17:44:13 -080079 char out[76]; /* exact fit */
Elliott Hughes872d4ec2011-10-21 17:07:15 -070080 unsigned int offset; /* offset to show while printing */
81
Elliott Hughesbfbf0e22012-03-29 18:09:19 -070082 if (show_actual_addresses_) {
Elliott Hughes398f64b2012-03-26 18:05:48 -070083 offset = reinterpret_cast<int>(addr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070084 } else {
85 offset = 0;
86 }
87 memset(out, ' ', sizeof(out)-1);
88 out[8] = ':';
Elliott Hughes872d4ec2011-10-21 17:07:15 -070089 out[sizeof(out)-1] = '\0';
90
Elliott Hughesbfbf0e22012-03-29 18:09:19 -070091 size_t byte_count = byte_count_;
Elliott Hughes398f64b2012-03-26 18:05:48 -070092 int gap = static_cast<int>(offset & 0x0f);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070093 while (byte_count) {
94 unsigned int lineOffset = offset & ~0x0f;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070095
96 char* hex = out;
97 char* asc = out + 59;
98
Elliott Hughes398f64b2012-03-26 18:05:48 -070099 for (int i = 0; i < 8; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700100 *hex++ = gHexDigit[lineOffset >> 28];
101 lineOffset <<= 4;
102 }
103 hex++;
104 hex++;
105
Elliott Hughes398f64b2012-03-26 18:05:48 -0700106 int count = std::min(static_cast<int>(byte_count), 16 - gap);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700107 CHECK_NE(count, 0);
108 CHECK_LE(count + gap, 16);
109
110 if (gap) {
111 /* only on first line */
112 hex += gap * 3;
113 asc += gap;
114 }
115
Elliott Hughes398f64b2012-03-26 18:05:48 -0700116 int i;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700117 for (i = gap ; i < count+gap; i++) {
118 *hex++ = gHexDigit[*addr >> 4];
119 *hex++ = gHexDigit[*addr & 0x0f];
120 hex++;
Elliott Hughes398f64b2012-03-26 18:05:48 -0700121 if (*addr >= 0x20 && *addr < 0x7f /*isprint(*addr)*/) {
122 *asc++ = *addr;
123 } else {
124 *asc++ = '.';
125 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700126 addr++;
127 }
Elliott Hughes398f64b2012-03-26 18:05:48 -0700128 for (; i < 16; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700129 /* erase extra stuff; only happens on last line */
130 *hex++ = ' ';
131 *hex++ = ' ';
132 hex++;
133 *asc++ = ' ';
134 }
135
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700136 os << out;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700137
138 gap = 0;
139 byte_count -= count;
140 offset += count;
141 }
142}
143
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700144std::ostream& operator<<(std::ostream& os, const HexDump& rhs) {
145 rhs.Dump(os);
146 return os;
147}
148
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700149} // namespace art