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 | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 23 | namespace { |
| 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 | |
| 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 | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 34 | if (errno_ != -1) { |
| 35 | buffer_ << ": " << strerror(errno_); |
| 36 | } |
| 37 | std::string msg(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 | { |
| 41 | art::MutexLock mu(GetLoggingLock()); |
| 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 | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 57 | if (severity_ == FATAL) { |
| 58 | art::Runtime::Abort(file_, line_number_); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | std::ostream& LogMessage::stream() { |
| 63 | return buffer_; |
| 64 | } |