Skip to content

Commit

Permalink
Initial UTF-8 support for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Tearnote committed Sep 10, 2019
1 parent b19576d commit e840ba2
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 16 deletions.
4 changes: 1 addition & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ set(CMAKE_C_STANDARD 11)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()
if(CMAKE_BUILD_TYPE MATCHES DEBUG)
set(CMAKE_VERBOSE_MAKEFILE ON)
else()
if(CMAKE_BUILD_TYPE STREQUAL "Release")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s -ffunction-sections -Wl,--gc-sections")
if(WIN32)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mwindows")
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Minote
A repo for an upcoming puzzle action game. Not playable, currently on
git as a personal backup.
A repo for an upcoming puzzle action game. Rather barebones right now,
but fully playable.

## Building
Tested working on Linux and Windows (MSYS2). Should be portable to any
Expand Down
5 changes: 4 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ Make logging Unicode-aware
Look into using https://github.com/mattiasgustavsson/libs/blob/master/docs/thread.md instead of winpthreads
Replace for loops for initialization and replace them with memset
Wrap memset and memcpy with macros
Make libraries use our own malloc when possible
Make libraries use our own malloc when possible
Simplify vertex quads with macros
Add some more de/initialization logging
Fix remaining %s to %U in log format strings
23 changes: 22 additions & 1 deletion src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

#include "glad/glad.h"
#include <GLFW/glfw3.h>
#include <unitypes.h>
#include <unistdio.h>
#include <unistr.h>
#ifdef WIN32
#ifndef NDEBUG
#include <windows.h>
#endif // NDEBUG
#endif // WIN32

#include <stdbool.h>
#include <stdarg.h>
Expand Down Expand Up @@ -50,6 +58,12 @@ void initLogging()
logError("Failed to open %s for writing: %s",
LOG_FILENAME, strerror(errno));
}
#ifdef WIN32
#ifndef NDEBUG
if (!SetConsoleOutputCP(65001))
logWarn("Failed to set console output to UTF-8");
#endif // NDEBUG
#endif // WIN32
}

void cleanupLogging()
Expand All @@ -70,8 +84,15 @@ static void logTo(FILE *file, int prio, const char *fmt, va_list ap)
fprintf(file, "%02d:%02d:%02d [%s] ",
timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec,
prioStrings[prio]);
vfprintf(file, fmt, ap);

uint8_t *ustring;
if (u8_u8_vasprintf(&ustring, (uint8_t *)fmt, ap) == -1) {
fputs("Failed to allocate memory for string\n", stderr);
return;
}
fwrite(ustring, u8_strlen(ustring), 1, file);
putc('\n', file);
free(ustring);
unlockMutex(&logMutex);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static void cleanup(void)
int main(void)
{
initLogging();
logInfo("Starting up %s %s", APP_NAME, APP_VERSION);
logInfo("Starting up %U %U", APP_NAME, APP_VERSION);
atexit(cleanup);
initTimer();
initState();
Expand Down
2 changes: 1 addition & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
#define MAIN_H

#define APP_NAME "Minote"
#define APP_VERSION "prealpha"
#define APP_VERSION "alpha1"

#endif // MAIN_H
8 changes: 1 addition & 7 deletions src/textrender.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,10 @@ static void queueGlyph(enum fontType font, ucs4_t codepoint, const vec3 position
static void
queueString(enum fontType font, vec3 position, float size, char *fmt, ...)
{
uint8_t *ufmt = (uint8_t *)fmt;
if (u8_check(ufmt, u8_strlen(ufmt))) {
logWarn("Invalid UTF-8 string passed");
return;
}

uint8_t *ustring;
va_list ap;
va_start(ap, fmt);
if (u8_u8_vasprintf(&ustring, ufmt, ap) == -1) {
if (u8_u8_vasprintf(&ustring, (uint8_t *)fmt, ap) == -1) {
logWarn("Failed to allocate memory for string");
va_end(ap);
return;
Expand Down

0 comments on commit e840ba2

Please sign in to comment.