/*
 * Copyright (c) 1994, 2021 Alex Leigh, All Rights Reserved
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF ALEX LEIGH The copyright
 * notice above does not evidence any actual or intended publication of such
 * source code.
 *
 * Disclosure, copying, distribution, or transmission of this code without
 * express permission is prohibited.
 */

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

#include "mecha.h"

static FILE *mGlog_fh;

void mFlog_init() {
    mGlog_fh = stderr;
}

void mFlog_msg_internal(const char *func, enum mEl_level level, const char *m, ...) {
    va_list ap;

    va_start(ap, m);

    switch (level) {
        case MECHA_LOG_OK:
            fprintf(mGlog_fh, "%s/ MECHA_LOG_OK: %s: ", mFtme_asctime(), func);
            vfprintf(mGlog_fh, m, ap);
            fprintf(mGlog_fh, "\n");
            fflush(mGlog_fh);
            break;
        case MECHA_LOG_WARN:
            fprintf(mGlog_fh, "%s/WRN: %s: ", mFtme_asctime(), func);
            vfprintf(mGlog_fh, m, ap);
            fprintf(mGlog_fh, "\n");
            fflush(mGlog_fh);
            break;
        case MECHA_LOG_ERROR:
            fprintf(mGlog_fh, "%s/ERR: %s: ", mFtme_asctime(), func);
            vfprintf(mGlog_fh, m, ap);
            fprintf(mGlog_fh, "\n");
            fflush(mGlog_fh);
            break;
        case MECHA_LOG_DIAG:
            fprintf(mGlog_fh, "%s/DBG: %s: ", mFtme_asctime(), func);
            vfprintf(mGlog_fh, m, ap);
            fprintf(mGlog_fh, "\n");
            fflush(mGlog_fh);
            break;
        default:
            fprintf(mGlog_fh, "%s/???: %s: ", mFtme_asctime(), func);
            vfprintf(mGlog_fh, m, ap);
            fprintf(mGlog_fh, "\n");
            fflush(mGlog_fh);
            break;
    }
}