soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
slog.cpp
1#include <souistd.h>
2#include <helper/slog.h>
3
4#include <interface/slog-i.h>
5
6#ifndef E_RANGE
7#define E_RANGE 9944
8#endif
9
10SNSBEGIN
11
12void Log::DefCallback(const char *tag, const char *pLogStr, int level, const char *file, int line, const char *fun, void *retAddr)
13{
14 ILogMgr *pLogMgr = GETLOGMGR();
15 BOOL bLog = false;
16 if (pLogMgr && pLogMgr->prePushLog(level))
17 {
18 bLog = pLogMgr->pushLog(level, tag, pLogStr, file, line, fun, retAddr);
19 }
20 if (!bLog || Log::s_enableEcho)
21 {
22 SYSTEMTIME wtm;
23 GetLocalTime(&wtm);
24 const int kMaxLog = Log::MAX_LOGLEN + 100;
25 char *logbuf2 = (char *)malloc(kMaxLog + 1);
26 tid_t tid = GetCurrentThreadId();
27#ifdef _WIN32
28 int nLen = _snprintf(logbuf2, kMaxLog, "tid=%u,%04d-%02d-%02d %02d:%02d:%02d %03dms %s,%s %s %s:%d\n", tid, wtm.wYear, wtm.wMonth, wtm.wDay, wtm.wHour, wtm.wMinute, wtm.wSecond, wtm.wMilliseconds, tag, pLogStr, fun, file, line);
29#else
30 int nLen = _snprintf(logbuf2, kMaxLog, "tid=%ld,%04d-%02d-%02d %02d:%02d:%02d %03dms %s,%s %s %s:%d\n", tid, wtm.wYear, wtm.wMonth, wtm.wDay, wtm.wHour, wtm.wMinute, wtm.wSecond, wtm.wMilliseconds, tag, pLogStr, fun, file, line);
31#endif //_WIN32
32 if (nLen > 0)
33 {
34 logbuf2[nLen] = 0;
35 OutputDebugStringA(logbuf2);
36 }
37 free(logbuf2);
38 }
39}
40
41//////////////////////////////////////////////////////////////////////////
42int Log::s_logLevel = LOG_LEVEL_INFO;
43bool Log::s_enableEcho = true;
44LogCallback Log::gs_logCallback = Log::DefCallback;
45
46Log::Log(const char *tag, int level, const char *filename, const char *funcname, int lineIndex, void *pAddr)
47 : m_level(level)
48 , m_pAddr(pAddr)
49 , m_file(filename)
50 , m_func(funcname)
51 , m_line(lineIndex)
52 , m_stream(m_logBuf, MAX_LOGLEN)
53{
54 assert(strlen(tag) < MAX_TAGLEN);
55 strcpy(m_tag, tag);
56}
57
59{
60 if (m_level < s_logLevel)
61 return;
62
63 if (gs_logCallback)
64 {
65 gs_logCallback(m_tag, m_logBuf, m_level, m_file, m_line, m_func, m_pAddr);
66 }
67 else if (s_enableEcho)
68 {
69 SYSTEMTIME wtm;
70 GetLocalTime(&wtm);
71 const int kMaxLog = Log::MAX_LOGLEN + 100;
72 char *logbuf2 = (char *)malloc(kMaxLog + 1);
73 int nLen = _snprintf(logbuf2, kMaxLog, "%s, %04d-%02d-%02d %02d:%02d:%02d %03dms %s %s %s:%d\n", m_tag, wtm.wYear, wtm.wMonth, wtm.wDay, wtm.wHour, wtm.wMinute, wtm.wSecond, wtm.wMilliseconds, m_logBuf, m_func, m_file, m_line);
74 if (nLen > 0)
75 {
76 logbuf2[nLen] = 0;
77 OutputDebugStringA(logbuf2);
78 }
79 free(logbuf2);
80 }
81}
82
84{
85 return m_stream;
86}
87
88void Log::setLogLevel(int nLevel)
89{
90 s_logLevel = nLevel;
91}
92
93// setLogCallback is not thread safe.
94// call this at the beginning of program please.
95void Log::setLogCallback(LogCallback logCallback)
96{
97 gs_logCallback = logCallback;
98}
99
100void Log::setLogEcho(bool bEnable)
101{
102 s_enableEcho = bEnable;
103}
104
105//////////////////////////////////////////////////////////////////////////
106
108{
109}
110
112{
113}
114
115SLogStream &SLogStream::writeWString(const wchar_t *t, int nLen)
116{
117 DWORD dwLen = WideCharToMultiByte(CP_ACP, 0, t, nLen, NULL, 0, NULL, NULL);
118 if (dwLen < Log::MAX_LOGLEN)
119 {
120 char buf[Log::MAX_LOGLEN];
121 dwLen = WideCharToMultiByte(CP_ACP, 0, t, nLen, buf, dwLen, NULL, NULL);
122 if (dwLen > 0)
123 {
124 buf[dwLen] = 0;
125 writeFormat("%s", buf);
126 }
127 }
128 return *this;
129}
130
131SLogStream &SLogStream::writeString(const char *t)
132{
133 writeFormat("%s", t);
134 return *this;
135}
136
137SLogStream &SLogStream::writeBinary(const SLogBinary &t)
138{
139 writeFormat("%s", "\r\n\t[");
140 for (int i = 0; i < t._len; i++)
141 {
142 if (i % 16 == 0)
143 {
144 writeFormat("%s", "\r\n\t");
145 *this << (void *)(t._buf + i);
146 writeFormat("%s", ": ");
147 }
148 writeFormat("%02x ", (unsigned char)t._buf[i]);
149 }
150 writeFormat("%s", "\r\n\t]\r\n\t");
151 return *this;
152}
153
154SLogStream::SLogStream(char *buf, int len)
155{
156 _begin = buf;
157 _end = buf + len;
158 _cur = _begin;
159 buf[0] = 0;
160}
161
162SLogStream &SLogStream::writeLongLong(long long t)
163{
164#if defined(WIN32) || defined(_WIN64)
165 writeFormat("%I64d", t);
166#else
167 writeFormat("%lld", t);
168#endif
169 return *this;
170}
171
172SLogStream &SLogStream::writeULongLong(unsigned long long t)
173{
174#if defined(WIN32) || defined(_WIN64)
175 writeFormat("%I64u", t);
176#else
177 writeFormat("%llu", t);
178#endif
179 return *this;
180}
181
182SLogStream &SLogStream::writePointer(const void *t)
183{
184#if defined(WIN32) || defined(_WIN64)
185 sizeof(t) == 8 ? writeFormat("0x%016I64x", (unsigned long long)t) : writeFormat("0x%08I64x", (unsigned long long)t);
186#else
187 sizeof(t) == 8 ? writeFormat("0x%016llx", (unsigned long long)t) : writeFormat("0x%08llx", (unsigned long long)t);
188#endif
189 return *this;
190}
191
192SLogStream &SLogStream::writeFormat(const wchar_t *fmt2, ...)
193{
194#ifndef _WIN32
195 wchar_t *fmt = wcsdup(fmt2);
196 wchar_t *p = wcsstr(fmt, L"%s");
197 while (p)
198 {
199 p[1] = L'S';
200 p = wcsstr(p + 2, L"%s");
201 }
202#else
203 const wchar_t *fmt = fmt2;
204#endif //_WIN32
205
206 wchar_t logbuf[SNS::Log::MAX_LOGLEN] = { 0 };
207 va_list args;
208 va_start(args, fmt2);
209 int ret = vswprintf(logbuf, SNS::Log::MAX_LOGLEN, fmt, args);
210 va_end(args);
211#ifndef _WIN32
212 free(fmt);
213#endif //_WIN32
214 return writeWString(logbuf, ret);
215}
216
218{
219 va_list args;
220 va_start(args, fmt);
221 if (_cur < _end)
222 {
223 int len = 0;
224 int count = (int)(_end - _cur) - 1;
225 len = vsnprintf(_cur, count, fmt, args);
226 if (len == count || (len == -1 && errno == E_RANGE))
227 {
228 len = count;
229 *(_end - 1) = '\0';
230 }
231 else if (len < 0)
232 {
233 *_cur = '\0';
234 len = 0;
235 }
236 _cur += len;
237 }
238 va_end(args);
239
240 return *this;
241}
242
244{
245 return writeBinary(binary);
246}
247
249{
250 return writeFormat("%.4lf", t);
251}
252
254{
255 return writeFormat("%.4f", t);
256}
257
258SLogStream &SLogStream::operator<<(unsigned long long t)
259{
260 return writeULongLong(t);
261}
262
264{
265 return writeLongLong(t);
266}
267
268SLogStream &SLogStream::operator<<(unsigned long t)
269{
270 return writeULongLong(t);
271}
272
274{
275 return writeLongLong(t);
276}
277
278SLogStream &SLogStream::operator<<(unsigned int t)
279{
280 return writeFormat("%u", t);
281}
282
284{
285 return writeFormat("%d", t);
286}
287
288SLogStream &SLogStream::operator<<(unsigned short t)
289{
290 return writeFormat("%u", (unsigned int)t);
291}
292
294{
295 return writeFormat("%d", (int)t);
296}
297
298SLogStream &SLogStream::operator<<(unsigned char t)
299{
300 return writeFormat("%u", (unsigned int)t);
301}
302
304{
305 return writeFormat("%c", t);
306}
307
309{
310 return writeWString(&t, 1);
311}
312
314{
315 return (t ? writeFormat("%s", "true") : writeFormat("%s", "false"));
316}
317
318SLogStream &SLogStream::operator<<(const wchar_t *t)
319{
320 return writeWString(t);
321}
322
324{
325 return writeString(t);
326}
327
329{
330 return writePointer(t);
331}
332
333SNSEND
~Log()
析构函数
Definition slog.cpp:58
Log(const char *tag, int level, const char *filename, const char *funcname, int lineIndex, void *pAddr)
构造函数
Definition slog.cpp:46
static void setLogEcho(bool bEnable)
设置日志回显功能
Definition slog.cpp:100
SLogStream & stream()
获取日志流对象
Definition slog.cpp:83
@ MAX_TAGLEN
最大标签长度
Definition slog.h:155
@ MAX_LOGLEN
最大日志长度
Definition slog.h:156
static void setLogCallback(LogCallback logCallback)
设置日志回调函数
Definition slog.cpp:95
static void setLogLevel(int nLevel)
设置日志级别
Definition slog.cpp:88
二进制日志数据类
Definition slog.h:27
int _len
二进制数据长度
Definition slog.h:41
const char * _buf
二进制数据缓冲区
Definition slog.h:40
日志流类,用于格式化日志输出
Definition slog.h:48
SLogStream(char *buf, int len)
构造函数
Definition slog.cpp:154
SLogStream & operator<<(const void *t)
重载运算符<<,用于输出各种类型的数据
Definition slog.cpp:328
SLogStream & writeFormat(const char *format,...)
格式化输出日志
Definition slog.cpp:217