out_file: use different API to do msgpack to json conversion
@@ -209,22 +209,23 @@ static void cb_file_flush(void *data, size_t bytes,
while (msgpack_unpack_next(&result, data, bytes, &off)) {
alloc_size = (off - last_off) + 128; /* JSON is larger than msgpack */
last_off = off;
- buf = (char *)flb_calloc(1, alloc_size);
- if (buf == NULL) {
- flb_errno();
- msgpack_unpacked_destroy(&result);
- fclose(fp);
- FLB_OUTPUT_RETURN(FLB_RETRY);
- }
flb_time_pop_from_msgpack(&tm, &result, &obj);
+
switch (ctx->format){
case FLB_OUT_FILE_FMT_JSON:
- if (flb_msgpack_obj_to_json(buf, alloc_size, obj) >= 0) {
+ buf = flb_msgpack_to_json_str(alloc_size, &result);
+ if (buf) {
fprintf(fp, "%s: [%f, %s]\n",
tag,
flb_time_to_double(&tm),
buf);
+ flb_free(buf);
+ }
+ else {
+ msgpack_unpacked_destroy(&result);
+ fclose(fp);
+ FLB_OUTPUT_RETURN(FLB_RETRY);
}
break;
case FLB_OUT_FILE_FMT_CSV:
@@ -234,10 +235,8 @@ static void cb_file_flush(void *data, size_t bytes,
ltsv_output(fp, &tm, obj, ctx);
break;
}
- flb_free(buf);
}
msgpack_unpacked_destroy(&result);
-
fclose(fp);
FLB_OUTPUT_RETURN(FLB_OK);
