#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
google开源库glog源码实现分析
最新推荐文章于 2024-01-01 08:15:01 发布
本文详细探讨了glog库的工作原理,重点在于其如何通过构造临时对象并在析构时将ostream内容刷新到日志文件。此外,还介绍了emptyclass类,该类辅助实现了类似cout的简洁日志记录方式。
摘要由CSDN通过智能技术生成