prosperon/source/engine/log.c

129 lines
2.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 <signal.h>
#include <fcntl.h>
#include "yugine.h"
2024-03-14 09:33:15 -05:00
#include "resources.h"
#include "quickjs/quickjs.h"
2021-11-30 21:29:18 -06:00
2023-04-28 20:55:24 -05:00
#include "script.h"
2024-05-15 16:34:03 -05:00
#ifdef __WIN32
#include "debugapi.h"
#endif
#define ESC "\033["
#define BLACK 30
#define RED 31
#define GREEN 32
#define YELLOW 33
#define BLUE 34
#define MAGENTA 35
#define CYAN 36
#define WHITE 37
2024-05-29 20:21:19 -05:00
#define COLOR(TXT, _C) ESC "22;" #_C "m" #TXT ESC "0m"
2024-03-14 09:33:15 -05:00
char *logstr[] = { "spam", "debug", "info", "warn", "error", "panic"};
char *logcolor[] = { COLOR(spam,37), COLOR(debug,32), COLOR(info,36), COLOR(warn,33), COLOR(error,31), COLOR(panic,45) };
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 */
static FILE *dump; /* where data is dumped to */
2023-10-26 11:48:02 -05:00
int stdout_lvl = LOG_ERROR;
void log_init()
{
2024-03-14 09:33:15 -05:00
#ifndef NDEBUG
if (!fexists(".prosperon")) {
logout = tmpfile();
2024-07-03 16:38:29 -05:00
writeout = tmpfile();
dump = tmpfile();
}
2024-03-18 08:16:25 -05:00
else {
logout = fopen(".prosperon/log.txt", "w");
writeout = fopen(".prosperon/transcript.txt", "w");
dump = fopen(".prosperon/quickjs.txt", "w");
quickjs_set_dumpout(dump);
}
quickjs_set_dumpout(dump);
2024-03-14 09:33:15 -05:00
#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);
fclose(dump);
}
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, ...)
{
2024-09-04 07:41:48 -05:00
return;
#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);
2024-09-04 07:41:48 -05:00
/*
2024-03-14 09:33:15 -05:00
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");
2024-09-04 07:41:48 -05:00
*/
if (priority == LOG_DEBUG || priority >= stdout_lvl) {
printf(logfmt, file, line, timebuf, logcolor[priority], catstr[category]);
2024-03-14 09:33:15 -05:00
va_list args;
va_start(args,message);
vprintf(message, args);
va_end(args);
printf("\n");
}
2024-05-30 12:05:51 -05:00
if (priority >= LOG_PANIC) {
2024-04-10 16:21:46 -05:00
js_stacktrace();
#ifdef __WIN32
DebugBreak();
#else
raise(SIGTRAP);
#endif
}
#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-09-04 07:41:48 -05:00
//fprintf(writeout, "%s", str);
2024-04-16 07:51:22 -05:00
#else
2024-09-04 07:41:48 -05:00
//printf(str);
//fflush(stdout);
2024-04-16 22:27:29 -05:00
#endif
2021-11-30 21:29:18 -06:00
}
2024-08-24 18:46:21 -05:00
void term_print(const char *str)
{
2024-09-04 07:41:48 -05:00
//fprintf(stdout, "%s", str);
//fflush(stdout);
2024-08-24 18:46:21 -05: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);
}