<commit_before>#ifndef __SANCUS_LOGGER_H__
#define __SANCUS_LOGGER_H__

#include <sancus/bit.h>

#include <stdarg.h>
#include <stdio.h>
#include <string.h>

enum sancus_log_level {
	SANCUS_LOG_ERROR_BIT,
	SANCUS_LOG_WARN_BIT,
	SANCUS_LOG_INFO_BIT,
	SANCUS_LOG_TRACE_BIT,
	SANCUS_LOG_DEBUG_BIT,

	SANCUS_LOG_NEXT_BIT = 8,
};

enum {
	SANCUS_LOG_ERR = BIT(SANCUS_LOG_ERROR_BIT),
	SANCUS_LOG_WARN = BIT(SANCUS_LOG_WARN_BIT),
	SANCUS_LOG_INFO = BIT(SANCUS_LOG_INFO_BIT),
	SANCUS_LOG_TRACE = BIT(SANCUS_LOG_TRACE_BIT),
	SANCUS_LOG_DEBUG = BIT(SANCUS_LOG_DEBUG_BIT),

	SANCUS_LOG_NEXT  = BIT(SANCUS_LOG_NEXT_BIT),

	SANCUS_LOG_QUIET   = SANCUS_LOG_ERR | SANCUS_LOG_WARN,
	SANCUS_LOG_NORMAL  = SANCUS_LOG_QUIET | SANCUS_LOG_INFO,
	SANCUS_LOG_VERBOSE = SANCUS_LOG_NORMAL | SANCUS_LOG_DEBUG,
};

struct sancus_logger {
	const struct sancus_logger *parent;

	const char *prefix;
	unsigned mask;
	ssize_t (*prefixer) (const struct sancus_logger *, char *, size_t);
};

#define SANCUS__LOGGER_INIT(S, M) { \
	.prefix = (S), \
	.mask = (M) == 0 ? SANCUS_LOG_NORMAL : (M), \
}

#define SANCUS_LOGGER_INIT(S, M) (struct sancus_logger) SANCUS__LOGGER_INIT(S, M)
#define DECL_SANCUS_LOGGER(N, S, M) struct sancus_logger N = SANCUS__LOGGER_INIT(S, M)

static inline void sancus_logger_init2(struct sancus_logger *log,
				       const struct sancus_logger *parent,
				       ssize_t (*f) (const struct sancus_logger *, char *, size_t),
				       unsigned mask,
				       const char *prefix)
{
	if (log != NULL) {
		if (mask == 0 && parent == NULL)
			mask = SANCUS_LOG_NORMAL;

		*log = (struct sancus_logger) {
			.parent = parent,

			.prefix = prefix,
			.mask = mask,
			.prefixer = f,
		};
	}
}

static inline
void sancus_logger_init(struct sancus_logger *log,
		       const char *prefix, unsigned mask)
{
	sancus_logger_init2(log, NULL, NULL, mask, prefix);
}

static inline
void sancus_logger_set_prefix(struct sancus_logger *log,
			      const char *prefix)
{
	log->prefix = prefix;
}

static inline
void sancus_logger_set_prefixer(struct sancus_logger *log,
				ssize_t (*f) (const struct sancus_logger *, char *, size_t))
{
	log->prefixer = f;
}

static inline
void sancus_logger_set_mask(struct sancus_logger *log,
			    unsigned mask)
{
	log->mask = mask;
}

static inline
void sancus_logger_extend_mask(struct sancus_logger *log,
			       unsigned mask)
{
	log->mask = sancus_bit_cms(log->mask, mask, mask, 0);
}

static inline
void sancus_logger_unset_mask(struct sancus_logger *log,
			      unsigned mask)
{
	log->mask = sancus_bit_cms(log->mask, 0, mask, 0);
}

static inline<commit_msg>logger: add accessor for parent and prefix literal<commit_after>#ifndef __SANCUS_LOGGER_H__
#define __SANCUS_LOGGER_H__

#include <sancus/bit.h>

#include <stdarg.h>
#include <stdio.h>
#include <string.h>

enum sancus_log_level {
	SANCUS_LOG_ERROR_BIT,
	SANCUS_LOG_WARN_BIT,
	SANCUS_LOG_INFO_BIT,
	SANCUS_LOG_TRACE_BIT,
	SANCUS_LOG_DEBUG_BIT,

	SANCUS_LOG_NEXT_BIT = 8,
};

enum {
	SANCUS_LOG_ERR = BIT(SANCUS_LOG_ERROR_BIT),
	SANCUS_LOG_WARN = BIT(SANCUS_LOG_WARN_BIT),
	SANCUS_LOG_INFO = BIT(SANCUS_LOG_INFO_BIT),
	SANCUS_LOG_TRACE = BIT(SANCUS_LOG_TRACE_BIT),
	SANCUS_LOG_DEBUG = BIT(SANCUS_LOG_DEBUG_BIT),

	SANCUS_LOG_NEXT  = BIT(SANCUS_LOG_NEXT_BIT),

	SANCUS_LOG_QUIET   = SANCUS_LOG_ERR | SANCUS_LOG_WARN,
	SANCUS_LOG_NORMAL  = SANCUS_LOG_QUIET | SANCUS_LOG_INFO,
	SANCUS_LOG_VERBOSE = SANCUS_LOG_NORMAL | SANCUS_LOG_DEBUG,
};

struct sancus_logger {
	const struct sancus_logger *parent;

	const char *prefix;
	unsigned mask;
	ssize_t (*prefixer) (const struct sancus_logger *, char *, size_t);
};

#define SANCUS__LOGGER_INIT(S, M) { \
	.prefix = (S), \
	.mask = (M) == 0 ? SANCUS_LOG_NORMAL : (M), \
}

#define SANCUS_LOGGER_INIT(S, M) (struct sancus_logger) SANCUS__LOGGER_INIT(S, M)
#define DECL_SANCUS_LOGGER(N, S, M) struct sancus_logger N = SANCUS__LOGGER_INIT(S, M)

static inline void sancus_logger_init2(struct sancus_logger *log,
				       const struct sancus_logger *parent,
				       ssize_t (*f) (const struct sancus_logger *, char *, size_t),
				       unsigned mask,
				       const char *prefix)
{
	if (log != NULL) {
		if (mask == 0 && parent == NULL)
			mask = SANCUS_LOG_NORMAL;

		*log = (struct sancus_logger) {
			.parent = parent,

			.prefix = prefix,
			.mask = mask,
			.prefixer = f,
		};
	}
}

static inline
void sancus_logger_init(struct sancus_logger *log,
		       const char *prefix, unsigned mask)
{
	sancus_logger_init2(log, NULL, NULL, mask, prefix);
}

static inline
const struct sancus_logger *sancus_logger_get_parent(const struct sancus_logger *log)
{
	return log != NULL ? log->parent : NULL;
}

static inline
void sancus_logger_set_prefix(struct sancus_logger *log,
			      const char *prefix)
{
	log->prefix = prefix;
}

static inline
const char *sancus_logger_get_prefix(const struct sancus_logger *log)
{
	return log != NULL ? log->prefix : NULL;
}

static inline
void sancus_logger_set_prefixer(struct sancus_logger *log,
				ssize_t (*f) (const struct sancus_logger *, char *, size_t))
{
	log->prefixer = f;
}

static inline
void sancus_logger_set_mask(struct sancus_logger *log,
			    unsigned mask)
{
	log->mask = mask;
}

static inline
void sancus_logger_extend_mask(struct sancus_logger *log,
			       unsigned mask)
{
	log->mask = sancus_bit_cms(log->mask, mask, mask, 0);
}

static inline
void sancus_logger_unset_mask(struct sancus_logger *log,
			      unsigned mask)
{
	log->mask = sancus_bit_cms(log->mask, 0, mask, 0);
}

static inline
{'fraction_non_alphanumeric': 0.10416287947645883, 'fraction_numerical': 0.0029085620796218867, 'mean_word_length': 4.597151576805697, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6503631', 'n_tokens_mistral': 2419, 'n_tokens_neox': 2246, 'n_words': 453}