<commit_before>/*

   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.

*/

#include "logs.h"

#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>
#include <sys/syscall.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#include "macros.h"
#include "util.h"

namespace logs {

static int _log_fd = STDERR_FILENO;
static bool _log_fd_isatty = true;
static enum llevel_t _log_level = INFO;
static bool _log_set = false;

__attribute__((constructor)) static void log_init(void) {
	_log_fd = fcntl(_log_fd, F_DUPFD_CLOEXEC, 0);
	if (_log_fd == -1) {
		_log_fd = STDERR_FILENO;
	}
	_log_fd_isatty = isatty(_log_fd);
}

bool logSet() {
	return _log_set;
}

/*
 * 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).
 */

void logLevel(enum llevel_t ll) {
	_log_level = ll;
}

void logFile(const std::string& logfile) {
	_log_set = true;
	/* Close previous log_fd */
	if (_log_fd > STDERR_FILENO) {
		close(_log_fd);
		_log_fd = STDERR_FILENO;
	}
	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());
	}
	_log_fd_isatty = (isatty(_log_fd) == 1);
}

void logMsg(enum llevel_t ll, const char* fn, int ln, bool perr, const char* fmt, ...) {
	if (ll < _log_level) {
		return;
	}<commit_msg>log: simplify logging code<commit_after>/*

   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.

*/

#include "logs.h"

#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>
#include <sys/syscall.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#include "macros.h"
#include "util.h"

namespace logs {

static int _log_fd = STDERR_FILENO;
static bool _log_fd_isatty = true;
static enum llevel_t _log_level = INFO;
static bool _log_set = false;

int getDupLogFd(int fd) {
	fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
	if (fd >= 0) {
		return fd;
	}
	return STDERR_FILENO;
}

/*
 * 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).
 */
__attribute__((constructor)) static void log_init(void) {
	_log_fd = getDupLogFd(STDERR_FILENO);
	_log_fd_isatty = isatty(_log_fd);
}

bool logSet() {
	return _log_set;
}

void logLevel(enum llevel_t ll) {
	_log_level = ll;
}

void logFile(const std::string& logfile) {
	_log_set = true;
	/* Close previous log_fd */
	if (_log_fd > STDERR_FILENO) {
		close(_log_fd);
	}
	if (TEMP_FAILURE_RETRY(_log_fd = open(logfile.c_str(),
				   O_CREAT | O_RDWR | O_APPEND | O_CLOEXEC, 0640)) == -1) {
		int saved_errno = errno;
		_log_fd = getDupLogFd(STDERR_FILENO);
		LOG_W(
		    "Couldn't open logfile open('%s'): %s", logfile.c_str(), strerror(saved_errno));
	}
	_log_fd_isatty = (isatty(_log_fd) == 1);
}

void logMsg(enum llevel_t ll, const char* fn, int ln, bool perr, const char* fmt, ...) {
	if (ll < _log_level) {
		return;
	}
{'fraction_non_alphanumeric': 0.13475815523059617, 'fraction_numerical': 0.008548931383577053, 'mean_word_length': 3.6216216216216215, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2151403', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1625, 'n_words': 492}