Use function for json error messages

This commit is contained in:
Leon Henrik Plickat 2021-06-21 16:10:46 +02:00
parent fec8a5db3d
commit ec680722d4

33
wlopm.c
View File

@ -201,6 +201,17 @@ static char *power_mode_to_string (enum zwlr_output_power_v1_mode mode)
return mode == ZWLR_OUTPUT_POWER_V1_MODE_ON ? "on" : "off"; return mode == ZWLR_OUTPUT_POWER_V1_MODE_ON ? "on" : "off";
} }
static void print_json_error (const char *output_name, const char *msg)
{
fprintf(stdout,
"%s\n {\n"
" \"output\": \"%s\",\n"
" \"error\": \"%s\"\n"
" }",
json_prev ? "," : "", output_name, msg);
json_prev = true;
}
static void do_operation (struct Operation *operation) static void do_operation (struct Operation *operation)
{ {
if ( *operation->name == '*' ) if ( *operation->name == '*' )
@ -215,16 +226,7 @@ static void do_operation (struct Operation *operation)
if ( output == NULL ) if ( output == NULL )
{ {
if (json) if (json)
{ print_json_error(operation->name, "output does not exist");
fprintf(stdout,
"%s\n {\n"
" \"output\": \"%s\",\n"
" \"error\": \"output does not exist\"\n"
" }",
json_prev ? "," : "",
operation->name);
json_prev = true;
}
else else
fprintf(stderr, "ERROR: Output '%s' does not exist.\n", fprintf(stderr, "ERROR: Output '%s' does not exist.\n",
operation->name); operation->name);
@ -338,16 +340,7 @@ static void sync_handle_done (void *data, struct wl_callback *wl_callback, uint3
if (output->operation_failed) if (output->operation_failed)
{ {
if (json) if (json)
{ print_json_error(output->name, "setting power mode failed");
fprintf(stdout,
"%s\n {\n"
" \"output\": \"%s\","
" \"error\": \"setting power mode failed\"\n"
" }",
json_prev ? "," : "",
output->name);
json_prev = true;
}
else else
fprintf(stderr, "ERROR: Setting power mode for output '%s' failed.\n", fprintf(stderr, "ERROR: Setting power mode for output '%s' failed.\n",
output->name); output->name);