1 // Copyright (c) 2005, 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.
31 // Sanjay Ghemawat <opensource@google.com>
34 #include "internal_logging.h"
35 #include <stdarg.h> // for va_end, va_start
36 #include <stdio.h> // for vsnprintf, va_list, etc
37 #include <stdlib.h> // for abort
38 #include <string.h> // for strlen, memcpy
40 #include <unistd.h> // for write()
43 #include <gperftools/malloc_extension.h>
44 #include "base/logging.h" // for perftools_vsnprintf
45 #include "base/spinlock.h" // for SpinLockHolder, SpinLock
47 static const int kLogBufSize
= 800;
49 // Variables for storing crash output. Allocated statically since we
50 // may not be able to heap-allocate while crashing.
51 static SpinLock
crash_lock(base::LINKER_INITIALIZED
);
52 static bool crashed
= false;
53 static const int kStatsBufferSize
= 16 << 10;
54 static char stats_buffer
[kStatsBufferSize
] = { 0 };
58 static void WriteMessage(const char* msg
, int length
) {
59 write(STDERR_FILENO
, msg
, length
);
62 void (*log_message_writer
)(const char* msg
, int length
) = WriteMessage
;
67 bool Add(const LogItem
& item
);
68 bool AddStr(const char* str
, int n
);
69 bool AddNum(uint64_t num
, int base
); // base must be 10 or 16.
71 static const int kBufSize
= 200;
77 void Log(LogMode mode
, const char* filename
, int line
,
78 LogItem a
, LogItem b
, LogItem c
, LogItem d
) {
80 state
.p_
= state
.buf_
;
81 state
.end_
= state
.buf_
+ sizeof(state
.buf_
);
82 state
.AddStr(filename
, strlen(filename
))
83 && state
.AddStr(":", 1)
84 && state
.AddNum(line
, 10)
85 && state
.AddStr("]", 1)
91 // Teminate with newline
92 if (state
.p_
>= state
.end_
) {
93 state
.p_
= state
.end_
- 1;
98 int msglen
= state
.p_
- state
.buf_
;
100 (*log_message_writer
)(state
.buf_
, msglen
);
104 bool first_crash
= false;
106 SpinLockHolder
l(&crash_lock
);
113 (*log_message_writer
)(state
.buf_
, msglen
);
114 if (first_crash
&& mode
== kCrashWithStats
) {
115 MallocExtension::instance()->GetStats(stats_buffer
, kStatsBufferSize
);
116 (*log_message_writer
)(stats_buffer
, strlen(stats_buffer
));
122 bool Logger::Add(const LogItem
& item
) {
123 // Separate items with spaces
131 return AddStr(item
.u_
.str
, strlen(item
.u_
.str
));
132 case LogItem::kUnsigned
:
133 return AddNum(item
.u_
.unum
, 10);
134 case LogItem::kSigned
:
135 if (item
.u_
.snum
< 0) {
136 // The cast to uint64_t is intentionally before the negation
137 // so that we do not attempt to negate -2^63.
138 return AddStr("-", 1)
139 && AddNum(- static_cast<uint64_t>(item
.u_
.snum
), 10);
141 return AddNum(static_cast<uint64_t>(item
.u_
.snum
), 10);
144 return AddStr("0x", 2)
145 && AddNum(reinterpret_cast<uintptr_t>(item
.u_
.ptr
), 16);
151 bool Logger::AddStr(const char* str
, int n
) {
161 bool Logger::AddNum(uint64_t num
, int base
) {
162 static const char kDigits
[] = "0123456789abcdef";
163 char space
[22]; // more than enough for 2^64 in smallest supported base (10)
164 char* end
= space
+ sizeof(space
);
168 *pos
= kDigits
[num
% base
];
170 } while (num
> 0 && pos
> space
);
171 return AddStr(pos
, end
- pos
);
174 } // end tcmalloc namespace
176 void TCMalloc_Printer::printf(const char* format
, ...) {
179 va_start(ap
, format
);
180 const int r
= perftools_vsnprintf(buf_
, left_
, format
, ap
);
183 // Perhaps an old glibc that returns -1 on truncation?
185 } else if (r
> left_
) {