Skip to content

Commit

Permalink
make it always write all log messages into OpenSource.log
Browse files Browse the repository at this point in the history
Also, make config parsing errors more verbose
  • Loading branch information
w23 committed Jan 11, 2024
1 parent 48b074f commit af1387f
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ tags
*.opendb
*.db
*.user
.cache
16 changes: 9 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ add_subdirectory(src/atto)

set(SOURCES
src/OpenSource.c
src/bsp.c
src/atlas.c
src/filemap.c
src/bsp.c
src/cache.c
src/camera.c
src/collection.c
src/vmfparser.c
src/material.c
src/texture.c
src/cache.c
src/dxt.c
src/render.c
src/filemap.c
src/log.c
src/material.c
src/profiler.c
src/render.c
src/texture.c
src/vmfparser.c
)

set(HEADERS
Expand All @@ -37,6 +38,7 @@ set(HEADERS
src/etcpack.h
src/filemap.h
src/libc.h
src/log.h
src/material.h
src/mempools.h
src/profiler.h
Expand Down
25 changes: 20 additions & 5 deletions src/OpenSource.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "collection.h"
#include "mempools.h"
#include "common.h"
#include "texture.h"
#include "log.h"
//#include "profiler.h"
#include "camera.h"
#include "vmfparser.h"
Expand Down Expand Up @@ -502,6 +502,7 @@ static VMFAction configLandmarkCallback(VMFState *state, VMFEntryType entry, con
state->callback = configPatchCallback;
break;
default:
PRINTF("%s: Unexpected entry %d", __FUNCTION__, entry);
return VMFAction_SemanticError;
}

Expand All @@ -518,12 +519,15 @@ static VMFAction configMapCallback(VMFState *state, VMFEntryType entry, const VM
} else if (strncasecmp("offset", kv->key.str, kv->key.length) == 0) {
cfg->map_offset = kv->value;
} else {
PRINTF("%s: Unexpected key \"" PRI_SV "\"", __FUNCTION__, PRI_SVV(kv->key));
return VMFAction_SemanticError;
}
break;
case VMFEntryType_SectionClose:
if (cfg->map_name.length < 1)
if (cfg->map_name.length < 1) {
PRINTF("%s: Invalid map name \"" PRI_SV "\"", __FUNCTION__, PRI_SVV(cfg->map_name));
return VMFAction_SemanticError;
}
Map *m = opensrcAllocMap(cfg->map_name);
if (m && cfg->map_offset.length >= 5) {
float x, y, z;
Expand All @@ -538,6 +542,7 @@ static VMFAction configMapCallback(VMFState *state, VMFEntryType entry, const VM
state->callback = configReadCallback;
break;
default:
PRINTF("%s: Unexpected entry %d", __FUNCTION__, entry);
return VMFAction_SemanticError;
}

Expand All @@ -549,14 +554,17 @@ static VMFAction configPatchCallback(VMFState *state, VMFEntryType entry, const

switch (entry) {
case VMFEntryType_SectionOpen:
if (kv->key.length < 1)
if (kv->key.length < 1) {
PRINTF("%s: Unexpected section \"" PRI_SV "\"", __FUNCTION__, PRI_SVV(kv->key));
return VMFAction_SemanticError;
}
cfg->map_name = kv->key;
state->callback = configLandmarkCallback;
break;
case VMFEntryType_SectionClose:
return VMFAction_Exit;
default:
PRINTF("%s: Unexpected entry %d", __FUNCTION__, entry);
return VMFAction_SemanticError;
}

Expand Down Expand Up @@ -586,19 +594,24 @@ static VMFAction configReadCallback(VMFState *state, VMFEntryType entry, const V
} else if (strncasecmp("z_far", kv->key.str, kv->key.length) == 0) {
// FIXME null-terminate
g.R = (float)atof(kv->value.str);
} else
} else {
PRINTF("%s: Unexpected key \"" PRI_SV "\"", __FUNCTION__, PRI_SVV(kv->key));
return VMFAction_SemanticError;
}
break;
case VMFEntryType_SectionOpen:
if (strncasecmp("patch_landmarks", kv->key.str, kv->key.length) == 0)
state->callback = configPatchCallback;
else if (strncasecmp("map", kv->key.str, kv->key.length) == 0) {
cfg->map_name.length = cfg->map_offset.length = 0;
state->callback = configMapCallback;
} else
} else {
PRINTF("%s: Unexpected section \"" PRI_SV "\"", __FUNCTION__, PRI_SVV(kv->key));
return VMFAction_SemanticError;
}
break;
default:
PRINTF("%s: Unexpected entry %d", __FUNCTION__, entry);
return VMFAction_SemanticError;
}

Expand Down Expand Up @@ -790,6 +803,8 @@ static Arg g_args[] = {
void attoAppInit(struct AAppProctable *proctable) {
//profilerInit();

logOpen("OpenSource.log");

g.collection_chain = NULL;
g.patches = NULL;
g.maps_count = 0;
Expand Down
12 changes: 9 additions & 3 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@

#define STR1(m) #m
#define STR(m) STR1(m)
#define PRINTF(fmt, ...) fprintf(stderr, __FILE__ ":" STR(__LINE__) ": " fmt "\n", __VA_ARGS__)
#define PRINT(msg) fprintf(stderr, __FILE__ ":" STR(__LINE__) ": %s\n", msg)

#define ASSERT(cond) if (!(cond)){PRINTF("%s failed", #cond); abort();}
#ifdef _MSC_VER
// MSVC compiler
#define PRINTF_ARG(fmt) _Printf_format_string_ fmt
#define PRINTF_ATTR(fmt_index, vargs_index)
#else
// gcc-compatible
#define PRINTF_ARG(fmt) fmt
#define PRINTF_ATTR(fmt_index, vargs_index) __attribute__((format(printf, fmt_index, vargs_index)))
#endif

#define COUNTOF(a) (sizeof(a) / sizeof(*(a)))

Expand Down
1 change: 1 addition & 0 deletions src/filemap.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "filemap.h"
#include "common.h"
#include "log.h"

#ifndef _WIN32
#include <sys/types.h>
Expand Down
32 changes: 32 additions & 0 deletions src/log.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once
#include "log.h"

#include <stdio.h>
#include <stdarg.h>

static struct {
FILE *logfile;
} glog = {0};

void logOpen(const char *logfile) {
glog.logfile = fopen(logfile, "w");
}

void logClose(void) {
fclose(glog.logfile);
}

void logPrintf(const char *format, ...) {
va_list args;
va_start(args, format);

if (glog.logfile) {
va_list args_copy;
va_copy(args_copy, args);
vfprintf(glog.logfile, format, args_copy);
va_end(args_copy);
}

vfprintf(stderr, format, args);
va_end(args);
}
11 changes: 11 additions & 0 deletions src/log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common.h"

void logOpen(const char *logfile);
void logClose(void);

void logPrintf(PRINTF_ARG(const char *format), ...) PRINTF_ATTR(0, 1);

#define PRINTF(fmt, ...) logPrintf(__FILE__ ":" STR(__LINE__) ": " fmt "\n", __VA_ARGS__)
#define PRINT(msg) logPrintf(__FILE__ ":" STR(__LINE__) ": %s\n", msg)

#define ASSERT(cond) if (!(cond)){PRINTF("%s failed", #cond); abort();}
1 change: 1 addition & 0 deletions src/mempools.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "common.h"
#include "log.h"
#include <stddef.h>

typedef struct Stack {
Expand Down
2 changes: 1 addition & 1 deletion src/vmfparser.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "vmfparser.h"
#include "libc.h"
#include "log.h"

typedef enum {
VMFTokenType_End,
Expand Down

0 comments on commit af1387f

Please sign in to comment.