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