JunkBox_Lib++ (for Windows) 1.10.1
Loading...
Searching...
No Matches
LogRingBuffer.cpp
Go to the documentation of this file.
1
7#include "LogRingBuffer.h"
8
9
10using namespace jbxl;
11
12
14{
15 //DEBUG_INFO("INFO: DESTRUCTOR: CLogRingBuffer");
16
17 if (pBuf!=NULL) del_Buffer_dim(&pBuf);
18
19 //DEBUG_INFO("INFO: DESTRUCTOR: CLogRingBuffer");
20}
21
22
24{
25 bool err_flag = false;
26
28 maxLineX = maxLineY = 0;
29 rPos = wPos = 0;
30 indentSize = 13; // 5 + 8 "1234 SERVER: "
31
32 pBuf = (Buffer*)malloc(sizeof(Buffer)*size);
33 kindData = (int*)malloc(sizeof(int)*size);
34 inputSrc = (int*)malloc(sizeof(int)*size);
35
36 if (pBuf!=NULL && inputSrc!=NULL && kindData!=NULL) {
37 memset(pBuf, 0, sizeof(Buffer)*size);
38 memset(kindData, 0, sizeof(int)*size);
39 memset(inputSrc, 0, sizeof(int)*size);
40 maxBufSize = size;
41
42 for (int i=0; i<size; i++) {
43 pBuf[i] = make_Buffer(LBUF);
44 if (pBuf[i].buf==NULL) {
45 err_flag = true;
46 break;
47 }
48 pBuf[i].state = size;
49 }
50 }
51 else {
52 err_flag = true;
53 }
54
55 if (err_flag) {
56 if (pBuf!=NULL) del_Buffer_dim(&pBuf);
57 if (kindData!=NULL) free(kindData);
58 if (inputSrc!=NULL) free(inputSrc);
59 kindData = inputSrc = NULL;
60 return 1;
61 }
62 return 0;
63}
64
65
67{
68 for (int i=0; i<maxBufSize; i++) {
69 pBuf[i].buf[0] = '\0';
70 pBuf[i].vldsz = 0;
71 }
72
73 tlDataSize = 0;
74 maxLineX = maxLineY = 0;
75 rPos = wPos = 0;
76
77 return;
78}
79
80
90void CLogRingBuffer::putRingBuffer(Buffer buf, int input, int kind)
91{
92 int i, nxt = 0;
93 bool recalcX = false;
94 char num[10];
95
96 if (buf.buf==NULL || buf.vldsz<=0) return;
97 Buffer* dim = decompline_Buffer_dim(buf, ON);
98 if (dim==NULL || dim->state<=0) return;
99 tlDataSize += buf.vldsz;
100
101#ifdef DEBUG
102 int total = 0;
103 for (i=0; i<dim->state; i++) {
104 total += dim[i].vldsz;
105 }
106 if (buf.vldsz!=total) {
107 DEBUG_ERR("ERROR: CLogRingBuffer::putRingBuffer(): mismatch total data size!! %d %d", buf.vldsz, total);
108 }
109#endif
110
111 // 前の行の続き
112 int pos = wPos - 1;
113 if (pos<0) pos += maxBufSize;
114 if (kindData[pos]==LOG_RB_TEXT_HALF_DATA && isText_Buffer(dim[0])
115 && inputSrc[pos]==input && input!=LOG_RB_INFO && kind==LOG_RB_UNKNOWN_DATA) {
116 cat_Buffer(&dim[0], &pBuf[pos]);
119 nxt = 1; // 処理済
120 }
121
122 // 新しい行.
123 pos = wPos - 1;
124 for (i=nxt; i<dim->state; i++) {
125 pos++; // == wPos
126 if (pos>=maxBufSize) pos -= maxBufSize;
127 if (!recalcX && maxLineY==maxBufSize && maxLineX==getLengthX(pos)) recalcX = true;
128
129 snprintf(num, 10, "%05d| ", pos);
130 copy_s2Buffer(num, &pBuf[pos]);
131 if (input==LOG_RB_ERROR) cat_s2Buffer("ERROR: ", &pBuf[pos]);
132 else if (input==LOG_RB_WARNING) cat_s2Buffer("WARN : ", &pBuf[pos]);
133 else if (input==LOG_RB_DEBUG) cat_s2Buffer("DEBUG: ", &pBuf[pos]);
134 else if (input==LOG_RB_INFO) cat_s2Buffer("INFO : ", &pBuf[pos]);
135 //else cat_s2Buffer(" ", &pBuf[pos]);
136
137 cat_Buffer(&dim[i], &pBuf[pos]);
139 else kindData[pos] = kind;
140 inputSrc[pos] = input;
142 }
143
144 // 最後のデータの再確認
145 if (nxt<dim->state && kind==LOG_RB_UNKNOWN_DATA) {
146 if (isText_Buffer(dim[dim->state-1])) {
147 unsigned char end = dim[dim->state-1].buf[dim[dim->state-1].vldsz-1];
148 if (end!=CHAR_CR && end!=CHAR_LF) kindData[pos] = LOG_RB_TEXT_HALF_DATA;
149 }
150 else {
152 }
153 }
154
155 pos++;
156 if (pos>=maxBufSize) pos -= maxBufSize;
157 wPos = pos;
158
159 // 次のn行を目印行にする.
160 Buffer spbuf = rept_Buffer(' ', 80);
161 cat_s2Buffer("\n", &spbuf);
162 for (i=0; i<2; i++) {
163 copy_Buffer(&spbuf, &pBuf[pos]);
164 inputSrc[pos] = LOG_RB_INFO;
166 pos++;
167 if (pos>=maxBufSize) pos -= maxBufSize;
168 }
169 free_Buffer(&spbuf);
170
171 // コンテキストのサイズを計算
172 if (maxLineY<maxBufSize) {
174 else maxLineY = wPos + 1;
175 }
176 //DEBUG_WARN("WARNING: MaxY = %d", maxLineY);
177
178 if (recalcX) {
179 maxLineX = 0;
180 for (i=0; i<maxBufSize; i++) {
182 }
183 }
184
185 del_Buffer_dim(&dim);
186 return;
187}
188
189
197{
198 while (n<0) n += maxBufSize;
199 if (n>=maxBufSize) n = n%maxBufSize;
200
201 Buffer dup = pBuf[n];
202 dup.buf = dup.buf + indentSize;
203 dup.vldsz = dup.vldsz - indentSize;
204
205 Buffer buf = dump_Buffer(dup);
206 wPos = n;
208
209 free_Buffer(&buf);
210}
211
212
218void CLogRingBuffer::putRingFormat(int input, char* fmt, ...)
219{
220 char* nfmt;
221 int len;
222 Buffer buf;
223
224 va_list args;
225 va_start(args, fmt);
226
227 len = (int)strlen(fmt);
228 nfmt = (char*)malloc(len+1);
229 if (nfmt==NULL) return;
230 strncpy(nfmt, fmt, len);
231 nfmt[len] = '\0';
232
233 buf = make_Buffer(LBUF);
234 vsnprintf((char*)buf.buf, LBUF-1, nfmt, args);
236 free(nfmt);
237
238 va_end(args);
239
240 putRingBuffer(buf, input, LOG_RB_TEXT_DATA);
241 free_Buffer(&buf);
242
243 return;
244}
245
246
253void CLogRingBuffer::putRingFormat(int input, char* fmt, va_list args)
254{
255 char* nfmt;
256 int len;
257 Buffer buf;
258
259 len = (int)strlen(fmt);
260 nfmt = (char*)malloc(len+1);
261 if (nfmt==NULL) return;
262 strncpy(nfmt, fmt, len);
263 nfmt[len] = '\0';
264
265 buf = make_Buffer(LBUF);
266 vsnprintf((char*)buf.buf, LBUF-1, nfmt, args);
268 free(nfmt);
269
270 putRingBuffer(buf, input, LOG_RB_TEXT_DATA);
271 free_Buffer(&buf);
272
273 return;
274}
275
276
285{
286 Buffer buf = make_Buffer(LBUF);
287
288 copy_Buffer(&pBuf[rPos], &buf);
289 buf.state = inputSrc[rPos];
290
291 rPos++;
293
294 return buf;
295}
296
297
306{
307 Buffer buf = init_Buffer();
308 buf.state = -1;
309
310 if (pos<0) return buf;
311 if (pos>=maxBufSize) pos = pos%maxBufSize;
312
313 buf = make_Buffer(LBUF);
314 copy_Buffer(&pBuf[pos], &buf);
315 buf.state = inputSrc[pos];
316
317 return buf;
318}
319
Log用 Ring Buffer ツール ヘッダ
#define LOG_RB_BINARY_DATA
#define LOG_RB_WARNING
#define LOG_RB_TEXT_DATA
#define LOG_RB_BINHEX_DATA
#define LOG_RB_DEBUG
#define LOG_RB_UNKNOWN_DATA
#define LOG_RB_INFO
#define LOG_RB_ERROR
#define LOG_RB_TEXT_HALF_DATA
Buffer make_Buffer(int sz)
Buffer型変数のバッファ部をつくり出す.
Definition buffer.cpp:71
void free_Buffer(Buffer *buf)
Buffer型変数のバッファ部を解放する
Definition buffer.cpp:128
Buffer init_Buffer()
初期化したBuffer型変数を返す.
Definition buffer.cpp:47
Buffer rept_Buffer(unsigned char cc, int n)
文字 ccを n回繰り返したBuffer型データを返す.
Definition buffer.cpp:234
int isText_Buffer(Buffer buf)
Buffer型変数 bufのバッファ部がテキストかどうか検査する.
Definition buffer.cpp:1373
int recalc_strlen_Buffer(Buffer *buf)
Buffer型変数のデータ部を文字列と見なして,その長さを返す.
Definition buffer.cpp:1310
int cat_Buffer(Buffer *src, Buffer *dst)
Buffer変数 srcから dstへバッファを catする.
Definition buffer.cpp:384
int copy_Buffer(Buffer *src, Buffer *dst)
Buffer型変数 srcから dstへバッファをコピーする.
Definition buffer.cpp:315
Buffer dump_Buffer(Buffer buf)
Buffer型変数のデバッグ用 16進を Buffer型変数に出力する.
Definition buffer.cpp:1254
#define copy_s2Buffer(src, dst)
copy_b2Buffer()
Definition buffer.h:108
#define cat_s2Buffer(src, dst)
cat_b2Buffer()
Definition buffer.h:122
void putRingFormat(int input, char *fmt,...)
void putRingBuffer(Buffer buf, int input=LOG_RB_MESG, int kind=LOG_RB_UNKNOWN_DATA)
virtual ~CLogRingBuffer(void)
void rewriteBinHexRingBuffer(int n, int input)
#define vsnprintf
Definition common.h:57
#define Max(x, y)
Definition common.h:247
#define LBUF
Definition common.h:146
#define snprintf
Definition common.h:56
#define ON
Definition common.h:230
Definition Brep.h:29
int vldsz
データの長さ.バイナリデータの場合も使用可能.文字列の場合は 0x00 を含まない.
Definition buffer.h:37
int state
変数の状態を表す.正常は JBXL_NORMAL
Definition buffer.h:38
unsigned char * buf
バッファの先頭へのポインタ.str[bufsz]は必ず 0x00となる.
Definition buffer.h:39
#define CHAR_CR
改行
Definition tools.h:78
#define CHAR_LF
ラインフィード
Definition tools.h:79
void del_Buffer_dim(Buffer **dim)
配列を削除する.
Definition xtools.cpp:1567
Buffer * decompline_Buffer_dim(Buffer buf, int mode)
データを行単位に分解する.改行は \r\n
Definition xtools.cpp:1479