您当前的位置: 首页 > 

phymat.nico

暂无认证

  • 1浏览

    0关注

    1967博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

google开源库glog源码实现分析

phymat.nico 发布时间:2017-10-18 16:34:13 ,浏览量:1

#include 
# 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 
pid_t GetTID() {
	return GetCurrentThreadId();
}

#include 
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 this to use the correct value for usecs
		// requires changing the signature for both this method and
		// LogSink::send().  This change needs to be done in a separate CL
		// so subclasses of LogSink can be updated at the same time.
		int usecs = 0;

		stream has_been_flushed_ = false;

	// If specified, prepend a prefix to each line.  For example:
	//    I1018 160715 f5d4fbb0 logging.cc:1153]
	//    (log level, GMT month, date, time, thread_id, file basename, line)
	// We exclude the thread_id for the default thread.
	if (1/*FLAGS_log_prefix && (line != kNoLogPrefix)*/) {
		stream() message_text_[data_->num_chars_to_log_ - 1] = original_final_char;
	}

	cout message_text_ has_been_flushed_ = true;
}

#define LOG(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()

#define COMPACT_GOOGLE_LOG_INFO LogMessage( \
      __FILE__, __LINE__)

class emptyclass
{
public:
	emptyclass()
	{}
	emptyclass(const char *file, int line)
	{
		strcpy(file_, file);
		line_ = line;
	}
	~emptyclass()
	{
		cout             
关注
打赏
1659628745
查看更多评论
0.0433s