您的位置:首页 > 编程语言 > Go语言

google glog分析2

2014-05-02 00:12 260 查看
分析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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: