#include "error.h"

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

#include <windows.h>

#include "types.h"

#if __STDC_VERSION__ >= 201112L
#define noreturn _Noreturn
#else
#define noreturn
#endif

void sw_debug(char *fmt, ...) {
	va_list argp;

	printf("[DEBUG] ");
	va_start(argp, fmt);
	vprintf(fmt, argp);
	va_end(argp);
	printf("\n");
	fflush(stdout);
}

noreturn void sw_error(char *fmt, ...) {
	va_list argp;

	fprintf(stderr, "[ERROR] ");
	va_start(argp, fmt);
	vfprintf(stderr, fmt, argp);
	va_end(argp);
	fprintf(stderr, "\n");

	exit(-1);
}

void sw_log(char *fmt, ...) {
	va_list argp;

	printf("[LOG] ");
	va_start(argp, fmt);
	vprintf(fmt, argp);
	va_end(argp);
	printf("\n");
	fflush(stdout);
}

void sw_win32_error() {
	u32 error;
	char *str;

	error = GetLastError();

	FormatMessage(
	    FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
	        FORMAT_MESSAGE_IGNORE_INSERTS,
	    0,
	    error,
	    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
	    (char *)&str,
	    0,
	    0
	);

	sw_error("[win32] %s", str);
}
