3 #include "config_portability.h"
12 #include "diagnostic.h"
16 #define MAXRECORDS 2500
18 static FILE *diagDestinations[] =
19 {DIAGSUPPRESS, DIAGSUPPRESS, DIAGSUPPRESS,
20 DIAGSUPPRESS, DIAGSUPPRESS, DIAGSUPPRESS};
21 static long recordCounts[] =
23 static void *lock = NULL; /* mutex for this modules */
25 /* we look quite extensively because I'm not quite sure what fprintf will
26 * do if the FILE* changes on the way */
28 PrintDiagnostic(DiagLevels level,
32 static const char *level_tags[] = {"", "", "Warning: ", "Error: ", "Fatal: ", "Debug: " };
34 /* we need a lock because we write recordCounts and we use
35 * diagDestinations[] */
36 if (GetNWSLock(&lock) == 0) {
37 fprintf(stderr, "PrintDiagnostic: Error: Couldn't obtain the lock\n");
39 if(diagDestinations[level] != DIAGSUPPRESS) {
40 if( (recordCounts[level]++ >= MAXRECORDS) &&
41 (diagDestinations[level] != stdout) &&
42 (diagDestinations[level] != stderr) ) {
43 /* We want to avoid filling up the disk space when the
44 * system is running for weeks at a time. It might be
45 * nice to save the old file under another name (maybe in
46 * /tmp), then reopen. That requires changing the
47 * DirectDiagnostics() interface to take a file name
48 * instead of a FILE *. */
49 rewind(diagDestinations[level]);
50 recordCounts[level] = 0;
53 fprintf(diagDestinations[level], "%.0f %d ", CurrentTime(), (int)getpid());
54 fprintf(diagDestinations[level], level_tags[level]);
55 vfprintf(diagDestinations[level], message, arguments);
56 fflush(diagDestinations[level]);
58 if (ReleaseNWSLock(&lock) == 0) {
59 fprintf(stderr, "PrintDiagnostic: Error: Couldn't release the lock\n");
65 DirectDiagnostics(DiagLevels level,
71 /* just an extra check */
72 if (whereTo != NULL) {
76 PrintDiagnostic(DIAGERROR, "DirectDiagnostic: fileno failed", NULL);
81 diagDestinations[level] = whereTo;
82 ReleaseNWSLock(&lock);
87 DiagnosticsDirection(DiagLevels level) {
88 return diagDestinations[level];
93 PositionedDiagnostic(DiagLevels level,
100 char *extendedMessage;
102 /* we assume that NWS lines won't be past 10 digits */
103 extendedMessage = (char *)MALLOC(strnlen(fileName, MAX_FILENAME_LENGTH)
104 + strnlen(message, MAX_MESSAGE_LENGTH) + 11);
105 if (extendedMessage == NULL) {
107 PrintDiagnostic(DIAGERROR, "PositionedDiagnostic: out of memory", NULL);
111 va_start(arguments, message);
112 sprintf(extendedMessage, "%s:%d %s", fileName, line, message);
113 PrintDiagnostic(level, extendedMessage, arguments);
116 free(extendedMessage);
121 Diagnostic(DiagLevels level,
126 va_start(arguments, message);
127 PrintDiagnostic(level, message, arguments);