From: Alexander Aksenov Date: Wed, 24 Oct 2018 14:49:30 +0000 (+0300) Subject: Logger: improve X-Git-Tag: submit/tizen/20181205.102314~7^2~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b73c633b0b4edba5eca7d6bcaffed3e3c965cbec;p=sdk%2Ftools%2Fnetcoredbg.git Logger: improve - remove Tizen-dependent definitions from main.cpp; - add manual log level specifying; - small rework of logger interface. Signed-off-by: Alexander Aksenov --- diff --git a/src/debug/netcoredbg/logger.cpp b/src/debug/netcoredbg/logger.cpp index 1053513..1d95d37 100644 --- a/src/debug/netcoredbg/logger.cpp +++ b/src/debug/netcoredbg/logger.cpp @@ -19,7 +19,7 @@ class DlogLogger : public LoggerImpl DlogLogger() {} ~DlogLogger() override {} void log(LogLevel level, const std::string& msg) override; - void vlog(LogLevel level, const std::string fmt, va_list args) override; + void vlog(LogLevel level, const std::string& fmt, va_list args) override; }; log_priority DlogLogger::MapLogLevel(LogLevel level) @@ -39,7 +39,7 @@ void DlogLogger::log(LogLevel level, const std::string &msg) dlog_print(MapLogLevel(level), "NETCOREDBG", "%s", msg.c_str()); } -void DlogLogger::vlog(LogLevel level, const std::string fmt, va_list args) +void DlogLogger::vlog(LogLevel level, const std::string &fmt, va_list args) { dlog_vprint(MapLogLevel(level), "NETCOREDBG", fmt.c_str(), args); } @@ -54,31 +54,40 @@ class NoLogger : public LoggerImpl public: NoLogger() {} ~NoLogger() override {} - void log(LogLevel level, const std::string &msg) override {} - void vlog(LogLevel level, const std::string fmt, va_list args) override {} + void log(LogLevel level, const std::string& msg) override {} + void vlog(LogLevel level, const std::string& fmt, va_list args) override {} }; class FileLogger : public LoggerImpl { - LogLevel l; - private: - const std::string filenameBase = "netcoredbg_"; + static const std::string filenameBase; + static const std::string debugStr; + static const std::string infoStr; + static const std::string warnStr; + static const std::string errorStr; + FILE *logFile; std::time_t timeNow; - static std::string FormatMessageString(const std::string &str); + static std::string FormatMessageString(LogLevel level, const std::string &str); + static const std::string& LevelToString(LogLevel level); public: - FileLogger(LogLevel level); + FileLogger(); ~FileLogger() override; void log(LogLevel level, const std::string& msg) override; - void vlog(LogLevel level, const std::string fmt, va_list args) override; + void vlog(LogLevel level, const std::string& fmt, va_list args) override; }; +const std::string FileLogger::filenameBase = "netcoredbg_"; +const std::string FileLogger::debugStr = "DEBUG"; +const std::string FileLogger::infoStr = "INFO"; +const std::string FileLogger::warnStr = "WARN"; +const std::string FileLogger::errorStr = "ERROR"; -FileLogger::FileLogger(LogLevel level) +FileLogger::FileLogger() { auto time = std::time(nullptr); std::ostringstream oss; @@ -86,7 +95,6 @@ FileLogger::FileLogger(LogLevel level) oss << std::put_time(std::localtime(&time), "%Y_%m_%d__%H_%M_%S"); logFile = fopen(std::string(filenameBase + oss.str() + ".log").c_str(), "w+"); - l = level; } FileLogger::~FileLogger() @@ -94,33 +102,42 @@ FileLogger::~FileLogger() fclose(logFile); } -std::string FileLogger::FormatMessageString(const std::string &str) +const std::string& FileLogger::LevelToString(LogLevel level) +{ + switch (level) { + case LOG_INFO: + return infoStr; + case LOG_WARN: + return warnStr; + case LOG_ERROR: + return errorStr; + case LOG_DEBUG: + default: + return debugStr; + } +} + +std::string FileLogger::FormatMessageString(LogLevel level, const std::string &str) { auto time = std::time(nullptr); std::ostringstream oss; - oss << std::put_time(std::localtime(&time), "%y-%m-%d %OH:%OM:%OS") << " " << str << std::endl; + oss << std::put_time(std::localtime(&time), "%y-%m-%d %OH:%OM:%OS") << " " << LevelToString(level) << " " << str << std::endl; return oss.str(); } void FileLogger::log(LogLevel level, const std::string& msg) { - if (level < l) - return; - if (logFile != NULL) { - fprintf(logFile, "%s", FormatMessageString(msg).c_str()); + fprintf(logFile, "%s", FormatMessageString(level, msg).c_str()); fflush(logFile); } } -void FileLogger::vlog(LogLevel level, const std::string fmt, va_list args) +void FileLogger::vlog(LogLevel level, const std::string& fmt, va_list args) { - if (level < l) - return; - if (logFile != NULL) { - vfprintf(logFile, FormatMessageString(fmt).c_str(), args); + vfprintf(logFile, FormatMessageString(level, fmt).c_str(), args); fflush(logFile); } } @@ -130,23 +147,41 @@ void FileLogger::vlog(LogLevel level, const std::string fmt, va_list args) std::shared_ptr Logger::logger = std::make_shared(); +const std::string Logger::fileStr = "file"; +const std::string Logger::nologStr = "off"; +#ifdef DEBUGGER_FOR_TIZEN +const std::string Logger::dlogStr = "dlog"; +#endif -void Logger::setLogging(LogType type, LogLevel level) +int Logger::setLogging(const std::string &type) { - switch (type) { - case FILE_LOG: - logger = std::make_shared(level); - break; - case DLOG_LOG: + if (type == Logger::fileStr) + { + logger = std::make_shared(); + } #ifdef DEBUGGER_FOR_TIZEN + else if (type == Logger::dlogStr) + { logger = std::make_shared(); - break; + } #endif - case NO_LOG: - default: + else + { logger = std::make_shared(); - break; + + if (type != Logger::nologStr) + return -1; } + + return 0; +} + +void Logger::levelLog(LogLevel level, const std::string fmt, ...) +{ + va_list args; + va_start(args, fmt); + logger->vlog(level, fmt, args); + va_end(args); } void Logger::log(const std::string fmt, ...) diff --git a/src/debug/netcoredbg/logger.h b/src/debug/netcoredbg/logger.h index 730e9e2..b38aa42 100644 --- a/src/debug/netcoredbg/logger.h +++ b/src/debug/netcoredbg/logger.h @@ -23,7 +23,7 @@ class LoggerImpl { public: virtual ~LoggerImpl() {}; - virtual void vlog(LogLevel level, const std::string fmt, va_list args) = 0; + virtual void vlog(LogLevel level, const std::string& fmt, va_list args) = 0; virtual void log(LogLevel level, const std::string& msg) = 0; }; @@ -48,11 +48,18 @@ class FuncLogger class Logger { private: + static const std::string fileStr; + static const std::string nologStr; +#ifdef DEBUGGER_FOR_TIZEN + static const std::string dlogStr; +#endif + static std::shared_ptr logger; public: Logger() {} - static void setLogging(LogType type, LogLevel level = LOG_INFO); + static int setLogging(const std::string &type); + static void levelLog(LogLevel level, const std::string fmt, ...); static void log(const std::string fmt, ...); static FuncLogger getFuncLogger(const std::string &func); }; @@ -65,3 +72,12 @@ class Logger #define LogFuncEntry() \ FuncLogger __funcLogger__ = Logger::getFuncLogger(std::string(__CROSS_FUNCTION__)); + + +#define __FILENAME__ (strrchr(__FILE__, DIRECTORY_SEPARATOR_STR_A) ? strrchr(__FILE__, DIRECTORY_SEPARATOR_STR_A) + 1 : __FILE__) + +#define LogWithLine(fmt, ...) \ + Logger::log("[" + std::string(__FILENAME__) + ":" + std::to_string(__LINE__) + "] " + fmt, ##__VA_ARGS__); + +#define LogLevelWithLine(level, fmt, ...) \ + Logger::levelLog(level, "[" + std::string(__FILENAME__) + ":" + std::to_string(__LINE__) + "] " + fmt, ##__VA_ARGS__); diff --git a/src/debug/netcoredbg/main.cpp b/src/debug/netcoredbg/main.cpp index 98f5368..00d052d 100644 --- a/src/debug/netcoredbg/main.cpp +++ b/src/debug/netcoredbg/main.cpp @@ -29,16 +29,8 @@ static void print_help() "--server[=port_num] Start the debugger listening for requests on the\n" " specified TCP/IP port instead of stdin/out. If port is not specified\n" " TCP %i will be used.\n" - #ifdef DEBUGGER_FOR_TIZEN - "--log[=] Enable logging to dlog. Supported\n" - " two log levels: info and debug. Info contains info, warn and error\n" - " messages, debug the same and debug messages in addition. Info level\n" - " is the default one. Overwrites other log types\n" - #endif - "--log-file[=] Enable logging to file which is created in 'current'. Supported\n" - " two log levels: info and debug. Info contains info, warn and error\n" - " messages, debug the same and debug messages in addition. Info level\n" - " is the default one. Overwrites other log types\n", + "--log[=] Enable logging. Supported logging to file and to dlog (only for Tizen)\n" + " File log by default. File is created in 'current' folder.\n", (int)DEFAULT_SERVER_PORT ); } @@ -54,9 +46,8 @@ int main(int argc, char *argv[]) } interpreterType = InterpreterMI; bool engineLogging = false; - LogType logType = NO_LOG; - LogLevel logLevel = LOG_INFO; std::string logFilePath; + std::string logType = "off"; uint16_t serverPort = 0; @@ -107,36 +98,13 @@ int main(int argc, char *argv[]) print_help(); return EXIT_SUCCESS; } -#ifdef DEBUGGER_FOR_TIZEN else if (strcmp(argv[i], "--log") == 0) { - logType = DLOG_LOG; + logType = "file"; } else if (strstr(argv[i], "--log=") == argv[i]) { - const std::string &debug = "debug"; - - logType = DLOG_LOG; - if (std::string(argv[i] + strlen("--log=")) == debug) - logLevel = LOG_DEBUG; - else - logLevel = LOG_INFO; - } -#endif - else if (strcmp(argv[i], "--log-file") == 0) - { - logType = FILE_LOG; - continue; - } - else if (strstr(argv[i], "--log-file=") == argv[i]) - { - const std::string &debug = "debug"; - - logType = FILE_LOG; - if (std::string(argv[i] + strlen("--log-file=")) == debug) - logLevel = LOG_DEBUG; - else - logLevel = LOG_INFO; + logType = argv[i] + strlen("--log="); } else if (strcmp(argv[i], "--server") == 0) { @@ -179,8 +147,12 @@ int main(int argc, char *argv[]) } } - Logger::setLogging(logType, logLevel); - Logger::log("Start logging. Level %d", logLevel); + if (Logger::setLogging(logType)) + { + fprintf(stderr, "Error: Invalid log type\n"); + return EXIT_FAILURE; + } + Logger::log("Start logging"); ManagedDebugger debugger; std::unique_ptr protocol;