blob: bf6ab919d32dd97de0df1330a25a9eced6adae08 [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 Hughes8daa0922011-09-11 13:46:25 -070025art::Mutex& GetLoggingLock() {
26 static art::Mutex lock("LogMessage lock");
Elliott Hughes5fe594f2011-09-08 12:33:17 -070027 return lock;
28}
29
Elliott Hughes13f5a582011-09-06 13:39:14 -070030LogMessage::~LogMessage() {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070031 // Finish constructing the message.
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070032 if (data_->error != -1) {
33 data_->buffer << ": " << strerror(data_->error);
Elliott Hughes13f5a582011-09-06 13:39:14 -070034 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070035 std::string msg(data_->buffer.str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070036
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 Hughes13f5a582011-09-06 13:39:14 -070051 }
52 }
53
Elliott Hughes5fe594f2011-09-08 12:33:17 -070054 // Abort if necessary.
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070055 if (data_->severity == FATAL) {
56 art::Runtime::Abort(data_->file, data_->line_number);
Elliott Hughes13f5a582011-09-06 13:39:14 -070057 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070058
59 delete data_;
Elliott Hughes13f5a582011-09-06 13:39:14 -070060}
61
62std::ostream& LogMessage::stream() {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070063 return data_->buffer;
Elliott Hughes13f5a582011-09-06 13:39:14 -070064}
Elliott Hughesf5a7a472011-10-07 14:31:02 -070065
66} // namespace art