EmxLog.hpp
1 //
2 // Created by xiong on 2021/7/19.
3 //
4 
5 #ifndef EMX_EmxLog_HPP
6 #define EMX_EmxLog_HPP
7 
8 #include <cstdio>
9 #include <netinet/in.h>
10 #include <sys/un.h>
11 #include "core/EmxTypeDef.hpp"
12 
13 //单条日志最大size
14 #define EMX_LOG_MAX_SIZE (1024*4)
15 
16 namespace Emx {
22  class Log {
23  public:
24 
26  enum class LevelE : uint8_t {
27  Crt = 0,
28  Err,
29  Warn,
30  Info,
31  Debug,
32  Trace,
33  };
34 
36  struct Config {
37 
39  struct Shared {
41  bool async;
42  } shared;
43 
45  struct Channel {
46  int32_t maxSizeKB;//-1:means infinity
47  int32_t maxNum;//日志缓存文件最大数量
48  char path[EMX_MAX_PATH_SIZE];
50  };
51 
52  Log();
53 
54  ~Log();
55 
65  void Print(LevelE level, const char *file, const char *func,
66  int32_t line, const char *fmt, ...);
67 
73  void Print(const char *fmt, ...);
74 
83  void Print(const char *file, const char *func,
84  int32_t line, const char *fmt, ...);
85 
91  void Save(const char *fmt, ...);
92 
97  void SetLevel(LevelE level);
98 
104 
109  void SetAsync(bool ena);
110 
115  bool GetAsync();
116 
123 
130 
136 
142 
143  private:
144  void CreateChannel();
145 
146  void DestroyChannel();
147 
148  struct Channel {
149  int32_t sock;
150  sockaddr_un address;
151  };
152  Channel m_ctrl;
153  Channel m_normal;
154  Channel m_flash;
155  Config::Shared *m_shared;
156  int m_shmId;
157  void *m_shm;
158  };
160 } // namespace Emx
162 extern Emx::Log emxLog;
163 
164 #define xlogpt(level, fmt, ...) \
165  emxLog.Print(level, __FILE__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__)
166 
168 #define emxlogc(fmt, ...) xlogpt(Emx::Log::LevelE::Crt, fmt, ##__VA_ARGS__)
170 #define emxloge(fmt, ...) xlogpt(Emx::Log::LevelE::Err, fmt, ##__VA_ARGS__)
172 #define emxlogw(fmt, ...) xlogpt(Emx::Log::LevelE::Warn, fmt, ##__VA_ARGS__)
174 #define emxlogi(fmt, ...) xlogpt(Emx::Log::LevelE::Info, fmt, ##__VA_ARGS__)
176 #define emxlogd(fmt, ...) xlogpt(Emx::Log::LevelE::Debug, fmt, ##__VA_ARGS__)
178 #define emxlogt(fmt, ...) xlogpt(Emx::Log::LevelE::Trace, fmt, ##__VA_ARGS__)
180 #define emxlogn(fmt, ...) emxLog.Print(fmt, ##__VA_ARGS__)
182 #define emxlogx(fmt, ...) emxLog.Print(__FILE__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__)
183 
184 #endif //EMX_EmxLog_HPP
185 
日志系统客户端
Definition: EmxLog.hpp:22
ErrCodeE GetConfig(Config &cfg)
获取日志系统配置
void Print(LevelE level, const char *file, const char *func, int32_t line, const char *fmt,...)
打印普通日志
ErrCodeE Resume()
恢复日志记录
void SetAsync(bool ena)
设置日志打印异步模式
void Print(const char *fmt,...)
打印不带额外格式化信息的普通日志
void Save(const char *fmt,...)
保存日志到flash
LevelE
日志等级
Definition: EmxLog.hpp:26
LevelE GetLevel()
获取全局日志等级
ErrCodeE SetConfig(Config &cfg)
配置日志系统
void SetLevel(LevelE level)
配置全局日志等级
ErrCodeE Pause()
暂停日志记录
void Print(const char *file, const char *func, int32_t line, const char *fmt,...)
打印普通日志,同步本地阻塞打印,无打印等级控制
bool GetAsync()
获取日志打印异步模式
ErrCodeE
错误码定义
Definition: EmxTypeDef.hpp:29
Definition: EmxGpio.hpp:10
LogServer存储通道的配置信息
Definition: EmxLog.hpp:45
char path[EMX_MAX_PATH_SIZE]
Definition: EmxLog.hpp:48
int32_t maxSizeKB
Definition: EmxLog.hpp:46
int32_t maxNum
Definition: EmxLog.hpp:47
各个进程包括LogServer共享的内存的数据
Definition: EmxLog.hpp:39
LevelE level
日志等级
Definition: EmxLog.hpp:40
bool async
是否是异步打印,异步打印由LogServer统一收集并打印,同步打印在各自进程的调用处打印
Definition: EmxLog.hpp:41
配置Log系统
Definition: EmxLog.hpp:36
struct Emx::Log::Config::Channel normal
struct Emx::Log::Config::Shared shared
struct Emx::Log::Config::Channel flash