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)
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);
}
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;
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()
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);
}
}
std::shared_ptr<LoggerImpl> Logger::logger = std::make_shared<NoLogger>();
+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<FileLogger>(level);
- break;
- case DLOG_LOG:
+ if (type == Logger::fileStr)
+ {
+ logger = std::make_shared<FileLogger>();
+ }
#ifdef DEBUGGER_FOR_TIZEN
+ else if (type == Logger::dlogStr)
+ {
logger = std::make_shared<DlogLogger>();
- break;
+ }
#endif
- case NO_LOG:
- default:
+ else
+ {
logger = std::make_shared<NoLogger>();
- 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, ...)
{
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;
};
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<LoggerImpl> 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);
};
#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__);
"--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[=<debug>] 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[=<debug>] 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[=<type>] 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
);
}
} interpreterType = InterpreterMI;
bool engineLogging = false;
- LogType logType = NO_LOG;
- LogLevel logLevel = LOG_INFO;
std::string logFilePath;
+ std::string logType = "off";
uint16_t serverPort = 0;
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)
{
}
}
- 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> protocol;