Lely core libraries
2.2.5
|
Go to the documentation of this file.
36 #pragma comment(lib, "wtsapi32.lib")
38 #elif _POSIX_C_SOURCE >= 200809L && !defined(__NEWLIB__)
43 #ifndef LELY_DIALOG_DIAG_TIMEOUT
44 #define LELY_DIALOG_DIAG_TIMEOUT 10
49 static void *diag_handle;
51 static void *diag_at_handle;
57 assert(!end || end >= begin);
59 const char *cp = begin;
60 while ((!end || cp < end) && *cp) {
63 if ((!end || cp < end) && *cp ==
'\n')
96 int r = snprintf(s, n,
"%s:", at->
filename);
100 r =
MIN((
size_t)r, n);
104 r = snprintf(s, n,
"%d:", at->
line);
108 r =
MIN((
size_t)r, n);
112 r = snprintf(s, n,
"%d:", at->
column);
127 *phandler = diag_handler;
129 *phandle = diag_handle;
135 diag_handler = handler;
136 diag_handle = handle;
143 *phandler = diag_at_handler;
145 *phandle = diag_at_handle;
151 diag_at_handler = handler;
152 diag_at_handle = handle;
159 va_start(ap, format);
160 vdiag(severity, errc, format, ap);
168 diag_handler(diag_handle, severity, errc, format, ap);
173 const char *format, ...)
176 va_start(ap, format);
177 vdiag_at(severity, errc, at, format, ap);
183 const char *format, va_list ap)
186 diag_at_handler(diag_at_handle, severity, errc, at, format, ap);
191 const char *format, ...)
194 va_start(ap, format);
195 vdiag_if(severity, errc, at, format, ap);
201 const char *format, va_list ap)
204 vdiag_at(severity, errc, at, format, ap);
209 const char *format, va_list ap)
216 const struct floc *at,
const char *format, va_list ap)
223 fprintf(stderr,
"%s\n", s);
235 const char *format, va_list ap)
237 const char *cmd = handle;
240 fprintf(stderr,
"%s: ", cmd);
250 const char *format, va_list ap)
257 const struct floc *at,
const char *format, va_list ap)
270 const char *format, va_list ap)
277 const struct floc *at,
const char *format, va_list ap)
279 LPSTR pTitle = (LPSTR)(handle ? handle :
"");
281 DWORD Style = MB_OK | MB_SETFOREGROUND | MB_TOPMOST;
284 case DIAG_INFO: Style |= MB_ICONINFORMATION;
break;
287 case DIAG_FATAL: Style |= MB_ICONERROR;
break;
292 char *pMessage = NULL;
295 WTSSendMessageA(WTS_CURRENT_SERVER_HANDLE,
296 WTSGetActiveConsoleSessionId(), pTitle,
297 strlen(pTitle), pMessage, strlen(pMessage),
298 Style, LELY_DIALOG_DIAG_TIMEOUT, &dwResponse,
312 const char *format, va_list ap)
319 const struct floc *at,
const char *format, va_list ap)
323 if (time(&timer) != -1) {
326 struct tm *timeptr = NULL;
327 if (localtime_s(&tm, &timer))
329 #elif defined(_POSIX_C_SOURCE)
331 struct tm *timeptr = localtime_r(&timer, &tm);
333 struct tm *timeptr = localtime(&timer);
338 if (strftime(buf,
sizeof(buf),
339 "%a, %d %b %Y %H:%M:%S %z", timeptr)
342 fprintf(stderr,
"%s: ", buf);
354 const char *format, va_list ap)
361 const struct floc *at,
const char *format, va_list ap)
363 #if _POSIX_C_SOURCE >= 200809L && !defined(__NEWLIB__)
366 int priority = LOG_USER;
368 case DIAG_DEBUG: priority |= LOG_DEBUG;
break;
369 case DIAG_INFO: priority |= LOG_INFO;
break;
372 case DIAG_FATAL: priority |= LOG_EMERG;
break;
378 syslog(priority,
"%s", s);
391 const char *format, va_list ap)
398 const char *format, va_list ap)
405 const struct floc *at,
const char *format, va_list ap)
418 r =
MIN((
size_t)r, n);
421 r = snprintf(s, n,
" ");
425 r =
MIN((
size_t)r, n);
431 case DIAG_DEBUG: r = snprintf(s, n,
"debug: ");
break;
433 case DIAG_WARNING: r = snprintf(s, n,
"warning: ");
break;
434 case DIAG_ERROR: r = snprintf(s, n,
"error: ");
break;
435 case DIAG_FATAL: r = snprintf(s, n,
"fatal: ");
break;
436 default: r = 0;
break;
441 r =
MIN((
size_t)r, n);
445 if (format && *format) {
446 r = vsnprintf(s, n, format, ap);
450 r =
MIN((
size_t)r, n);
454 r = snprintf(s, n,
": ");
458 r =
MIN((
size_t)r, n);
465 const char *errstr =
errc2str(errc);
467 r = snprintf(s, n,
"%s", errstr);
479 const struct floc *at,
const char *format, va_list ap)
490 char *s = malloc(n + 1);
511 const char *cmd = path;
515 while (cmd >= path && *cmd !=
'\\')
517 while (cmd >= path && *cmd !=
'/')
void vdiag(enum diag_severity severity, int errc, const char *format, va_list ap)
Emits a diagnostic message.
diag_severity
The severity of a diagnostic message.
void default_diag_handler(void *handle, enum diag_severity severity, int errc, const char *format, va_list ap)
The default diag() handler.
void daemon_diag_at_handler(void *handle, enum diag_severity severity, int errc, const struct floc *at, const char *format, va_list ap)
The diag_at() handler for daemons.
void diag_if(enum diag_severity severity, int errc, const struct floc *at, const char *format,...)
Emits a diagnostic message occurring at a location in a text file.
@ DIAG_INFO
An informational message.
#define MIN(a, b)
Returns the minimum of a and b.
void vdiag_at(enum diag_severity severity, int errc, const struct floc *at, const char *format, va_list ap)
Emits a diagnostic message occurring at a location in a text file.
void diag_set_handler(diag_handler_t *handler, void *handle)
Sets the handler function for diag().
void cmd_diag_handler(void *handle, enum diag_severity severity, int errc, const char *format, va_list ap)
The diag() handler used for command-line programs.
int snprintf_floc(char *s, size_t n, const struct floc *at)
Prints a file location to a string buffer.
void diag_get_handler(diag_handler_t **phandler, void **phandle)
Retrieves the handler function for diag().
@ DIAG_FATAL
A fatal error, which SHOULD result in program termination.
size_t floc_lex(struct floc *at, const char *begin, const char *end)
Increments a file location by reading characters from a memory buffer.
A location in a text file.
void syslog_diag_handler(void *handle, enum diag_severity severity, int errc, const char *format, va_list ap)
The diag() handler used for the system logging facilities.
const char * errc2str(int errc)
Returns a string describing a native error code.
void default_diag_at_handler(void *handle, enum diag_severity severity, int errc, const struct floc *at, const char *format, va_list ap)
The default diag_at() handler.
int line
The line number (starting from 1).
int vsnprintf_diag(char *s, size_t n, enum diag_severity severity, int errc, const char *format, va_list ap)
Prints a diagnostic message to a string buffer.
int vsnprintf_diag_at(char *s, size_t n, enum diag_severity severity, int errc, const struct floc *at, const char *format, va_list ap)
Prints a diagnostic message occurring at a location in a text file to a string buffer.
int column
The column number (starting from 1).
void dialog_diag_at_handler(void *handle, enum diag_severity severity, int errc, const struct floc *at, const char *format, va_list ap)
The diag_at() handler for dialog boxes.
void syslog_diag_at_handler(void *handle, enum diag_severity severity, int errc, const struct floc *at, const char *format, va_list ap)
The diag_at() handler used for the system logging facilities.
void diag_at(enum diag_severity severity, int errc, const struct floc *at, const char *format,...)
Emits a diagnostic message occurring at a location in a text file.
void diag_handler_t(void *handle, enum diag_severity severity, int errc, const char *format, va_list ap)
The function type of a handler for diag().
const char * cmdname(const char *path)
Extracts the command name from a path.
void vdiag_if(enum diag_severity severity, int errc, const struct floc *at, const char *format, va_list ap)
Emits a diagnostic message occurring at a location in a text file.
@ DIAG_DEBUG
A debug message.
void daemon_diag_handler(void *handle, enum diag_severity severity, int errc, const char *format, va_list ap)
The diag() handler for daemons.
void diag_at_get_handler(diag_at_handler_t **phandler, void **phandle)
Retrieves the handler function for diag_at().
void log_diag_at_handler(void *handle, enum diag_severity severity, int errc, const struct floc *at, const char *format, va_list ap)
The diag_at() handler used for log-files.
void diag_at_set_handler(diag_at_handler_t *handler, void *handle)
Sets the handler function for diag_at().
int vasprintf_diag(char **ps, enum diag_severity severity, int errc, const char *format, va_list ap)
Equivalent to vsnprintf_diag(), except that it allocates a string large enough to hold the output,...
void dialog_diag_handler(void *handle, enum diag_severity severity, int errc, const char *format, va_list ap)
The diag() handler for dialog boxes.
void log_diag_handler(void *handle, enum diag_severity severity, int errc, const char *format, va_list ap)
The diag() handler for log files.
void diag_at_handler_t(void *handle, enum diag_severity severity, int errc, const struct floc *at, const char *format, va_list ap)
The function type of a handler for diag_at().
const char * filename
The name of the file.
int vasprintf_diag_at(char **ps, enum diag_severity severity, int errc, const struct floc *at, const char *format, va_list ap)
Equivalent to vsnprintf_diag_at(), except that it allocates a string large enough to hold the output,...
void diag(enum diag_severity severity, int errc, const char *format,...)
Emits a diagnostic message.