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 | |
| 19 | #include "runtime.h" |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 20 | #include "thread.h" |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 21 | #include "utils.h" |
| 22 | |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 23 | namespace art { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 24 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 25 | LogVerbosity gLogVerbosity; |
| 26 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 27 | static Mutex& GetLoggingLock() { |
| 28 | static Mutex logging_lock("LogMessage lock"); |
| 29 | return logging_lock; |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 32 | LogMessage::~LogMessage() { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 33 | // Finish constructing the message. |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 34 | if (data_->error != -1) { |
| 35 | data_->buffer << ": " << strerror(data_->error); |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 36 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 37 | std::string msg(data_->buffer.str()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 38 | |
| 39 | // Do the actual logging with the lock held. |
| 40 | { |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 41 | MutexLock mu(GetLoggingLock()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 42 | 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 Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 56 | // Abort if necessary. |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 57 | if (data_->severity == FATAL) { |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 58 | Runtime::Abort(data_->file, data_->line_number); |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 59 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 60 | |
| 61 | delete data_; |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | std::ostream& LogMessage::stream() { |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 65 | return data_->buffer; |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 66 | } |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 67 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 68 | /* |
| 69 | * Print a hex dump in this format: |
| 70 | * |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 71 | * 01234567: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 0123456789abcdef |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 72 | * |
| 73 | * Does not use printf() or other string-formatting calls. |
| 74 | */ |
| 75 | void HexDump(const void* address, size_t byte_count, bool show_actual_address) { |
| 76 | static const char gHexDigit[] = "0123456789abcdef"; |
| 77 | const unsigned char* addr = reinterpret_cast<const unsigned char*>(address); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 78 | char out[76]; /* exact fit */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 79 | unsigned int offset; /* offset to show while printing */ |
| 80 | |
| 81 | if (show_actual_address) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame^] | 82 | offset = reinterpret_cast<int>(addr); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 83 | } else { |
| 84 | offset = 0; |
| 85 | } |
| 86 | memset(out, ' ', sizeof(out)-1); |
| 87 | out[8] = ':'; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 88 | out[sizeof(out)-1] = '\0'; |
| 89 | |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame^] | 90 | int gap = static_cast<int>(offset & 0x0f); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 91 | while (byte_count) { |
| 92 | unsigned int lineOffset = offset & ~0x0f; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 93 | |
| 94 | char* hex = out; |
| 95 | char* asc = out + 59; |
| 96 | |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame^] | 97 | for (int i = 0; i < 8; i++) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 98 | *hex++ = gHexDigit[lineOffset >> 28]; |
| 99 | lineOffset <<= 4; |
| 100 | } |
| 101 | hex++; |
| 102 | hex++; |
| 103 | |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame^] | 104 | int count = std::min(static_cast<int>(byte_count), 16 - gap); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 105 | CHECK_NE(count, 0); |
| 106 | CHECK_LE(count + gap, 16); |
| 107 | |
| 108 | if (gap) { |
| 109 | /* only on first line */ |
| 110 | hex += gap * 3; |
| 111 | asc += gap; |
| 112 | } |
| 113 | |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame^] | 114 | int i; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 115 | for (i = gap ; i < count+gap; i++) { |
| 116 | *hex++ = gHexDigit[*addr >> 4]; |
| 117 | *hex++ = gHexDigit[*addr & 0x0f]; |
| 118 | hex++; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame^] | 119 | if (*addr >= 0x20 && *addr < 0x7f /*isprint(*addr)*/) { |
| 120 | *asc++ = *addr; |
| 121 | } else { |
| 122 | *asc++ = '.'; |
| 123 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 124 | addr++; |
| 125 | } |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame^] | 126 | for (; i < 16; i++) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 127 | /* erase extra stuff; only happens on last line */ |
| 128 | *hex++ = ' '; |
| 129 | *hex++ = ' '; |
| 130 | hex++; |
| 131 | *asc++ = ' '; |
| 132 | } |
| 133 | |
| 134 | LOG(INFO) << out; |
| 135 | |
| 136 | gap = 0; |
| 137 | byte_count -= count; |
| 138 | offset += count; |
| 139 | } |
| 140 | } |
| 141 | |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 142 | } // namespace art |