]> git.sur5r.net Git - i3/i3status/blob - src/print_load.c
clang-format-3.5 -i **/*.[ch], update modeline
[i3/i3status] / src / print_load.c
1 // vim:ts=4:sw=4:expandtab
2 #include "i3status.h"
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_version.h>
8
9 void print_load(yajl_gen json_gen, char *buffer, const char *format, const float max_threshold) {
10     char *outwalk = buffer;
11 /* Get load */
12
13 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(linux) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun) || defined(__DragonFly__)
14     double loadavg[3];
15     const char *walk;
16     bool colorful_output = false;
17
18     if (getloadavg(loadavg, 3) == -1)
19         goto error;
20
21     for (walk = format; *walk != '\0'; walk++) {
22         if (*walk != '%') {
23             *(outwalk++) = *walk;
24             continue;
25         }
26         if (loadavg[0] >= max_threshold) {
27             START_COLOR("color_bad");
28             colorful_output = true;
29         }
30
31         if (BEGINS_WITH(walk + 1, "1min")) {
32             outwalk += sprintf(outwalk, "%1.2f", loadavg[0]);
33             walk += strlen("1min");
34         }
35
36         if (BEGINS_WITH(walk + 1, "5min")) {
37             outwalk += sprintf(outwalk, "%1.2f", loadavg[1]);
38             walk += strlen("5min");
39         }
40
41         if (BEGINS_WITH(walk + 1, "15min")) {
42             outwalk += sprintf(outwalk, "%1.2f", loadavg[2]);
43             walk += strlen("15min");
44         }
45         if (colorful_output)
46             END_COLOR;
47     }
48
49     *outwalk = '\0';
50     OUTPUT_FULL_TEXT(buffer);
51
52     return;
53 error:
54 #endif
55     OUTPUT_FULL_TEXT("cant read load");
56     (void)fputs("i3status: Cannot read system load using getloadavg()\n", stderr);
57 }