utils: new function to print human readable sizes
@@ -403,3 +403,28 @@ int flb_utils_time_split(char *time, int *sec, long *nsec)
return 0;
}
+
+void flb_utils_bytes_to_human_readable_size(size_t bytes,
+ char *out_buf, size_t size)
+{
+ unsigned long i;
+ unsigned long u = 1024;
+ static const char *__units[] = {
+ "b", "K", "M", "G",
+ "T", "P", "E", "Z", "Y", NULL
+ };
+
+ for (i = 0; __units[i] != NULL; i++) {
+ if ((bytes / u) == 0) {
+ break;
+ }
+ u *= 1024;
+ }
+ if (!i) {
+ snprintf(out_buf, size, "%lu%s", (long unsigned int) bytes, __units[0]);
+ }
+ else {
+ float fsize = (float) ((double) bytes / (u / 1024));
+ snprintf(out_buf, size, "%.1f%s", fsize, __units[i]);
+ }
+}
