2015-05-15 05:44:48 +08:00
|
|
|
/*
|
|
|
|
|
|
|
|
nsjail - logging
|
|
|
|
-----------------------------------------
|
|
|
|
|
|
|
|
Copyright 2014 Google Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
*/
|
2017-10-18 20:27:34 +08:00
|
|
|
|
2018-02-11 00:49:15 +08:00
|
|
|
#include "logs.h"
|
2015-05-15 05:44:48 +08:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2015-05-15 22:02:15 +08:00
|
|
|
#include <sys/syscall.h>
|
2017-09-14 04:03:21 +08:00
|
|
|
#include <sys/types.h>
|
2015-05-15 05:44:48 +08:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-04-13 05:49:10 +08:00
|
|
|
#include "macros.h"
|
2018-02-12 10:29:25 +08:00
|
|
|
#include "util.h"
|
2017-10-18 20:27:34 +08:00
|
|
|
|
2018-02-11 00:49:15 +08:00
|
|
|
namespace logs {
|
2018-02-10 01:55:42 +08:00
|
|
|
|
2018-02-11 03:32:04 +08:00
|
|
|
static int _log_fd = STDERR_FILENO;
|
|
|
|
static bool _log_fd_isatty = true;
|
|
|
|
static enum llevel_t _log_level = INFO;
|
2018-06-07 22:51:50 +08:00
|
|
|
static bool _log_set = false;
|
2015-05-15 05:44:48 +08:00
|
|
|
|
2019-08-22 19:59:15 +08:00
|
|
|
static void setDupLogFdOr(int fd, int orfd) {
|
|
|
|
int saved_errno = errno;
|
|
|
|
_log_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
|
|
|
|
if (_log_fd == -1) {
|
|
|
|
_log_fd = fcntl(orfd, F_DUPFD_CLOEXEC, 0);
|
2018-06-08 00:42:34 +08:00
|
|
|
}
|
2019-08-22 19:59:15 +08:00
|
|
|
if (_log_fd == -1) {
|
|
|
|
_log_fd = orfd;
|
|
|
|
}
|
|
|
|
_log_fd_isatty = (isatty(_log_fd) == 1);
|
|
|
|
errno = saved_errno;
|
2018-06-07 22:51:50 +08:00
|
|
|
}
|
|
|
|
|
2015-05-15 05:44:48 +08:00
|
|
|
/*
|
|
|
|
* Log to stderr by default. Use a dup()d fd, because in the future we'll associate the
|
|
|
|
* connection socket with fd (0, 1, 2).
|
|
|
|
*/
|
2019-08-20 20:16:21 +08:00
|
|
|
__attribute__((constructor)) static void log_init(void) {
|
2019-08-22 19:59:15 +08:00
|
|
|
setDupLogFdOr(STDERR_FILENO, STDERR_FILENO);
|
2019-08-20 20:16:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool logSet() {
|
|
|
|
return _log_set;
|
|
|
|
}
|
2018-06-07 22:51:50 +08:00
|
|
|
|
2022-08-05 14:42:37 +08:00
|
|
|
void setLogLevel(enum llevel_t ll) {
|
2018-06-07 22:51:50 +08:00
|
|
|
_log_level = ll;
|
|
|
|
}
|
|
|
|
|
2022-08-05 14:42:37 +08:00
|
|
|
enum llevel_t getLogLevel(void) {
|
|
|
|
return _log_level;
|
|
|
|
}
|
|
|
|
|
2019-10-03 01:43:58 +08:00
|
|
|
void logFile(const std::string& log_file, int log_fd) {
|
2018-06-08 00:37:17 +08:00
|
|
|
_log_set = true;
|
2019-10-03 01:43:58 +08:00
|
|
|
int newlogfd = -1;
|
|
|
|
if (!log_file.empty()) {
|
|
|
|
newlogfd = TEMP_FAILURE_RETRY(
|
|
|
|
open(log_file.c_str(), O_CREAT | O_RDWR | O_APPEND | O_CLOEXEC, 0640));
|
|
|
|
if (newlogfd == -1) {
|
|
|
|
PLOG_W("Couldn't open('%s')", log_file.c_str());
|
|
|
|
}
|
2015-05-15 05:44:48 +08:00
|
|
|
}
|
2019-08-25 17:23:20 +08:00
|
|
|
/* Close previous log_fd */
|
|
|
|
if (_log_fd > STDERR_FILENO) {
|
|
|
|
close(_log_fd);
|
|
|
|
}
|
2019-10-03 01:43:58 +08:00
|
|
|
setDupLogFdOr(newlogfd, log_fd);
|
2019-08-25 17:23:20 +08:00
|
|
|
close(newlogfd);
|
2015-05-15 05:44:48 +08:00
|
|
|
}
|
|
|
|
|
2018-02-10 01:55:42 +08:00
|
|
|
void logMsg(enum llevel_t ll, const char* fn, int ln, bool perr, const char* fmt, ...) {
|
2018-02-11 03:32:04 +08:00
|
|
|
if (ll < _log_level) {
|
2015-05-15 05:44:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char strerr[512];
|
2017-10-18 21:41:16 +08:00
|
|
|
if (perr) {
|
2015-05-15 05:44:48 +08:00
|
|
|
snprintf(strerr, sizeof(strerr), "%s", strerror(errno));
|
|
|
|
}
|
2018-12-16 14:47:22 +08:00
|
|
|
struct {
|
2017-10-09 05:00:45 +08:00
|
|
|
const char* const descr;
|
|
|
|
const char* const prefix;
|
2017-05-22 01:47:41 +08:00
|
|
|
const bool print_funcline;
|
|
|
|
const bool print_time;
|
2018-12-16 14:47:22 +08:00
|
|
|
} static const logLevels[] = {
|
2017-10-26 06:26:02 +08:00
|
|
|
{"D", "\033[0;4m", true, true},
|
|
|
|
{"I", "\033[1m", false, true},
|
|
|
|
{"W", "\033[0;33m", true, true},
|
|
|
|
{"E", "\033[1;31m", true, true},
|
|
|
|
{"F", "\033[7;35m", true, true},
|
|
|
|
{"HR", "\033[0m", false, false},
|
|
|
|
{"HB", "\033[1m", false, false},
|
2015-05-15 05:44:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Start printing logs */
|
2018-12-16 18:55:33 +08:00
|
|
|
std::string msg;
|
2018-02-11 03:32:04 +08:00
|
|
|
if (_log_fd_isatty) {
|
2018-12-16 18:55:33 +08:00
|
|
|
msg.append(logLevels[ll].prefix);
|
2015-05-15 05:44:48 +08:00
|
|
|
}
|
2019-01-21 01:41:44 +08:00
|
|
|
if (ll != HELP && ll != HELP_BOLD) {
|
|
|
|
msg.append("[").append(logLevels[ll].descr).append("]");
|
|
|
|
}
|
2016-10-21 22:49:50 +08:00
|
|
|
if (logLevels[ll].print_time) {
|
2018-12-16 18:55:33 +08:00
|
|
|
msg.append("[").append(util::timeToStr(time(NULL))).append("]");
|
2016-10-21 22:49:50 +08:00
|
|
|
}
|
2015-05-15 05:44:48 +08:00
|
|
|
if (logLevels[ll].print_funcline) {
|
2018-12-16 18:55:33 +08:00
|
|
|
msg.append("[")
|
|
|
|
.append(std::to_string(getpid()))
|
|
|
|
.append("] ")
|
|
|
|
.append(fn)
|
|
|
|
.append("():")
|
2018-12-16 21:22:01 +08:00
|
|
|
.append(std::to_string(ln));
|
2015-05-15 05:44:48 +08:00
|
|
|
}
|
|
|
|
|
2018-12-16 21:22:01 +08:00
|
|
|
char* strp;
|
2015-05-15 05:44:48 +08:00
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2018-12-16 21:22:01 +08:00
|
|
|
int ret = vasprintf(&strp, fmt, args);
|
2015-05-15 05:44:48 +08:00
|
|
|
va_end(args);
|
2018-12-16 21:22:01 +08:00
|
|
|
if (ret == -1) {
|
|
|
|
msg.append(" [logs internal]: MEMORY ALLOCATION ERROR");
|
2018-12-16 18:55:33 +08:00
|
|
|
} else {
|
2018-12-16 21:22:01 +08:00
|
|
|
msg.append(" ").append(strp);
|
2018-12-16 18:55:33 +08:00
|
|
|
free(strp);
|
|
|
|
}
|
2017-10-18 21:41:16 +08:00
|
|
|
if (perr) {
|
2018-12-16 18:55:33 +08:00
|
|
|
msg.append(": ").append(strerr);
|
2015-05-15 05:44:48 +08:00
|
|
|
}
|
2018-02-11 03:32:04 +08:00
|
|
|
if (_log_fd_isatty) {
|
2018-12-16 18:55:33 +08:00
|
|
|
msg.append("\033[0m");
|
2015-05-15 05:44:48 +08:00
|
|
|
}
|
2018-12-16 18:55:33 +08:00
|
|
|
msg.append("\n");
|
2015-05-15 05:44:48 +08:00
|
|
|
/* End printing logs */
|
|
|
|
|
2021-11-25 05:25:57 +08:00
|
|
|
TEMP_FAILURE_RETRY(write(_log_fd, msg.c_str(), msg.size()));
|
2018-12-16 18:55:33 +08:00
|
|
|
|
2015-05-15 05:44:48 +08:00
|
|
|
if (ll == FATAL) {
|
2017-09-25 13:08:22 +08:00
|
|
|
exit(0xff);
|
2015-05-15 05:44:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 03:32:04 +08:00
|
|
|
void logStop(int sig) {
|
|
|
|
LOG_I("Server stops due to fatal signal (%d) caught. Exiting", sig);
|
|
|
|
}
|
2018-02-10 01:55:42 +08:00
|
|
|
|
2018-02-11 00:49:15 +08:00
|
|
|
} // namespace logs
|