static rstatus_t dn_create_pidfile(struct instance *nci) { char pid[DN_UINTMAX_MAXLEN]; int fd, pid_len; ssize_t n; fd = open(nci->pid_filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (fd < 0) { log_error("opening pid file '%s' failed: %s", nci->pid_filename, strerror(errno)); return DN_ERROR; } nci->pidfile = 1; pid_len = dn_snprintf(pid, DN_UINTMAX_MAXLEN, "%d", nci->pid); n = dn_write(fd, pid, pid_len); if (n < 0) { log_error("write to pid file '%s' failed: %s", nci->pid_filename, strerror(errno)); return DN_ERROR; } close(fd); return DN_OK; }
void _log_stderr(const char *fmt, ...) { struct logger *l = &logger; int len, size, errno_save; char buf[4 * LOG_MAX_LEN]; va_list args; ssize_t n; errno_save = errno; len = 0; /* length of output buffer */ size = 4 * LOG_MAX_LEN; /* size of output buffer */ va_start(args, fmt); len += dn_vscnprintf(buf, size, fmt, args); va_end(args); buf[len++] = '\n'; n = dn_write(STDERR_FILENO, buf, len); if (n < 0) { l->nerror++; } errno = errno_save; }
/* * Hexadecimal dump in the canonical hex + ascii display * See -C option in man hexdump */ void _log_hexdump(const char *file, int line, char *data, int datalen, const char *fmt, ...) { struct logger *l = &logger; char buf[8 * LOG_MAX_LEN]; int i, off, len, size, errno_save; ssize_t n; if (l->fd < 0) { return; } /* log hexdump */ errno_save = errno; off = 0; /* data offset */ len = 0; /* length of output buffer */ size = 8 * LOG_MAX_LEN; /* size of output buffer */ while (datalen != 0 && (len < size - 1)) { char *save, *str; unsigned char c; int savelen; len += dn_scnprintf(buf + len, size - len, "%08x ", off); save = data; savelen = datalen; for (i = 0; datalen != 0 && i < 16; data++, datalen--, i++) { c = (unsigned char)(*data); str = (i == 7) ? " " : " "; len += dn_scnprintf(buf + len, size - len, "%02x%s", c, str); } for ( ; i < 16; i++) { str = (i == 7) ? " " : " "; len += dn_scnprintf(buf + len, size - len, " %s", str); } data = save; datalen = savelen; len += dn_scnprintf(buf + len, size - len, " |"); for (i = 0; datalen != 0 && i < 16; data++, datalen--, i++) { c = (unsigned char)(isprint(*data) ? *data : '.'); len += dn_scnprintf(buf + len, size - len, "%c", c); } len += dn_scnprintf(buf + len, size - len, "|\n"); off += 16; } n = dn_write(l->fd, buf, len); if (n < 0) { l->nerror++; } errno = errno_save; }
void _log(const char *file, int line, int panic, const char *fmt, ...) { struct logger *l = &logger; int len, size, errno_save; char buf[LOG_MAX_LEN]; va_list args; ssize_t n; if (l->fd < 0) { return; } errno_save = errno; len = 0; /* length of output buffer */ size = LOG_MAX_LEN; /* size of output buffer */ struct timeval curTime; gettimeofday(&curTime, NULL); char buffer [80]; strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", localtime(&curTime.tv_sec)); len += dn_scnprintf(buf + len, size - len, "[%.*s.%03d] %s:%d ", strlen(buffer) - 1, buffer, (int64_t)curTime.tv_usec / 1000, file, line); va_start(args, fmt); len += dn_vscnprintf(buf + len, size - len, fmt, args); va_end(args); buf[len++] = '\n'; n = dn_write(l->fd, buf, len); if (n < 0) { l->nerror++; } errno = errno_save; if (panic) { abort(); } }
// internal function void dnm_cli_printf_v (const char *format, va_list arg) { static BOOLEAN wasError = FALSE; INT32S len, hdrLen; INT32S res; char buf[DN_CLI_CTRL_SIZE]; BOOLEAN prevVal; CS_LOCAL_VAR; // ((dn_cli_ctrlMsg_t *)buf)->cmdId = DN_CLI_CMD_TYPE_TRACE; #ifdef WIN32 if (dnm_cli_v.pCliLogFile != NULL) vfprintf(dnm_cli_v.pCliLogFile, format, arg); #endif // hdrLen = sizeof(dn_cli_ctrlMsg_t); hdrLen = 0; if (wasError) { // Add "..." SNPRINTF(buf + hdrLen, sizeof(buf) - hdrLen, ERR_INDICATOR); hdrLen += strlen(ERR_INDICATOR); } len = VSPRINTF(buf + hdrLen, sizeof(buf) - hdrLen, format, arg); if(len < 0) // error - print '***********' len = SNPRINTF(buf + hdrLen, sizeof(buf) - hdrLen, "*** CLI_LEN_ERROR ***\r\n"); buf[sizeof(buf)-1] = 0; len += hdrLen; if (len>sizeof(buf)) len = sizeof(buf); prevVal = wasError; res = dn_write(DN_CLI_DEV_ID, buf, len); OS_ENTER_CRITICAL(); if (res == DN_ERR_NO_RESOURCES || (!prevVal && wasError)) wasError = TRUE; else wasError = FALSE; OS_EXIT_CRITICAL(); }