You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 line
4.6 KiB

1 月之前
  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. //
  5. // Logger implementation that can be shared by all environments
  6. // where enough posix functionality is available.
  7. #ifndef STORAGE_LEVELDB_UTIL_POSIX_LOGGER_H_
  8. #define STORAGE_LEVELDB_UTIL_POSIX_LOGGER_H_
  9. #include <sys/time.h>
  10. #include <cassert>
  11. #include <cstdarg>
  12. #include <cstdio>
  13. #include <ctime>
  14. #include <sstream>
  15. #include <thread>
  16. #include "leveldb/env.h"
  17. namespace leveldb {
  18. class PosixLogger final : public Logger {
  19. public:
  20. // Creates a logger that writes to the given file.
  21. //
  22. // The PosixLogger instance takes ownership of the file handle.
  23. explicit PosixLogger(std::FILE* fp) : fp_(fp) { assert(fp != nullptr); }
  24. ~PosixLogger() override { std::fclose(fp_); }
  25. void Logv(const char* format, std::va_list arguments) override {
  26. // Record the time as close to the Logv() call as possible.
  27. struct ::timeval now_timeval;
  28. ::gettimeofday(&now_timeval, nullptr);
  29. const std::time_t now_seconds = now_timeval.tv_sec;
  30. struct std::tm now_components;
  31. ::localtime_r(&now_seconds, &now_components);
  32. // Record the thread ID.
  33. constexpr const int kMaxThreadIdSize = 32;
  34. std::ostringstream thread_stream;
  35. thread_stream << std::this_thread::get_id();
  36. std::string thread_id = thread_stream.str();
  37. if (thread_id.size() > kMaxThreadIdSize) {
  38. thread_id.resize(kMaxThreadIdSize);
  39. }
  40. // We first attempt to print into a stack-allocated buffer. If this attempt
  41. // fails, we make a second attempt with a dynamically allocated buffer.
  42. constexpr const int kStackBufferSize = 512;
  43. char stack_buffer[kStackBufferSize];
  44. static_assert(sizeof(stack_buffer) == static_cast<size_t>(kStackBufferSize),
  45. "sizeof(char) is expected to be 1 in C++");
  46. int dynamic_buffer_size = 0; // Computed in the first iteration.
  47. for (int iteration = 0; iteration < 2; ++iteration) {
  48. const int buffer_size =
  49. (iteration == 0) ? kStackBufferSize : dynamic_buffer_size;
  50. char* const buffer =
  51. (iteration == 0) ? stack_buffer : new char[dynamic_buffer_size];
  52. // Print the header into the buffer.
  53. int buffer_offset = std::snprintf(
  54. buffer, buffer_size, "%04d/%02d/%02d-%02d:%02d:%02d.%06d %s ",
  55. now_components.tm_year + 1900, now_components.tm_mon + 1,
  56. now_components.tm_mday, now_components.tm_hour, now_components.tm_min,
  57. now_components.tm_sec, static_cast<int>(now_timeval.tv_usec),
  58. thread_id.c_str());
  59. // The header can be at most 28 characters (10 date + 15 time +
  60. // 3 delimiters) plus the thread ID, which should fit comfortably into the
  61. // static buffer.
  62. assert(buffer_offset <= 28 + kMaxThreadIdSize);
  63. static_assert(28 + kMaxThreadIdSize < kStackBufferSize,
  64. "stack-allocated buffer may not fit the message header");
  65. assert(buffer_offset < buffer_size);
  66. // Print the message into the buffer.
  67. std::va_list arguments_copy;
  68. va_copy(arguments_copy, arguments);
  69. buffer_offset +=
  70. std::vsnprintf(buffer + buffer_offset, buffer_size - buffer_offset,
  71. format, arguments_copy);
  72. va_end(arguments_copy);
  73. // The code below may append a newline at the end of the buffer, which
  74. // requires an extra character.
  75. if (buffer_offset >= buffer_size - 1) {
  76. // The message did not fit into the buffer.
  77. if (iteration == 0) {
  78. // Re-run the loop and use a dynamically-allocated buffer. The buffer
  79. // will be large enough for the log message, an extra newline and a
  80. // null terminator.
  81. dynamic_buffer_size = buffer_offset + 2;
  82. continue;
  83. }
  84. // The dynamically-allocated buffer was incorrectly sized. This should
  85. // not happen, assuming a correct implementation of std::(v)snprintf.
  86. // Fail in tests, recover by truncating the log message in production.
  87. assert(false);
  88. buffer_offset = buffer_size - 1;
  89. }
  90. // Add a newline if necessary.
  91. if (buffer[buffer_offset - 1] != '\n') {
  92. buffer[buffer_offset] = '\n';
  93. ++buffer_offset;
  94. }
  95. assert(buffer_offset <= buffer_size);
  96. std::fwrite(buffer, 1, buffer_offset, fp_);
  97. std::fflush(fp_);
  98. if (iteration != 0) {
  99. delete[] buffer;
  100. }
  101. break;
  102. }
  103. }
  104. private:
  105. std::FILE* const fp_;
  106. };
  107. } // namespace leveldb
  108. #endif // STORAGE_LEVELDB_UTIL_POSIX_LOGGER_H_