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
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-02-11 03:32:04 +08:00
|
|
|
__attribute__((constructor)) static void log_init(void) {
|
2018-06-08 00:42:34 +08:00
|
|
|
_log_fd = fcntl(_log_fd, F_DUPFD_CLOEXEC, 0);
|
|
|
|
if (_log_fd == -1) {
|
|
|
|
_log_fd = STDERR_FILENO;
|
|
|
|
}
|
2018-06-08 00:43:08 +08:00
|
|
|
_log_fd_isatty = isatty(_log_fd);
|
2018-02-11 03:32:04 +08:00
|
|
|
}
|
2017-10-24 22:20:51 +08:00
|
|
|
|
2018-06-07 22:51:50 +08:00
|
|
|
bool logSet() {
|
2018-06-08 00:37:17 +08:00
|
|
|
return _log_set;
|
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).
|
|
|
|
*/
|
2018-06-07 22:51:50 +08:00
|
|
|
|
|
|
|
void logLevel(enum llevel_t ll) {
|
|
|
|
_log_level = ll;
|
|
|
|
}
|
|
|
|
|
|
|
|
void logFile(const std::string& logfile) {
|
2018-06-08 00:37:17 +08:00
|
|
|
_log_set = true;
|
2017-05-26 21:22:59 +08:00
|
|
|
/* Close previous log_fd */
|
2018-02-11 03:32:04 +08:00
|
|
|
if (_log_fd > STDERR_FILENO) {
|
|
|
|
close(_log_fd);
|
|
|
|
_log_fd = STDERR_FILENO;
|
2017-05-26 21:22:59 +08:00
|
|
|
}
|
2018-06-08 00:42:34 +08:00
|
|
|
if (TEMP_FAILURE_RETRY(_log_fd = open(logfile.c_str(),
|
|
|
|
O_CREAT | O_RDWR | O_APPEND | O_CLOEXEC, 0640)) == -1) {
|
|
|
|
_log_fd = STDERR_FILENO;
|
|
|
|
PLOG_W("Couldn't open logfile open('%s')", logfile.c_str());
|
2015-05-15 05:44:48 +08:00
|
|
|
}
|
2018-06-07 22:57:43 +08:00
|
|
|
_log_fd_isatty = (isatty(_log_fd) == 1);
|
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
|
|
|
}
|
2018-12-16 18:55:33 +08:00
|
|
|
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("():")
|
|
|
|
.append(std::to_string(ln))
|
|
|
|
.append(" ");
|
2015-05-15 05:44:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2018-12-16 18:55:33 +08:00
|
|
|
char* strp;
|
2015-05-15 05:44:48 +08:00
|
|
|
va_end(args);
|
2018-12-16 18:55:33 +08:00
|
|
|
if (vasprintf(&strp, fmt, args) == -1) {
|
|
|
|
msg.append("[logs internal]: MEMORY ALLOCATION ERROR");
|
|
|
|
} else {
|
|
|
|
msg.append(strp);
|
|
|
|
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 */
|
|
|
|
|
2018-12-16 18:55:33 +08:00
|
|
|
if (write(_log_fd, msg.c_str(), msg.size()) == -1) {
|
|
|
|
dprintf(_log_fd, "%s", msg.c_str());
|
|
|
|
}
|
|
|
|
|
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
|