Skip to content

Commit c6f5c1b

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 5ac6a3d commit c6f5c1b

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

plugins/in_tail/tail_config.c

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
#include <fluent-bit/flb_info.h>
2424
#include <fluent-bit/flb_log.h>
2525
#include <fluent-bit/flb_input.h>
26-
26+
#ifdef FLB_HAVE_ICONV
27+
#include <fluent-bit/flb_iconv.h>
28+
#endif
2729
#include <stdlib.h>
2830
#include <fcntl.h>
2931

@@ -46,6 +48,9 @@ struct flb_tail_config *flb_tail_config_create(struct flb_input_instance *i_ins,
4648
long nsec;
4749
ssize_t bytes;
4850
const char *tmp;
51+
#ifdef FLB_HAVE_ICONV
52+
char *tmp2;
53+
#endif
4954
struct flb_tail_config *ctx;
5055

5156
ctx = flb_calloc(1, sizeof(struct flb_tail_config));
@@ -321,6 +326,27 @@ struct flb_tail_config *flb_tail_config_create(struct flb_input_instance *i_ins,
321326
}
322327
#endif
323328

329+
#ifdef FLB_HAVE_ICONV
330+
tmp = flb_input_get_property("from_encoding", i_ins);
331+
tmp2 = flb_input_get_property("encoding", i_ins);
332+
if(tmp) {
333+
if(!tmp2) {
334+
tmp2 = "UTF8";
335+
} else if(!strcasecmp(tmp2,"default")) {
336+
tmp2 = "";
337+
}
338+
if(!strcasecmp(tmp,"default")) {
339+
tmp = "";
340+
}
341+
ctx->iconvert = flb_iconv_open(tmp2,tmp);
342+
if(ctx->iconvert == NULL) {
343+
flb_error("[in_tail] cannot init iconv: '%s'=>'%s'", tmp, tmp2);
344+
}
345+
} else {
346+
ctx->iconvert = NULL;
347+
}
348+
#endif
349+
324350
#ifdef FLB_HAVE_METRICS
325351
flb_metrics_add(FLB_TAIL_METRIC_F_OPENED,
326352
"files_opened", ctx->i_ins->metrics);
@@ -352,7 +378,12 @@ int flb_tail_config_destroy(struct flb_tail_config *config)
352378
}
353379
#endif
354380

355-
#ifdef FLB_HAVE_SQLDB
381+
#ifdef FLB_HAVE_ICONV
382+
if(config->iconvert) {
383+
iconv_close(config->iconvert);
384+
}
385+
#endif
386+
#ifdef FLB_HAVE_SQLDB
356387
if (config->db != NULL) {
357388
flb_tail_db_close(config->db);
358389
}

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
@@ -112,6 +116,10 @@ struct flb_tail_config {
112116

113117
/* Plugin input instance */
114118
struct flb_input_instance *i_ins;
119+
120+
#ifdef FLB_HAVE_ICONV
121+
struct flb_iconv *iconvert;
122+
#endif
115123
};
116124

117125
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)