My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include "Core.h"

NAMESPACE_UPP

#ifdef PLATFORM_WINCE
const char *FromSysChrSet(const wchar *s)
{
static char out[256];
FromUnicode(out, s, wstrlen(s), CHARSET_DEFAULT);
return out;
}

const wchar *ToSysChrSet(const char *s)
{
static wchar out[1024];
ToUnicode(out, s, strlen(s), CHARSET_DEFAULT);
return out;
}
#endif

LogStream::LogStream()
{
#ifdef PLATFORM_POSIX
hfile = -1;
#else
hfile = INVALID_HANDLE_VALUE;
#endif
part = 0;
sizelimit = 0;
*filename = 0;
options = LOG_FILE;
depth = 0;
bol = false;
}

LogStream::~LogStream() {}

void LogStream::Close()
{
#ifdef PLATFORM_POSIX
if(hfile >= 0)
close(hfile);
hfile = -1;
#else
if(hfile != INVALID_HANDLE_VALUE)
CloseHandle(hfile);
hfile = INVALID_HANDLE_VALUE;
#endif
}

bool LogStream::Delete()
{
Close();
if(*filename) {
if(!FileDelete(filename)) {
BugLog() << "Error deleting " << filename << ": " << GetLastErrorMessage();
return false;
}
*filename = 0;
}
return true;
}

void LogStream::Create(const char *path, bool append)
{
Close();

strcpy(filename, path);
strcpy(backup, filename);
strcat(backup, ".old");

#if defined(PLATFORM_WIN32)

#if defined(PLATFORM_WINCE)
wchar_t pwcs[512];
mbstowcs(pwcs, backup, strlen(backup));
DeleteFile(pwcs);
#else
DeleteFile(backup);
#endif

#elif defined(PLATFORM_POSIX)
unlink(backup);
#else
#error
#endif

#if defined(PLATFORM_WIN32)
#if defined(PLATFORM_WINCE)
wchar_t wfilename[512];
mbstowcs(wfilename, filename, strlen(filename));
MoveFile(wfilename, pwcs);
#else
MoveFile(filename, backup);
#endif
#elif defined(PLATFORM_POSIX)
rename(filename, backup);
#else
#error
#endif

filesize = 0;

#ifdef PLATFORM_WIN32
hfile = CreateFile(ToSysChrSet(filename),
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
append ? OPEN_ALWAYS : CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if(append)
filesize = (int)SetFilePointer(hfile, 0, NULL, FILE_END);
#else
hfile = open(filename, append ? O_CREAT|O_RDWR|O_APPEND : O_CREAT|O_RDWR|O_TRUNC, 0644);
if(append)
filesize = (int)lseek(hfile, 0, SEEK_END);
#endif
wrlim = ptr = (byte *)this;
p = buffer;

Time t = GetSysTime();
#ifdef PLATFORM_WINCE
wchar exe[512];
#else
char exe[512];
#endif
char user[500];
*user = 0;

#ifdef PLATFORM_WIN32
GetModuleFileName(AppGetHandle(), exe, 512);
#ifndef PLATFORM_WINCE
dword w = 2048;
::GetUserNameA(user, &w);
#endif
#else //#
const char *procexepath_();
strcpy(exe, procexepath_());
const char *uenv = getenv("USER");
strcpy(user, uenv ? uenv : "boot");
#endif

char h[1000];
sprintf(h, "* %s %02d.%02d.%04d %02d:%02d:%02d, user: %s\n",
FromSysChrSet(exe),
t.day, t.month, t.year, t.hour, t.minute, t.second, user);
#ifdef PLATFORM_WIN32
dword n;
WriteFile(hfile, h, (dword)strlen(h), &n, NULL);
if(part) {
sprintf(h, ", #%d", part);
WriteFile(hfile, h, (dword)strlen(h) , &n, NULL);
}
WriteFile(hfile, "\r\n", 2, &n, NULL);
#else
IGNORE_RESULT(
write(hfile, h, strlen(h))
);
if(part) {
sprintf(h, ", #%d", part);
IGNORE_RESULT(
write(hfile, h, strlen(h))
);
}
IGNORE_RESULT(
write(hfile, "\r\n", 2)
);
#endif
bol = true;
}

void LogStream::Flush()
{
int count = (int)(p - buffer);
if(count == 0) return;
if(options & LOG_COUT)
Cout().Put(buffer, count);
if(options & LOG_CERR)
Cerr().Put(buffer, count);
#ifdef PLATFORM_WIN32
if(options & LOG_FILE)
if(hfile != INVALID_HANDLE_VALUE) {
dword n;
WriteFile(hfile, buffer, count, &n, NULL);
}
if(options & LOG_DBG) {
*p = 0;
::OutputDebugString((LPCSTR)buffer);
}
#else
if(options & LOG_FILE)
if(hfile >= 0)
IGNORE_RESULT(
write(hfile, buffer, count)
);
if(options & LOG_DBG)
Cerr().Put(buffer, count);
#endif
filesize += count;
p = buffer;
if(sizelimit > 0 && filesize > sizelimit)
Create(filename, false);
}

void LogStream::Put0(int w)
{
if(w == LOG_BEGIN)
depth = min(depth + 1, 20);
else
if(w == LOG_END)
depth = max(depth - 1, 0);
else {
if(bol) {
bol = false;
for(int q = depth; q--;)
Put0('\t');
if(options & LOG_TIMESTAMP) {
char h[60];
Time t = GetSysTime();
sprintf(h, "%02d.%02d.%04d %02d:%02d:%02d ",
t.day, t.month, t.year, t.hour, t.minute, t.second);
const char *s = h;
while(*s)
Put0(*s++);
}
}
*p++ = w;
if(w == '\n') {
Flush();
bol = true;
}
else
if(p == buffer + 512)
Flush();
}
}

void LogStream::_Put(int w)
{
CriticalSection::Lock __(cs);
Put0(w);
}

void LogStream::_Put(const void *data, dword size)
{
CriticalSection::Lock __(cs);
const byte *q = (byte *)data;
while(size--)
Put0(*q++);
}

bool LogStream::IsOpen() const
{
#ifdef PLATFORM_POSIX
return hfile >= 0;
#else
return hfile != INVALID_HANDLE_VALUE;
#endif
}

/*
static void sLarge(String& text, size_t *large, int count, const char *txt)
{
int n = min(1024, count);
Sort(large, large + n, StdLess<size_t>());
int i = 0;
while(i < n) {
size_t q = large[i];
int nn = i++;
while(i < n && large[i] == q) i++;
nn = i - nn;
if(q < 10000)
text << Format("%4d B, %5d %s (%6d KB)\r\n", (int)(uintptr_t)q, nn, txt, (int)(uintptr_t)((nn * q) >> 10));
else
text << Format("%4d`KB, %5d %s (%6d KB)\r\n", (int)(uintptr_t)(q >> 10), nn, txt, (int)(uintptr_t)((nn * q) >> 10));
}
}
*/

String AsString(const MemoryProfile& mem)
{
String text;
int acount = 0;
size_t asize = 0;
int fcount = 0;
size_t fsize = 0;
for(int i = 0; i < 1024; i++)
if(mem.allocated[i]) {
int sz = 4 * i;
text << Format("%4d B, %6d allocated (%5d KB), %6d fragmented (%5d KB)\n",
sz, mem.allocated[i], (mem.allocated[i] * sz) >> 10,
mem.fragmented[i], (mem.fragmented[i] * sz) >> 10);
acount += mem.allocated[i];
asize += mem.allocated[i] * sz;
fcount += mem.fragmented[i];
fsize += mem.fragmented[i] * sz;
}
text << Format(" TOTAL, %6d allocated (%5d KB), %6d fragmented (%5d KB)\n",
acount, int(asize >> 10), fcount, int(fsize >> 10));
text << "Free pages " << mem.freepages << " (" << mem.freepages * 4 << " KB)\n";
text << "Large block count " << mem.large_count
<< ", total size " << (mem.large_total >> 10) << " KB\n";
// sLarge(text, mem.large_size, mem.large_count, "allocated");
text << "Large fragments count " << mem.large_free_count
<< ", total size " << (mem.large_free_total >> 10) << " KB\n";
// sLarge(text, mem.large_free_size, mem.large_free_count, "fragments");
return text;
}


#ifdef _MULTITHREADED

StaticCriticalSection sLogLock;

void LockLog()
{
sLogLock.Enter();
}

void UnlockLog()
{
sLogLock.Leave();
}

#endif

#ifdef flagCHECKINIT

void InitBlockBegin__(const char *fn, int line) {
RLOG(fn << " " << line << " init block");
#ifdef HEAPDBG
MemoryCheckDebug();
#else
MemoryCheck();
#endif
}

void InitBlockEnd__(const char *fn, int line) {
RLOG(fn << " " << line << " init block finished");
#ifdef HEAPDBG
MemoryCheckDebug();
#else
MemoryCheck();
#endif
}

#endif

END_UPP_NAMESPACE

Change log

r4457 by cxl on Jan 21, 2012   Diff
*uppsrc: Fixed many GCC warnings
Go to: 
Project members, sign in to write a code review

Older revisions

r3629 by cxl on Jul 9, 2011   Diff
.CtrlLib, CtrlCore, Painter, RichEdit:
minor fixes
r2844 by cxl on Nov 12, 2010   Diff
Core: VppLog:  LOG_TIMESTAMP option
r540 by cxl on Oct 18, 2008   Diff
A++ Alt+C fix
All revisions of this file

File info

Size: 7030 bytes, 351 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting