diff --git a/src/main/util/Module.mk b/src/main/util/Module.mk index ab8d495..e6dc7c4 100644 --- a/src/main/util/Module.mk +++ b/src/main/util/Module.mk @@ -9,7 +9,6 @@ src_util := \ hex.c \ iobuf.c \ list.c \ - log.c \ math.c \ mem.c \ msg-thread.c \ @@ -18,7 +17,6 @@ src_util := \ proc.c \ signal.c \ str.c \ - thread.c \ time.c \ winres.c \ diff --git a/src/main/util/log.c b/src/main/util/log.c deleted file mode 100644 index 7da1d63..0000000 --- a/src/main/util/log.c +++ /dev/null @@ -1,134 +0,0 @@ -#include "util/log.h" -#include "util/str.h" - -#include - -#include -#include -#include -#include - -static log_writer_t log_writer; -static void *log_writer_ctx; -static enum log_level log_level; - -static void log_builtin_fatal(const char *module, const char *fmt, ...); -static void log_builtin_info(const char *module, const char *fmt, ...); -static void log_builtin_misc(const char *module, const char *fmt, ...); -static void log_builtin_warning(const char *module, const char *fmt, ...); -static void log_builtin_format( - enum log_level msg_level, const char *module, const char *fmt, va_list ap); - -#define IMPLEMENT_SINK(name, msg_level) \ - static void name(const char *module, const char *fmt, ...) \ - { \ - va_list ap; \ - \ - va_start(ap, fmt); \ - log_builtin_format(msg_level, module, fmt, ap); \ - va_end(ap); \ - } - -IMPLEMENT_SINK(log_builtin_info, LOG_LEVEL_INFO) -IMPLEMENT_SINK(log_builtin_misc, LOG_LEVEL_MISC) -IMPLEMENT_SINK(log_builtin_warning, LOG_LEVEL_WARNING) - -static void log_builtin_fatal(const char *module, const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - log_builtin_format(LOG_LEVEL_FATAL, module, fmt, ap); - va_end(ap); - - DebugBreak(); - ExitProcess(EXIT_FAILURE); -} - -static void log_builtin_format( - enum log_level msg_level, const char *module, const char *fmt, va_list ap) -{ - static const char chars[] = "FWIM"; - - /* 64k so we can log data dumps of rs232 without crashing */ - char line[65536]; - char msg[65536]; - int result; - - if (msg_level <= log_level) { - str_vformat(msg, sizeof(msg), fmt, ap); - result = str_format( - line, sizeof(line), "%c:%s: %s\n", chars[msg_level], module, msg); - - log_writer(log_writer_ctx, line, result); - } -} - -void log_assert_body(const char *file, int line, const char *function) -{ - log_impl_fatal("assert", "%s:%d: function `%s'", file, line, function); -} - -void log_to_external( - log_formatter_t misc, - log_formatter_t info, - log_formatter_t warning, - log_formatter_t fatal) -{ - log_impl_misc = misc; - log_impl_info = info; - log_impl_warning = warning; - log_impl_fatal = fatal; -} - -void log_to_writer(log_writer_t writer, void *ctx) -{ - log_impl_misc = log_builtin_misc; - log_impl_info = log_builtin_info; - log_impl_warning = log_builtin_warning; - log_impl_fatal = log_builtin_fatal; - - if (writer != NULL) { - log_writer = writer; - log_writer_ctx = ctx; - } else { - log_writer = log_writer_null; - } -} - -void log_set_level(enum log_level new_level) -{ - log_level = new_level; -} - -void log_writer_debug(void *ctx, const char *chars, size_t nchars) -{ - OutputDebugStringA(chars); -} - -void log_writer_stdout(void *ctx, const char *chars, size_t nchars) -{ - printf("%s", chars); -} - -void log_writer_stderr(void *ctx, const char *chars, size_t nchars) -{ - fprintf(stderr, "%s", chars); -} - -void log_writer_file(void *ctx, const char *chars, size_t nchars) -{ - fwrite(chars, 1, nchars, (FILE *) ctx); - fflush((FILE *) ctx); -} - -void log_writer_null(void *ctx, const char *chars, size_t nchars) -{ -} - -log_formatter_t log_impl_misc = log_builtin_misc; -log_formatter_t log_impl_info = log_builtin_info; -log_formatter_t log_impl_warning = log_builtin_warning; -log_formatter_t log_impl_fatal = log_builtin_fatal; -static log_writer_t log_writer = log_writer_null; -static enum log_level log_level = LOG_LEVEL_MISC; diff --git a/src/main/util/log.h b/src/main/util/log.h deleted file mode 100644 index 47603fa..0000000 --- a/src/main/util/log.h +++ /dev/null @@ -1,90 +0,0 @@ -#ifndef UTIL_LOG_H -#define UTIL_LOG_H - -#include -#include - -#include "bemanitools/glue.h" - -#include "util/defs.h" - -/* Dynamically retargetable logging system modeled on (and potentially - integrateable with) the one found in AVS2 */ - -/* BUILD_MODULE is passed in as a command-line #define by the makefile */ - -#ifndef LOG_MODULE -#define LOG_MODULE STRINGIFY(BUILD_MODULE) -#endif - -#ifndef LOG_SUPPRESS - -#define log_misc(...) log_impl_misc(LOG_MODULE, __VA_ARGS__) -#define log_info(...) log_impl_info(LOG_MODULE, __VA_ARGS__) -#define log_warning(...) log_impl_warning(LOG_MODULE, __VA_ARGS__) - -/* This doesn't really belong here, but it's what libavs does so w/e */ - -#define log_assert(x) \ - do { \ - if (!(x)) { \ - log_assert_body(__FILE__, __LINE__, __FUNCTION__); \ - } \ - } while (0) - -#else - -#define log_misc(...) -#define log_info(...) -#define log_warning(...) -#define log_assert(x) \ - do { \ - if (!(x)) { \ - abort(); \ - } \ - } while (0) - -#endif - -#define log_fatal(...) \ - do { \ - log_impl_fatal(LOG_MODULE, __VA_ARGS__); \ - abort(); \ - } while (0) - -typedef void (*log_writer_t)(void *ctx, const char *chars, size_t nchars); - -extern log_formatter_t log_impl_misc; -extern log_formatter_t log_impl_info; -extern log_formatter_t log_impl_warning; -extern log_formatter_t log_impl_fatal; - -enum log_level { - LOG_LEVEL_FATAL = 0, - LOG_LEVEL_WARNING = 1, - LOG_LEVEL_INFO = 2, - LOG_LEVEL_MISC = 3, -}; - -void log_assert_body(const char *file, int line, const char *function); -void log_to_external( - log_formatter_t misc, - log_formatter_t info, - log_formatter_t warning, - log_formatter_t fatal); -void log_to_writer(log_writer_t writer, void *ctx); - -void log_set_level(enum log_level new_level); - -/* I tried to make this API match the function signature of the AVS log writer - callback, but then the signature changed and the explicit line breaks - being passed to that callback went away. So we don't try to track that API - any more. Launcher defines its own custom writer anyway. */ - -void log_writer_debug(void *ctx, const char *chars, size_t nchars); -void log_writer_stdout(void *ctx, const char *chars, size_t nchars); -void log_writer_stderr(void *ctx, const char *chars, size_t nchars); -void log_writer_file(void *ctx, const char *chars, size_t nchars); -void log_writer_null(void *ctx, const char *chars, size_t nchars); - -#endif diff --git a/src/main/util/thread.c b/src/main/util/thread.c deleted file mode 100644 index daa8267..0000000 --- a/src/main/util/thread.c +++ /dev/null @@ -1,92 +0,0 @@ -#include -#include - -#include -#include - -#include "util/defs.h" -#include "util/thread.h" - -struct shim_ctx { - HANDLE barrier; - int (*proc)(void *); - void *ctx; -}; - -thread_create_t thread_impl_create = crt_thread_create; -thread_join_t thread_impl_join = crt_thread_join; -thread_destroy_t thread_impl_destroy = crt_thread_destroy; - -static unsigned int STDCALL crt_thread_shim(void *outer_ctx) -{ - struct shim_ctx *sctx = outer_ctx; - int (*proc)(void *); - void *inner_ctx; - - proc = sctx->proc; - inner_ctx = sctx->ctx; - - SetEvent(sctx->barrier); - - return proc(inner_ctx); -} - -int crt_thread_create( - int (*proc)(void *), void *ctx, uint32_t stack_sz, unsigned int priority) -{ - struct shim_ctx sctx; - uintptr_t thread_id; - - sctx.barrier = CreateEvent(NULL, TRUE, FALSE, NULL); - sctx.proc = proc; - sctx.ctx = ctx; - - thread_id = _beginthreadex(NULL, stack_sz, crt_thread_shim, &sctx, 0, NULL); - - WaitForSingleObject(sctx.barrier, INFINITE); - CloseHandle(sctx.barrier); - - return (int) thread_id; -} - -void crt_thread_destroy(int thread_id) -{ - CloseHandle((HANDLE) (uintptr_t) thread_id); -} - -void crt_thread_join(int thread_id, int *result) -{ - WaitForSingleObject((HANDLE) (uintptr_t) thread_id, INFINITE); - - if (result) { - GetExitCodeThread((HANDLE) (uintptr_t) thread_id, (DWORD *) result); - } -} - -void thread_api_init( - thread_create_t create, thread_join_t join, thread_destroy_t destroy) -{ - if (create == NULL || join == NULL || destroy == NULL) { - abort(); - } - - thread_impl_create = create; - thread_impl_join = join; - thread_impl_destroy = destroy; -} - -int thread_create( - int (*proc)(void *), void *ctx, uint32_t stack_sz, unsigned int priority) -{ - return thread_impl_create(proc, ctx, stack_sz, priority); -} - -void thread_join(int thread_id, int *result) -{ - thread_impl_join(thread_id, result); -} - -void thread_destroy(int thread_id) -{ - thread_impl_destroy(thread_id); -} diff --git a/src/main/util/thread.h b/src/main/util/thread.h deleted file mode 100644 index dca396b..0000000 --- a/src/main/util/thread.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef UTIL_THREAD_H -#define UTIL_THREAD_H - -#include - -#include "bemanitools/glue.h" - -int crt_thread_create( - int (*proc)(void *), void *ctx, uint32_t stack_sz, unsigned int priority); -void crt_thread_join(int thread_id, int *result); -void crt_thread_destroy(int thread_id); - -void thread_api_init( - thread_create_t create, thread_join_t join, thread_destroy_t destroy); -int thread_create( - int (*proc)(void *), void *ctx, uint32_t stack_sz, unsigned int priority); -void thread_join(int thread_id, int *result); -void thread_destroy(int thread_id); - -#endif