| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 #ifdef HAVE_UNISTD_H | 42 #ifdef HAVE_UNISTD_H |
| 43 #include <unistd.h> // for write() | 43 #include <unistd.h> // for write() |
| 44 #endif | 44 #endif |
| 45 #include <string.h> // for strlen(), strcmp() | 45 #include <string.h> // for strlen(), strcmp() |
| 46 #include <assert.h> | 46 #include <assert.h> |
| 47 #include <errno.h> // for errno | 47 #include <errno.h> // for errno |
| 48 #include "base/commandlineflags.h" | 48 #include "base/commandlineflags.h" |
| 49 | 49 |
| 50 // On some systems (like freebsd), we can't call write() at all in a | 50 // On some systems (like freebsd), we can't call write() at all in a |
| 51 // global constructor, perhaps because errno hasn't been set up. | 51 // global constructor, perhaps because errno hasn't been set up. |
| 52 // (In windows, we can't call it because it might call malloc.) |
| 52 // Calling the write syscall is safer (it doesn't set errno), so we | 53 // Calling the write syscall is safer (it doesn't set errno), so we |
| 53 // prefer that. Note we don't care about errno for logging: we just | 54 // prefer that. Note we don't care about errno for logging: we just |
| 54 // do logging on a best-effort basis. | 55 // do logging on a best-effort basis. |
| 55 #ifdef HAVE_SYS_SYSCALL_H | 56 #if defined(_MSC_VER) |
| 57 #define WRITE_TO_STDERR(buf, len) WriteToStderr(buf, len); // in port.cc |
| 58 #elif defined(HAVE_SYS_SYSCALL_H) |
| 56 #include <sys/syscall.h> | 59 #include <sys/syscall.h> |
| 57 #define WRITE_TO_STDERR(buf, len) syscall(SYS_write, STDERR_FILENO, buf, len) | 60 #define WRITE_TO_STDERR(buf, len) syscall(SYS_write, STDERR_FILENO, buf, len) |
| 58 #else | 61 #else |
| 59 #define WRITE_TO_STDERR(buf, len) write(STDERR_FILENO, buf, len) | 62 #define WRITE_TO_STDERR(buf, len) write(STDERR_FILENO, buf, len) |
| 60 #endif | 63 #endif |
| 61 | 64 |
| 65 // MSVC and mingw define their own, safe version of vnsprintf (the |
| 66 // windows one in broken) in port.cc. Everyone else can use the |
| 67 // version here. We had to give it a unique name for windows. |
| 68 #ifndef _WIN32 |
| 69 # define perftools_vsnprintf vsnprintf |
| 70 #endif |
| 71 |
| 62 | 72 |
| 63 // We log all messages at this log-level and below. | 73 // We log all messages at this log-level and below. |
| 64 // INFO == -1, WARNING == -2, ERROR == -3, FATAL == -4 | 74 // INFO == -1, WARNING == -2, ERROR == -3, FATAL == -4 |
| 65 DECLARE_int32(verbose); | 75 DECLARE_int32(verbose); |
| 66 | 76 |
| 67 // CHECK dies with a fatal error if condition is not true. It is *not* | 77 // CHECK dies with a fatal error if condition is not true. It is *not* |
| 68 // controlled by NDEBUG, so the check will be executed regardless of | 78 // controlled by NDEBUG, so the check will be executed regardless of |
| 69 // compilation mode. Therefore, it is safe to do things like: | 79 // compilation mode. Therefore, it is safe to do things like: |
| 70 // CHECK(fp->Write(x) == 4) | 80 // CHECK(fp->Write(x) == 4) |
| 71 // Note we use write instead of printf/puts to avoid the risk we'll | 81 // Note we use write instead of printf/puts to avoid the risk we'll |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 #ifdef ERROR | 191 #ifdef ERROR |
| 182 #undef ERROR // may conflict with ERROR macro on windows | 192 #undef ERROR // may conflict with ERROR macro on windows |
| 183 #endif | 193 #endif |
| 184 enum LogSeverity {INFO = -1, WARNING = -2, ERROR = -3, FATAL = -4}; | 194 enum LogSeverity {INFO = -1, WARNING = -2, ERROR = -3, FATAL = -4}; |
| 185 | 195 |
| 186 // NOTE: we add a newline to the end of the output if it's not there already | 196 // NOTE: we add a newline to the end of the output if it's not there already |
| 187 inline void LogPrintf(int severity, const char* pat, va_list ap) { | 197 inline void LogPrintf(int severity, const char* pat, va_list ap) { |
| 188 // We write directly to the stderr file descriptor and avoid FILE | 198 // We write directly to the stderr file descriptor and avoid FILE |
| 189 // buffering because that may invoke malloc() | 199 // buffering because that may invoke malloc() |
| 190 char buf[1600]; | 200 char buf[1600]; |
| 191 vsnprintf(buf, sizeof(buf)-1, pat, ap); | 201 perftools_vsnprintf(buf, sizeof(buf)-1, pat, ap); |
| 192 if (buf[0] != '\0' && buf[strlen(buf)-1] != '\n') { | 202 if (buf[0] != '\0' && buf[strlen(buf)-1] != '\n') { |
| 193 assert(strlen(buf)+1 < sizeof(buf)); | 203 assert(strlen(buf)+1 < sizeof(buf)); |
| 194 strcat(buf, "\n"); | 204 strcat(buf, "\n"); |
| 195 } | 205 } |
| 196 WRITE_TO_STDERR(buf, strlen(buf)); | 206 WRITE_TO_STDERR(buf, strlen(buf)); |
| 197 if ((severity) == FATAL) | 207 if ((severity) == FATAL) |
| 198 abort(); // LOG(FATAL) indicates a big problem, so don't run atexit() calls | 208 abort(); // LOG(FATAL) indicates a big problem, so don't run atexit() calls |
| 199 } | 209 } |
| 200 | 210 |
| 201 // Note that since the order of global constructors is unspecified, | 211 // Note that since the order of global constructors is unspecified, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 223 } | 233 } |
| 224 | 234 |
| 225 // This isn't technically logging, but it's also IO and also is an | 235 // This isn't technically logging, but it's also IO and also is an |
| 226 // attempt to be "raw" -- that is, to not use any higher-level libc | 236 // attempt to be "raw" -- that is, to not use any higher-level libc |
| 227 // routines that might allocate memory or (ideally) try to allocate | 237 // routines that might allocate memory or (ideally) try to allocate |
| 228 // locks. We use an opaque file handle (not necessarily an int) | 238 // locks. We use an opaque file handle (not necessarily an int) |
| 229 // to allow even more low-level stuff in the future. | 239 // to allow even more low-level stuff in the future. |
| 230 // Like other "raw" routines, these functions are best effort, and | 240 // Like other "raw" routines, these functions are best effort, and |
| 231 // thus don't return error codes (except RawOpenForWriting()). | 241 // thus don't return error codes (except RawOpenForWriting()). |
| 232 #if defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__) | 242 #if defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__) |
| 243 #ifndef NOMINMAX |
| 244 #define NOMINMAX // @#!$& windows |
| 245 #endif |
| 233 #include <windows.h> | 246 #include <windows.h> |
| 234 typedef HANDLE RawFD; | 247 typedef HANDLE RawFD; |
| 235 const RawFD kIllegalRawFD = INVALID_HANDLE_VALUE; | 248 const RawFD kIllegalRawFD = INVALID_HANDLE_VALUE; |
| 236 #else | 249 #else |
| 237 typedef int RawFD; | 250 typedef int RawFD; |
| 238 const RawFD kIllegalRawFD = -1; // what open returns if it fails | 251 const RawFD kIllegalRawFD = -1; // what open returns if it fails |
| 239 #endif // defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__) | 252 #endif // defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__) |
| 240 | 253 |
| 241 RawFD RawOpenForWriting(const char* filename); // uses default permissions | 254 RawFD RawOpenForWriting(const char* filename); // uses default permissions |
| 242 void RawWrite(RawFD fd, const char* buf, size_t len); | 255 void RawWrite(RawFD fd, const char* buf, size_t len); |
| 243 void RawClose(RawFD fd); | 256 void RawClose(RawFD fd); |
| 244 | 257 |
| 245 #endif // _LOGGING_H_ | 258 #endif // _LOGGING_H_ |
| OLD | NEW |