google开源库glog源码实现分析

Posted shixin_0125

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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 << "[" << LogSeverityNames[severity][0]
<< setw(2) << 1 + tm_time->tm_mon
<< setw(2) << tm_time->tm_mday
<<
<< setw(2) << tm_time->tm_hour << :
<< setw(2) << tm_time->tm_min << :
<< setw(2) << tm_time->tm_sec << .
<< setw(6) << usecs
<<
<< setfill( ) << setw(5) << GetTID() << setfill(0)
<<
<< file << : << line << "] ";

stream << string(message, message_len);
GLog库

Google-glog 日志库使用手记

google-glog 入门教程

C++谷歌开源项目glog学习笔记

Google glog 使用

谷歌日志库GLog 使用说明