-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdprintf.cpp
More file actions
314 lines (271 loc) · 6.81 KB
/
dprintf.cpp
File metadata and controls
314 lines (271 loc) · 6.81 KB
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
/*
20000211 ljz Removed MajorEvent and MinorEvent instances of 'Debug'.
Cosmetics. Added UserLog and TroubleLog.
Added timestamp option.
Added CriticalSection when printing to file or stdout.
20011110 mvh Post release 1.3.10: keep log files closed while working
20021027 mvh Avoid crash when logfile cannot be opened
20041003 mvh Note: max 1200 chars (use %.1000s to print long strings)
20060618 mvh Define _SH_DENYNO if needed
20070105 mvh Added timestamp for linux users (thanks, Mark Pearson)
20070406 mvh Use reentrant ctime_r and localtime_r when available
20070408 mvh Protected entire .printf() with global critical section
20070712 mvh Closed/opened critical section where recursive call to printf
Would hang linux version with sqlite
20071120 mvh Use fputs instead of fprintf at end: otherwise % removed
20080821 mvh Fix for Solaris SUNwspro compiler
*/
# include <stdio.h>
# include <stdarg.h>
#ifndef UNIX
# include <time.h>
# include <windows.h>
# include <share.h>
#else
# include "wintypes.hpp"
# include "npipe.hpp"
#endif
# include "dicom.hpp"
# include "dprintf.hpp"
#ifndef _SH_DENYNO
#define _SH_DENYNO SH_DENYNO
#endif
extern char ServerName[];
#ifndef WIN32
# include <unistd.h>
# include <dirent.h>
# define EnterCriticalSection(a) pthread_mutex_lock(a)
# define LeaveCriticalSection(a) pthread_mutex_unlock(a)
# define CRITICAL_SECTION pthread_mutex_t
# define InitializeCriticalSection(a) pthread_mutex_init(a, NULL);
# define DeleteCriticalSection(a) pthread_mutex_destroy(a);
#endif
static CRITICAL_SECTION CriticalFile;
static int CriticalFileExists=0;
/* Constructor and Destructor */
Debug::Debug()
{
if (!CriticalFileExists)
{ CriticalFileExists=1;
InitializeCriticalSection(&CriticalFile);
}
BDebug = 0;
Debugfp = NULL;
CloseOnOff = 0;
UseMessagePipe = 0;
UseUDP = 0;
bAddTimeStamps = 0;
}
Debug::~Debug()
{
Off();
}
/* Several overloaded Debug 'ON' functions */
void Debug::On()
{
Off();
if (!CriticalFileExists)
{ CriticalFileExists=1;
InitializeCriticalSection(&CriticalFile);
}
Debugfp = stdout;
BDebug = 1 ;
CloseOnOff = 0;
UseMessagePipe = 0;
}
void Debug::On(FILE* fp)
{
Off();
if(!fp) return;
if (!CriticalFileExists)
{ CriticalFileExists=1;
InitializeCriticalSection(&CriticalFile);
}
Debugfp = fp;
BDebug = 1;
CloseOnOff = 0;
UseMessagePipe = 0;
}
void Debug::On(char* filename)
{
Off();
if(!filename) return;
if (!CriticalFileExists)
{ CriticalFileExists=1;
InitializeCriticalSection(&CriticalFile);
}
//#ifndef UNIX
// Debugfp = _fsopen(filename, "a", _SH_DENYNO);
//#else
// Debugfp = fopen(filename, "a");
//#endif
// if(!Debugfp)
// return;
Debugfp = NULL;
strcpy(FilePipeName, filename);
BDebug = 3;
CloseOnOff = 0;
UseMessagePipe = 0;
}
void Debug::OnMsgPipe(char* MsgPipe)
{
Off();
if (!CriticalFileExists)
{ CriticalFileExists=1;
InitializeCriticalSection(&CriticalFile);
}
BDebug = 1;
UseMessagePipe = 1;
CloseOnOff = 0;
DescribeSource = TRUE;
strcpy(FilePipeName, MsgPipe);
}
void Debug::OnMsgPipe(char* MsgPipe, BOOL DSource)
{
Off();
if (!CriticalFileExists)
{ CriticalFileExists=1;
InitializeCriticalSection(&CriticalFile);
}
BDebug = 1;
UseMessagePipe = 1;
CloseOnOff = 0;
DescribeSource = DSource;
strcpy(FilePipeName, MsgPipe);
}
void Debug::OnUDP(char* Host, char* Port)
{
Off();
if (!CriticalFileExists)
{ CriticalFileExists=1;
InitializeCriticalSection(&CriticalFile);
}
if(!SocketUDP.BindUDPClient(Host, Port))
{ printf("FAILED TO BIND UDP CLIENT\n");
return;
}
UseUDP = 1;
BDebug = 1;
CloseOnOff = 0;
DescribeSource = TRUE;
}
/* One Debug 'OFF' function */
void Debug::Off()
{
if(!BDebug)
return;
// DeleteCriticalSection(&CriticalFile);
BDebug = 0;
if(CloseOnOff)
fclose(Debugfp);
CloseOnOff = 0;
UseMessagePipe = 0;
UseUDP = 0;
}
/* Finally the PRINT function */
#ifndef localtime_r
#define localtime_r(a, b) memcpy(b, localtime(a), sizeof(struct tm))
#endif
int Debug::printf(char *fmt, ...)
{
int rc = 0;
va_list vargs;
char s[1250];
time_t Time;
struct tm LocalTime;
int iLength;
/* Debug::On() must be called first */
if(!BDebug)
return rc;
EnterCriticalSection(&CriticalFile);
if(DescribeSource)
sprintf(s, "[%s] ", ServerName);
else
s[0] = 0;
#ifndef UNIX
if (bAddTimeStamps)
{
time(&Time); /* Get time as long integer. */
localtime_r(&Time, &LocalTime); /* Convert to local time. */
sprintf(s + strlen(s), "%04d%02d%02d %02d:%02d:%02d ",
LocalTime.tm_year + 1900,
LocalTime.tm_mon + 1,
LocalTime.tm_mday,
LocalTime.tm_hour,
LocalTime.tm_min,
LocalTime.tm_sec);
}
#else
if (bAddTimeStamps)
{ char TimeString[128], buf[64];
Time = time(NULL);
#ifdef SOLARIS
strcpy(TimeString, ctime_r(&Time, buf, 64));
#else
strcpy(TimeString, ctime_r(&Time, buf));
#endif
TimeString[strlen(TimeString)-1] = '\0';
strcat(s, TimeString);
strcat(s, " ");
}
#endif
iLength = strlen(s);
va_start(vargs, fmt);
vsprintf(s + iLength, fmt, vargs);
va_end(vargs);
LeaveCriticalSection(&CriticalFile);
if ((strstr(s + iLength, "***")) && (this != &TroubleLog))
TroubleLog.printf("%s", s + iLength);
EnterCriticalSection(&CriticalFile);
if(!UseMessagePipe)
{ if(!UseUDP)
{ if (BDebug==3)
{ // append to a file that is opened/closed everytime
#ifndef UNIX
Debugfp = _fsopen(FilePipeName, "a", _SH_DENYNO);
#else
Debugfp = fopen(FilePipeName, "a");
#endif
if (Debugfp)
{ rc = fputs(s, Debugfp);
fclose(Debugfp);
}
}
else if(Debugfp)
{ // Output to a file that remains open or stdout
rc = fputs(s, Debugfp);
fflush(Debugfp);
}
}
else
{ // Output to a UDP PORT
rc = 1;
SocketUDP.SendBinary((BYTE*)s,(UINT)strlen(s));
}
}
else
{ // Output to a named pipe
#ifndef UNIX
if(WaitNamedPipe(FilePipeName, 1))
{ rc = 1;
CallNamedPipe(FilePipeName, s, strlen(s)+1, NULL, 0, NULL, NMPWAIT_NOWAIT);
}
#else
NamedPipe aMessagePipe(FilePipeName);
aMessagePipe.Connect();
if(aMessagePipe.Write((unsigned char*)s, strlen(s)+1) == strlen(s) + 1)
rc = 1;
aMessagePipe.Disconnect();
#endif
}
LeaveCriticalSection(&CriticalFile);
return rc;
}
/* Global instances of the logging functionality */
Debug SystemDebug;
Debug OperatorConsole;
Debug AccessUpdate;
//Debug MajorEvent;
//Debug MinorEvent;
Debug UserLog;
Debug TroubleLog;