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>
|
2022-12-14 13:01:42 -06:00
|
|
|
#include <unistd.h>
|
2022-12-19 12:16:51 -06:00
|
|
|
#include <stdio.h>
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-04-28 20:55:24 -05:00
|
|
|
#include "script.h"
|
|
|
|
|
2023-04-24 17:22:18 -05:00
|
|
|
int logLevel = 1;
|
|
|
|
|
|
|
|
/* Four levels of log:
|
|
|
|
0 info
|
|
|
|
1 warn
|
|
|
|
2 error
|
|
|
|
3 critical
|
|
|
|
*/
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-11-17 16:48:20 -06:00
|
|
|
//char *logstr[] = { "INFO", "WARN", "\x1b[1;31mERROR\x1b[0m", "CRITICAL" };
|
2023-01-31 17:41:09 -06:00
|
|
|
char *logstr[] = { "info", "warn", "error", "critical" };
|
|
|
|
char *catstr[] = {"engine", "script"};
|
2022-12-14 13:01:42 -06:00
|
|
|
|
2022-12-15 17:30:22 -06:00
|
|
|
FILE *logfile = NULL;
|
2022-11-24 01:54:17 -06:00
|
|
|
|
2023-03-17 10:25:35 -05:00
|
|
|
#define CONSOLE_BUF 1024*1024*5/* 5MB */
|
2022-12-19 12:16:51 -06:00
|
|
|
|
2023-04-28 20:55:24 -05:00
|
|
|
char lastlog[ERROR_BUFFER+1] = {'\0'};
|
|
|
|
char consolelog[CONSOLE_BUF+1] = {'\0'};
|
2022-12-19 12:16:51 -06:00
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
void mYughLog(int category, int priority, int line, const char *file, const char *message, ...)
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
2023-04-28 12:49:18 -05:00
|
|
|
|
2021-11-30 21:29:18 -06:00
|
|
|
if (priority >= logLevel) {
|
|
|
|
time_t now = time(0);
|
|
|
|
char *dt = ctime(&now);
|
|
|
|
dt[strlen(dt) - 1] = '\0'; // The above time conversion adds a \n; this removes it
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, message);
|
|
|
|
char msgbuffer[ERROR_BUFFER] = { '\0' };
|
|
|
|
vsnprintf(msgbuffer, ERROR_BUFFER, message, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
char buffer[ERROR_BUFFER] = { '\0' };
|
2023-02-02 17:52:15 -06:00
|
|
|
snprintf(buffer, ERROR_BUFFER, "%s:%d: %s, %s: %s\n", file, line, logstr[priority], catstr[category], msgbuffer);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-04-24 17:22:18 -05:00
|
|
|
log_print(buffer);
|
2023-04-28 12:49:18 -05:00
|
|
|
|
|
|
|
if (priority >= 2)
|
|
|
|
js_stacktrace();
|
2023-04-24 17:22:18 -05:00
|
|
|
}
|
|
|
|
}
|
2022-12-15 17:30:22 -06:00
|
|
|
|
2023-04-24 17:22:18 -05:00
|
|
|
void log_print(const char *str)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s", str);
|
|
|
|
fflush(stderr);
|
2023-03-17 10:25:35 -05:00
|
|
|
|
2023-04-24 17:22:18 -05:00
|
|
|
strncat(consolelog, str, CONSOLE_BUF);
|
2022-12-15 17:30:22 -06:00
|
|
|
|
2023-04-24 17:22:18 -05:00
|
|
|
if (logfile) {
|
|
|
|
fprintf(logfile, "%s", str);
|
|
|
|
fflush(logfile);
|
|
|
|
}
|
2022-12-15 17:30:22 -06:00
|
|
|
|
2023-04-24 17:22:18 -05:00
|
|
|
snprintf(lastlog, ERROR_BUFFER, "%s", str);
|
2022-11-24 01:54:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void log_setfile(char *file) {
|
2022-12-14 13:01:42 -06:00
|
|
|
freopen(file, "w", stderr);
|
2022-12-15 17:30:22 -06:00
|
|
|
freopen(file, "w", stdout);
|
2022-11-24 01:54:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void log_cat(FILE *f) {
|
|
|
|
char out[1024];
|
|
|
|
|
|
|
|
while (fgets(out, sizeof(out), f)) {
|
|
|
|
out[strcspn(out, "\n")] = '\0';
|
|
|
|
YughInfo(out);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlushGLErrors()
|
|
|
|
{
|
|
|
|
GLenum glErr = GL_NO_ERROR;
|
|
|
|
glErr = glGetError();
|
|
|
|
while (glErr != GL_NO_ERROR) {
|
2022-02-06 10:14:57 -06:00
|
|
|
YughLog(0, 3,
|
2021-11-30 21:29:18 -06:00
|
|
|
"GL Error: %d", glErr);
|
|
|
|
glErr = glGetError();
|
|
|
|
}
|
|
|
|
}
|