0a7edf8e2e58bc63b8e008f64a1a0efe2438cb3c
[sdk] / extras / include / dpl.ec
1 // dpl.ec
2 // Functions:
3 //    _dpl   - Debug Print Line
4 //    _dplf  - Debug Print Line Format
5 //    _dpcl  - Debug Print Channel Line
6 //    _dpclf - Debug Print Channel Line Format
7 // Usage:
8 //    #define _DPL_ON
9 //    #include <dpl.eh>
10 //    import "dpl.ec"
11 //    _dpl(int indent, ...);
12 //    _dplf(int indent, const char * format, ...);
13 //    _dpcl(const char ** channelNames, int channel, int indent, ...);
14 //    _dpclf(const char ** channelNames, int channel, int indent, const char * format, ...);
15 // Example Usage:
16 //    const char * dpln[] = { null, "MISC", null };
17 //    enum dplc { none, misc=1 }; // use =0 to disable printing of specific channels
18 //    _dpcl(dpln, dplc::misc, 0, "example: ", number, point, etc);
19
20 import "ecere"
21
22 #include <stdarg.h>
23
24 char * _printNow()
25 {
26    int c;
27    char * s[6];
28    char * time;
29    DateTime now;
30    now.GetLocalTime();
31    for(c=0; c<6; c++)
32       s[c] = new char[8];
33    sprintf(s[0], "%04d", now.year);
34    sprintf(s[1], "%02d", now.month+1);
35    sprintf(s[2], "%02d", now.day);
36    sprintf(s[3], "%02d", now.hour);
37    sprintf(s[4], "%02d", now.minute);
38    sprintf(s[5], "%02d", now.second);
39    time = PrintString("*", s[0], s[1], s[2], "-", s[3], s[4], s[5], "*");
40    for(c=0; c<6; c++)
41       delete s[c];
42    return time;
43 }
44
45 // Debug Print Line (_dpl)
46 #ifdef _DPL_ON
47 #define _dpl(...) __dpl(__FILE__, __LINE__, ##__VA_ARGS__)
48 #else
49 #define _dpl(...)
50 #endif
51 void __dpl(const char * file, int line, int indent, typed_object object, ...)
52 {
53    int c;
54    char * time = _printNow();
55    char string[MAX_F_STRING];
56    va_list args;
57    printf("%s %s:% 5d: ", time, file, line);
58    for(c = 0; c<indent; c++)
59       printf(" ");
60    va_start(args, object);
61    PrintStdArgsToBuffer(string, sizeof(string), object, args);
62    printf(string);
63    va_end(args);
64    printf("\n");
65    delete time;
66 }
67
68 // Debug Print Line Format (_dplf)
69 void __dplf(const char * file, int line, int indent, const char * format, ...)
70 {
71    int c;
72    char * time = _printNow();
73    char string[MAX_F_STRING];
74    va_list args;
75    va_start(args, format);
76    vsnprintf(string, sizeof(string), format, args);
77    string[sizeof(string)-1] = '\0';
78    printf("%s %s:% 5d: ", time, file, line);
79    for(c = 0; c<indent; c++)
80       printf(" ");
81    printf("%s\n", string);
82    va_end(args);
83    delete time;
84 }
85
86 // Debug Print Channel Line (_dpcl)
87 void __dpcl(const char * file, int line, const char ** channels, int channel, int indent, typed_object object, ...)
88 {
89    bool chan = channel && channels && channels[channel];
90    if(chan || !channels)
91    {
92       int c;
93       char * time = _printNow();
94       char string[MAX_F_STRING];
95       va_list args;
96       printf("%s %s:% 5d: %s%s", time, file, line, chan ? channels[channel] : "", chan && channels[channel][0] ? ": " : "");
97       for(c = 0; c<indent; c++)
98          printf(" ");
99       va_start(args, object);
100       PrintStdArgsToBuffer(string, sizeof(string), object, args);
101       printf(string);
102       va_end(args);
103       printf("\n");
104       delete time;
105    }
106 }
107
108 // Debug Print Channel Line Format (_dpclf)
109 void __dpclf(const char * file, int line, const char ** channels, int channel, int indent, const char * format, ...)
110 {
111    bool chan = channel && channels && channels[channel];
112    if(chan || !channels)
113    {
114       int c;
115       char * time = _printNow();
116       char string[MAX_F_STRING];
117       va_list args;
118       va_start(args, format);
119       vsnprintf(string, sizeof(string), format, args);
120       string[sizeof(string)-1] = '\0';
121       printf("%s %s:% 5d: %s%s", time, file, line, chan ? channels[channel] : "", chan && channels[channel][0] ? ": " : "");
122       for(c = 0; c<indent; c++)
123          printf(" ");
124       printf("%s\n", string);
125       va_end(args);
126       delete time;
127    }
128 }