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>
|
2023-09-25 12:29:04 -05:00
|
|
|
#include "yugine.h"
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-04-28 20:55:24 -05:00
|
|
|
#include "script.h"
|
|
|
|
|
2023-09-02 06:53:52 -05:00
|
|
|
int logLevel = 0;
|
2023-04-24 17:22:18 -05:00
|
|
|
|
|
|
|
/* Four levels of log:
|
|
|
|
0 info
|
|
|
|
1 warn
|
|
|
|
2 error
|
|
|
|
3 critical
|
|
|
|
*/
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-01-31 17:41:09 -06:00
|
|
|
char *logstr[] = { "info", "warn", "error", "critical" };
|
2023-05-27 10:13:20 -05:00
|
|
|
char *catstr[] = {"engine", "script", "render"};
|
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-09-25 16:34:48 -05:00
|
|
|
#define ERROR_BUFFER 1024
|
2023-10-03 17:16:38 -05:00
|
|
|
#define CONSOLE_BUF 1024*1024 /* 5MB */
|
2022-12-19 12:16:51 -06:00
|
|
|
|
2023-12-22 07:14:44 -06:00
|
|
|
char *consolelog = NULL;
|
2023-09-13 07:31:00 -05:00
|
|
|
|
2023-10-26 11:48:02 -05:00
|
|
|
static FILE *sout;
|
|
|
|
|
2023-09-13 07:31:00 -05:00
|
|
|
void log_init()
|
|
|
|
{
|
|
|
|
consolelog = malloc(CONSOLE_BUF+1);
|
2023-12-22 07:14:44 -06:00
|
|
|
consolelog[0] = '\0';
|
2023-11-01 15:33:22 -05:00
|
|
|
sout = fdopen(dup(1),"w");
|
2023-10-26 11:48:02 -05:00
|
|
|
sout = stdout;
|
2023-09-13 07:31:00 -05:00
|
|
|
}
|
2022-12-19 12:16:51 -06:00
|
|
|
|
2023-10-26 11:48:02 -05:00
|
|
|
const char *logfmt = "%s:%d: %s, %s: %s\n";
|
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-09-03 12:50:02 -05:00
|
|
|
#ifndef NDEBUG
|
2023-10-23 08:08:11 -05:00
|
|
|
if (priority >= logLevel) {
|
|
|
|
time_t now = time(0);
|
|
|
|
struct tm *tinfo = localtime(&now);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-10-23 08:08:11 -05:00
|
|
|
double ticks = (double)clock()/CLOCKS_PER_SEC;
|
2023-05-03 13:50:37 -05:00
|
|
|
|
2023-10-26 11:48:02 -05:00
|
|
|
va_list args, arg2;
|
2023-10-23 08:08:11 -05:00
|
|
|
va_start(args, message);
|
2023-10-26 11:48:02 -05:00
|
|
|
va_copy(arg2, args);
|
|
|
|
int len = vsnprintf(NULL, 0, message, args)+1;
|
|
|
|
char *msg = malloc(len);
|
2023-10-23 08:08:11 -05:00
|
|
|
va_end(args);
|
2023-12-22 07:14:44 -06:00
|
|
|
vsnprintf(msg, len, message, arg2);
|
2023-10-26 11:48:02 -05:00
|
|
|
va_end(arg2);
|
|
|
|
|
|
|
|
len = snprintf(NULL, 0, logfmt, file,line,logstr[priority], catstr[category], msg)+1;
|
|
|
|
char *buffer = malloc(len);
|
2023-12-22 07:14:44 -06:00
|
|
|
snprintf(buffer, len, logfmt, file, line, logstr[priority], catstr[category], msg);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-12-20 17:20:29 -06:00
|
|
|
fprintf(stderr, "%s", buffer);
|
2023-10-23 08:08:11 -05:00
|
|
|
fflush(stderr);
|
2023-10-26 11:48:02 -05:00
|
|
|
|
|
|
|
free(msg);
|
|
|
|
free(buffer);
|
2023-04-24 17:22:18 -05:00
|
|
|
}
|
2023-09-25 12:29:04 -05:00
|
|
|
|
2023-05-24 20:45:50 -05:00
|
|
|
#endif
|
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)
|
|
|
|
{
|
2023-10-26 11:48:02 -05:00
|
|
|
fprintf(sout, "%s", str);
|
|
|
|
fflush(sout);
|
2023-10-23 08:08:11 -05:00
|
|
|
|
2023-10-16 09:40:43 -05:00
|
|
|
#ifndef NDEBUG
|
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);
|
|
|
|
}
|
2023-10-16 09:40:43 -05:00
|
|
|
#endif
|
2023-10-09 18:10:10 -05:00
|
|
|
}
|
2022-12-15 17:30:22 -06:00
|
|
|
|
2023-10-17 12:22:06 -05:00
|
|
|
void log_clear()
|
|
|
|
{
|
|
|
|
consolelog[0] = 0;
|
|
|
|
}
|
|
|
|
|
2023-10-09 18:10:10 -05:00
|
|
|
void console_print(const char *str)
|
|
|
|
{
|
2023-10-16 09:40:43 -05:00
|
|
|
#ifndef NDEBUG
|
2023-10-09 18:10:10 -05:00
|
|
|
strncat(consolelog, str, CONSOLE_BUF);
|
2023-10-16 09:40:43 -05:00
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
}
|
2023-09-02 06:53:52 -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) {
|
2023-09-25 12:29:04 -05:00
|
|
|
lvl = 3-lvl;
|
2023-10-26 11:48:02 -05:00
|
|
|
mYughLog(2, lvl, line, file, "tag: %s, id: %d, msg: %s", tag, id, msg);
|
2023-09-02 06:53:52 -05:00
|
|
|
}
|