summaryrefslogtreecommitdiff
path: root/src/print_cpu_usage.c
diff options
context:
space:
mode:
authorMichael Stapelberg <michael@stapelberg.de>2012-03-25 20:55:55 +0200
committerMichael Stapelberg <michael@stapelberg.de>2012-03-25 22:05:00 +0200
commit12b1bfa9b8485de88b0bda82821c021aee197673 (patch)
treeb6d87f7ed5876109863a6599992e351509a30d58 /src/print_cpu_usage.c
parentafb0525235c57fc4896633003a45bfb013af38d2 (diff)
Properly output JSON with libyajl
Diffstat (limited to 'src/print_cpu_usage.c')
-rw-r--r--src/print_cpu_usage.c15
1 files changed, 6 insertions, 9 deletions
diff --git a/src/print_cpu_usage.c b/src/print_cpu_usage.c
index d97a7fa..24afca7 100644
--- a/src/print_cpu_usage.c
+++ b/src/print_cpu_usage.c
@@ -3,6 +3,7 @@
#include <limits.h>
#include <stdio.h>
#include <string.h>
+#include <yajl/yajl_gen.h>
#ifdef __FreeBSD__
#include <sys/types.h>
@@ -20,15 +21,13 @@ static int prev_idle = 0;
* percentage.
*
*/
-void print_cpu_usage(const char *format) {
+void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format) {
const char *walk;
+ char *outwalk = buffer;
char buf[1024];
int curr_user = 0, curr_nice = 0, curr_system = 0, curr_idle = 0, curr_total;
int diff_idle, diff_total, diff_usage;
- if (output_format == O_I3BAR)
- printf("{\"name\":\"cpu_usage\", \"full_text\":\"");
-
#if defined(LINUX)
static char statpath[512];
strcpy(statpath, "/proc/stat");
@@ -64,19 +63,17 @@ void print_cpu_usage(const char *format) {
#endif
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
- putchar(*walk);
+ *(outwalk++) = *walk;
continue;
}
if (strncmp(walk+1, "usage", strlen("usage")) == 0) {
- printf("%02d%%", diff_usage);
+ outwalk += sprintf(outwalk, "%02d%%", diff_usage);
walk += strlen("usage");
}
}
- if (output_format == O_I3BAR)
- printf("\"}");
-
+ OUTPUT_FULL_TEXT(buffer);
return;
error:
(void)fputs("Cannot read usage\n", stderr);