summary refs log blame commit diff
path: root/log.c
blob: 8a5710529dfc1d16029d909a889653c99fc505ff (plain) (tree)
1
2
3
4
5
6
7
8
9
                                            
                                                               





                   
                    



                                  
















                                    

































                                          









                                        








                                         
// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2020-2021 Alyssa Ross <hi@alyssa.is>

#define _GNU_SOURCE

#include "log.h"

#include <err.h>
#include <stdbool.h>
#include <stdlib.h>

enum verbosity verbosity = errors;

bool set_verbosity(int opt)
{
	switch (opt) {
	case 'q':
		verbosity = nothing;
		return true;
	case 'Q':
		verbosity = errors;
		return true;
	case 'v':
		verbosity = all;
		return true;
	}

	return false;
}

void veloge(const char *fmt, va_list args)
{
	if (verbosity)
		vwarn(fmt, args);
}

void velog(const char *fmt, va_list args)
{
	if (verbosity)
		vwarnx(fmt, args);
}

void elog(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	velog(fmt, ap);
	va_end(ap);
}

void vilog(const char *fmt, va_list args)
{
	if (verbosity == all)
		vwarnx(fmt, args);
}

void ilog(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	vilog(fmt, ap);
	va_end(ap);
}

void die(int eval, const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	velog(fmt, ap);
	va_end(ap);

	exit(eval);
}

void diee(int eval, const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	veloge(fmt, ap);
	va_end(ap);

	exit(eval);
}