root/trunk/log_api/LogAPI.d

Revision 410, 1.9 kB (checked in by BCS, 4 years ago)

better return value logging
header comment

Line 
1 import std.stdio;
2 /**
3 The intended use for this lib is for debugging the external API's. For example
4 I wrote it because I was having issues with a mysql wrapper lib and wanted to
5 known what api calls were being made.
6
7 To use it, replace the call to be logged with a wrapped call:
8
9 ulong r = mysql_real_escape_string(connection, ret.ptr, string.ptr, string.length);
10 ulong r = TraceAPI!("mysql_real_escape_string)(__FILE__,__LINE__,connection, ret.ptr, string.ptr, string.length);
11
12 a little fun with regex and you can have a whole file done in about a minute
13
14 This software is distributed without any warranty of any kind and is NOT suitable for any
15 production usage.
16
17 Author: Benjamin Shropshire  shro8822 -> vandals uidaho edu
18 */
19
20 template MixinTraceAPI()
21 {
22 template TraceAPI(char[] fn)
23 {
24     // Handy sed rules
25     // s/\(MyLibPrefix_\w*\)(/TraceAPI!("$1")(__FILE__,__LINE__,/
26     // s/,\s*)/)/
27     static if(is(typeof(mixin(fn)) R == return) && is(typeof(mixin(fn)) T == function))
28     {
29         debug(compileTimeTrace) pragma(msg, R.stringof~" "~fn~T.stringof);
30         R TraceAPI(char[] file, int line, T t)
31         {
32             debug(runTimeTrace)
33             {
34                 int strlen(char* _){for(int i=0;;i++)if(!_[i])return i;}
35                 writef(" %s:%s %s ",file, line, fn);
36                 foreach(i,a;T)
37                 {
38                     static if(is(a == char*))
39                         writef(`, "%s"`, t[i][0..strlen(t[i])]);
40                     else static if(is(a : int))
41                         writef(", %d", t[i]);
42                     else
43                         writef(", [%s]%s", a.stringof,t[i]);
44                 }
45                 writef(\n);
46             }
47             static if(is(R == void))
48             {
49                 mixin(fn~"(t);");
50                 debug(runTimeTrace) writef(" %s:%s %s  = void\n",file, line, fn);
51                 return;
52             }
53             else
54             {
55                 R ret = mixin(fn~"(t)");
56                 debug(runTimeTrace) writef(" %s:%s %s  = ",file, line, fn);
57                 static if(is(R == char*))
58                     writef(`"%s"\n`, ret[0..strlen(ret)]);
59                 else static if(is(R : int))
60                     writef("%d\n", ret);
61                 else
62                     writef("[%s]%s\n", R.stringof,ret);
63                 return ret;
64             }
65         }
66     }
67     else
68         static assert(false, fn);
69 }
70 }
Note: See TracBrowser for help on using the browser.