errlog.c 1.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/*
 * errlog.c
 * Copyright 2017 Peter Jones <pjones@redhat.com>
 *
 * Distributed under terms of the GPLv3 license.
 */

#include "shim.h"

static CHAR16 **errs = NULL;
static UINTN nerrs = 0;

EFI_STATUS
VLogError(const char *file, int line, const char *func, CHAR16 *fmt, va_list args)
{
	va_list args2;
	UINTN size = 0, size2;
	CHAR16 **newerrs;

	size = SPrint(NULL, 0, L"%a:%d %a() ", file, line, func);
	va_copy(args2, args);
	size2 = VSPrint(NULL, 0, fmt, args2);
	va_end(args2);

	newerrs = ReallocatePool(errs, (nerrs + 1) * sizeof(*errs),
				       (nerrs + 3) * sizeof(*errs));
	if (!newerrs)
		return EFI_OUT_OF_RESOURCES;

	newerrs[nerrs] = AllocatePool(size*2+2);
	if (!newerrs[nerrs])
		return EFI_OUT_OF_RESOURCES;
	newerrs[nerrs+1] = AllocatePool(size2*2+2);
	if (!newerrs[nerrs+1])
		return EFI_OUT_OF_RESOURCES;

	SPrint(newerrs[nerrs], size*2+2, L"%a:%d %a() ", file, line, func);
	va_copy(args2, args);
	VSPrint(newerrs[nerrs+1], size2*2+2, fmt, args2);
	va_end(args2);

	nerrs += 2;
	newerrs[nerrs] = NULL;
	errs = newerrs;

	return EFI_SUCCESS;
}

EFI_STATUS
50
LogError_(const char *file, int line, const char *func, CHAR16 *fmt, ...)
51 52
{
	va_list args;
53
	EFI_STATUS efi_status;
54 55

	va_start(args, fmt);
56
	efi_status = VLogError(file, line, func, fmt, args);
57 58
	va_end(args);

59
	return efi_status;
60 61 62 63 64 65 66
}

VOID
PrintErrors(VOID)
{
	UINTN i;

67 68 69
	if (!verbose)
		return;

70
	for (i = 0; i < nerrs; i++)
71
		console_print(L"%s", errs[i]);
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
}

VOID
ClearErrors(VOID)
{
	UINTN i;

	for (i = 0; i < nerrs; i++)
		FreePool(errs[i]);
	FreePool(errs);
	nerrs = 0;
	errs = NULL;
}

// vim:fenc=utf-8:tw=75