Skip to content

Commit

Permalink
rework logging to not require C variadics
Browse files Browse the repository at this point in the history
  • Loading branch information
lxnt committed Jun 2, 2016
1 parent 0bb833e commit 33c102c
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 97 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ if (USE_SDL2)
endif()

if (BUILD_STATIC)
add_library(zhban_s STATIC zhban.c utf.c logging.c)
add_library(zhban_s STATIC zhban.c utf.c)
install(TARGETS zhban_s ARCHIVE DESTINATION lib)
endif()

add_library(zhban SHARED zhban.c utf.c logging.c)
add_library(zhban SHARED zhban.c utf.c)
target_link_libraries(zhban ${PKG_HBZ_LIBRARIES} ${PKG_FT2_LIBRARIES})
if (USE_SDL2)
target_link_libraries(zhban ${PKG_SDL2_LIBRARIES})
Expand Down
45 changes: 0 additions & 45 deletions logging.c

This file was deleted.

19 changes: 17 additions & 2 deletions python/zhban/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,32 @@ def bind(libpath = None):
class ZhbanFail(Exception):
pass

_LOG_SINK_ADAPTER = ctypes.CFUNCTYPE(None, ctypes.c_uint32, ctypes.c_char_p, ctypes.c_uint32)

class Zhban(object):
def __init__(self, fontbuf, pixheight, subpix = True, loglevel = 0,
LOG_LEVEL = ( "null", "fatal", "error", "warn ", "info ", "trace" )

def _log_sink_adapter(self, level, buf, buflen):
msg = ctypes.string_at(buf, buflen).decode("utf-8")
if self.logsink is None:
sys.stderr.write("[{}] {}\n".format(self.LOG_LEVEL[level], msg))
else:
self.logsink(self, level, msg)

def __init__(self, fontbuf, pixheight, subpix = True, loglevel = 0, logsink = None,
gllim = 1<<20, szlim = 1<<16, rrlim = 1<<22, libpath = None):
if type(fontbuf) is bytes:
self._fbuf = fontbuf
else:
self._fbuf = bytes(fontbuf) # have to copy it.
self._lib = bind(libpath)
sp = 1 if subpix else 0

self.logsink = logsink

self._z = self._lib.zhban_open(self._fbuf, len(self._fbuf), pixheight,
sp, gllim, szlim, rrlim, loglevel, 0)
sp, gllim, szlim, rrlim, loglevel,
_LOG_SINK_ADAPTER(self._log_sink_adapter))
if not bool(self._z):
raise ZhbanFail

Expand Down
5 changes: 4 additions & 1 deletion python/zhban/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,10 @@ def main():
if not pa.font:
ap.error("Font is required.")

zhban = Zhban(open(pa.font, "rb").read(), pa.size, loglevel=pa.l, subpix=pa.sp,
def log_sink(z, ll, msg):
print("custom_log_sink: [{}] {}".format(z.LOG_LEVEL[ll], msg))

zhban = Zhban(open(pa.font, "rb").read(), pa.size, loglevel=pa.l, subpix=pa.sp, logsink = log_sink,
libpath = os.environ.get('PYSDL2_DLL_PATH'))

zhban.set_script(pa.dir, pa.script, pa.lang)
Expand Down
41 changes: 0 additions & 41 deletions zhban-internal.h

This file was deleted.

45 changes: 41 additions & 4 deletions zhban.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <stddef.h>

#include "zhban.h"
#include "zhban-internal.h"

#include <uthash.h>
#include <utlist.h>
Expand All @@ -51,6 +50,14 @@ typedef uint32_t refcount_t;
# define ZHBAN_GETREF(rc) ((rc))
#endif

#if defined(__GNUC__) || defined(__clang__)
# define ATTR_UNUSED __attribute__((unused))
#else
# define ATTR_UNUSED
#endif

#define LOG_BUFFER_LEN 1024

//{ context

typedef struct _shape shape_t;
Expand All @@ -66,7 +73,8 @@ typedef struct _zhban_internal {
zhban_t outer;

int32_t log_level;
logsink_t log_sink;
zhban_logsink_t log_sink;
char log_buffer[LOG_BUFFER_LEN];

uint32_t pixheight;
uint32_t subpixel_positioning; /* cache translated glyphs */
Expand Down Expand Up @@ -98,6 +106,35 @@ typedef struct _zhban_internal {

} zhban_internal_t;

//{ logging

static void logrintf(int msg_level, zhban_internal_t *z, const char *fmt, ...) {
int written = 0;
if (msg_level <= z->log_level) {
va_list ap;
va_start(ap, fmt);
written = vsnprintf(z->log_buffer, LOG_BUFFER_LEN, fmt, ap);
va_end(ap);
z->log_sink(msg_level, z->log_buffer, written);
}
if (msg_level == ZHLOG_FATAL)
abort();
}

#define log_trace(obj, fmt, args...) do { logrintf(ZHLOG_TRACE, obj, "%s(): " fmt, __func__, ## args); } while(0)
#define log_info(obj, fmt, args...) do { logrintf(ZHLOG_INFO, obj, "%s(): " fmt, __func__, ## args); } while(0)
#define log_warn(obj, fmt, args...) do { logrintf(ZHLOG_WARN, obj, "%s(): " fmt, __func__, ## args); } while(0)
#define log_error(obj, fmt, args...) do { logrintf(ZHLOG_ERROR, obj, "%s(): " fmt, __func__, ## args); } while(0)
#define log_fatal(obj, fmt, args...) do { logrintf(ZHLOG_FATAL, obj, "%s(): " fmt, __func__, ## args); } while(0)

const char *log_level_name[] = { "null", "fatal", "error", "warn ", "info ", "trace" };

static void logsink_stderr(const int level, const char *buf, const uint32_t len ATTR_UNUSED) {
fprintf(stderr, "[%s] %s\n", log_level_name[level], buf);
}

//}

static int force_ucs2_charmap(FT_Face ftf) {
for(int i = 0; i < ftf->num_charmaps; i++)
if (( (ftf->charmaps[i]->platform_id == 0)
Expand All @@ -111,7 +148,7 @@ static int force_ucs2_charmap(FT_Face ftf) {
zhban_t *zhban_open(const void *data, const uint32_t datalen, uint32_t pixheight,
uint32_t subpx,
uint32_t glyphlimit, uint32_t shaperlimit, uint32_t renderlimit,
int32_t loglevel, logsink_t logsink) {
int32_t loglevel, zhban_logsink_t logsink) {

zhban_internal_t *rv = malloc(sizeof(zhban_internal_t));
if (!rv)
Expand All @@ -123,7 +160,7 @@ zhban_t *zhban_open(const void *data, const uint32_t datalen, uint32_t pixheight
rv->outer.bitmap_limit = renderlimit;

rv->log_level = loglevel;
rv->log_sink = logsink ? logsink : printfsink;
rv->log_sink = logsink ? logsink : logsink_stderr;

if ((rv->ft_err = FT_Init_FreeType(&rv->ft_lib)))
goto error;
Expand Down
4 changes: 2 additions & 2 deletions zhban.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ typedef struct _zhban_bitmap {
#define ZHLOG_ERROR 2
#define ZHLOG_FATAL 1

typedef void (*logsink_t)(const int level, const char *fmt, va_list ap);
typedef void (*zhban_logsink_t)(const int level, const char *buffer, const uint32_t len);

/* prepare to use a font face that FreeType2 can handle.
data, size - buffer with the font data (must not be freed or modified before drop() call)
Expand All @@ -121,7 +121,7 @@ ZHB_EXPORT zhban_t *zhban_open(const void *data, const uint32_t size,
uint32_t pixheight,
uint32_t subpixel_positioning,
uint32_t glyphlimit, uint32_t sizerlimit, uint32_t renderlimit,
int llevel, logsink_t lsink);
int llevel, zhban_logsink_t lsink);
ZHB_EXPORT void zhban_drop(zhban_t *);

/* HarfBuzz specifics for non-latin/cyrillic scripts:
Expand Down

0 comments on commit 33c102c

Please sign in to comment.