google开源库glog源码实现分析

【google开源库glog源码实现分析】少年恃险若平地,独倚长剑凌清秋。这篇文章主要讲述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() constreturn pptr() - pbase();
char* pbase() constreturn 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() constreturn ctr_;
void set_ctr(int ctr)ctr_ = ctr;
LogStream* self() constreturn self_;

// Legacy std::streambuf methods.
size_t pcount() constreturn streambuf_.pcount();
char* pbase() constreturn streambuf_.pbase();
char* str() constreturn 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);

    推荐阅读