diff --git a/mace/core/registry.h b/mace/core/registry.h index 241f8515053d30d7d3fec9515e14720a5a08bdcd..f2762dab164747d2f27a0ddea226d3d3beafc9b0 100644 --- a/mace/core/registry.h +++ b/mace/core/registry.h @@ -40,10 +40,6 @@ class Registry { registry_[key] = creator; } - inline bool Has(const SrcType &key) const { - return registry_.count(key) != 0; - } - std::unique_ptr Create(const SrcType &key, Args... args) const { if (registry_.count(key) == 0) { LOG(FATAL) << "Key not registered: " << key; @@ -51,17 +47,6 @@ class Registry { return registry_.at(key)(args...); } - /** - * Returns the keys currently registered as a vector. - */ - std::vector Keys() const { - std::vector keys; - for (const auto &it : registry_) { - keys.push_back(it.first); - } - return keys; - } - private: std::map registry_; std::mutex register_mutex_; @@ -97,27 +82,12 @@ class Registerer { typedef Registerer \ Registerer##RegistryName; -/* -#define MACE_DEFINE_TYPED_REGISTRY(RegistryName, SrcType, ObjectType, ...) \ - Registry *RegistryName() { \ - static Registry *registry = \ - new Registry(); \ - return registry; \ - } -*/ - #define MACE_DECLARE_REGISTRY(RegistryName, ObjectType, ...) \ MACE_DECLARE_TYPED_REGISTRY(RegistryName, std::string, ObjectType, \ ##__VA_ARGS__) -/* -#define MACE_DEFINE_REGISTRY(RegistryName, ObjectType, ...) \ - MACE_DEFINE_TYPED_REGISTRY(RegistryName, std::string, ObjectType, \ - ##__VA_ARGS__) -*/ - -#define MACE_REGISTER_TYPED_CLASS(RegistryName, registry, key, ...) \ - Registerer##RegistryName MACE_ANONYMOUS_VARIABLE(l_##RegistryName)( \ +#define MACE_REGISTER_TYPED_CLASS(RegistryName, registry, key, ...) \ + Registerer##RegistryName MACE_ANONYMOUS_VARIABLE(RegistryName)( \ key, registry, Registerer##RegistryName::DefaultCreator<__VA_ARGS__>); #define MACE_REGISTER_CLASS(RegistryName, registry, key, ...) \ diff --git a/mace/utils/logging.cc b/mace/utils/logging.cc index a8b06e698a1772918467f74101de2921d25e8cb3..514feeaa087bb6428b9dfe8936a729a8a47be3ed 100644 --- a/mace/utils/logging.cc +++ b/mace/utils/logging.cc @@ -16,10 +16,10 @@ #include #include +#include #if defined(ANDROID) || defined(__ANDROID__) #include #include -#include #endif namespace mace { @@ -28,8 +28,13 @@ namespace logging { LogMessage::LogMessage(const char *fname, int line, int severity) : fname_(fname), line_(line), severity_(severity) {} -#if defined(ANDROID) || defined(__ANDROID__) +void LogMessage::DealWithFatal() { + // When there is a fatal log, now we simply abort. + abort(); +} + void LogMessage::GenerateLogMessage() { +#if defined(ANDROID) || defined(__ANDROID__) int android_log_level; switch (severity_) { case INFO: @@ -61,74 +66,47 @@ void LogMessage::GenerateLogMessage() { // Also log to stderr (for standalone Android apps). std::cerr << "IWEF"[severity_] << " " << ss.str() << std::endl; - - // Android logging at level FATAL does not terminate execution, so abort() - // is still required to stop the program. - if (severity_ == FATAL) { - abort(); - } -} - #else - -void LogMessage::GenerateLogMessage() { fprintf(stderr, "%c %s:%d] %s\n", "IWEF"[severity_], fname_, line_, str().c_str()); -} #endif + // When there is a fatal log, terminate execution + if (severity_ == FATAL) { + DealWithFatal(); + } +} namespace { -// Parse log level (int64_t) from environment variable (char*) -int64_t LogLevelStrToInt(const char *mace_env_var_val) { +int LogLevelStrToInt(const char *mace_env_var_val) { if (mace_env_var_val == nullptr) { return 0; } - - // Ideally we would use env_var / safe_strto64, but it is - // hard to use here without pulling in a lot of dependencies, - // so we use std:istringstream instead - std::string min_log_level(mace_env_var_val); - std::istringstream ss(min_log_level); - int64_t level; - if (!(ss >> level)) { - // Invalid vlog level setting, set level to default (0) - level = 0; - } - - return level; + // Simply use atoi here. Return 0 if convert unsuccessfully. + return atoi(mace_env_var_val); } -int64_t MinLogLevelFromEnv() { - const char *mace_env_var_val = getenv("MACE_CPP_MIN_LOG_LEVEL"); - return LogLevelStrToInt(mace_env_var_val); +int MinLogLevelFromEnv() { + // Read the min log level from env once during the first call to logging. + static int log_level = LogLevelStrToInt(getenv("MACE_CPP_MIN_LOG_LEVEL")); + return log_level; } -int64_t MinVLogLevelFromEnv() { - const char *mace_env_var_val = getenv("MACE_CPP_MIN_VLOG_LEVEL"); - return LogLevelStrToInt(mace_env_var_val); +int MinVLogLevelFromEnv() { + // Read the min vlog level from env once during the first call to logging. + static int vlog_level = LogLevelStrToInt(getenv("MACE_CPP_MIN_VLOG_LEVEL")); + return vlog_level; } } // namespace LogMessage::~LogMessage() { - // Read the min log level once during the first call to logging. - static int64_t min_log_level = MinLogLevelFromEnv(); + int min_log_level = MinLogLevelFromEnv(); if (severity_ >= min_log_level) GenerateLogMessage(); } -int64_t LogMessage::MinVLogLevel() { - static int64_t min_vlog_level = MinVLogLevelFromEnv(); - return min_vlog_level; -} - -LogMessageFatal::LogMessageFatal(const char *file, int line) - : LogMessage(file, line, FATAL) {} -LogMessageFatal::~LogMessageFatal() { - // abort() ensures we don't return (we promised we would not via - // ATTRIBUTE_NORETURN). - GenerateLogMessage(); - abort(); +int LogMessage::MinVLogLevel() { + return MinVLogLevelFromEnv(); } } // namespace logging diff --git a/mace/utils/logging.h b/mace/utils/logging.h index 1f540fa692d793a69230a1f564104a0a1eba5bb2..0a56fb657281f024b02a9dbde20f404bf48f7fd0 100644 --- a/mace/utils/logging.h +++ b/mace/utils/logging.h @@ -30,61 +30,37 @@ namespace mace { -const int INFO = 0; // base_logging::INFO; -const int WARNING = 1; // base_logging::WARNING; -const int ERROR = 2; // base_logging::ERROR; -const int FATAL = 3; // base_logging::FATAL; -const int NUM_SEVERITIES = 4; // base_logging::NUM_SEVERITIES; +// Log severity level constants. +const int INFO = 0; +const int WARNING = 1; +const int ERROR = 2; +const int FATAL = 3; namespace logging { -class LogMessage : public std::basic_ostringstream { +class LogMessage : public std::ostringstream { public: LogMessage(const char *fname, int line, int severity); ~LogMessage(); - // Returns the minimum log level for VLOG statements. - // E.g., if MinVLogLevel() is 2, then VLOG(2) statements will produce output, - // but VLOG(3) will not. Defaults to 0. - static int64_t MinVLogLevel(); + static int MinVLogLevel(); - protected: + private: void GenerateLogMessage(); + void DealWithFatal(); - private: const char *fname_; int line_; int severity_; }; -// LogMessageFatal ensures the process will exit in failure after -// logging this message. -class LogMessageFatal : public LogMessage { - public: - LogMessageFatal(const char *file, int line); - ~LogMessageFatal(); -}; - -#define _MACE_LOG_INFO \ - ::mace::logging::LogMessage(__FILE__, __LINE__, mace::INFO) -#define _MACE_LOG_WARNING \ - ::mace::logging::LogMessage(__FILE__, __LINE__, mace::WARNING) -#define _MACE_LOG_ERROR \ - ::mace::logging::LogMessage(__FILE__, __LINE__, mace::ERROR) -#define _MACE_LOG_FATAL ::mace::logging::LogMessageFatal(__FILE__, __LINE__) - -#define _MACE_LOG_QFATAL _MACE_LOG_FATAL - -#define LOG(severity) _MACE_LOG_##severity - -// Set MACE_CPP_MIN_VLOG_LEVEL environment to update minimum log -// level -// of VLOG -#define VLOG_IS_ON(lvl) ((lvl) <= ::mace::logging::LogMessage::MinVLogLevel()) +#define LOG(severity) \ + ::mace::logging::LogMessage(__FILE__, __LINE__, mace::severity) -#define VLOG(lvl) \ - if (VLOG_IS_ON(lvl)) \ - ::mace::logging::LogMessage(__FILE__, __LINE__, mace::INFO) +// Set MACE_CPP_MIN_VLOG_LEVEL environment to update minimum log level of VLOG. +// Only when vlog_level <= MinVLogLevel(), it will produce output. +#define VLOG_IS_ON(vll) ((vll) <= ::mace::logging::LogMessage::MinVLogLevel()) +#define VLOG(vll) if (VLOG_IS_ON(vll)) LOG(INFO) // MACE_CHECK/MACE_ASSERT dies with a fatal error if condition is not true. // MACE_ASSERT is controlled by NDEBUG ('-c opt' for bazel) while MACE_CHECK @@ -92,14 +68,14 @@ class LogMessageFatal : public LogMessage { // Therefore, it is safe to do things like: // MACE_CHECK(fp->Write(x) == 4) // MACE_CHECK(fp->Write(x) == 4, "Write failed") -// which are not correct for MACE_ASSERT. +// which are not safe for MACE_ASSERT. #define MACE_CHECK(condition, ...) \ - if (!(condition)) \ + if (!(condition)) \ LOG(FATAL) << "Check failed: " #condition " " << MakeString(__VA_ARGS__) #ifndef NDEBUG #define MACE_ASSERT(condition, ...) \ - if (!(condition)) \ + if (!(condition)) \ LOG(FATAL) << "Assert failed: " #condition " " << MakeString(__VA_ARGS__) #else #define MACE_ASSERT(condition, ...) ((void)0) @@ -108,12 +84,12 @@ class LogMessageFatal : public LogMessage { template T &&CheckNotNull(const char *file, int line, const char *exprtext, T &&t) { if (t == nullptr) { - LogMessageFatal(file, line) << std::string(exprtext); + ::mace::logging::LogMessage(file, line, FATAL) << std::string(exprtext); } return std::forward(t); } -#define MACE_CHECK_NOTNULL(val) \ +#define MACE_CHECK_NOTNULL(val) \ ::mace::logging::CheckNotNull(__FILE__, __LINE__, \ "'" #val "' Must be non NULL", (val))