summaryrefslogtreecommitdiff
path: root/src/logging_linux.cc
blob: 4be989d7601103b21e9bb79d4d4b3e2da8bc2784 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2011 Google Inc. All Rights Reserved.
// Author: enh@google.com (Elliott Hughes)

#include "logging.h"

#include "runtime.h"
#include "scoped_ptr.h"
#include "stringprintf.h"

#include <cstdio>
#include <cstring>
#include <iostream>
#include <sys/types.h>
#include <unistd.h>

// glibc doesn't expose gettid(2).
#define __KERNEL__
# include <linux/unistd.h>
#ifdef _syscall0
_syscall0(pid_t,gettid)
#else
pid_t gettid() { return syscall(__NR_gettid);}
#endif
#undef __KERNEL__

LogMessage::LogMessage(const char* file, int line, LogSeverity severity, int error)
: line_(line), severity_(severity), errno_(error)
{
  const char* last_slash = strrchr(file, '/');
  file_ = (last_slash == NULL) ? file : last_slash + 1;
  stream() << StringPrintf("%c %5d %5d %s:%d] ",
      "IWEF"[severity], getpid(), gettid(), file_, line);
}

LogMessage::~LogMessage() {
  if (errno_ != -1) {
    stream() << ": " << strerror(errno_);
  }
  stream() << std::endl;
  if (severity_ == FATAL) {
    art::Runtime::Abort(file_, line_);
  }
}

std::ostream& LogMessage::stream() {
  return std::cerr;
}