Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 1 | // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 | // Copyright (c) 2007, Google Inc. |
| 3 | // All rights reserved. |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 4 | // |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 8 | // |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 18 | // |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | // --- |
| 32 | // This file just provides storage for FLAGS_verbose. |
| 33 | |
| 34 | #include <config.h> |
| 35 | #include "base/logging.h" |
| 36 | #include "base/commandlineflags.h" |
| 37 | |
| 38 | DEFINE_int32(verbose, EnvToInt("PERFTOOLS_VERBOSE", 0), |
| 39 | "Set to numbers >0 for more verbose output, or <0 for less. " |
| 40 | "--verbose == -4 means we log fatal errors only."); |
| 41 | |
| 42 | |
| 43 | #if defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__) |
| 44 | |
| 45 | // While windows does have a POSIX-compatible API |
| 46 | // (_open/_write/_close), it acquires memory. Using this lower-level |
| 47 | // windows API is the closest we can get to being "raw". |
| 48 | RawFD RawOpenForWriting(const char* filename) { |
| 49 | // CreateFile allocates memory if file_name isn't absolute, so if |
| 50 | // that ever becomes a problem then we ought to compute the absolute |
| 51 | // path on its behalf (perhaps the ntdll/kernel function isn't aware |
| 52 | // of the working directory?) |
| 53 | RawFD fd = CreateFileA(filename, GENERIC_WRITE, 0, NULL, |
| 54 | CREATE_ALWAYS, 0, NULL); |
| 55 | if (fd != kIllegalRawFD && GetLastError() == ERROR_ALREADY_EXISTS) |
| 56 | SetEndOfFile(fd); // truncate the existing file |
| 57 | return fd; |
| 58 | } |
| 59 | |
| 60 | void RawWrite(RawFD handle, const char* buf, size_t len) { |
| 61 | while (len > 0) { |
| 62 | DWORD wrote; |
| 63 | BOOL ok = WriteFile(handle, buf, len, &wrote, NULL); |
| 64 | // We do not use an asynchronous file handle, so ok==false means an error |
| 65 | if (!ok) break; |
| 66 | buf += wrote; |
| 67 | len -= wrote; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void RawClose(RawFD handle) { |
| 72 | CloseHandle(handle); |
| 73 | } |
| 74 | |
| 75 | #else // _WIN32 || __CYGWIN__ || __CYGWIN32__ |
| 76 | |
| 77 | #ifdef HAVE_SYS_TYPES_H |
| 78 | #include <sys/types.h> |
| 79 | #endif |
| 80 | #ifdef HAVE_UNISTD_H |
| 81 | #include <unistd.h> |
| 82 | #endif |
| 83 | #ifdef HAVE_FCNTL_H |
| 84 | #include <fcntl.h> |
| 85 | #endif |
| 86 | |
| 87 | // Re-run fn until it doesn't cause EINTR. |
| 88 | #define NO_INTR(fn) do {} while ((fn) < 0 && errno == EINTR) |
| 89 | |
| 90 | RawFD RawOpenForWriting(const char* filename) { |
| 91 | return open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0664); |
| 92 | } |
| 93 | |
| 94 | void RawWrite(RawFD fd, const char* buf, size_t len) { |
| 95 | while (len > 0) { |
| 96 | ssize_t r; |
| 97 | NO_INTR(r = write(fd, buf, len)); |
| 98 | if (r <= 0) break; |
| 99 | buf += r; |
| 100 | len -= r; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void RawClose(RawFD fd) { |
| 105 | NO_INTR(close(fd)); |
| 106 | } |
| 107 | |
| 108 | #endif // _WIN32 || __CYGWIN__ || __CYGWIN32__ |