#include "logger.h"

FILE *logfile = NULL;

void logger_open(const char* filename) {
    if( logfile )
        logger_close();

    logfile = fopen(filename, "w");
}

void logger(double t, int N, double *y) {
    if( !logfile )
        logfile = stdout;

    fprintf(logfile, "%.12e", t);

    for( int i = 0; i < N; i++ )
        fprintf(logfile, ",%.12e", y[i]);

    fprintf(logfile, "\n");
}

void logger_close() {
    if( logfile && logfile != stdout && fclose(logfile) )
        printf("Failed to close logfile. Error no: %d", errno);
    logfile = NULL;
}
