#include "psLib.h"

static int primary(int i)
{
    if (i != 0) {			// let's pretend it's an I/O error
	return psError("tst.error.primary", PS_ERR_IO, 1, "Primary error");
    }

    return 0;
}

static int middle(void)
{
    if (primary(1) != 0) {
	return psError("tst.error.middle", PS_ERR_UNKNOWN, 0, "Secondary error");
    }

    return 0;
}

static int toplevel(void)
{
    if (middle() != 0) {
	return psError("tst.error", PS_ERR_UNKNOWN, 0, "Toplevel error");
    }

    return 0;
}

int main(void)
{
    psTraceSetLevel("", 10);		// turn on all tracing
    psLogSetDestination(PS_LOG_NONE);	// turn off error logging

    if (toplevel() != 0) {
	psErrorStackPrint(stdout, "Traceback:\n");

	if (psErrorLast()->code == PS_ERR_UNKNOWN) {
	    fprintf(stderr, "Last error is of unknown type\n");
	}
	if (psErrorGet(2)->code == PS_ERR_IO) {
	    fprintf(stderr, "Third oldest error is of type IO\n");
	}
    }

    psErrorClear();
    psErrorStackPrint(stdout, "Traceback:\n");

    if (psErrorLast()->code == PS_ERR_NONE) {
	fprintf(stderr, "No errors. Hurrah\n");
    }

    return 0;
}
