fastdo  0.6.16
eienlog.hpp
浏览该文件的文档.
1 #ifndef __EIENLOG_HPP__
2 #define __EIENLOG_HPP__
3 
4 #include "eiennet.hpp"
5 
7 namespace eienlog
8 {
9 #ifdef EIENLOG_DLL_USE
10  #if defined(_MSC_VER) || defined(WIN32)
11  #pragma warning( disable: 4251 )
12  #ifdef EIENLOG_DLL_EXPORTS
13  #define EIENLOG_DLL __declspec(dllexport)
14  #else
15  #define EIENLOG_DLL __declspec(dllimport)
16  #endif
17 
18  #define EIENLOG_API __stdcall
19  #else
20  #define EIENLOG_DLL
21  #define EIENLOG_API
22  #endif
23 #else
24  #define EIENLOG_DLL
25  #define EIENLOG_API
26 #endif
27 
28 #define EIENLOG_FUNC_DECL(ret) EIENLOG_DLL ret EIENLOG_API
29 #define EIENLOG_FUNC_IMPL(ret) ret EIENLOG_API
30 
31 // 日志UDP协议相关结构体
32 
33 #define LOG_CHUNK_SIZE 80
34 
37 {
42 };
43 
45 #define LOG_FG_COLOR( r, g, b ) (winux::uint16)( (r) | ( (g) << 5 ) | ( (b) << 10 ) )
46 
48 #define LOG_BG_COLOR( r, g, b ) (winux::uint16)( (r) | ( (g) << 4 ) | ( (b) << 8 ) )
49 
52 {
53  lfcBlack = LOG_FG_COLOR( 0, 0, 0 ),
54 
55  lfcMaroon = LOG_FG_COLOR( 16, 0, 0 ),
56  lfcAtrovirens = LOG_FG_COLOR( 0, 16, 0 ),
58  lfcNavy = LOG_FG_COLOR( 0, 0, 16 ),
62 
63  lfcSilver = LOG_FG_COLOR( 24, 24, 24 ),
64 
65  lfcRed = LOG_FG_COLOR( 31, 0, 0 ),
66  lfcGreen = LOG_FG_COLOR( 0, 31, 0 ),
68  lfcBlue = LOG_FG_COLOR( 0, 0, 31 ),
72 };
73 
76 {
77  lbcBlack = LOG_BG_COLOR( 0, 0, 0 ),
78 
79  lbcMaroon = LOG_BG_COLOR( 8, 0, 0 ),
82  lbcNavy = LOG_BG_COLOR( 0, 0, 8 ),
86 
87  lbcSilver = LOG_BG_COLOR( 12, 12, 12 ),
88 
89  lbcRed = LOG_BG_COLOR( 15, 0, 0 ),
90  lbcGreen = LOG_BG_COLOR( 0, 15, 0 ),
92  lbcBlue = LOG_BG_COLOR( 0, 0, 15 ),
96 };
97 
99 union LogFlag
100 {
101  struct
102  {
109  };
111 
112  LogFlag( winux::uint32 value = 0 ) : value(value)
113  {
114  }
115 };
116 
119 {
127 };
128 
131 {
132  char logSpace[1];
133 };
134 
136 struct LogRecord
137 {
139  time_t utcTime;
141 };
142 
145 {
146 public:
152  LogWriter( winux::String const & addr, winux::ushort port, winux::uint16 chunkSize = LOG_CHUNK_SIZE );
153 
155  size_t logEx( winux::Buffer const & data, bool useFgColor, winux::uint16 fgColor, bool useBgColor, winux::uint16 bgColor, winux::uint8 logEncoding, bool isBinary );
156 
158  size_t log( winux::String const & str, bool useFgColor, winux::uint16 fgColor, bool useBgColor, winux::uint16 bgColor, winux::uint8 logEncoding );
159 
161  size_t logBin( winux::Buffer const & data, bool useFgColor, winux::uint16 fgColor, bool useBgColor, winux::uint16 bgColor )
162  {
163  return this->logEx( data, useFgColor, fgColor, useBgColor, bgColor, 0, true );
164  }
165 
171  size_t log( winux::String const & str, winux::uint8 logEncoding = leUtf8 )
172  {
173  return this->log( str, false, 0, false, 0, logEncoding );
174  }
175 
180  size_t logBin( winux::Buffer const & data )
181  {
182  return this->logBin( data, false, 0, false, 0 );
183  }
184 
186  size_t logColor( winux::String const & str, winux::Mixed const & fgColor = winux::mxNull, winux::Mixed const & bgColor = winux::mxNull, winux::uint8 logEncoding = leUtf8 )
187  {
188  return this->log( str, !fgColor.isNull(), fgColor, !bgColor.isNull(), bgColor, logEncoding );
189  }
190 
192  size_t logBinColor( winux::Buffer const & data, winux::Mixed const & fgColor = winux::mxNull, winux::Mixed const & bgColor = winux::mxNull )
193  {
194  return this->logBin( data, !fgColor.isNull(), fgColor, !bgColor.isNull(), bgColor );
195  }
196 
197  int errNo() const { return _errno; }
198 
199 private:
202  winux::uint16 const _chunkSize;
203  int _errno;
204 };
205 
208 {
209 public:
211  {
212  std::vector< winux::Packet<LogChunk> > chunks;
213  time_t lastUpdate;
214  };
215 
221  LogReader( winux::String const & addr, winux::ushort port, winux::uint16 chunkSize = LOG_CHUNK_SIZE );
222 
228  bool readChunk( winux::Packet<LogChunk> * chunk, eiennet::ip::EndPoint * ep );
229 
236  bool readRecord( LogRecord * record, time_t waitTimeout = 3000, time_t updateTimeout = 3000 );
237 
238  int errNo() const { return _errno; }
239 
240 private:
243  std::map< winux::uint32, LogChunksData > _chunksMap;
244  winux::uint16 const _chunkSize;
245  int _errno;
246 };
247 
248 
250 EIENLOG_FUNC_DECL(bool) EnableLog( winux::String const & addr = $T("127.0.0.1"), winux::ushort port = 22345, winux::uint16 chunkSize = LOG_CHUNK_SIZE );
251 
254 
256 EIENLOG_FUNC_DECL(void) WriteLog( winux::String const & str );
257 
259 EIENLOG_FUNC_DECL(void) WriteLogBin( void const * data, size_t size );
260 
265 
269 EIENLOG_FUNC_DECL(size_t) LogBin( winux::Buffer const & data );
270 
275 
280 
281 template < typename... _ArgType >
282 inline static void LogColorOutput( winux::Mixed const & fgColor, winux::Mixed const & bgColor, _ArgType&& ... arg )
283 {
284  std::basic_ostringstream<winux::tchar> sout;
285  winux::OutputV( sout, std::forward<_ArgType>(arg)... );
286  LogColor( sout.str(), fgColor, bgColor );
287 }
288 
289 //#define __LOG__
290 #ifdef __LOG__
291 #define LOG(s) eienlog::WriteLog(s)
292 #define LOG_BIN(d,s) eienlog::WriteLogBin((d),(s))
293 #else
294 #define LOG(s)
295 #define LOG_BIN(d,s)
296 #endif
297 
298 
299 } // namespace eienlog
300 
301 
302 #endif // __EIENLOG_HPP__
winux::uint32 value
Definition: eienlog.hpp:110
日志读取器
Definition: eienlog.hpp:207
winux::uint64 utcTime
UTC时间戳(ms)
Definition: eienlog.hpp:126
winux::uint32 id
记录ID
Definition: eienlog.hpp:125
winux::uint32 flag
日志样式FLAG
Definition: eienlog.hpp:140
日志写入器
Definition: eienlog.hpp:144
Mixed const mxNull
Mixed(MT_NULL)常量对象
LogFlag(winux::uint32 value=0)
Definition: eienlog.hpp:112
static void LogColorOutput(winux::Mixed const &fgColor, winux::Mixed const &bgColor, _ArgType &&...arg)
Definition: eienlog.hpp:282
winux::uint32 flag
控制日志样式或颜色等信息
Definition: eienlog.hpp:124
LogFgColor
日志颜色
Definition: eienlog.hpp:51
#define LOG_BG_COLOR(r, g, b)
背景色: R4G4B4
Definition: eienlog.hpp:48
void DisableLog()
禁用日志
数据包,用来表示一些POD结构体用于网络通信。通常这类结构体实际大小 > sizeof(这类结构体) ...
Definition: utilities.hpp:1087
日志记录
Definition: eienlog.hpp:136
XString< tchar > String
Definition: utilities.hpp:261
日志样式旗标
Definition: eienlog.hpp:99
winux::uint16 bgColor
背景色R4G4B4
Definition: eienlog.hpp:105
size_t LogBin(winux::Buffer const &data)
写二进制日志
日志功能,通过UDP协议高效的收发日志
Definition: eienlog.hpp:7
#define LOG_CHUNK_SIZE
Definition: eienlog.hpp:33
winux::uint16 realLen
数据包日志数据占用长度
Definition: eienlog.hpp:121
winux::uint16 index
分块编号
Definition: eienlog.hpp:122
size_t log(winux::String const &str, winux::uint8 logEncoding=leUtf8)
发送字符串日志
Definition: eienlog.hpp:171
size_t logBin(winux::Buffer const &data, bool useFgColor, winux::uint16 fgColor, bool useBgColor, winux::uint16 bgColor)
发送二进制日志
Definition: eienlog.hpp:161
int errNo() const
Definition: eienlog.hpp:238
winux::uint16 bgColorUse
背景色是否使用
Definition: eienlog.hpp:106
size_t logColor(winux::String const &str, winux::Mixed const &fgColor=winux::mxNull, winux::Mixed const &bgColor=winux::mxNull, winux::uint8 logEncoding=leUtf8)
发送字符串日志,可指定颜色
Definition: eienlog.hpp:186
winux::uint16 fgColor
颜色R5G5B5
Definition: eienlog.hpp:103
日志分块头部
Definition: eienlog.hpp:118
#define EIENLOG_DLL
Definition: eienlog.hpp:24
缓冲区,表示内存中一块二进制数据(利用malloc/realloc进行内存分配)
Definition: utilities.hpp:906
bool EnableLog(winux::String const &addr=$"127.0.0.1", winux::ushort port=22345, winux::uint16 chunkSize=80)
启用日志
#define LOG_FG_COLOR(r, g, b)
前景色: R5G5B5
Definition: eienlog.hpp:45
winux::uint16 chunkSize
数据包固定大小0~65535
Definition: eienlog.hpp:120
size_t logBin(winux::Buffer const &data)
发送二进制日志
Definition: eienlog.hpp:180
void WriteLog(winux::String const &str)
写字符串日志(带进程ID)
winux::uint16 total
总块数
Definition: eienlog.hpp:123
void WriteLogBin(void const *data, size_t size)
写二进制日志
winux::Buffer data
日志数据
Definition: eienlog.hpp:138
#define EIENLOG_FUNC_DECL(ret)
Definition: eienlog.hpp:28
winux::uint16 logEncoding
日志编码
Definition: eienlog.hpp:107
日志分块
Definition: eienlog.hpp:130
std::vector< winux::Packet< LogChunk > > chunks
Definition: eienlog.hpp:212
size_t LogBinColor(winux::Buffer const &data, winux::Mixed const &fgColor=winux::mxNull, winux::Mixed const &bgColor=winux::mxNull)
写二进制日志。可设置颜色参数
unsigned short ushort
Definition: utilities.hpp:218
混合体,能表示多种类型的值
Definition: utilities.hpp:1440
unsigned short uint16
Definition: utilities.hpp:218
size_t logBinColor(winux::Buffer const &data, winux::Mixed const &fgColor=winux::mxNull, winux::Mixed const &bgColor=winux::mxNull)
发送二进制日志,可指定颜色
Definition: eienlog.hpp:192
size_t Log(winux::String const &str, winux::uint8 logEncoding=leUtf8)
写字符串日志
winux::uint16 fgColorUse
颜色是否使用
Definition: eienlog.hpp:104
LogBgColor
日志背景颜色
Definition: eienlog.hpp:75
unsigned __int64 uint64
Definition: utilities.hpp:230
unsigned int uint32
Definition: utilities.hpp:215
unsigned char uint8
Definition: utilities.hpp:220
static void OutputV(std::basic_ostream< _ChTy > &out)
Definition: console.hpp:185
size_t LogColor(winux::String const &str, winux::Mixed const &fgColor=winux::mxNull, winux::Mixed const &bgColor=winux::mxNull, winux::uint8 logEncoding=leUtf8)
写字符串日志。可设置颜色参数
time_t utcTime
UTC时间戳(ms)
Definition: eienlog.hpp:139
winux::uint16 binary
是否二进制数据
Definition: eienlog.hpp:108
int errNo() const
Definition: eienlog.hpp:197
LogEncoding
日志编码。Local表示本地多字节编码,不同国家可能不同
Definition: eienlog.hpp:36