123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- #ifndef CNSTREAM_CORE_LOGGING_HPP_
- #define CNSTREAM_CORE_LOGGING_HPP_
- #include <gflags/gflags.h>
- #include <time.h>
- #include <string>
- #include <streambuf>
- #include <ostream>
- DECLARE_string(log_filter);
- DECLARE_int32(min_log_level);
- DECLARE_int32(flush_log_file_secs);
- DECLARE_bool(log_to_stderr);
- DECLARE_bool(log_to_file);
- #define STR(src) #src
- #define LOGF(category) \
- cnstream::LogMessage(STR(category), __FILE__, __LINE__, cnstream::LogSeverity::LOG_FATAL).stream()
- #define LOGF_IF(category, condition) \
- !(condition) ? (void) 0 : cnstream::LogMessageVoidify() & LOGF(category)
- #define LOGE(category) \
- cnstream::LogMessage(STR(category), __FILE__, __LINE__, cnstream::LogSeverity::LOG_ERROR).stream()
- #define LOGE_IF(category, condition) \
- !(condition) ? (void) 0 : cnstream::LogMessageVoidify() & LOGE(category)
- #define LOGW(category) \
- cnstream::LogMessage(STR(category), __FILE__, __LINE__, cnstream::LogSeverity::LOG_WARNING).stream()
- #define LOGW_IF(category, condition) \
- !(condition) ? (void) 0 : cnstream::LogMessageVoidify() & LOGW(category)
- #define LOGI(category) cnstream::LogMessage(STR(category), __FILE__, __LINE__, cnstream::LogSeverity::LOG_INFO).stream()
- #define LOGI_IF(category, condition) \
- !(condition) ? (void) 0 : cnstream::LogMessageVoidify() & LOGI(category)
- #define LOGD(category) \
- cnstream::LogMessage(STR(category), __FILE__, __LINE__, cnstream::LogSeverity::LOG_DEBUG).stream()
- #define LOGD_IF(category, condition) \
- !(condition) ? (void) 0 : cnstream::LogMessageVoidify() & LOGD(category)
- #define LOGT(category) \
- cnstream::LogMessage(STR(category), __FILE__, __LINE__, cnstream::LogSeverity::LOG_TRACE).stream()
- #define LOGT_IF(category, condition) \
- !(condition) ? (void) 0 : cnstream::LogMessageVoidify() & LOGT(category)
- #define LOGA(category) cnstream::LogMessage(STR(category), __FILE__, __LINE__, cnstream::LogSeverity::LOG_ALL).stream()
- #define LOGA_IF(category, condition) \
- !(condition) ? (void) 0 : cnstream::LogMessageVoidify() & LOGA(category)
- namespace cnstream {
- enum class LogSeverity { LOG_FATAL = 0, LOG_ERROR, LOG_WARNING, LOG_INFO, LOG_DEBUG, LOG_TRACE, LOG_ALL };
- class LogSink {
- public:
- virtual ~LogSink() { }
- virtual void Send(LogSeverity severity, const char* category,
- const char* filename, int line,
- const struct ::tm* tm_time, int32_t usecs,
- const char* message, size_t message_len) = 0;
- virtual void WaitTillSent() { }
- static std::string ToString(LogSeverity severity, const char* category,
- const char* filename, int line,
- const struct ::tm* tm_time, int32_t usecs,
- const char* message, size_t message_len);
- };
- class LogMessageVoidify {
- public:
- LogMessageVoidify() { }
-
-
- void operator&(std::ostream&) { }
- };
- class LogMessage {
- public:
- class LogStreamBuf : public std::streambuf {
- public:
-
- LogStreamBuf(char *buf, int len) {
- setp(buf, buf + len - 2);
- }
-
- virtual int_type overflow(int_type ch) {
- return ch;
- }
-
- size_t pcount() const { return pptr() - pbase(); }
- char* pbase() const { return std::streambuf::pbase(); }
- };
- class LogStream : public std::ostream {
- public:
- LogStream(char *buf, int len)
- : std::ostream(NULL),
- streambuf_(buf, len) {
- rdbuf(&streambuf_);
- }
-
- size_t pcount() const { return streambuf_.pcount(); }
- char* pbase() const { return streambuf_.pbase(); }
- char* str() const { return pbase(); }
- private:
- LogStream(const LogStream&) = delete;
- LogStream& operator=(const LogStream&) = delete;
- LogStreamBuf streambuf_;
- };
- LogMessage(const char* category, const char* file, int line, LogSeverity severity);
- ~LogMessage();
- void Init(const char* category, const char* file, int line, LogSeverity severity);
- std::ostream& stream();
- struct LogMessageData;
- private:
- LogMessage(const LogMessage&) = delete;
- LogMessage& operator=(const LogMessage&) = delete;
- void Flush();
- void SendToLog();
- LogMessageData* data_;
- LogMessageData* allocated_;
- static const size_t MaxLogMsgLen;
- };
- void InitCNStreamLogging(const char* log_dir);
- void AddLogSink(LogSink* log_sink);
- void RemoveLogSink(LogSink* log_sink);
- void ShutdownCNStreamLogging();
- }
- #endif
|