comlog_sink.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (c) 2015 Baidu.com, Inc. All Rights Reserved
  2. //
  3. // Redirect LOG() into comlog.
  4. //
  5. // Author: Ge,Jun (gejun@baidu.com)
  6. // Date: Mon Jul 20 12:39:39 CST 2015
  7. #ifndef BASE_COMLOG_SINK_H
  8. #define BASE_COMLOG_SINK_H
  9. #include "base/logging.h"
  10. struct com_device_t;
  11. template <typename T> struct DefaultSingletonTraits;
  12. namespace comspace {
  13. class Event;
  14. }
  15. namespace logging {
  16. enum ComlogSplitType {
  17. COMLOG_SPLIT_TRUNCT = 0,
  18. COMLOG_SPLIT_SIZECUT = 1,
  19. COMLOG_SPLIT_DATECUT = 2,
  20. };
  21. // Options to setup ComlogSink.
  22. struct ComlogSinkOptions {
  23. ComlogSinkOptions();
  24. // true - "AFILE", false - "FILE"
  25. // default: false.
  26. bool async;
  27. // Use F W N T instead of FATAL WARNING NOTICE TRACE for shorter prefixes
  28. // and better alignment.
  29. // default: true
  30. bool shorter_log_level;
  31. // The directory to put logs. Could be absolute or relative path.
  32. // default: "log"
  33. std::string log_dir;
  34. // Name of the process. Use argv[0] when it's empty.
  35. // default: ""
  36. std::string process_name;
  37. // Logs longer than this value are truncated.
  38. // default: 2048
  39. int max_log_length;
  40. // Print VLOG(n) as WARNING instead of TRACE since many online servers
  41. // disable TRACE logs.
  42. // default: true;
  43. bool print_vlog_as_warning;
  44. // Split Comlog type:
  45. // COMLOG_SPLIT_TRUNCT: rotate the log file every 2G written.
  46. // COMLOG_SPLIT_SIZECUT: move existing logs into a separate file every xxx MB written.
  47. // COMLOG_SPLIT_DATECUT: move existing logs into a separate file periodically.
  48. // default: COMLOG_SPLIT_TRUNCT
  49. ComlogSplitType split_type;
  50. // [ Effective when split_type is COMLOG_SPLIT_SIZECUT ]
  51. // Move existing logs into a separate file suffixed with datetime every so many MB written.
  52. // Default: 2048
  53. int cut_size_megabytes;
  54. // Remove oldest cutoff log files when they exceed so many megabytes(roughly)
  55. // Default: 0 (unlimited)
  56. int quota_size;
  57. // [ Effective when split_type is COMLOG_SPLIT_DATECUT ]
  58. // Move existing logs into a separate file suffixed with datetime every so many minutes.
  59. // Example: my_app.log is moved to my_app.log.20160905113104
  60. // Default: 60
  61. int cut_interval_minutes;
  62. // Remove cutoff log files older than so many minutes:
  63. // quota_day * 24 * 60 + quota_hour * 60 + quota_min
  64. // Default: 0 (unlimited)
  65. int quota_day;
  66. int quota_hour;
  67. int quota_min;
  68. // Open wf appender device for WARNING/ERROR/FATAL
  69. // default: false
  70. bool enable_wf_device;
  71. };
  72. // The LogSink to flush logs into comlog. Notice that this is a singleton class.
  73. // [ Setup from a Configure file ]
  74. // if (logging::ComlogSink::GetInstance()->SetupFromConfig("log/log.conf") != 0) {
  75. // LOG(ERROR) << "Fail to setup comlog";
  76. // return -1;
  77. // }
  78. // logging::SetLogSink(ComlogSink::GetInstance());
  79. //
  80. // [ Setup from ComlogSinkOptions ]
  81. // if (logging::ComlogSink::GetInstance()->Setup(NULL/*default options*/) != 0) {
  82. // LOG(ERROR) << "Fail to setup comlog";
  83. // return -1;
  84. // }
  85. // logging::SetLogSink(ComlogSink::GetInstance());
  86. class ComlogSink : public LogSink {
  87. public:
  88. // comlog can have only one instance due to its global open/close.
  89. static ComlogSink* GetInstance();
  90. // Setup comlog in different ways: from a Configure file or
  91. // ComlogSinkOptions. Notice that setup can be done multiple times.
  92. int SetupFromConfig(const std::string& conf_path);
  93. int Setup(const ComlogSinkOptions* options);
  94. // Close comlog and release resources. This method is automatically
  95. // called before Setup and destruction.
  96. void Unload();
  97. // @LogSink
  98. bool OnLogMessage(int severity, const char* file, int line,
  99. const base::StringPiece& content);
  100. private:
  101. ComlogSink();
  102. ~ComlogSink();
  103. friend struct DefaultSingletonTraits<ComlogSink>;
  104. int SetupDevice(com_device_t* dev, const char* type, const char* file, bool is_wf);
  105. bool _init;
  106. ComlogSinkOptions _options;
  107. com_device_t* _dev;
  108. };
  109. class ComlogInitializer {
  110. public:
  111. ComlogInitializer() {
  112. if (com_logstatus() != LOG_NOT_DEFINED) {
  113. com_openlog_r();
  114. }
  115. }
  116. ~ComlogInitializer() {
  117. if (com_logstatus() != LOG_NOT_DEFINED) {
  118. com_closelog_r();
  119. }
  120. }
  121. private:
  122. DISALLOW_COPY_AND_ASSIGN(ComlogInitializer);
  123. };
  124. } // namespace logging
  125. #endif // BASE_COMLOG_SINK_H