errlog.c 1.58 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/*
 * errlog.c
 * Copyright 2017 Peter Jones <pjones@redhat.com>
 *
 * Distributed under terms of the GPLv3 license.
 */

#include "shim.h"

#ifdef LogError
#undef LogError
#endif

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
LogError(const char *file, int line, const char *func, CHAR16 *fmt, ...)
{
	va_list args;
	EFI_STATUS status;

	va_start(args, fmt);
	status = VLogError(file, line, func, fmt, args);
	va_end(args);

	return status;
}

VOID
PrintErrors(VOID)
{
	UINTN i;

71 72 73
	if (!verbose)
		return;

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	for (i = 0; i < nerrs; i++)
		Print(L"%s", errs[i]);
}

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