cpup_usage: param struct
This commit is contained in:
parent
9a6f96b309
commit
6b2f4cd20c
13
i3status.c
13
i3status.c
@ -854,7 +854,18 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
CASE_SEC("cpu_usage") {
|
||||
SEC_OPEN_MAP("cpu_usage");
|
||||
print_cpu_usage(json_gen, buffer, cfg_getstr(sec, "format"), cfg_getstr(sec, "format_above_threshold"), cfg_getstr(sec, "format_above_degraded_threshold"), cfg_getstr(sec, "path"), cfg_getfloat(sec, "max_threshold"), cfg_getfloat(sec, "degraded_threshold"));
|
||||
cpu_usage_ctx_t ctx = {
|
||||
.json_gen = json_gen,
|
||||
.buf = buffer,
|
||||
.buflen = sizeof(buffer),
|
||||
.format = cfg_getstr(sec, "format"),
|
||||
.format_above_threshold = cfg_getstr(sec, "format_above_threshold"),
|
||||
.format_above_degraded_threshold = cfg_getstr(sec, "format_above_degraded_threshold"),
|
||||
.path = cfg_getstr(sec, "path"),
|
||||
.max_threshold = cfg_getfloat(sec, "max_threshold"),
|
||||
.degraded_threshold = cfg_getfloat(sec, "degraded_threshold"),
|
||||
};
|
||||
print_cpu_usage(&ctx);
|
||||
SEC_CLOSE_MAP;
|
||||
}
|
||||
|
||||
|
@ -324,7 +324,20 @@ typedef struct {
|
||||
void print_path_exists(path_exists_ctx_t *ctx);
|
||||
|
||||
void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, const char *format_above_threshold, int);
|
||||
void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const char *format_above_threshold, const char *format_above_degraded_threshold, const char *path, const float max_threshold, const float degraded_threshold);
|
||||
|
||||
typedef struct {
|
||||
yajl_gen json_gen;
|
||||
char *buf;
|
||||
const size_t buflen;
|
||||
const char *format;
|
||||
const char *format_above_threshold;
|
||||
const char *format_above_degraded_threshold;
|
||||
const char *path;
|
||||
const float max_threshold;
|
||||
const float degraded_threshold;
|
||||
} cpu_usage_ctx_t;
|
||||
|
||||
void print_cpu_usage(cpu_usage_ctx_t *ctx);
|
||||
|
||||
typedef struct {
|
||||
yajl_gen json_gen;
|
||||
|
@ -58,13 +58,14 @@ static struct cpu_usage *curr_cpus = NULL;
|
||||
* percentage.
|
||||
*
|
||||
*/
|
||||
void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const char *format_above_threshold, const char *format_above_degraded_threshold, const char *path, const float max_threshold, const float degraded_threshold) {
|
||||
const char *selected_format = format;
|
||||
void print_cpu_usage(cpu_usage_ctx_t *ctx) {
|
||||
const char *selected_format = ctx->format;
|
||||
const char *walk;
|
||||
char *outwalk = buffer;
|
||||
char *outwalk = ctx->buf;
|
||||
struct cpu_usage curr_all = {0, 0, 0, 0, 0};
|
||||
int diff_idle, diff_total, diff_usage;
|
||||
bool colorful_output = false;
|
||||
#define json_gen ctx->json_gen
|
||||
|
||||
#if defined(__linux__)
|
||||
|
||||
@ -79,9 +80,9 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const
|
||||
}
|
||||
|
||||
memcpy(curr_cpus, prev_cpus, cpu_count * sizeof(struct cpu_usage));
|
||||
FILE *f = fopen(path, "r");
|
||||
FILE *f = fopen(ctx->path, "r");
|
||||
if (f == NULL) {
|
||||
fprintf(stderr, "i3status: open %s: %s\n", path, strerror(errno));
|
||||
fprintf(stderr, "i3status: open %s: %s\n", ctx->path, strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
curr_cpu_count = get_nprocs();
|
||||
@ -160,16 +161,16 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const
|
||||
goto error;
|
||||
#endif
|
||||
|
||||
if (diff_usage >= max_threshold) {
|
||||
if (diff_usage >= ctx->max_threshold) {
|
||||
START_COLOR("color_bad");
|
||||
colorful_output = true;
|
||||
if (format_above_threshold != NULL)
|
||||
selected_format = format_above_threshold;
|
||||
} else if (diff_usage >= degraded_threshold) {
|
||||
if (ctx->format_above_threshold != NULL)
|
||||
selected_format = ctx->format_above_threshold;
|
||||
} else if (diff_usage >= ctx->degraded_threshold) {
|
||||
START_COLOR("color_degraded");
|
||||
colorful_output = true;
|
||||
if (format_above_degraded_threshold != NULL)
|
||||
selected_format = format_above_degraded_threshold;
|
||||
if (ctx->format_above_degraded_threshold != NULL)
|
||||
selected_format = ctx->format_above_degraded_threshold;
|
||||
}
|
||||
|
||||
for (walk = selected_format; *walk != '\0'; walk++) {
|
||||
@ -210,7 +211,7 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const
|
||||
if (colorful_output)
|
||||
END_COLOR;
|
||||
|
||||
OUTPUT_FULL_TEXT(buffer);
|
||||
OUTPUT_FULL_TEXT(ctx->buf);
|
||||
return;
|
||||
error:
|
||||
OUTPUT_FULL_TEXT("cant read cpu usage");
|
||||
|
Loading…
Reference in New Issue
Block a user