log_buffer.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #pragma once
  6. #include <ctime>
  7. #include "memory/arena.h"
  8. #include "port/sys_time.h"
  9. #include "rocksdb/env.h"
  10. #include "util/autovector.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. class Logger;
  13. // A class to buffer info log entries and flush them in the end.
  14. class LogBuffer {
  15. public:
  16. // log_level: the log level for all the logs
  17. // info_log: logger to write the logs to
  18. LogBuffer(const InfoLogLevel log_level, Logger* info_log);
  19. // Add a log entry to the buffer. Use default max_log_size.
  20. // max_log_size indicates maximize log size, including some metadata.
  21. void AddLogToBuffer(size_t max_log_size, const char* format, va_list ap);
  22. size_t IsEmpty() const { return logs_.empty(); }
  23. // Flush all buffered log to the info log.
  24. void FlushBufferToLog();
  25. static const size_t kDefaultMaxLogSize = 512;
  26. private:
  27. // One log entry with its timestamp
  28. struct BufferedLog {
  29. struct timeval now_tv; // Timestamp of the log
  30. char message[1]; // Beginning of log message
  31. };
  32. const InfoLogLevel log_level_;
  33. Logger* info_log_;
  34. Arena arena_;
  35. autovector<BufferedLog*> logs_;
  36. };
  37. // Add log to the LogBuffer for a delayed info logging. It can be used when
  38. // we want to add some logs inside a mutex.
  39. // max_log_size indicates maximize log size, including some metadata.
  40. extern void LogToBuffer(LogBuffer* log_buffer, size_t max_log_size,
  41. const char* format, ...);
  42. // Same as previous function, but with default max log size.
  43. extern void LogToBuffer(LogBuffer* log_buffer, const char* format, ...);
  44. } // namespace ROCKSDB_NAMESPACE