PageRenderTime 25ms CodeModel.GetById 14ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/third_party/glog/src/raw_logging.cc

http://github.com/tomahawk-player/tomahawk
C++ | 172 lines | 104 code | 17 blank | 51 comment | 13 complexity | 117d6f3b60766fb0b69af9c31aa570a9 MD5 | raw file
  1// Copyright (c) 2006, Google Inc.
  2// All rights reserved.
  3//
  4// Redistribution and use in source and binary forms, with or without
  5// modification, are permitted provided that the following conditions are
  6// met:
  7//
  8//     * Redistributions of source code must retain the above copyright
  9// notice, this list of conditions and the following disclaimer.
 10//     * Redistributions in binary form must reproduce the above
 11// copyright notice, this list of conditions and the following disclaimer
 12// in the documentation and/or other materials provided with the
 13// distribution.
 14//     * Neither the name of Google Inc. nor the names of its
 15// contributors may be used to endorse or promote products derived from
 16// this software without specific prior written permission.
 17//
 18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 29//
 30// Author: Maxim Lifantsev
 31//
 32// logging_unittest.cc covers the functionality herein
 33
 34#include "utilities.h"
 35
 36#include <stdarg.h>
 37#include <stdio.h>
 38#include <errno.h>
 39#ifdef HAVE_UNISTD_H
 40# include <unistd.h>               // for close() and write()
 41#endif
 42#include <fcntl.h>                 // for open()
 43#include <time.h>
 44#include "config.h"
 45#include "glog/logging.h"          // To pick up flag settings etc.
 46#include "glog/raw_logging.h"
 47#include "base/commandlineflags.h"
 48
 49#ifdef HAVE_STACKTRACE
 50# include "stacktrace.h"
 51#endif
 52
 53#if defined(HAVE_SYSCALL_H)
 54#include <syscall.h>                 // for syscall()
 55#elif defined(HAVE_SYS_SYSCALL_H)
 56#include <sys/syscall.h>                 // for syscall()
 57#endif
 58#ifdef HAVE_UNISTD_H
 59# include <unistd.h>
 60#endif
 61
 62#if defined(HAVE_SYSCALL_H) || defined(HAVE_SYS_SYSCALL_H)
 63# define safe_write(fd, s, len)  syscall(SYS_write, fd, s, len)
 64#else
 65  // Not so safe, but what can you do?
 66# define safe_write(fd, s, len)  write(fd, s, len)
 67#endif
 68
 69_START_GOOGLE_NAMESPACE_
 70
 71// Data for RawLog__ below. We simply pick up the latest
 72// time data created by a normal log message to avoid calling
 73// localtime_r which can allocate memory.
 74static struct ::tm last_tm_time_for_raw_log;
 75static int last_usecs_for_raw_log;
 76
 77void RawLog__SetLastTime(const struct ::tm& t, int usecs) {
 78  memcpy(&last_tm_time_for_raw_log, &t, sizeof(last_tm_time_for_raw_log));
 79  last_usecs_for_raw_log = usecs;
 80}
 81
 82// CAVEAT: vsnprintf called from *DoRawLog below has some (exotic) code paths
 83// that invoke malloc() and getenv() that might acquire some locks.
 84// If this becomes a problem we should reimplement a subset of vsnprintf
 85// that does not need locks and malloc.
 86
 87// Helper for RawLog__ below.
 88// *DoRawLog writes to *buf of *size and move them past the written portion.
 89// It returns true iff there was no overflow or error.
 90static bool DoRawLog(char** buf, int* size, const char* format, ...) {
 91  va_list ap;
 92  va_start(ap, format);
 93  int n = vsnprintf(*buf, *size, format, ap);
 94  va_end(ap);
 95  if (n < 0 || n > *size) return false;
 96  *size -= n;
 97  *buf += n;
 98  return true;
 99}
100
101// Helper for RawLog__ below.
102inline static bool VADoRawLog(char** buf, int* size,
103                              const char* format, va_list ap) {
104  int n = vsnprintf(*buf, *size, format, ap);
105  if (n < 0 || n > *size) return false;
106  *size -= n;
107  *buf += n;
108  return true;
109}
110
111static const int kLogBufSize = 3000;
112static bool crashed = false;
113static CrashReason crash_reason;
114static char crash_buf[kLogBufSize + 1] = { 0 };  // Will end in '\0'
115
116void RawLog__(LogSeverity severity, const char* file, int line,
117              const char* format, ...) {
118  if (!(FLAGS_logtostderr || severity >= FLAGS_stderrthreshold ||
119        FLAGS_alsologtostderr || !IsGoogleLoggingInitialized())) {
120    return;  // this stderr log message is suppressed
121  }
122  // can't call localtime_r here: it can allocate
123  struct ::tm& t = last_tm_time_for_raw_log;
124  char buffer[kLogBufSize];
125  char* buf = buffer;
126  int size = sizeof(buffer);
127
128  // NOTE: this format should match the specification in base/logging.h
129  DoRawLog(&buf, &size, "%c%02d%02d %02d:%02d:%02d.%06d %5u %s:%d] RAW: ",
130           LogSeverityNames[severity][0],
131           1 + t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec,
132           last_usecs_for_raw_log,
133           static_cast<unsigned int>(GetTID()),
134           const_basename(const_cast<char *>(file)), line);
135
136  // Record the position and size of the buffer after the prefix
137  const char* msg_start = buf;
138  const int msg_size = size;
139
140  va_list ap;
141  va_start(ap, format);
142  bool no_chop = VADoRawLog(&buf, &size, format, ap);
143  va_end(ap);
144  if (no_chop) {
145    DoRawLog(&buf, &size, "\n");
146  } else {
147    DoRawLog(&buf, &size, "RAW_LOG ERROR: The Message was too long!\n");
148  }
149  // We make a raw syscall to write directly to the stderr file descriptor,
150  // avoiding FILE buffering (to avoid invoking malloc()), and bypassing
151  // libc (to side-step any libc interception).
152  // We write just once to avoid races with other invocations of RawLog__.
153  safe_write(STDERR_FILENO, buffer, strlen(buffer));
154  if (severity == FATAL)  {
155    if (!sync_val_compare_and_swap(&crashed, false, true)) {
156      crash_reason.filename = file;
157      crash_reason.line_number = line;
158      memcpy(crash_buf, msg_start, msg_size);  // Don't include prefix
159      crash_reason.message = crash_buf;
160#ifdef HAVE_STACKTRACE
161      crash_reason.depth =
162          GetStackTrace(crash_reason.stack, ARRAYSIZE(crash_reason.stack), 1);
163#else
164      crash_reason.depth = 0;
165#endif
166      SetCrashReason(&crash_reason);
167    }
168    LogMessage::Fail();  // abort()
169  }
170}
171
172_END_GOOGLE_NAMESPACE_