Blame src/raw_logging.cc

Packit 18d29c
// Copyright (c) 2006, Google Inc.
Packit 18d29c
// All rights reserved.
Packit 18d29c
//
Packit 18d29c
// Redistribution and use in source and binary forms, with or without
Packit 18d29c
// modification, are permitted provided that the following conditions are
Packit 18d29c
// met:
Packit 18d29c
//
Packit 18d29c
//     * Redistributions of source code must retain the above copyright
Packit 18d29c
// notice, this list of conditions and the following disclaimer.
Packit 18d29c
//     * Redistributions in binary form must reproduce the above
Packit 18d29c
// copyright notice, this list of conditions and the following disclaimer
Packit 18d29c
// in the documentation and/or other materials provided with the
Packit 18d29c
// distribution.
Packit 18d29c
//     * Neither the name of Google Inc. nor the names of its
Packit 18d29c
// contributors may be used to endorse or promote products derived from
Packit 18d29c
// this software without specific prior written permission.
Packit 18d29c
//
Packit 18d29c
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 18d29c
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 18d29c
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 18d29c
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 18d29c
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 18d29c
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 18d29c
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 18d29c
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 18d29c
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 18d29c
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 18d29c
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 18d29c
//
Packit 18d29c
// Author: Maxim Lifantsev
Packit 18d29c
//
Packit 18d29c
// logging_unittest.cc covers the functionality herein
Packit 18d29c
Packit 18d29c
#include "utilities.h"
Packit 18d29c
Packit 18d29c
#include <stdarg.h>
Packit 18d29c
#include <stdio.h>
Packit 18d29c
#include <errno.h>
Packit 18d29c
#ifdef HAVE_UNISTD_H
Packit 18d29c
# include <unistd.h>               // for close() and write()
Packit 18d29c
#endif
Packit 18d29c
#include <fcntl.h>                 // for open()
Packit 18d29c
#include <time.h>
Packit 18d29c
#include "config.h"
Packit 18d29c
#include "glog/logging.h"          // To pick up flag settings etc.
Packit 18d29c
#include "glog/raw_logging.h"
Packit 18d29c
#include "base/commandlineflags.h"
Packit 18d29c
Packit 18d29c
#ifdef HAVE_STACKTRACE
Packit 18d29c
# include "stacktrace.h"
Packit 18d29c
#endif
Packit 18d29c
Packit 18d29c
#if defined(HAVE_SYSCALL_H)
Packit 18d29c
#include <syscall.h>                 // for syscall()
Packit 18d29c
#elif defined(HAVE_SYS_SYSCALL_H)
Packit 18d29c
#include <sys/syscall.h>                 // for syscall()
Packit 18d29c
#endif
Packit 18d29c
#ifdef HAVE_UNISTD_H
Packit 18d29c
# include <unistd.h>
Packit 18d29c
#endif
Packit 18d29c
Packit 18d29c
#if defined(HAVE_SYSCALL_H) || defined(HAVE_SYS_SYSCALL_H)
Packit 18d29c
# define safe_write(fd, s, len)  syscall(SYS_write, fd, s, len)
Packit 18d29c
#else
Packit 18d29c
  // Not so safe, but what can you do?
Packit 18d29c
# define safe_write(fd, s, len)  write(fd, s, len)
Packit 18d29c
#endif
Packit 18d29c
Packit 18d29c
_START_GOOGLE_NAMESPACE_
Packit 18d29c
Packit 18d29c
// Data for RawLog__ below. We simply pick up the latest
Packit 18d29c
// time data created by a normal log message to avoid calling
Packit 18d29c
// localtime_r which can allocate memory.
Packit 18d29c
static struct ::tm last_tm_time_for_raw_log;
Packit 18d29c
static int last_usecs_for_raw_log;
Packit 18d29c
Packit 18d29c
void RawLog__SetLastTime(const struct ::tm& t, int usecs) {
Packit 18d29c
  memcpy(&last_tm_time_for_raw_log, &t, sizeof(last_tm_time_for_raw_log));
Packit 18d29c
  last_usecs_for_raw_log = usecs;
Packit 18d29c
}
Packit 18d29c
Packit 18d29c
// CAVEAT: vsnprintf called from *DoRawLog below has some (exotic) code paths
Packit 18d29c
// that invoke malloc() and getenv() that might acquire some locks.
Packit 18d29c
// If this becomes a problem we should reimplement a subset of vsnprintf
Packit 18d29c
// that does not need locks and malloc.
Packit 18d29c
Packit 18d29c
// Helper for RawLog__ below.
Packit 18d29c
// *DoRawLog writes to *buf of *size and move them past the written portion.
Packit 18d29c
// It returns true iff there was no overflow or error.
Packit 18d29c
static bool DoRawLog(char** buf, int* size, const char* format, ...) {
Packit 18d29c
  va_list ap;
Packit 18d29c
  va_start(ap, format);
Packit 18d29c
  int n = vsnprintf(*buf, *size, format, ap);
Packit 18d29c
  va_end(ap);
Packit 18d29c
  if (n < 0 || n > *size) return false;
Packit 18d29c
  *size -= n;
Packit 18d29c
  *buf += n;
Packit 18d29c
  return true;
Packit 18d29c
}
Packit 18d29c
Packit 18d29c
// Helper for RawLog__ below.
Packit 18d29c
inline static bool VADoRawLog(char** buf, int* size,
Packit 18d29c
                              const char* format, va_list ap) {
Packit 18d29c
  int n = vsnprintf(*buf, *size, format, ap);
Packit 18d29c
  if (n < 0 || n > *size) return false;
Packit 18d29c
  *size -= n;
Packit 18d29c
  *buf += n;
Packit 18d29c
  return true;
Packit 18d29c
}
Packit 18d29c
Packit 18d29c
static const int kLogBufSize = 3000;
Packit 18d29c
static bool crashed = false;
Packit 18d29c
static CrashReason crash_reason;
Packit 18d29c
static char crash_buf[kLogBufSize + 1] = { 0 };  // Will end in '\0'
Packit 18d29c
Packit 18d29c
void RawLog__(LogSeverity severity, const char* file, int line,
Packit 18d29c
              const char* format, ...) {
Packit 18d29c
  if (!(FLAGS_logtostderr || severity >= FLAGS_stderrthreshold ||
Packit 18d29c
        FLAGS_alsologtostderr || !IsGoogleLoggingInitialized())) {
Packit 18d29c
    return;  // this stderr log message is suppressed
Packit 18d29c
  }
Packit 18d29c
  // can't call localtime_r here: it can allocate
Packit 18d29c
  struct ::tm& t = last_tm_time_for_raw_log;
Packit 18d29c
  char buffer[kLogBufSize];
Packit 18d29c
  char* buf = buffer;
Packit 18d29c
  int size = sizeof(buffer);
Packit 18d29c
Packit 18d29c
  // NOTE: this format should match the specification in base/logging.h
Packit 18d29c
  DoRawLog(&buf, &size, "%c%02d%02d %02d:%02d:%02d.%06d %5u %s:%d] RAW: ",
Packit 18d29c
           LogSeverityNames[severity][0],
Packit 18d29c
           1 + t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec,
Packit 18d29c
           last_usecs_for_raw_log,
Packit 18d29c
           static_cast<unsigned int>(GetTID()),
Packit 18d29c
           const_basename(const_cast<char *>(file)), line);
Packit 18d29c
Packit 18d29c
  // Record the position and size of the buffer after the prefix
Packit 18d29c
  const char* msg_start = buf;
Packit 18d29c
  const int msg_size = size;
Packit 18d29c
Packit 18d29c
  va_list ap;
Packit 18d29c
  va_start(ap, format);
Packit 18d29c
  bool no_chop = VADoRawLog(&buf, &size, format, ap);
Packit 18d29c
  va_end(ap);
Packit 18d29c
  if (no_chop) {
Packit 18d29c
    DoRawLog(&buf, &size, "\n");
Packit 18d29c
  } else {
Packit 18d29c
    DoRawLog(&buf, &size, "RAW_LOG ERROR: The Message was too long!\n");
Packit 18d29c
  }
Packit 18d29c
  // We make a raw syscall to write directly to the stderr file descriptor,
Packit 18d29c
  // avoiding FILE buffering (to avoid invoking malloc()), and bypassing
Packit 18d29c
  // libc (to side-step any libc interception).
Packit 18d29c
  // We write just once to avoid races with other invocations of RawLog__.
Packit 18d29c
  safe_write(STDERR_FILENO, buffer, strlen(buffer));
Packit 18d29c
  if (severity == GLOG_FATAL)  {
Packit 18d29c
    if (!sync_val_compare_and_swap(&crashed, false, true)) {
Packit 18d29c
      crash_reason.filename = file;
Packit 18d29c
      crash_reason.line_number = line;
Packit 18d29c
      memcpy(crash_buf, msg_start, msg_size);  // Don't include prefix
Packit 18d29c
      crash_reason.message = crash_buf;
Packit 18d29c
#ifdef HAVE_STACKTRACE
Packit 18d29c
      crash_reason.depth =
Packit 18d29c
          GetStackTrace(crash_reason.stack, ARRAYSIZE(crash_reason.stack), 1);
Packit 18d29c
#else
Packit 18d29c
      crash_reason.depth = 0;
Packit 18d29c
#endif
Packit 18d29c
      SetCrashReason(&crash_reason);
Packit 18d29c
    }
Packit 18d29c
    LogMessage::Fail();  // abort()
Packit 18d29c
  }
Packit 18d29c
}
Packit 18d29c
Packit 18d29c
_END_GOOGLE_NAMESPACE_