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