2
0
mirror of https://github.com/checkpoint-restore/criu synced 2025-08-29 05:18:00 +00:00
criu/log.c
Kir Kolyshkin 0b237ae9f2 pr_perror(): print error at the end of line
This is a standard convention to print error message (i.e. strerror(errno))
at the end of line, like this:

        Cannot remove file: Permission denied

So pr_perror is fixed to follow this convention (using GNU extension
%m helps a lot here). Unfortunately, due to this we have to make
pr_perror() print a new line character, too, so we had to strip it
from the all pr_perror() invocations.

That (appending a newline) also makes pr_perror() a black sheep
in the herd of pr_* helpers, but what can we do? Worst case scenario
is an extra newline after an error message, not too harmful.

An alternative approach (stripping the newline from the passed format
string and re-adding it) was discussed thoroughly, and it was decided
that such a hack looks a bit too dirty.

Signed-off-by: Kir Kolyshkin <kir@openvz.org>
Acked-by: Pavel Emelyanov <xemul@parallels.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
2012-01-31 15:49:15 +04:00

72 lines
1.1 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <stdbool.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <fcntl.h>
#include "compiler.h"
#include "types.h"
#include "util.h"
/*
* Note pr_ helpers rely on this
* descriptor!
*/
static int logfd = STDERR_FILENO;
int get_logfd(void)
{
return logfd;
}
int init_log(const char *name)
{
struct rlimit rlimit;
int fd = STDERR_FILENO;
if (getrlimit(RLIMIT_NOFILE, &rlimit)) {
pr_err("can't get rlimit: %m\n");
return -1;
}
if (name) {
fd = open(name, O_CREAT | O_WRONLY);
if (fd == -1) {
pr_perror("Can't create log file %s", name);
return -1;
}
}
logfd = rlimit.rlim_cur - 1;
if (reopen_fd_as(logfd, fd) < 0) {
pr_err("can't duplicate descriptor %d->%d: %m\n",
fd, logfd);
logfd = STDERR_FILENO;
goto err;
}
return 0;
err:
if (name)
close(fd);
return -1;
}
void fini_log(void)
{
if (logfd != STDERR_FILENO &&
logfd != STDIN_FILENO &&
logfd != STDERR_FILENO)
close(logfd);
logfd = STDERR_FILENO;
}