Skip to content

Commit 32ebcaa

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 5e034c2 commit 32ebcaa

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

plugins/in_tail/tail_config.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
#include <fluent-bit/flb_info.h>
2222
#include <fluent-bit/flb_input_plugin.h>
23-
23+
#ifdef FLB_HAVE_ICONV
24+
#include <fluent-bit/flb_iconv.h>
25+
#endif
2426
#include <stdlib.h>
2527
#include <fcntl.h>
2628

@@ -42,6 +44,10 @@ struct flb_tail_config *flb_tail_config_create(struct flb_input_instance *ins,
4244
int i;
4345
long nsec;
4446
const char *tmp;
47+
ssize_t bytes;
48+
#ifdef FLB_HAVE_ICONV
49+
char *tmp2;
50+
#endif
4551
struct flb_tail_config *ctx;
4652

4753
ctx = flb_calloc(1, sizeof(struct flb_tail_config));
@@ -237,6 +243,27 @@ struct flb_tail_config *flb_tail_config_create(struct flb_input_instance *ins,
237243
}
238244
#endif
239245

246+
#ifdef FLB_HAVE_ICONV
247+
tmp = flb_input_get_property("from_encoding", i_ins);
248+
tmp2 = flb_input_get_property("encoding", i_ins);
249+
if(tmp) {
250+
if(!tmp2) {
251+
tmp2 = "UTF8";
252+
} else if(!strcasecmp(tmp2,"default")) {
253+
tmp2 = "";
254+
}
255+
if(!strcasecmp(tmp,"default")) {
256+
tmp = "";
257+
}
258+
ctx->iconvert = flb_iconv_open(tmp2,tmp);
259+
if(ctx->iconvert == NULL) {
260+
flb_error("[in_tail] cannot init iconv: '%s'=>'%s'", tmp, tmp2);
261+
}
262+
} else {
263+
ctx->iconvert = NULL;
264+
}
265+
#endif
266+
240267
#ifdef FLB_HAVE_METRICS
241268
flb_metrics_add(FLB_TAIL_METRIC_F_OPENED,
242269
"files_opened", ctx->ins->metrics);
@@ -268,7 +295,13 @@ int flb_tail_config_destroy(struct flb_tail_config *config)
268295
}
269296
#endif
270297

271-
#ifdef FLB_HAVE_SQLDB
298+
#ifdef FLB_HAVE_ICONV
299+
if(config->iconvert) {
300+
iconv_close(config->iconvert);
301+
}
302+
#endif
303+
304+
#ifdef FLB_HAVE_SQLDB
272305
if (config->db != NULL) {
273306
flb_tail_db_close(config->db);
274307
}

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
@@ -111,6 +115,10 @@ struct flb_tail_config {
111115

112116
/* Plugin input instance */
113117
struct flb_input_instance *ins;
118+
119+
#ifdef FLB_HAVE_ICONV
120+
struct flb_iconv *iconvert;
121+
#endif
114122
};
115123

116124
struct flb_tail_config *flb_tail_config_create(struct flb_input_instance *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)