2015-08-09 07:01:22 +10:00
|
|
|
#include "log.h"
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-08-13 17:24:03 +10:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2015-08-09 07:01:22 +10:00
|
|
|
|
|
|
|
int colored = 1;
|
|
|
|
int v = 0;
|
|
|
|
|
|
|
|
const char *verbosity_colors[] = {
|
|
|
|
"", // L_SILENT
|
|
|
|
"\x1B[1;31m", // L_ERROR
|
|
|
|
"\x1B[1;34m", // L_INFO
|
|
|
|
"\x1B[1;30m", // L_DEBUG
|
|
|
|
};
|
|
|
|
|
|
|
|
void init_log(int verbosity) {
|
|
|
|
v = verbosity;
|
2015-08-13 17:24:03 +10:00
|
|
|
/* set FD_CLOEXEC flag to prevent programs called with exec to write into
|
|
|
|
* logs */
|
|
|
|
int i, flag;
|
|
|
|
int fd[] = { STDOUT_FILENO, STDIN_FILENO, STDERR_FILENO };
|
|
|
|
for (i = 0; i < 3; ++i) {
|
|
|
|
flag = fcntl(fd[i], F_GETFD);
|
|
|
|
if (flag != -1) {
|
|
|
|
fcntl(fd[i], F_SETFD, flag | FD_CLOEXEC);
|
|
|
|
}
|
|
|
|
}
|
2015-08-09 07:01:22 +10:00
|
|
|
}
|
|
|
|
|
2015-08-10 04:35:56 +10:00
|
|
|
void sway_log_colors(int mode) {
|
2015-08-11 05:09:51 +10:00
|
|
|
colored = (mode == 1) ? 1 : 0;
|
2015-08-10 04:35:56 +10:00
|
|
|
}
|
|
|
|
|
2015-08-09 07:01:22 +10:00
|
|
|
void sway_abort(char *format, ...) {
|
|
|
|
fprintf(stderr, "ERROR: ");
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
vfprintf(stderr, format, args);
|
|
|
|
va_end(args);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sway_log(int verbosity, char* format, ...) {
|
|
|
|
if (verbosity <= v) {
|
|
|
|
int c = verbosity;
|
|
|
|
if (c > sizeof(verbosity_colors) / sizeof(char *)) {
|
|
|
|
c = sizeof(verbosity_colors) / sizeof(char *) - 1;
|
|
|
|
}
|
2015-08-10 04:35:56 +10:00
|
|
|
|
2015-08-10 04:41:54 +10:00
|
|
|
if (colored) {
|
2015-08-10 04:35:56 +10:00
|
|
|
fprintf(stderr, verbosity_colors[c]);
|
|
|
|
}
|
|
|
|
|
2015-08-09 07:01:22 +10:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
vfprintf(stderr, format, args);
|
|
|
|
va_end(args);
|
2015-08-10 04:35:56 +10:00
|
|
|
|
2015-08-10 04:41:54 +10:00
|
|
|
if (colored) {
|
2015-08-10 10:23:56 +10:00
|
|
|
fprintf(stderr, "\x1B[0m");
|
2015-08-10 04:35:56 +10:00
|
|
|
}
|
2015-08-10 10:23:56 +10:00
|
|
|
fprintf(stderr, "\n");
|
2015-08-09 07:01:22 +10:00
|
|
|
}
|
|
|
|
}
|