1// Copyright (c) 2007, Google Inc. 2// All rights reserved. 3// 4// Redistribution and use in source and binary forms, with or without 5// modification, are permitted provided that the following conditions are 6// met: 7// 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 13// distribution. 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. 17// 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. 29 30// --- 31// This file just provides storage for FLAGS_verbose. 32 33#include <config.h> 34#include "base/logging.h" 35#include "base/commandlineflags.h" 36 37DEFINE_int32(verbose, EnvToInt("PERFTOOLS_VERBOSE", 0), 38 "Set to numbers >0 for more verbose output, or <0 for less. " 39 "--verbose == -4 means we log fatal errors only."); 40 41 42#if defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__) 43 44// While windows does have a POSIX-compatible API 45// (_open/_write/_close), it acquires memory. Using this lower-level 46// windows API is the closest we can get to being "raw". 47RawFD RawOpenForWriting(const char* filename) { 48 // CreateFile allocates memory if file_name isn't absolute, so if 49 // that ever becomes a problem then we ought to compute the absolute 50 // path on its behalf (perhaps the ntdll/kernel function isn't aware 51 // of the working directory?) 52 RawFD fd = CreateFileA(filename, GENERIC_WRITE, 0, NULL, 53 CREATE_ALWAYS, 0, NULL); 54 if (fd != kIllegalRawFD && GetLastError() == ERROR_ALREADY_EXISTS) 55 SetEndOfFile(fd); // truncate the existing file 56 return fd; 57} 58 59void RawWrite(RawFD handle, const char* buf, size_t len) { 60 while (len > 0) { 61 DWORD wrote; 62 BOOL ok = WriteFile(handle, buf, len, &wrote, NULL); 63 // We do not use an asynchronous file handle, so ok==false means an error 64 if (!ok) break; 65 buf += wrote; 66 len -= wrote; 67 } 68} 69 70void RawClose(RawFD handle) { 71 CloseHandle(handle); 72} 73 74#else // _WIN32 || __CYGWIN__ || __CYGWIN32__ 75 76#ifdef HAVE_SYS_TYPES_H 77#include <sys/types.h> 78#endif 79#ifdef HAVE_UNISTD_H 80#include <unistd.h> 81#endif 82#ifdef HAVE_FCNTL_H 83#include <fcntl.h> 84#endif 85 86// Re-run fn until it doesn't cause EINTR. 87#define NO_INTR(fn) do {} while ((fn) < 0 && errno == EINTR) 88 89RawFD RawOpenForWriting(const char* filename) { 90 return open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0664); 91} 92 93void RawWrite(RawFD fd, const char* buf, size_t len) { 94 while (len > 0) { 95 ssize_t r; 96 NO_INTR(r = write(fd, buf, len)); 97 if (r <= 0) break; 98 buf += r; 99 len -= r; 100 } 101} 102 103void RawClose(RawFD fd) { 104 NO_INTR(close(fd)); 105} 106 107#endif // _WIN32 || __CYGWIN__ || __CYGWIN32__ 108