google开源库glog源码实现分析

#include <vector>
# define GOOGLE_GLOG_DLL_DECL  __declspec(dllexport)
# define GOOGLE_GLOG_DLL_DECL_FOR_UNITTESTS  __declspec(dllimport)

namespace base_logging {

	// LogMessage::LogStream is a std::ostream backed by this streambuf.
	// This class ignores overflow and leaves two bytes at the end of the
	// buffer to allow for a '\n' and '\0'.
	class GOOGLE_GLOG_DLL_DECL LogStreamBuf : public std::streambuf {
	public:
		// REQUIREMENTS: "len" must be >= 2 to account for the '\n' and '\n'.
		LogStreamBuf(char *buf, int len) {
			setp(buf, buf + len - 2);
		}
		// This effectively ignores overflow.
		virtual int_type overflow(int_type ch) {
			return ch;
		}

		// Legacy public ostrstream method.
		size_t pcount() const { return pptr() - pbase(); }
		char* pbase() const { return std::streambuf::pbase(); }
	};

}  // namespace base_logging

class GOOGLE_GLOG_DLL_DECL LogStream : public std::ostream {
#ifdef _MSC_VER
# pragma warning(default: 4275)
#endif
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:
	LogStream(const LogStream&);
	LogStream& operator=(const LogStream&);
	base_logging::LogStreamBuf streambuf_;
	int ctr_;  // Counter hack (for the LOG_EVERY_X() macro)
	LogStream *self_;  // Consistency check hack
};

typedef int LogSeverity;

//static Mutex log_mutex;

// Number of messages sent at each severity.  Under log_mutex.
//int64 LogMessage::num_messages_[NUM_SEVERITIES] = { 0, 0, 0, 0 };

// Globally disable log writing (if disk is full)
static bool stop_writing = false;

const char*const LogSeverityNames[NUM_SEVERITIES] = {
	"INFO", "WARNING", "ERROR", "FATAL"
};

// Has the user called SetExitOnDFatal(true)?
static bool exit_on_dfatal = true;

const char* GetLogSeverityName(LogSeverity severity) {
	return LogSeverityNames[severity];
}

typedef int pid_t;
#include <processthreadsapi.h>
pid_t GetTID() {
	return GetCurrentThreadId();
}

#include <iomanip>
class GOOGLE_GLOG_DLL_DECL LogSink {
public:
	virtual ~LogSink() {}
	virtual void send(LogSeverity severity, const char* full_filename,
		const char* base_filename, int line,
		const struct ::tm* tm_time,
		const char* message, size_t message_len) = 0;
	virtual void WaitTillSent()
	{}
	static std::string ToString(LogSeverity severity, const char* file, int line,
		const struct ::tm* tm_time,
		const char* message, size_t message_len)
	{
		ostringstream stream(string(message, message_len));
		stream.fill('0');

		// FIXME(jrvb): Updating thi
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值