google glog分析2
来源:互联网 发布:怎么阅读java源码 编辑:程序博客网 时间:2024/06/05 15:10
分析logging.h,这个文件很重要,定义了各种宏,不同的宏开关定制不同的功能。
GOOGLE_STRIP_LOG是限制等级,低于这个等级的都不会被显示。这是在编译期完成的。从下面的代码就可以看出:
#if GOOGLE_STRIP_LOG == 0#define COMPACT_GOOGLE_LOG_INFO google::LogMessage( \ __FILE__, __LINE__)#define LOG_TO_STRING_INFO(message) google::LogMessage( \ __FILE__, __LINE__, google::GLOG_INFO, message)#else#define COMPACT_GOOGLE_LOG_INFO google::NullStream()#define LOG_TO_STRING_INFO(message) google::NullStream()#endif#if GOOGLE_STRIP_LOG <= 1#define COMPACT_GOOGLE_LOG_WARNING google::LogMessage( \ __FILE__, __LINE__, google::GLOG_WARNING)#define LOG_TO_STRING_WARNING(message) google::LogMessage( \ __FILE__, __LINE__, google::GLOG_WARNING, message)#else#define COMPACT_GOOGLE_LOG_WARNING google::NullStream()#define LOG_TO_STRING_WARNING(message) google::NullStream()#endif#if GOOGLE_STRIP_LOG <= 2#define COMPACT_GOOGLE_LOG_ERROR google::LogMessage( \ __FILE__, __LINE__, google::GLOG_ERROR)#define LOG_TO_STRING_ERROR(message) google::LogMessage( \ __FILE__, __LINE__, google::GLOG_ERROR, message)#else#define COMPACT_GOOGLE_LOG_ERROR google::NullStream()#define LOG_TO_STRING_ERROR(message) google::NullStream()#endif#if GOOGLE_STRIP_LOG <= 3#define COMPACT_GOOGLE_LOG_FATAL google::LogMessageFatal( \ __FILE__, __LINE__)#define LOG_TO_STRING_FATAL(message) google::LogMessage( \ __FILE__, __LINE__, google::GLOG_FATAL, message)#else#define COMPACT_GOOGLE_LOG_FATAL google::NullStreamFatal()#define LOG_TO_STRING_FATAL(message) google::NullStreamFatal()#endif
我们代码里使用的LOG(INFO)宏定义会经#define LOG(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()转化为COMPACT_GOOGLE_LOG_INFO.stream();而COMPACT_GOOGLE_LOG_INFO又会转化为#define LOG_TO_STRING_INFO(message) google::LogMessage( \
__FILE__, __LINE__, google::GLOG_INFO, message).stream()
看下#define GOOGLE_LOG_INFO(counter) google::LogMessage(__FILE__, __LINE__, google::GLOG_INFO, counter, &google::LogMessage::SendToLog)
下面就着重分析下LogMessage这个类:
class LogMessage {public: enum { // Passing kNoLogPrefix for the line number disables the // log-message prefix. Useful for using the LogMessage // infrastructure as a printing utility. See also the --log_prefix // flag for controlling the log-message prefix on an // application-wide basis. kNoLogPrefix = -1 }; class LogStream : public std::ostream { public: LogStream(char *buf, int len, int ctr) : std::ostream(NULL), streambuf_(buf, len), ctr_(ctr), self_(this) { rdbuf(&streambuf_); } int ctr() const { return ctr_; } void set_ctr(int ctr) { ctr_ = ctr; } LogStream* self() const { return self_; } // Legacy std::streambuf methods. size_t pcount() const { return streambuf_.pcount(); } char* pbase() const { return streambuf_.pbase(); } char* str() const { return pbase(); } private: base_logging::LogStreamBuf streambuf_; int ctr_; // Counter hack (for the LOG_EVERY_X() macro) LogStream *self_; // Consistency check hack };public: // icc 8 requires this typedef to avoid an internal compiler error. typedef void (LogMessage::*SendMethod)(); LogMessage(const char* file, int line, LogSeverity severity, int ctr, SendMethod send_method); LogMessage(const char* file, int line); LogMessage(const char* file, int line, LogSeverity severity); LogMessage(const char* file, int line, LogSeverity severity, LogSink* sink, bool also_send_to_log); LogMessage(const char* file, int line, LogSeverity severity, std::vector<std::string>* outvec); LogMessage(const char* file, int line, LogSeverity severity, std::string* message); LogMessage(const char* file, int line, const CheckOpString& result); ~LogMessage(); void Flush(); static const size_t kMaxLogMessageLen; void SendToLog(); // Actually dispatch to the logs void SendToSyslogAndLog(); // Actually dispatch to syslog and the logs // Call abort() or similar to perform LOG(FATAL) crash. static void Fail() ; std::ostream& stream(); int preserved_errno() const; static int64 num_messages(int severity); struct LogMessageData;private: void SendToSinkAndLog(); // Send to sink if provided and dispatch to the logs void SendToSink(); // Send to sink if provided, do nothing otherwise. void WriteToStringAndLog(); void SaveOrSendToLog(); // Save to stringvec if provided, else to logs void Init(const char* file, int line, LogSeverity severity, void (LogMessage::*send_method)()); void RecordCrashReason(glog_internal_namespace_::CrashReason* reason); static int64 num_messages_[NUM_SEVERITIES]; // under log_mutex LogMessageData* allocated_; LogMessageData* data_; friend class LogDestination; LogMessage(const LogMessage&); void operator=(const LogMessage&);};
logmsg里嵌套了一个LogStream;LogStream继承自ostream 代码都很好看懂,时间晚了,也累了,明天继续看,不过看到下面留有扩展接口,很兴奋:
// A Logger is the interface used by logging modules to emit entries
// to a log. A typical implementation will dump formatted data to a
// sequence of files. We also provide interfaces that will forward
// the data to another thread so that the invoker never blocks.
// Implementations should be thread-safe since the logging system
// will write to them from multiple threads.
0 0
- google glog分析2
- google glog源码分析
- google开源库glog源码实现分析
- Google glog
- google glog
- Google/glog
- Google glog
- google-glog功能介绍
- google glog 使用方法
- glog(Google Logging Library)
- google glog简介
- Google glog 使用方法
- Google glog 使用方法
- google-glog安装使用
- google-glog功能介绍
- Google Glog Demo Code.
- google glog使用
- 【C++】google glog库
- jsp NetBeans 简单调试
- Centos 创建 bridge
- postfix的一些零零碎碎
- 李航统计学习方法-改进的迭代尺度算法(IIS)总结
- OpenSessionInView总结
- google glog分析2
- 黑马程序员_Java基础[18]_匿名对象、匿名内部类
- ios 动画中如何正确响应button的各种事件
- shell学习简单基础知识
- Android 9Patch右和下的线
- ExtJS核心工具方法
- Qt模块化笔记之multimedia——实时录音放音的另类实现
- 几种常见的鼻炎你了解吗
- SMTP 状态码