Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | // Author: enh@google.com (Elliott Hughes) |
| 3 | |
| 4 | #include "logging.h" |
| 5 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame^] | 6 | #include "runtime.h" |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 7 | #include "scoped_ptr.h" |
| 8 | #include "stringprintf.h" |
| 9 | |
| 10 | #include <cstdio> |
| 11 | #include <cstring> |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 12 | #include <iostream> |
| 13 | #include <sys/types.h> |
| 14 | #include <unistd.h> |
| 15 | |
| 16 | // glibc doesn't expose gettid(2). |
| 17 | #define __KERNEL__ |
| 18 | # include <linux/unistd.h> |
| 19 | #ifdef _syscall0 |
| 20 | _syscall0(pid_t,gettid) |
| 21 | #else |
| 22 | pid_t gettid() { return syscall(__NR_gettid);} |
| 23 | #endif |
| 24 | #undef __KERNEL__ |
| 25 | |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 26 | LogMessage::LogMessage(const char* file, int line, LogSeverity severity, int error) |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame^] | 27 | : line_(line), severity_(severity), errno_(error) |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 28 | { |
| 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; |
| 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 | } |