Skip to content

Commit 834ace4

Browse files
committed
in_tail: add support for charset decoding
With "from_encoding" parameter you can define input charset for in_tail plugin. Requires FLB_ICONV (flb_iconv) Signed-off-by: Jukka Pihl <[email protected]>
1 parent d17c93f commit 834ace4

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

plugins/in_tail/tail_config.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
#include <fluent-bit/flb_info.h>
2222
#include <fluent-bit/flb_log.h>
2323
#include <fluent-bit/flb_input.h>
24-
24+
#ifdef FLB_HAVE_ICONV
25+
#include <fluent-bit/flb_iconv.h>
26+
#endif
2527
#include <stdlib.h>
2628
#include <fcntl.h>
2729

@@ -44,6 +46,9 @@ struct flb_tail_config *flb_tail_config_create(struct flb_input_instance *i_ins,
4446
long nsec;
4547
ssize_t bytes;
4648
char *tmp;
49+
#ifdef FLB_HAVE_ICONV
50+
char *tmp2;
51+
#endif
4752
struct flb_tail_config *ctx;
4853

4954
ctx = flb_calloc(1, sizeof(struct flb_tail_config));
@@ -313,6 +318,27 @@ struct flb_tail_config *flb_tail_config_create(struct flb_input_instance *i_ins,
313318
}
314319
}
315320

321+
#ifdef FLB_HAVE_ICONV
322+
tmp = flb_input_get_property("from_encoding", i_ins);
323+
tmp2 = flb_input_get_property("encoding", i_ins);
324+
if(tmp) {
325+
if(!tmp2) {
326+
tmp2 = "UTF8";
327+
} else if(!strcasecmp(tmp2,"default")) {
328+
tmp2 = "";
329+
}
330+
if(!strcasecmp(tmp,"default")) {
331+
tmp = "";
332+
}
333+
ctx->iconvert = flb_iconv_open(tmp2,tmp);
334+
if(ctx->iconvert == NULL) {
335+
flb_error("[in_tail] cannot init iconv: '%s'=>'%s'", tmp, tmp2);
336+
}
337+
} else {
338+
ctx->iconvert = NULL;
339+
}
340+
#endif
341+
316342
#ifdef FLB_HAVE_METRICS
317343
flb_metrics_add(FLB_TAIL_METRIC_F_OPENED,
318344
"files_opened", ctx->i_ins->metrics);
@@ -344,6 +370,11 @@ int flb_tail_config_destroy(struct flb_tail_config *config)
344370
}
345371
#endif
346372

373+
#ifdef FLB_HAVE_ICONV
374+
if(config->iconvert) {
375+
iconv_close(config->iconvert);
376+
}
377+
#endif
347378
if (config->db != NULL) {
348379
flb_tail_db_close(config->db);
349380
}

plugins/in_tail/tail_config.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
#ifdef FLB_HAVE_REGEX
2929
#include <fluent-bit/flb_regex.h>
3030
#endif
31+
#ifdef FLB_HAVE_ICONV
32+
#include <iconv.h>
33+
#endif
34+
3135

3236
/* Metrics */
3337
#ifdef FLB_HAVE_METRICS
@@ -110,6 +114,10 @@ struct flb_tail_config {
110114

111115
/* Plugin input instance */
112116
struct flb_input_instance *i_ins;
117+
118+
#ifdef FLB_HAVE_ICONV
119+
struct flb_iconv *iconvert;
120+
#endif
113121
};
114122

115123
struct flb_tail_config *flb_tail_config_create(struct flb_input_instance *i_ins,

plugins/in_tail/tail_file.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#include <fluent-bit/flb_regex.h>
3232
#include <fluent-bit/flb_hash.h>
3333
#endif
34+
#ifdef FLB_HAVE_ICONV
35+
#include <fluent-bit/flb_iconv.h>
36+
#endif
3437

3538
#include "tail.h"
3639
#include "tail_file.h"
@@ -236,6 +239,10 @@ static int process_content(struct flb_tail_file *file, off_t *bytes)
236239
msgpack_sbuffer *out_sbuf;
237240
msgpack_packer *out_pck;
238241
struct flb_tail_config *ctx = file->config;
242+
#ifdef FLB_HAVE_ICONV
243+
char *iconv_data;
244+
size_t iconv_len;
245+
#endif
239246

240247
/* Create a temporal msgpack buffer */
241248
msgpack_sbuffer_init(&mp_sbuf);
@@ -278,6 +285,19 @@ static int process_content(struct flb_tail_file *file, off_t *bytes)
278285
line_len = len - crlf;
279286
repl_line = NULL;
280287

288+
#ifdef FLB_HAVE_ICONV
289+
iconv_data = NULL;
290+
line = data;
291+
line_len = len;
292+
if(ctx->iconvert) {
293+
ret = flb_iconv_execute(ctx->iconvert, data, len, &iconv_data, &iconv_len, FLB_ICONV_ACCEPT_NOT_CHANGED);
294+
if(ret == FLB_ICONV_SUCCESS) {
295+
line = iconv_data;
296+
line_len = iconv_len;
297+
}
298+
}
299+
#endif
300+
281301
if (ctx->docker_mode) {
282302
ret = flb_tail_dmode_process_content(now, line, line_len,
283303
&repl_line, &repl_line_len,
@@ -365,6 +385,11 @@ static int process_content(struct flb_tail_file *file, off_t *bytes)
365385
#endif
366386

367387
go_next:
388+
#ifdef FLB_HAVE_ICONV
389+
if(iconv_data) {
390+
flb_free(iconv_data);
391+
}
392+
#endif
368393
flb_free(repl_line);
369394
repl_line = NULL;
370395
/* Adjust counters */

0 commit comments

Comments
 (0)