Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

out_file: support csv format #241

Merged
merged 1 commit into from
Apr 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 54 additions & 5 deletions plugins/out_file/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

struct flb_file_conf {
char *out_file;
char *delimiter;
int format;
};

static int cb_file_init(struct flb_output_instance *ins,
Expand All @@ -50,18 +52,58 @@ static int cb_file_init(struct flb_output_instance *ins,
return -1;
}

conf->format = FLB_OUT_FILE_FMT_JSON;/* default */
conf->delimiter = ",";/* default */

/* Optional output file name/path */
tmp = flb_output_get_property("Path", ins);
if (tmp) {
conf->out_file = tmp;
}

/* Optional, file format */
tmp = flb_output_get_property("Format", ins);
if (tmp && !strcasecmp(tmp, "csv")){
conf->format = FLB_OUT_FILE_FMT_CSV;
}

tmp = flb_output_get_property("delimiter", ins);
if (tmp && !strcasecmp(tmp, "\\t")) {
conf->delimiter = "\t";
}
else if (tmp) {
conf->delimiter = tmp;
}

/* Set the context */
flb_output_set_context(ins, conf);

return 0;
}

static int csv_output(FILE *fp,
struct flb_time *tm,
msgpack_object *obj,
struct flb_file_conf *ctx)
{
msgpack_object_kv *kv = NULL;
int i;
int map_size;

if (obj->type == MSGPACK_OBJECT_MAP && obj->via.map.size > 0) {
kv = obj->via.map.ptr;
map_size = obj->via.map.size;
fprintf(fp, "%f%s", flb_time_to_double(tm), ctx->delimiter);
for (i=0; i<map_size-1; i++) {
msgpack_object_print(fp, (kv+i)->val);
fprintf(fp, "%s", ctx->delimiter);
}
msgpack_object_print(fp, (kv+(map_size-1))->val);
fprintf(fp, "\n");
}
return 0;
}

static void cb_file_flush(void *data, size_t bytes,
char *tag, int tag_len,
struct flb_input_instance *i_ins,
Expand Down Expand Up @@ -113,11 +155,18 @@ static void cb_file_flush(void *data, size_t bytes,
}

flb_time_pop_from_msgpack(&tm, &result, &obj);
if (flb_msgpack_obj_to_json(buf, alloc_size, obj) >= 0) {
fprintf(fp, "%s: [%f, %s]\n",
tag,
flb_time_to_double(&tm),
buf);
switch (ctx->format){
case FLB_OUT_FILE_FMT_JSON:
if (flb_msgpack_obj_to_json(buf, alloc_size, obj) >= 0) {
fprintf(fp, "%s: [%f, %s]\n",
tag,
flb_time_to_double(&tm),
buf);
}
break;
case FLB_OUT_FILE_FMT_CSV:
csv_output(fp, &tm, obj, ctx);
break;
}
flb_free(buf);
}
Expand Down
6 changes: 6 additions & 0 deletions plugins/out_file/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@
#ifndef FLB_OUT_FILE
#define FLB_OUT_FILE

enum {
FLB_OUT_FILE_FMT_JSON,
FLB_OUT_FILE_FMT_CSV,
FLB_OUT_FILE_FMT_OTHER,
};

#endif