Skip to content

Commit

Permalink
Merge pull request #3 from jdiez17/log_colors_option
Browse files Browse the repository at this point in the history
log, commands: Add log_colors command
  • Loading branch information
ddevault committed Aug 9, 2015
2 parents ec2fedf + 69edcb8 commit 9f554b1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
17 changes: 17 additions & 0 deletions sway/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,30 @@ int cmd_set(struct sway_config *config, int argc, char **argv) {
return 0;
}

int cmd_log_colors(struct sway_config *config, int argc, char **argv) {
if (argc != 1) {
sway_log(L_ERROR, "Invalid log_colors command (expected 1 argument, got %d)", argc);
return 1;
}

if (strcasecmp(argv[0], "no") != 0 && strcasecmp(argv[0], "yes") != 0) {
sway_log(L_ERROR, "Invalid log_colors command (expected `yes` or `no`, got '%s')", argv[0]);
return 1;
}

sway_log_colors(!strcasecmp(argv[0], "yes"));
return 0;
}


/* Keep alphabetized */
struct cmd_handler handlers[] = {
{ "bindsym", cmd_bindsym },
{ "exec", cmd_exec },
{ "exit", cmd_exit },
{ "focus_follows_mouse", cmd_focus_follows_mouse },
{ "layout", cmd_layout },
{ "log_colors", cmd_log_colors },
{ "set", cmd_set },
};

Expand Down
15 changes: 13 additions & 2 deletions sway/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ void init_log(int verbosity) {
v = verbosity;
}

void sway_log_colors(int mode) {
colored = (mode == 1) ? 1 : 0;
}

void sway_abort(char *format, ...) {
fprintf(stderr, "ERROR: ");
va_list args;
Expand All @@ -33,11 +37,18 @@ void sway_log(int verbosity, char* format, ...) {
if (c > sizeof(verbosity_colors) / sizeof(char *)) {
c = sizeof(verbosity_colors) / sizeof(char *) - 1;
}
fprintf(stderr, verbosity_colors[c]);

if (colored) {
fprintf(stderr, verbosity_colors[c]);
}

va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
fprintf(stderr, "\x1B[0m\n");

if (colored) {
fprintf(stderr, "\x1B[0m\n");
}
}
}
1 change: 1 addition & 0 deletions sway/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ typedef enum {
} log_importance_t;

void init_log(int verbosity);
void sway_log_colors(int mode);
void sway_log(int verbosity, char* format, ...);
void sway_abort(char* format, ...);

Expand Down

0 comments on commit 9f554b1

Please sign in to comment.