log: add log_error_errno

Prints error message with strerror(errno) attached, for convenience.

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui 2020-02-26 20:16:55 +00:00
parent 49d620de4f
commit 630d6f3a26
No known key found for this signature in database
GPG Key ID: 37C999F617EA1A47
2 changed files with 9 additions and 9 deletions

View File

@ -45,8 +45,7 @@ static void file_watch_ev_cb(EV_P attr_unused, struct ev_io *w, int revent attr_
auto ret = read(w->fd, &inotify_event, sizeof(struct inotify_event)); auto ret = read(w->fd, &inotify_event, sizeof(struct inotify_event));
if (ret < 0) { if (ret < 0) {
if (errno != EAGAIN) { if (errno != EAGAIN) {
log_error("Failed to read from inotify fd: %s", log_error_errno("Failed to read from inotify fd");
strerror(errno));
} }
break; break;
} }
@ -57,7 +56,7 @@ static void file_watch_ev_cb(EV_P attr_unused, struct ev_io *w, int revent attr_
int ret = kevent(fwr->w.fd, NULL, 0, &ev, 1, &timeout); int ret = kevent(fwr->w.fd, NULL, 0, &ev, 1, &timeout);
if (ret <= 0) { if (ret <= 0) {
if (ret < 0) { if (ret < 0) {
log_error("Failed to get kevent: %s", strerror(errno)); log_error_errno("Failed to get kevent");
} }
break; break;
} }
@ -82,13 +81,13 @@ void *file_watch_init(EV_P) {
#ifdef HAS_INOTIFY #ifdef HAS_INOTIFY
fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if (fd < 0) { if (fd < 0) {
log_error("inotify_init1 failed: %s", strerror(errno)); log_error_errno("inotify_init1 failed");
return NULL; return NULL;
} }
#elif HAS_KQUEUE #elif HAS_KQUEUE
fd = kqueue(); fd = kqueue();
if (fd < 0) { if (fd < 0) {
log_error("Failed to create kqueue: %s", strerror(errno)); log_error_errno("Failed to create kqueue");
return NULL; return NULL;
} }
#else #else
@ -131,14 +130,13 @@ bool file_watch_add(void *_fwr, const char *filename, file_watch_cb_t cb, void *
wd = inotify_add_watch(fwr->w.fd, filename, wd = inotify_add_watch(fwr->w.fd, filename,
IN_CLOSE_WRITE | IN_MOVE_SELF | IN_DELETE_SELF); IN_CLOSE_WRITE | IN_MOVE_SELF | IN_DELETE_SELF);
if (wd < 0) { if (wd < 0) {
log_error("Failed to watch file \"%s\": %s", filename, strerror(errno)); log_error_errno("Failed to watch file \"%s\"", filename);
return false; return false;
} }
#elif HAS_KQUEUE #elif HAS_KQUEUE
wd = open(filename, O_RDONLY); wd = open(filename, O_RDONLY);
if (wd < 0) { if (wd < 0) {
log_error("Cannot open file \"%s\" for watching: %s", filename, log_error_errno("Cannot open file \"%s\" for watching", filename);
strerror(errno));
return false; return false;
} }
@ -160,7 +158,7 @@ bool file_watch_add(void *_fwr, const char *filename, file_watch_cb_t cb, void *
.udata = NULL, .udata = NULL,
}; };
if (kevent(fwr->w.fd, &ev, 1, NULL, 0, NULL) < 0) { if (kevent(fwr->w.fd, &ev, 1, NULL, 0, NULL) < 0) {
log_error("Failed to register kevent: %s", strerror(errno)); log_error_errno("Failed to register kevent");
close(wd); close(wd);
return false; return false;
} }

View File

@ -37,6 +37,8 @@ enum log_level {
#define log_error(x, ...) LOG(ERROR, x, ##__VA_ARGS__) #define log_error(x, ...) LOG(ERROR, x, ##__VA_ARGS__)
#define log_fatal(x, ...) LOG(FATAL, x, ##__VA_ARGS__) #define log_fatal(x, ...) LOG(FATAL, x, ##__VA_ARGS__)
#define log_error_errno(x, ...) LOG(ERROR, x ": %s", ##__VA_ARGS__, strerror(errno))
struct log; struct log;
struct log_target; struct log_target;