OLD | NEW |
1 // Copyright (c) 2005, Google Inc. | 1 // Copyright (c) 2005, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 #include "base/commandlineflags.h" | 49 #include "base/commandlineflags.h" |
50 | 50 |
51 // On some systems (like freebsd), we can't call write() at all in a | 51 // On some systems (like freebsd), we can't call write() at all in a |
52 // global constructor, perhaps because errno hasn't been set up. | 52 // global constructor, perhaps because errno hasn't been set up. |
53 // (In windows, we can't call it because it might call malloc.) | 53 // (In windows, we can't call it because it might call malloc.) |
54 // Calling the write syscall is safer (it doesn't set errno), so we | 54 // Calling the write syscall is safer (it doesn't set errno), so we |
55 // prefer that. Note we don't care about errno for logging: we just | 55 // prefer that. Note we don't care about errno for logging: we just |
56 // do logging on a best-effort basis. | 56 // do logging on a best-effort basis. |
57 #if defined(_MSC_VER) | 57 #if defined(_MSC_VER) |
58 #define WRITE_TO_STDERR(buf, len) WriteToStderr(buf, len); // in port.cc | 58 #define WRITE_TO_STDERR(buf, len) WriteToStderr(buf, len); // in port.cc |
| 59 #elif defined(__ANDROID__) || defined(ANDROID) |
| 60 #include <android/log.h> |
| 61 #define WRITE_TO_STDERR(buf, len) \ |
| 62 __android_log_write(ANDROID_LOG_ERROR, "gperftools", buf) |
59 #elif defined(HAVE_SYS_SYSCALL_H) | 63 #elif defined(HAVE_SYS_SYSCALL_H) |
60 #include <sys/syscall.h> | 64 #include <sys/syscall.h> |
61 #define WRITE_TO_STDERR(buf, len) syscall(SYS_write, STDERR_FILENO, buf, len) | 65 #define WRITE_TO_STDERR(buf, len) syscall(SYS_write, STDERR_FILENO, buf, len) |
62 #else | 66 #else |
63 #define WRITE_TO_STDERR(buf, len) write(STDERR_FILENO, buf, len) | 67 #define WRITE_TO_STDERR(buf, len) write(STDERR_FILENO, buf, len) |
64 #endif | 68 #endif |
65 | 69 |
66 // MSVC and mingw define their own, safe version of vnsprintf (the | 70 // MSVC and mingw define their own, safe version of vnsprintf (the |
67 // windows one in broken) in port.cc. Everyone else can use the | 71 // windows one in broken) in port.cc. Everyone else can use the |
68 // version here. We had to give it a unique name for windows. | 72 // version here. We had to give it a unique name for windows. |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 // NOTE: we add a newline to the end of the output if it's not there already | 201 // NOTE: we add a newline to the end of the output if it's not there already |
198 inline void LogPrintf(int severity, const char* pat, va_list ap) { | 202 inline void LogPrintf(int severity, const char* pat, va_list ap) { |
199 // We write directly to the stderr file descriptor and avoid FILE | 203 // We write directly to the stderr file descriptor and avoid FILE |
200 // buffering because that may invoke malloc() | 204 // buffering because that may invoke malloc() |
201 char buf[1600]; | 205 char buf[1600]; |
202 perftools_vsnprintf(buf, sizeof(buf)-1, pat, ap); | 206 perftools_vsnprintf(buf, sizeof(buf)-1, pat, ap); |
203 if (buf[0] != '\0' && buf[strlen(buf)-1] != '\n') { | 207 if (buf[0] != '\0' && buf[strlen(buf)-1] != '\n') { |
204 assert(strlen(buf)+1 < sizeof(buf)); | 208 assert(strlen(buf)+1 < sizeof(buf)); |
205 strcat(buf, "\n"); | 209 strcat(buf, "\n"); |
206 } | 210 } |
| 211 #if defined(__ANDROID__) || defined(ANDROID) |
| 212 android_LogPriority priority = ANDROID_LOG_UNKNOWN; |
| 213 switch (severity) { |
| 214 case INFO: { |
| 215 priority = ANDROID_LOG_INFO; |
| 216 break; |
| 217 } |
| 218 case WARNING: { |
| 219 priority = ANDROID_LOG_WARN; |
| 220 break; |
| 221 } |
| 222 case ERROR: { |
| 223 priority = ANDROID_LOG_ERROR; |
| 224 break; |
| 225 } |
| 226 case FATAL: { |
| 227 priority = ANDROID_LOG_FATAL; |
| 228 break; |
| 229 } |
| 230 } |
| 231 __android_log_write(priority, "gperftools", buf); |
| 232 #else // defined(__ANDROID__) || defined(ANDROID) |
207 WRITE_TO_STDERR(buf, strlen(buf)); | 233 WRITE_TO_STDERR(buf, strlen(buf)); |
| 234 #endif // defined(__ANDROID__) || defined(ANDROID) |
208 if ((severity) == FATAL) { | 235 if ((severity) == FATAL) { |
209 // LOG(FATAL) indicates a big problem, so don't run atexit() calls | 236 // LOG(FATAL) indicates a big problem, so don't run atexit() calls |
210 tcmalloc::Abort(); | 237 tcmalloc::Abort(); |
211 } | 238 } |
212 } | 239 } |
213 | 240 |
214 // Note that since the order of global constructors is unspecified, | 241 // Note that since the order of global constructors is unspecified, |
215 // global code that calls RAW_LOG may execute before FLAGS_verbose is set. | 242 // global code that calls RAW_LOG may execute before FLAGS_verbose is set. |
216 // Such code will run with verbosity == 0 no matter what. | 243 // Such code will run with verbosity == 0 no matter what. |
217 #define VLOG_IS_ON(severity) (FLAGS_verbose >= severity) | 244 #define VLOG_IS_ON(severity) (FLAGS_verbose >= severity) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 #else | 279 #else |
253 typedef int RawFD; | 280 typedef int RawFD; |
254 const RawFD kIllegalRawFD = -1; // what open returns if it fails | 281 const RawFD kIllegalRawFD = -1; // what open returns if it fails |
255 #endif // defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__) | 282 #endif // defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__) |
256 | 283 |
257 RawFD RawOpenForWriting(const char* filename); // uses default permissions | 284 RawFD RawOpenForWriting(const char* filename); // uses default permissions |
258 void RawWrite(RawFD fd, const char* buf, size_t len); | 285 void RawWrite(RawFD fd, const char* buf, size_t len); |
259 void RawClose(RawFD fd); | 286 void RawClose(RawFD fd); |
260 | 287 |
261 #endif // _LOGGING_H_ | 288 #endif // _LOGGING_H_ |
OLD | NEW |