1 // Copyright (c) 2006, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Author: Maxim Lifantsev
32 // logging_unittest.cc covers the functionality herein
34 #include "utilities.h"
40 # include <unistd.h> // for close() and write()
42 #include <fcntl.h> // for open()
45 #include "glog/logging.h" // To pick up flag settings etc.
46 #include "glog/raw_logging.h"
47 #include "base/commandlineflags.h"
49 #ifdef HAVE_STACKTRACE
50 # include "stacktrace.h"
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()
62 #if defined(HAVE_SYSCALL_H) || defined(HAVE_SYS_SYSCALL_H)
63 # define safe_write(fd, s, len) syscall(SYS_write, fd, s, len)
65 // Not so safe, but what can you do?
66 # define safe_write(fd, s, len) write(fd, s, len)
69 _START_GOOGLE_NAMESPACE_
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.
74 static struct ::tm last_tm_time_for_raw_log;
75 static int last_usecs_for_raw_log;
77 void 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;
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.
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.
90 static bool DoRawLog(char** buf, int* size, const char* format, ...) {
93 int n = vsnprintf(*buf, *size, format, ap);
95 if (n < 0 || n > *size) return false;
101 // Helper for RawLog__ below.
102 inline 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;
111 static const int kLogBufSize = 3000;
112 static bool crashed = false;
113 static CrashReason crash_reason;
114 static char crash_buf[kLogBufSize + 1] = { 0 }; // Will end in '\0'
116 void 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
122 // can't call localtime_r here: it can allocate
123 struct ::tm& t = last_tm_time_for_raw_log;
124 char buffer[kLogBufSize];
126 int size = sizeof(buffer);
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);
136 // Record the position and size of the buffer after the prefix
137 const char* msg_start = buf;
138 const int msg_size = size;
141 va_start(ap, format);
142 bool no_chop = VADoRawLog(&buf, &size, format, ap);
145 DoRawLog(&buf, &size, "\n");
147 DoRawLog(&buf, &size, "RAW_LOG ERROR: The Message was too long!\n");
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 == GLOG_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
162 GetStackTrace(crash_reason.stack, ARRAYSIZE(crash_reason.stack), 1);
164 crash_reason.depth = 0;
166 SetCrashReason(&crash_reason);
168 LogMessage::Fail(); // abort()
172 _END_GOOGLE_NAMESPACE_