prosperon/source/engine/debug/log.c

78 lines
1.8 KiB
C
Raw Normal View History

2021-11-30 21:29:18 -06:00
#include "log.h"
2022-02-06 10:14:57 -06:00
#include "render.h"
2021-11-30 21:29:18 -06:00
#include <time.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
2022-12-19 12:16:51 -06:00
#include <stdio.h>
#include "yugine.h"
2024-03-14 09:33:15 -05:00
#include "resources.h"
2021-11-30 21:29:18 -06:00
2023-04-28 20:55:24 -05:00
#include "script.h"
2024-03-14 09:33:15 -05:00
char *logstr[] = { "spam", "debug", "info", "warn", "error", "panic"};
2023-05-27 10:13:20 -05:00
char *catstr[] = {"engine", "script", "render"};
2024-03-14 09:33:15 -05:00
static FILE *logout; /* where logs are written to */
static FILE *writeout; /* where console is written to */
2023-10-26 11:48:02 -05:00
void log_init()
{
2024-03-14 09:33:15 -05:00
#ifndef NDEBUG
logout = fopen(".prosperon/log.txt", "w");
writeout = fopen(".prosperon/transcript.txt", "w");
#endif
}
2022-12-19 12:16:51 -06:00
2024-03-14 09:33:15 -05:00
void log_shutdown()
2021-11-30 21:29:18 -06:00
{
2024-03-14 09:33:15 -05:00
fclose(logout);
fclose(writeout);
}
2024-03-14 09:33:15 -05:00
const char *logfmt = "%s:%d: [%s] %s, %s: ";
void mYughLog(int category, int priority, int line, const char *file, const char *message, ...)
{
#ifndef NDEBUG
2024-03-14 09:33:15 -05:00
time_t now = time(NULL);
struct tm *tinfo = localtime(&now);
char timebuf[80];
strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tinfo);
fprintf(logout, logfmt, file, line, timebuf, logstr[priority], catstr[category]);
va_list args;
va_start(args, message);
vfprintf(logout, message, args);
va_end(args);
fprintf(logout, "\n");
if (priority == LOG_DEBUG) {
printf(logfmt, file, line, timebuf, logstr[priority], catstr[category]);
va_list args;
va_start(args,message);
vprintf(message, args);
va_end(args);
printf("\n");
fflush(stdout);
}
#endif
2023-10-09 18:10:10 -05:00
}
2024-03-14 09:33:15 -05:00
/* print to stdout and console */
void log_print(const char *str)
2023-10-09 18:10:10 -05:00
{
#ifndef NDEBUG
2024-03-14 09:33:15 -05:00
fprintf(writeout, str);
#endif
2024-03-14 09:33:15 -05:00
printf(str);
fflush(stdout);
2021-11-30 21:29:18 -06:00
}
void sg_logging(const char *tag, uint32_t lvl, uint32_t id, const char *msg, uint32_t line, const char *file, void *data) {
2024-03-14 09:33:15 -05:00
lvl = LOG_PANIC-lvl;
mYughLog(LOG_RENDER, lvl, line, file, "tag: %s, id: %d, msg: %s", tag, id, msg);
}