From 5946a08a79fc3d90db363570edadd5d177ea8dbd Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Thu, 24 Nov 2022 07:54:17 +0000 Subject: [PATCH] Add versioning to logs; add backtrace on crash; add automatic logging to a logfile every run --- Makefile | 26 ++++++++++------ source/engine/log.c | 24 +++++++++++++-- source/engine/log.h | 5 +++ source/engine/yugine.c | 69 ++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 111 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 0ce8877..9458678 100755 --- a/Makefile +++ b/Makefile @@ -56,7 +56,12 @@ COMPINCLUDE = $(edirs) WARNING_FLAGS = -Wno-everything #-Wno-incompatible-function-pointer-types -Wall -Wwrite-strings -Wunsupported -Wall -Wextra -Wwrite-strings -Wno-unused-parameter -Wno-unused-function -Wno-missing-braces -Wno-incompatible-function-pointer-types -Wno-gnu-statement-expression -Wno-complex-component-init -pedantic -COMPILER_FLAGS = $(includeflag) -I/usr/local/include -g -O0 -MD $(WARNING_FLAGS) -c $< -o $@ + +SEM = 0.0.1 +COM != git rev-parse --short HEAD +VER = $(SEM)-$(COM) + +COMPILER_FLAGS = $(includeflag) -I/usr/local/include -g -rdynamic -O0 -MD $(WARNING_FLAGS) -DDBG=1 -DVER=\"$(VER)\" -c $< -o $@ LIBPATH = -L./bin -L/usr/local/lib @@ -69,7 +74,7 @@ ifeq ($(UNAME), Windows_NT) CLIBS = glew32 EXT = .exe else - LINKER_FLAGS = -g + LINKER_FLAGS = -g -rdynamic ELIBS = engine pthread yughc mruby c m dl CLIBS = EXT = @@ -91,6 +96,9 @@ INCLUDE = $(BIN)include LINK = $(LIBPATH) $(LINKER_FLAGS) $(LELIBS) + + + .PHONY: yugine yugine: $(yuginec:.%.c=$(objprefix)%.o) $(ENGINE) $(BIN)libportaudio.a $(BIN)libglfw3.a @@ -98,17 +106,17 @@ yugine: $(yuginec:.%.c=$(objprefix)%.o) $(ENGINE) $(BIN)libportaudio.a $(BIN)lib $(CC) $< $(LINK) -o yugine @echo Finished build +dist: yugine + mkdir -p bin/dist + cp yugine bin/dist + cp -rf assets/fonts bin/dist + cp -rf source/scripts bin/dist + cp -rf source/shaders bin/dist + tar -czf yugine-$(VER).tar.gz --directory bin/dist . install: yugine cp yugine ~/.local/bin -pin: yugine - cp yugine pinball - mkdir -p pinball/fonts pinball/scripts pinball/shaders - cp -f assets/fonts/* pinball/fonts - cp -f source/scripts/* pinball/scripts - cp -rf source/shaders/* pinball/shaders - $(ENGINE): $(eobjects) @echo Making library engine.a @ar r $(ENGINE) $(eobjects) diff --git a/source/engine/log.c b/source/engine/log.c index 494aee9..8ad187d 100644 --- a/source/engine/log.c +++ b/source/engine/log.c @@ -5,7 +5,6 @@ #include #include #include -#include #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); } } diff --git a/source/engine/log.h b/source/engine/log.h index 67dd597..19caacf 100644 --- a/source/engine/log.h +++ b/source/engine/log.h @@ -1,6 +1,8 @@ #ifndef LOG_H #define LOG_H +#include + #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 diff --git a/source/engine/yugine.c b/source/engine/yugine.c index 7016e8e..e928c08 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -7,6 +7,12 @@ #include "script.h" #include "editor.h" #include "log.h" +#include +#include + +#include +#include +#include #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);