Logger: add format string input
[sdk/tools/netcoredbg.git] / src / debug / netcoredbg / logger.h
1 #pragma once
2
3 #include <string>
4 #include <memory>
5 #include <cstdarg>
6
7
8 enum LogType {
9     NO_LOG = 0,
10     FILE_LOG,
11     DLOG_LOG,
12 };
13
14 enum LogLevel {
15     LOG_DEBUG = 0,
16     LOG_INFO,
17     LOG_WARN,
18     LOG_ERROR,
19 };
20
21
22 class LoggerImpl
23 {
24     public:
25         virtual ~LoggerImpl() {};
26         virtual void vlog(LogLevel level, const std::string fmt, va_list args) = 0;
27         virtual void log(LogLevel level, const std::string& msg) = 0;
28 };
29
30 class FuncLogger
31 {
32     private:
33         const std::shared_ptr<LoggerImpl> &logger;
34         const std::string func;
35
36     public:
37         FuncLogger(const std::shared_ptr<LoggerImpl> &logger, const std::string &func) : logger(logger), func(func)
38         {
39             logger->log(LOG_DEBUG, "> " + func);
40         }
41
42         ~FuncLogger()
43         {
44             logger->log(LOG_DEBUG, "< " + func);
45         }
46 };
47
48 class Logger
49 {
50     private:
51         static std::shared_ptr<LoggerImpl> logger;
52
53     public:
54         Logger() {}
55         static void setLogging(LogType type, LogLevel level = LOG_INFO);
56         static void log(const std::string fmt, ...);
57         static FuncLogger getFuncLogger(const std::string &func);
58 };
59
60 #ifdef WIN32
61 #define __CROSS_FUNCTION__ __FUNCSIG__
62 #else // WIN32
63 #define __CROSS_FUNCTION__ __PRETTY_FUNCTION__
64 #endif // WIN32
65
66 #define LogFuncEntry()  \
67     FuncLogger __funcLogger__ = Logger::getFuncLogger(std::string(__CROSS_FUNCTION__));