teditor  1.8.0@@fee5e94
Terminal based editor written in C++
logger.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdio.h>
4 #include <stdarg.h>
5 #include <string>
6 #include "utils.h"
7 
8 namespace teditor {
9 
10 template <typename Clazz, typename ClazzArgs>
12 public:
13  SingletonHandler(const ClazzArgs& args) {
14  ASSERT(Clazz::inst == nullptr,
15  "Singleton object is already initialized!");
16  Clazz::inst = new Clazz(args);
17  }
18 
20  if(Clazz::inst != nullptr) delete Clazz::inst;
21  Clazz::inst = nullptr;
22  }
23 };
24 
25 
26 class Editor;
27 
28 class Logger {
29 public:
30  static void setLevel(int le);
31  static void log(int lev, const char* fmt, ...);
32  static void messages(Editor& ed, const char* fmt, ...);
33  static void msgBar(Editor& ed, const char* fmt, ...);
34  static int logLevel();
35 
36 private:
37  Logger(const std::string& file_);
38  ~Logger();
39  void open();
40  bool removeNewLine(std::string& buf);
41 
42  friend class SingletonHandler<Logger, std::string>;
43  static Logger* inst;
44  std::string file;
45  FILE* logfp;
46  int level;
47 };
48 
49 #define CMBAR(ed, fmt, ...) Logger::msgBar(ed, fmt, ##__VA_ARGS__)
50 #define MESSAGE(ed, fmt, ...) Logger::messages(ed, fmt, ##__VA_ARGS__)
51 
52 #define CMBAR_MSG(ed, fmt, ...) do { \
53  CMBAR(ed, fmt, ##__VA_ARGS__); \
54  MESSAGE(ed, fmt, ##__VA_ARGS__); \
55  } while(0)
56 
57 #define LOG(lev, fmt, ...) do { \
58  Logger::log(lev, "%s:%d: Lev=%d DLev=%d ", __FILE__, __LINE__, \
59  lev, Logger::logLevel()); \
60  Logger::log(lev, fmt, ##__VA_ARGS__); \
61  } while(0)
62 
63 #define FATAL(fmt, ...) LOG(0, fmt, ##__VA_ARGS__)
64 #define ERROR(fmt, ...) LOG(10, fmt, ##__VA_ARGS__)
65 #define WARN(fmt, ...) LOG(100, fmt, ##__VA_ARGS__)
66 
67 #ifdef DEBUG_BUILD
68 #define INFO(fmt, ...) LOG(1000, fmt, ##__VA_ARGS__)
69 #define DEBUG(fmt, ...) LOG(10000, fmt, ##__VA_ARGS__)
70 #define ULTRA_DEBUG(fmt, ...) LOG(100000, fmt, ##__VA_ARGS__)
71 #else // DEBUG_BUILD
72 #define INFO(fmt, ...)
73 #define DEBUG(fmt, ...)
74 #define ULTRA_DEBUG(fmt, ...)
75 #endif // DEBUG_BUILD
76 
77 } // end namespace teditor
teditor::Editor::getCmBar
CmdMsgBar & getCmBar()
Definition: editor.h:34
teditor::Logger::messages
static void messages(Editor &ed, const char *fmt,...)
Definition: logger.cpp:66
teditor::Logger::msgBar
static void msgBar(Editor &ed, const char *fmt,...)
Definition: logger.cpp:55
logger.h
teditor::SingletonHandler
Definition: logger.h:11
teditor::Logger::log
static void log(int lev, const char *fmt,...)
Definition: logger.cpp:44
teditor::Logger::logLevel
static int logLevel()
Definition: logger.cpp:15
teditor::SingletonHandler::~SingletonHandler
~SingletonHandler()
Definition: logger.h:19
utils.h
teditor::CmdMsgBar::clear
void clear() override
Definition: cmd_msg_bar.cpp:134
ASSERT
#define ASSERT(check, fmt,...)
Macro to assert with runtime_error exception if the check fails.
Definition: utils.h:35
teditor::SingletonHandler::SingletonHandler
SingletonHandler(const ClazzArgs &args)
Definition: logger.h:13
teditor::Editor
Definition: editor.h:23
std
Definition: number.h:8
teditor::Logger
Definition: logger.h:28
teditor::Logger::setLevel
static void setLevel(int le)
Definition: logger.cpp:11
teditor::Buffer::insert
virtual void insert(char c)
Definition: buffer.cpp:25
editor.h
teditor::format
std::string format(const char *fmt, va_list &vl)
Definition: utils.cpp:22
teditor::Editor::getMessagesBuff
Buffer & getMessagesBuff()
Definition: editor.cpp:329
teditor
Definition: any.hpp:10