logging.cc 3.7 KB

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