-
Notifications
You must be signed in to change notification settings - Fork 0
/
debugLog.d
280 lines (255 loc) · 6.24 KB
/
debugLog.d
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
/++++
debugLog.d
setdebuglog();
outLog("d1:",d1, 2 , 3, "DebugLog", 'a', 1.23);
struct S {
char[20] c;
int i;
long l;
double d;
}
S ss;
ss.c[0] = 'a';
ss.i = 1;
ss.l = 2;
ss.d = 0.1;
outdumpLog(cast(void *)&ss, ss.sizeof);
++++/
module debugLog;
import std.stdio;
import core.vararg;
import std.ascii: isPrintable;
import std.string: format, lastIndexOf;
import std.file: append;
import std.datetime;
//
static int LogFlag;
static string debugLogFilename;
/++
enum logStatus {
NON,
LogOnly,
WithConsole
}
++/
void test(int i)
{
int[] arry = [1, 2, 3, 4];
if (i == 1) {
writeln(1);
}
else if (i == 2) {
foreach (v ; arry) {
writeln(v);
}
}
}
void outLog(string file = __FILE__, int line = __LINE__, T...)(T t)
{
// _outLogV(format("%s(%d)-[%s]", file, line, getDateTimeStr()), t);
_outLogV(format("%s(%d)[%s]", file, line, getDateTimeStr()), t);
}
// outLog("d1:",d1, 2 , 3, "DebugLog", 'a', 1.23);
//
//void outLog(T...)(T t)
//{
// _outDebugLog(format("%s(%d)", __FILE__, __LINE__), t);
// _outLogV(format("%s(%d)[%s]", __FILE__, __LINE__, getDateTimeStr()), t);
//}
// setDebugLog();
void setDebugLog(int flag = 1)
{
const string ext = "debug_log.txt";
import core.runtime: Runtime;
string logfilename;
if (Runtime.args.length)
logfilename = Runtime.args[0];
else
logfilename = "main";
debugLogFilename = ext;
if (logfilename.length) {
int n = lastIndexOf(logfilename, ".");
if ( n > 0 )
debugLogFilename = logfilename[0 .. n] ~ "." ~ ext;
else
debugLogFilename = logfilename ~ "." ~ ext;
}
else {
assert(0);
}
LogFlag = flag;
outLog(format("==debuglog %s", debugLogFilename));
}
static void _outLog(lazy string dg)
{
if (LogFlag) {
append(debugLogFilename, dg());
}
}
static void _outLoglf(lazy string dg)
{
if (LogFlag) {
append(debugLogFilename, dg() ~ "\n");
}
}
static void _outDebugLog(string s, lazy string dg)
{
if (LogFlag) {
string sout = s ~ format("[%s]", getDateTimeStr()) ~ dg();
append(debugLogFilename, sout ~ "\n");
// writeln(debugLogFilename, sout);
// stdout.writeln(sout);
}
}
static void _outLogV(...)
{
string str;
for (int i = 0; i < _arguments.length; i++)
{
if (_arguments[i] == typeid(char)) {
char c = va_arg!(char)(_argptr);
str ~= format("%c", c);
}
else if (_arguments[i] == typeid(string)) {
string s = va_arg!(string)(_argptr);
str ~= format("%s", s);
}
else if (_arguments[i] == typeid(byte)) {
byte n = va_arg!(byte)(_argptr);
str ~= format("%d", n);
}
else if (_arguments[i] == typeid(ubyte)) {
byte n = va_arg!(ubyte)(_argptr);
str ~= format("%d", n);
}
else if (_arguments[i] == typeid(short)) {
short n = va_arg!(short)(_argptr);
str ~= format("%d", n);
}
else if (_arguments[i] == typeid(ushort)) {
short n = va_arg!(ushort)(_argptr);
str ~= format("%d", n);
}
else if (_arguments[i] == typeid(int)) {
int n = va_arg!(int)(_argptr);
str ~= format("%d", n);
}
else if (_arguments[i] == typeid(uint)) {
int n = va_arg!(uint)(_argptr);
str ~= format("%d", n);
}
else if (_arguments[i] == typeid(long)) {
long n = va_arg!(long)(_argptr);
str ~= format("%d", n);
}
else if (_arguments[i] == typeid(ulong)) {
long n = va_arg!(ulong)(_argptr);
str ~= format("%d", n);
}
else if (_arguments[i] == typeid(float)) {
float f = va_arg!(float)(_argptr);
str ~= format("%f", f);
}
else if (_arguments[i] == typeid(double)) {
double d = va_arg!(double)(_argptr);
str ~= format("%g", d);
}
else {
assert(0);
}
}
_outLoglf(str);
}
static string getDateTimeStr()
{
SysTime cTime = Clock.currTime();
string tms = format(
"%04d/%02d/%02d-%02d:%02d:%02d",
cTime.year,
cTime.month,
cTime.day,
cTime.hour,
cTime.minute,
cTime.second);
return tms;
}
static string getDateStr()
{
SysTime cTime = Clock.currTime();
string tms = format(
"%04d/%02d/%02d",
cTime.year,
cTime.month,
cTime.day);
return tms;
}
unittest
{
struct S {
char[20] c;
int i;
long l;
double d;
}
S ss;
ss.c[0] = 'a';
ss.i = 1;
ss.l = 2;
ss.d = 0.3;
outdumpLog(cast(void *)&ss, ss.sizeof);
}
//void outdumpLog(string file = __FILE__, int line = __LINE__, T...)(T t)
void outdumpLog(string file = __FILE__, int line = __LINE__, T, U)(T t, U u)
{
_outDebugLog(format("%s(%d)", file, line), format("dump:%d byte", u));
_dumpLog(t, u);
}
//
static void _dumpLog(void *Buff, uint byteSize)
{
const int PrintLen = 16;
ubyte[PrintLen] dumpBuff;
void printCount(uint n) {
_outLog(format("%06d: ", n));
}
void printBody() {
string s;
foreach (int i, ubyte v; dumpBuff) {
if (i == PrintLen / 2) {
s ~= " ";
}
s ~= format("%02X ", v);
}
_outLog(s);
}
void printAscii() {
string s;
char c;
foreach (ubyte v; dumpBuff) {
c = cast(char)v;
if (! isPrintable(c))
c = '.';
s ~= format("%c", c);
}
_outLoglf(s);
}
// Main
uint endPrint;
for (uint i; i < byteSize + PrintLen; i += PrintLen) {
endPrint = i + PrintLen;
if (byteSize < endPrint) {
uint end = byteSize - i;
dumpBuff = dumpBuff.init;
dumpBuff[0 .. end] = cast(ubyte[]) Buff[i .. byteSize];
printCount(i);
printBody();
printAscii();
break;
}
dumpBuff = cast(ubyte[]) Buff[i .. endPrint];
printCount(i);
printBody();
printAscii();
}
}
//