#endif
#include <cassert>
-#include <climits>
-#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
-#include <memory>
#include <sstream>
#include <string>
-#include <vector>
#ifndef __FILENAME__
#define __FILENAME__ \
}
};
-// Interface class for logging backends. The custom LogBackend which wants
-// log using LOG() macro should be implement following interface.
-class ILogBackend {
+class LogCatcher {
public:
- virtual void WriteLog(LogLevel level, const std::string& tag,
- const std::string& logstr) = 0;
-};
+ LogCatcher(LogLevel level, const char* tag)
+ : level_(level), tag_(tag) { }
-class DLogBackend : public ILogBackend {
- public:
- void WriteLog(LogLevel level, const std::string& tag,
- const std::string& logstr) override {
- dlog_print(LogLevelToPriority(level), tag.c_str(), "%s",
- Escape(logstr).c_str());
+ void operator&(const StringStream<char>& str) const {
+ dlog_print(LogLevelToPriority(level_), tag_.c_str(), "%s",
+ Escape(str.str()).c_str());
}
private:
}
return escaped;
}
-};
-
-class LogCore {
- public:
- // Do not call this function at destructor of global object
- static LogCore& GetCore() {
- static LogCore core;
- return core;
- }
-
- void AddLogBackend(std::shared_ptr<ILogBackend> backend) {
- backend_list_.emplace_back(backend);
- }
-
- void Log(LogLevel level, const std::string& tag, const std::string& log) {
- for (auto& backend : backend_list_)
- backend->WriteLog(level, tag, log);
- }
-
- private:
- LogCore() {
- // add default dlog backend
- AddLogBackend(std::shared_ptr<ILogBackend>(new DLogBackend()));
- }
- ~LogCore() = default;
- LogCore(const LogCore&) = delete;
- LogCore& operator=(const LogCore&) = delete;
-
- std::vector<std::shared_ptr<ILogBackend>> backend_list_;
-};
-
-class LogCatcher {
- public:
- LogCatcher(LogLevel level, const char* tag)
- : level_(level), tag_(tag) { }
-
- void operator&(const StringStream<char>& str) const {
- LogCore::GetCore().Log(level_, tag_, str.str());
- }
-
- private:
LogLevel level_;
std::string tag_;
};
} // namespace utils
-
-inline static const constexpr char* __tag_for_logging() {
- return "";
-}
-
inline static const constexpr char* __tag_for_project() {
return PROJECT_TAG;
}
-// To be defined in class namespace if user want different log tag for given
-// scope
-#define SCOPE_LOG_TAG(TAG) \
- inline static const constexpr char* __tag_for_logging() { \
- return #TAG; \
- } \
-
// Simple logging macro of following usage:
// LOG(LEVEL) << object_1 << object_2 << object_n;
// where:
::utils::LogLevel::LOG_ ## LEVEL, __tag_for_project()) \
& ::utils::StringStream<char>() \
<< std::string(::utils::LogTag<::utils::LogLevel::LOG_ ## LEVEL>::value) \
- << " " << std::setw(25) << std::left << __tag_for_logging() \
<< " : " << std::setw(36) \
<< (std::string(__FILENAME__) + ":" + std::to_string(__LINE__)).c_str() \
<< std::setw(0) << " : " \