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