util-logopenfile: add write function

Introduce a function LogFileWrite that will handle the writing with
respect of the type defined in the configuration. This is used in
this patch to remove the write complexity from output-json.
pull/1712/head
Eric Leblond 11 years ago committed by Victor Julien
parent 60ea49c777
commit a13be67b5e

@ -119,10 +119,6 @@ void OutputJsonRegisterTests (void)
#define OUTPUT_BUFFER_SIZE 65535
#ifndef OS_WIN32
static int alert_syslog_level = DEFAULT_ALERT_SYSLOG_LEVEL;
#endif /* OS_WIN32 */
TmEcode OutputJson (ThreadVars *, Packet *, void *, PacketQueue *, PacketQueue *);
TmEcode OutputJsonThreadInit(ThreadVars *, void *, void **);
TmEcode OutputJsonThreadDeinit(ThreadVars *, void *);
@ -361,56 +357,8 @@ int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer *buffer)
if (unlikely(js_s == NULL))
return TM_ECODE_OK;
SCMutexLock(&file_ctx->fp_mutex);
if (file_ctx->type == LOGFILE_TYPE_SYSLOG)
{
if (file_ctx->prefix != NULL)
{
syslog(alert_syslog_level, "%s%s", file_ctx->prefix, js_s);
}
else
{
syslog(alert_syslog_level, "%s", js_s);
}
}
else if (file_ctx->type == LOGFILE_TYPE_FILE ||
file_ctx->type == LOGFILE_TYPE_UNIX_DGRAM ||
file_ctx->type == LOGFILE_TYPE_UNIX_STREAM)
{
if (file_ctx->prefix != NULL)
{
MemBufferWriteString(buffer, "%s%s\n", file_ctx->prefix, js_s);
}
else
{
MemBufferWriteString(buffer, "%s\n", js_s);
}
file_ctx->Write((const char *)MEMBUFFER_BUFFER(buffer),
MEMBUFFER_OFFSET(buffer), file_ctx);
}
#if HAVE_LIBHIREDIS
else if (file_ctx->type == LOGFILE_TYPE_REDIS) {
/* FIXME go async here */
redisReply *reply = redisCommand(file_ctx->redis, "%s %s %s",
file_ctx->redis_setup.command,
file_ctx->redis_setup.key,
js_s);
switch (reply->type) {
case REDIS_REPLY_ERROR:
SCLogWarning(SC_WARN_NO_UNITTESTS, "Redis error: %s", reply->str);
break;
case REDIS_REPLY_INTEGER:
SCLogDebug("Redis integer %lld", reply->integer);
break;
default:
SCLogError(SC_ERR_INVALID_VALUE,
"Redis default triggered with %d", reply->type);
break;
}
freeReplyObject(reply);
}
#endif
SCMutexUnlock(&file_ctx->fp_mutex);
LogFileWrite(file_ctx, buffer, js_s, strlen(js_s));
free(js_s);
return 0;
}
@ -577,7 +525,7 @@ OutputCtx *OutputJsonInitCtx(ConfNode *conf)
if (level_s != NULL) {
int level = SCMapEnumNameToValue(level_s, SCSyslogGetLogLevelMap());
if (level != -1) {
alert_syslog_level = level;
json_ctx->file_ctx->syslog_setup.alert_syslog_level = level;
}
}

@ -387,3 +387,43 @@ int LogFileFreeCtx(LogFileCtx *lf_ctx)
SCReturnInt(1);
}
int LogFileWrite(LogFileCtx *file_ctx, MemBuffer *buffer, char *string, size_t string_len)
{
SCMutexLock(&file_ctx->fp_mutex);
if (file_ctx->type == LOGFILE_TYPE_SYSLOG) {
syslog(file_ctx->syslog_setup.alert_syslog_level, "%s", string);
} else if (file_ctx->type == LOGFILE_TYPE_FILE ||
file_ctx->type == LOGFILE_TYPE_UNIX_DGRAM ||
file_ctx->type == LOGFILE_TYPE_UNIX_STREAM)
{
MemBufferWriteString(buffer, "%s\n", string);
file_ctx->Write((const char *)MEMBUFFER_BUFFER(buffer),
MEMBUFFER_OFFSET(buffer), file_ctx);
}
#if HAVE_LIBHIREDIS
else if (file_ctx->type == LOGFILE_TYPE_REDIS) {
/* FIXME go async here */
redisReply *reply = redisCommand(file_ctx->redis, "%s %s %s",
file_ctx->redis_setup.command,
file_ctx->redis_setup.key,
string);
switch (reply->type) {
case REDIS_REPLY_ERROR:
SCLogWarning(SC_WARN_NO_UNITTESTS, "Redis error: %s", reply->str);
break;
case REDIS_REPLY_INTEGER:
SCLogDebug("Redis integer %lld", reply->integer);
break;
default:
SCLogError(SC_ERR_INVALID_VALUE,
"Redis default triggered with %d", reply->type);
break;
}
freeReplyObject(reply);
}
#endif
SCMutexUnlock(&file_ctx->fp_mutex);
return 0;
}

@ -26,6 +26,7 @@
#include "conf.h" /* ConfNode */
#include "tm-modules.h" /* LogFileCtx */
#include "util-buffer.h"
#ifdef HAVE_LIBHIREDIS
#include "hiredis/hiredis.h"
@ -41,6 +42,10 @@ enum LogFileType { LOGFILE_TYPE_FILE,
LOGFILE_TYPE_UNIX_STREAM,
LOGFILE_TYPE_REDIS };
typedef struct SyslogSetup_ {
int alert_syslog_level;
} SyslogSetup;
#ifdef HAVE_LIBHIREDIS
enum RedisMode { REDIS_LIST, REDIS_CHANNEL };
@ -62,9 +67,12 @@ typedef struct LogFileCtx_ {
#endif
};
union {
SyslogSetup syslog_setup;
#ifdef HAVE_LIBHIREDIS
RedisSetup redis_setup;
RedisSetup redis_setup;
#endif
};
int (*Write)(const char *buffer, int buffer_len, struct LogFileCtx_ *fp);
void (*Close)(struct LogFileCtx_ *fp);
@ -115,6 +123,7 @@ typedef struct LogFileCtx_ {
LogFileCtx *LogFileNewCtx(void);
int LogFileFreeCtx(LogFileCtx *);
int LogFileWrite(LogFileCtx *file_ctx, MemBuffer *buffer, char *string, size_t string_len);
int SCConfLogOpenGeneric(ConfNode *conf, LogFileCtx *, const char *, int);
int SCConfLogReopen(LogFileCtx *);

Loading…
Cancel
Save