log: Log errors while writing log info

This commit adds logic to log errors during output. Errors are logged
once and the number of errors is maintained.
pull/5376/head
Jeff Lucovsky 5 years ago committed by Victor Julien
parent b9458adf8a
commit 00f77f9643

@ -210,8 +210,17 @@ static int SCLogFileWriteNoLock(const char *buffer, int buffer_len, LogFileCtx *
if (log_ctx->fp) {
SCClearErrUnlocked(log_ctx->fp);
ret = SCFwriteUnlocked(buffer, buffer_len, 1, log_ctx->fp);
SCFflushUnlocked(log_ctx->fp);
if (1 != SCFwriteUnlocked(buffer, buffer_len, 1, log_ctx->fp)) {
/* Only the first error is logged */
if (!log_ctx->output_errors) {
SCLogError(SC_ERR_LOG_OUTPUT, "%s error while writing to %s",
SCFerrorUnlocked(log_ctx->fp) ? strerror(errno) : "unknown error",
log_ctx->filename);
}
log_ctx->output_errors++;
} else {
SCFflushUnlocked(log_ctx->fp);
}
}
return ret;
@ -250,8 +259,17 @@ static int SCLogFileWrite(const char *buffer, int buffer_len, LogFileCtx *log_ct
if (log_ctx->fp) {
clearerr(log_ctx->fp);
ret = fwrite(buffer, buffer_len, 1, log_ctx->fp);
fflush(log_ctx->fp);
if (1 != fwrite(buffer, buffer_len, 1, log_ctx->fp)) {
/* Only the first error is logged */
if (!log_ctx->output_errors) {
SCLogError(SC_ERR_LOG_OUTPUT, "%s error while writing to %s",
ferror(log_ctx->fp) ? strerror(errno) : "unknown error",
log_ctx->filename);
}
log_ctx->output_errors++;
} else {
fflush(log_ctx->fp);
}
}
}
@ -285,6 +303,11 @@ static void SCLogFileCloseNoLock(LogFileCtx *log_ctx)
{
if (log_ctx->fp)
fclose(log_ctx->fp);
if (log_ctx->output_errors) {
SCLogError(SC_ERR_LOG_OUTPUT, "There were %" PRIu64 " output errors to %s",
log_ctx->output_errors, log_ctx->filename);
}
}
static void SCLogFileClose(LogFileCtx *log_ctx)

@ -148,6 +148,8 @@ typedef struct LogFileCtx_ {
/* Socket types may need to drop events to keep from blocking
* Suricata. */
uint64_t dropped;
uint64_t output_errors;
} LogFileCtx;
/* Min time (msecs) before trying to reconnect a Unix domain socket */

Loading…
Cancel
Save