Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 2 | |
| 3 | #include "logging.h" |
| 4 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 5 | #include "runtime.h" |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 6 | #include "scoped_ptr.h" |
| 7 | #include "stringprintf.h" |
| 8 | |
| 9 | #include <cstdio> |
| 10 | #include <cstring> |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 11 | #include <iostream> |
| 12 | #include <sys/types.h> |
| 13 | #include <unistd.h> |
| 14 | |
| 15 | // glibc doesn't expose gettid(2). |
| 16 | #define __KERNEL__ |
| 17 | # include <linux/unistd.h> |
| 18 | #ifdef _syscall0 |
| 19 | _syscall0(pid_t,gettid) |
| 20 | #else |
| 21 | pid_t gettid() { return syscall(__NR_gettid);} |
| 22 | #endif |
| 23 | #undef __KERNEL__ |
| 24 | |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 25 | LogMessage::LogMessage(const char* file, int line, LogSeverity severity, int error) |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 26 | : line_(line), severity_(severity), errno_(error) |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 27 | { |
Elliott Hughes | ce8d9f5 | 2011-08-25 13:55:20 -0700 | [diff] [blame] | 28 | stream_state_ = stream().flags(); |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 29 | const char* last_slash = strrchr(file, '/'); |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 30 | file_ = (last_slash == NULL) ? file : last_slash + 1; |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 31 | stream() << StringPrintf("%c %5d %5d %s:%d] ", |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 32 | "IWEF"[severity], getpid(), gettid(), file_, line); |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | LogMessage::~LogMessage() { |
| 36 | if (errno_ != -1) { |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 37 | stream() << ": " << strerror(errno_); |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 38 | } |
| 39 | stream() << std::endl; |
Elliott Hughes | ce8d9f5 | 2011-08-25 13:55:20 -0700 | [diff] [blame] | 40 | stream().flags(stream_state_); |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 41 | if (severity_ == FATAL) { |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 42 | art::Runtime::Abort(file_, line_); |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
| 46 | std::ostream& LogMessage::stream() { |
| 47 | return std::cerr; |
| 48 | } |