Add versioning to logs; add backtrace on crash; add automatic logging to a logfile every run

This commit is contained in:
2022-11-24 07:54:17 +00:00
parent 012f1cc1fc
commit 5946a08a79
4 changed files with 111 additions and 13 deletions

View File

@@ -5,7 +5,6 @@
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#define logLevel 0
@@ -13,6 +12,8 @@
char *logstr[] = { "INFO", "WARN", "ERROR", "CRITICAL" };
char *catstr[] = {"ENGINE"};
FILE *fout = NULL;
void mYughLog(int category, int priority, int line, const char *file, const char *message, ...)
{
if (priority >= logLevel) {
@@ -30,7 +31,26 @@ void mYughLog(int category, int priority, int line, const char *file, const char
snprintf(buffer, ERROR_BUFFER, "%s | %s | %s [ %s:%d ] %s\n", logstr[priority], catstr[0], dt, file, line, msgbuffer);
printf("%s", buffer);
fflush(stdout);
fflush(stdout);
if (fout) {
fprintf(fout, "%s", buffer);
fflush(fout);
}
}
}
void log_setfile(char *file) {
YughInfo("Opening output log %s.", file);
fout = fopen(file, "w");
}
void log_cat(FILE *f) {
char out[1024];
while (fgets(out, sizeof(out), f)) {
out[strcspn(out, "\n")] = '\0';
YughInfo(out);
}
}

View File

@@ -1,6 +1,8 @@
#ifndef LOG_H
#define LOG_H
#include <stdio.h>
#define ERROR_BUFFER 2048
#define LOG_INFO 0
@@ -20,4 +22,7 @@ void FlushGLErrors();
int TestSDLError(int sdlErr);
void log_setfile(char *file);
void log_cat(FILE *f);
#endif

View File

@@ -7,6 +7,12 @@
#include "script.h"
#include "editor.h"
#include "log.h"
#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>
#include <signal.h>
#include <time.h>
#include "string.h"
@@ -22,19 +28,78 @@ double updateMS = 1/60.f;
static int ed = 1;
void seghandle(int sig) {
void *ents[512];
size_t size;
size = backtrace(ents, 512);
if (strsignal(sig)) {
YughCritical("CRASH! Signal: %s.", strsignal(sig));
}
else {
YughCritical("CRASH! Signal: %d.", sig);
}
YughCritical("====================BACKTRACE====================");
char **stackstr = backtrace_symbols(ents, size);
for (int i = 0; i < size; i++) {
YughCritical(stackstr[i]);
}
exit(1);
}
int main(int argc, char **args) {
for (int i = 1; i < argc; i++) {
if (args[i][0] == '-') {
if (strncmp(&args[i][1], "play", 4) == 0) {
ed = 0;
switch(args[i][1]) {
case 'p':
if (strncmp(&args[i][2], "lay", 3))
continue;
ed = 0;
break;
case 'l':
if (i+1 < argc && args[i+1][0] != '-') {
log_setfile(args[i+1]);
i++;
continue;
}
else {
YughError("Expected a file for command line arg '-l'.");
exit(1);
}
}
}
}
if (DBG) {
time_t now = time(NULL);
char fname[100];
snprintf(fname, 100, "yugine-%d.log", now);
log_setfile(fname);
}
YughInfo("Starting yugine version %s.", VER);
signal(SIGSEGV, seghandle);
FILE *sysinfo = NULL;
sysinfo = popen("uname -a", "r");
if (!sysinfo) {
YughWarn("Failed to get sys info.");
} else {
log_cat(sysinfo);
pclose(sysinfo);
}
engine_init();
const GLFWvidmode *vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
YughInfo("Refresh rate is %d", vidmode->refreshRate);