win_logger.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. //
  10. // Logger implementation that can be shared by all environments
  11. // where enough posix functionality is available.
  12. #include "port/win/win_logger.h"
  13. #include "port/win/io_win.h"
  14. #include <algorithm>
  15. #include <stdio.h>
  16. #include <time.h>
  17. #include <fcntl.h>
  18. #include <atomic>
  19. #include "rocksdb/env.h"
  20. #include "monitoring/iostats_context_imp.h"
  21. #include "port/sys_time.h"
  22. namespace ROCKSDB_NAMESPACE {
  23. namespace port {
  24. WinLogger::WinLogger(uint64_t (*gettid)(), Env* env, HANDLE file,
  25. const InfoLogLevel log_level)
  26. : Logger(log_level),
  27. file_(file),
  28. gettid_(gettid),
  29. log_size_(0),
  30. last_flush_micros_(0),
  31. env_(env),
  32. flush_pending_(false) {
  33. assert(file_ != NULL);
  34. assert(file_ != INVALID_HANDLE_VALUE);
  35. }
  36. void WinLogger::DebugWriter(const char* str, int len) {
  37. assert(file_ != INVALID_HANDLE_VALUE);
  38. DWORD bytesWritten = 0;
  39. BOOL ret = WriteFile(file_, str, len, &bytesWritten, NULL);
  40. if (ret == FALSE) {
  41. std::string errSz = GetWindowsErrSz(GetLastError());
  42. fprintf(stderr, errSz.c_str());
  43. }
  44. }
  45. WinLogger::~WinLogger() {
  46. CloseInternal();
  47. }
  48. Status WinLogger::CloseImpl() {
  49. return CloseInternal();
  50. }
  51. Status WinLogger::CloseInternal() {
  52. Status s;
  53. if (INVALID_HANDLE_VALUE != file_) {
  54. BOOL ret = FlushFileBuffers(file_);
  55. if (ret == 0) {
  56. auto lastError = GetLastError();
  57. s = IOErrorFromWindowsError("Failed to flush LOG on Close() ",
  58. lastError);
  59. }
  60. ret = CloseHandle(file_);
  61. // On error the return value is zero
  62. if (ret == 0 && s.ok()) {
  63. auto lastError = GetLastError();
  64. s = IOErrorFromWindowsError("Failed to flush LOG on Close() ",
  65. lastError);
  66. }
  67. file_ = INVALID_HANDLE_VALUE;
  68. closed_ = true;
  69. }
  70. return s;
  71. }
  72. void WinLogger::Flush() {
  73. assert(file_ != INVALID_HANDLE_VALUE);
  74. if (flush_pending_) {
  75. flush_pending_ = false;
  76. // With Windows API writes go to OS buffers directly so no fflush needed
  77. // unlike with C runtime API. We don't flush all the way to disk
  78. // for perf reasons.
  79. }
  80. last_flush_micros_ = env_->NowMicros();
  81. }
  82. void WinLogger::Logv(const char* format, va_list ap) {
  83. IOSTATS_TIMER_GUARD(logger_nanos);
  84. assert(file_ != INVALID_HANDLE_VALUE);
  85. const uint64_t thread_id = (*gettid_)();
  86. // We try twice: the first time with a fixed-size stack allocated buffer,
  87. // and the second time with a much larger dynamically allocated buffer.
  88. char buffer[500];
  89. std::unique_ptr<char[]> largeBuffer;
  90. for (int iter = 0; iter < 2; ++iter) {
  91. char* base;
  92. int bufsize;
  93. if (iter == 0) {
  94. bufsize = sizeof(buffer);
  95. base = buffer;
  96. } else {
  97. bufsize = 30000;
  98. largeBuffer.reset(new char[bufsize]);
  99. base = largeBuffer.get();
  100. }
  101. char* p = base;
  102. char* limit = base + bufsize;
  103. struct timeval now_tv;
  104. gettimeofday(&now_tv, nullptr);
  105. const time_t seconds = now_tv.tv_sec;
  106. struct tm t;
  107. localtime_s(&t, &seconds);
  108. p += snprintf(p, limit - p, "%04d/%02d/%02d-%02d:%02d:%02d.%06d %llx ",
  109. t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour,
  110. t.tm_min, t.tm_sec, static_cast<int>(now_tv.tv_usec),
  111. static_cast<long long unsigned int>(thread_id));
  112. // Print the message
  113. if (p < limit) {
  114. va_list backup_ap;
  115. va_copy(backup_ap, ap);
  116. int done = vsnprintf(p, limit - p, format, backup_ap);
  117. if (done > 0) {
  118. p += done;
  119. } else {
  120. continue;
  121. }
  122. va_end(backup_ap);
  123. }
  124. // Truncate to available space if necessary
  125. if (p >= limit) {
  126. if (iter == 0) {
  127. continue; // Try again with larger buffer
  128. } else {
  129. p = limit - 1;
  130. }
  131. }
  132. // Add newline if necessary
  133. if (p == base || p[-1] != '\n') {
  134. *p++ = '\n';
  135. }
  136. assert(p <= limit);
  137. const size_t write_size = p - base;
  138. DWORD bytesWritten = 0;
  139. BOOL ret = WriteFile(file_, base, static_cast<DWORD>(write_size),
  140. &bytesWritten, NULL);
  141. if (ret == FALSE) {
  142. std::string errSz = GetWindowsErrSz(GetLastError());
  143. fprintf(stderr, errSz.c_str());
  144. }
  145. flush_pending_ = true;
  146. assert((bytesWritten == write_size) || (ret == FALSE));
  147. if (bytesWritten > 0) {
  148. log_size_ += write_size;
  149. }
  150. uint64_t now_micros =
  151. static_cast<uint64_t>(now_tv.tv_sec) * 1000000 + now_tv.tv_usec;
  152. if (now_micros - last_flush_micros_ >= flush_every_seconds_ * 1000000) {
  153. flush_pending_ = false;
  154. // With Windows API writes go to OS buffers directly so no fflush needed
  155. // unlike with C runtime API. We don't flush all the way to disk
  156. // for perf reasons.
  157. last_flush_micros_ = now_micros;
  158. }
  159. break;
  160. }
  161. }
  162. size_t WinLogger::GetLogFileSize() const { return log_size_; }
  163. }
  164. } // namespace ROCKSDB_NAMESPACE