Skip to content

Commit dea7701

Browse files
committed
in_syslog: support for input encoding to utf8
* encoding uses flb_encoding * refactoring syslog_prot_process and syslog_prot_process_udp to use syslog_prot_process_msg per message. Signed-off-by: Jukka Pihl <[email protected]>
1 parent ddc7b38 commit dea7701

File tree

3 files changed

+83
-43
lines changed

3 files changed

+83
-43
lines changed

plugins/in_syslog/syslog.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#include <fluent-bit/flb_info.h>
2525
#include <fluent-bit/flb_input.h>
2626

27+
#ifdef FLB_HAVE_UTF8_ENCODER
28+
#include <fluent-bit/flb_encoding.h>
29+
#endif
30+
2731
/* Syslog modes */
2832
#define FLB_SYSLOG_UNIX_TCP 1
2933
#define FLB_SYSLOG_UNIX_UDP 2
@@ -63,6 +67,11 @@ struct flb_syslog {
6367
struct mk_list connections;
6468
struct mk_event_loop *evl;
6569
struct flb_input_instance *ins;
70+
71+
#ifdef FLB_HAVE_UTF8_ENCODER
72+
struct flb_encoding *encoding;
73+
#endif
74+
6675
};
6776

6877
#endif

plugins/in_syslog/syslog_conf.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,19 @@ struct flb_syslog *syslog_conf_create(struct flb_input_instance *ins,
134134
return NULL;
135135
}
136136

137+
#ifdef FLB_HAVE_UTF8_ENCODER
138+
/* utf8 encoder */
139+
tmp = flb_input_get_property("encoding", ins);
140+
if (tmp) {
141+
ctx->encoding = flb_encoding_open(tmp);
142+
if (!ctx->encoding) {
143+
flb_error("[in_syslog] illegal encoding: %s", tmp);
144+
syslog_conf_destroy(ctx);
145+
return NULL;
146+
}
147+
}
148+
#endif
149+
137150
return ctx;
138151
}
139152

@@ -143,6 +156,13 @@ int syslog_conf_destroy(struct flb_syslog *ctx)
143156
flb_free(ctx->buffer_data);
144157
ctx->buffer_data = NULL;
145158
}
159+
160+
#ifdef FLB_HAVE_UTF8_ENCODER
161+
if(ctx->encoding) {
162+
flb_encoding_close(ctx->encoding);
163+
}
164+
#endif
165+
146166
syslog_server_destroy(ctx);
147167
flb_free(ctx);
148168

plugins/in_syslog/syslog_prot.c

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,64 @@ static inline int pack_line(struct flb_syslog *ctx,
5252
return 0;
5353
}
5454

55+
56+
int syslog_prot_process_msg(struct flb_syslog *ctx, char *p, size_t len)
57+
{
58+
int ret;
59+
void *out_buf;
60+
size_t out_size;
61+
struct flb_time out_time;
62+
#ifdef FLB_HAVE_UTF8_ENCODER
63+
char *decoded = NULL;
64+
size_t decoded_size;
65+
#endif
66+
67+
#ifdef FLB_HAVE_UTF8_ENCODER
68+
if (ctx->encoding) {
69+
ret = flb_encoding_decode(ctx->encoding, p, len, &decoded, &decoded_size);
70+
if (ret != FLB_ENCODING_SUCCESS) {
71+
flb_plg_error(ctx->ins, "decoding failed '%.*s'", p, len);
72+
goto finish;
73+
}
74+
p = decoded;
75+
len = decoded_size;
76+
}
77+
#endif
78+
79+
/* Process the string */
80+
ret = flb_parser_do(ctx->parser, p, len,
81+
&out_buf, &out_size, &out_time);
82+
if (ret < 0) {
83+
flb_plg_warn(ctx->ins, "error parsing log message with parser '%s'",
84+
ctx->parser->name);
85+
flb_plg_debug(ctx->ins, "unparsed log message: %.*s", len, p);
86+
goto finish;
87+
}
88+
89+
if (flb_time_to_double(&out_time) == 0.0) {
90+
flb_time_get(&out_time);
91+
}
92+
93+
pack_line(ctx, &out_time, out_buf, out_size);
94+
ret = 0;
95+
96+
finish:
97+
98+
#ifdef FLB_HAVE_UTF8_ENCODER
99+
if (decoded) {
100+
flb_free(decoded);
101+
}
102+
#endif
103+
return ret;
104+
105+
}
106+
55107
int syslog_prot_process(struct syslog_conn *conn)
56108
{
57109
int len;
58-
int ret;
59110
char *p;
60111
char *eof;
61112
char *end;
62-
void *out_buf;
63-
size_t out_size;
64-
struct flb_time out_time;
65-
struct flb_syslog *ctx = conn->ctx;
66113

67114
eof = conn->buf_data;
68115
end = conn->buf_data + conn->buf_len;
@@ -96,21 +143,7 @@ int syslog_prot_process(struct syslog_conn *conn)
96143
continue;
97144
}
98145

99-
/* Process the string */
100-
ret = flb_parser_do(ctx->parser, p, len,
101-
&out_buf, &out_size, &out_time);
102-
if (ret >= 0) {
103-
if (flb_time_to_double(&out_time) == 0.0) {
104-
flb_time_get(&out_time);
105-
}
106-
pack_line(ctx, &out_time, out_buf, out_size);
107-
flb_free(out_buf);
108-
}
109-
else {
110-
flb_plg_warn(ctx->ins, "error parsing log message with parser '%s'",
111-
ctx->parser->name);
112-
flb_plg_debug(ctx->ins, "unparsed log message: %.*s", len, p);
113-
}
146+
syslog_prot_process_msg(conn->ctx, p, len);
114147

115148
conn->buf_parsed += len + 1;
116149
end = conn->buf_data + conn->buf_len;
@@ -129,27 +162,5 @@ int syslog_prot_process(struct syslog_conn *conn)
129162

130163
int syslog_prot_process_udp(char *buf, size_t size, struct flb_syslog *ctx)
131164
{
132-
int ret;
133-
void *out_buf;
134-
size_t out_size;
135-
struct flb_time out_time = {0};
136-
137-
ret = flb_parser_do(ctx->parser, buf, size,
138-
&out_buf, &out_size, &out_time);
139-
if (ret >= 0) {
140-
if (flb_time_to_double(&out_time) == 0) {
141-
flb_time_get(&out_time);
142-
}
143-
pack_line(ctx, &out_time, out_buf, out_size);
144-
flb_free(out_buf);
145-
}
146-
else {
147-
flb_plg_warn(ctx->ins, "error parsing log message with parser '%s'",
148-
ctx->parser->name);
149-
flb_plg_debug(ctx->ins, "unparsed log message: %.*s",
150-
(int) size, buf);
151-
return -1;
152-
}
153-
154-
return 0;
165+
return syslog_prot_process_msg(ctx, buf, size);
155166
}

0 commit comments

Comments
 (0)