1 // vim:ts=4:sw=4:expandtab
2 #include <config.h>
3 #include "i3status.h"
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <yajl/yajl_gen.h>
8 #include <yajl/yajl_version.h>
9
10 #include "i3status.h"
11
12 #define STRING_SIZE 10
13
print_load(load_ctx_t * ctx)14 void print_load(load_ctx_t *ctx) {
15 char *outwalk = ctx->buf;
16 /* Get load */
17
18 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__linux__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun) || defined(__DragonFly__)
19 double loadavg[3];
20 const char *selected_format = ctx->format;
21 bool colorful_output = false;
22
23 if (getloadavg(loadavg, 3) == -1)
24 goto error;
25
26 if (loadavg[0] >= ctx->max_threshold) {
27 START_COLOR("color_bad");
28 colorful_output = true;
29 if (ctx->format_above_threshold != NULL)
30 selected_format = ctx->format_above_threshold;
31 }
32
33 char string_loadavg_1[STRING_SIZE];
34 char string_loadavg_5[STRING_SIZE];
35 char string_loadavg_15[STRING_SIZE];
36
37 snprintf(string_loadavg_1, STRING_SIZE, "%1.2f", loadavg[0]);
38 snprintf(string_loadavg_5, STRING_SIZE, "%1.2f", loadavg[1]);
39 snprintf(string_loadavg_15, STRING_SIZE, "%1.2f", loadavg[2]);
40
41 placeholder_t placeholders[] = {
42 {.name = "%1min", .value = string_loadavg_1},
43 {.name = "%5min", .value = string_loadavg_5},
44 {.name = "%15min", .value = string_loadavg_15}};
45
46 const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
47 char *formatted = format_placeholders(selected_format, &placeholders[0], num);
48 OUTPUT_FORMATTED;
49 free(formatted);
50
51 if (colorful_output)
52 END_COLOR;
53
54 *outwalk = '\0';
55 OUTPUT_FULL_TEXT(ctx->buf);
56
57 return;
58 error:
59 #endif
60 OUTPUT_FULL_TEXT("cant read load");
61 (void)fputs("i3status: Cannot read system load using getloadavg()\n", stderr);
62 }
63