#include "plat.h" #ifdef plat_posix #ifndef _POSIX_SOURCE #define _POSIX_SOURCE #endif #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include #include #include #include #include #include #include #include extern int isatty(int); extern int fileno(FILE*); int imp_assert( int val, const char* expr, const char* file, int line ) { if (!val) { print_err( "%d:%s: Assertion failed: %s.\n", line, file, expr ); pbreak(420); return 0; } return 1; } void print(const char* fmt, ...) { va_list args; va_start(args, fmt); vfprintf(stdout, fmt, args); va_end(args); } void print_err(const char* fmt, ...) { va_list args; va_start(args, fmt); if (isatty(fileno(stderr))) { fprintf(stderr, "\033[31;31m"); } vfprintf(stderr, fmt, args); if (isatty(fileno(stderr))) { fprintf(stderr, "\033[0m"); } va_end(args); } void print_war(const char* fmt, ...) { va_list args; va_start(args, fmt); if (isatty(fileno(stderr))) { fprintf(stderr, "\033[31;35m"); } vfprintf(stderr, fmt, args); if (isatty(fileno(stderr))) { fprintf(stderr, "\033[0m"); } va_end(args); } void pbreak(int code) { #if defined(DEBUG) && defined(plat_x86) __asm__("int3;"); (void)code; #else exit(code); #endif } static clockid_t global_clock; static unsigned long global_freq; static int time_init = 0; void init_timer(void) { struct timespec ts; global_clock = CLOCK_REALTIME; global_freq = 1000000000; time_init = 1; #if defined(_POSIX_MONOTONIC_CLOCK) if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { global_clock = CLOCK_MONOTONIC; } #else (void)ts; #endif } uint64_t get_time(void) { struct timespec ts; if (!time_init) init_timer(); clock_gettime(global_clock, &ts); return (uint64_t)ts.tv_sec * global_freq + (uint64_t)ts.tv_nsec; } #endif #ifdef plat_win #define _POSIX_SOURCE #define _GNU_SOURCE #include #include #include #include #include int imp_assert( int val, const char* expr, const char* file, int line ) { if (!val) { print_err( "%d:%s: Assertion failed: %s.\n", line, file, expr ); pbreak(420); return 0; } return 1; } void print(const char* fmt, ...) { char buf[1024]; va_list args; va_start(args, fmt); vsprintf(buf, fmt, args); va_end(args); OutputDebugStringA(buf); } void print_err(const char* fmt, ...) { char buf[1024]; va_list args; va_start(args, fmt); vsprintf(buf, fmt, args); va_end(args); OutputDebugStringA(buf); } void print_war(const char* fmt, ...) { char buf[1024]; va_list args; va_start(args, fmt); vsprintf(buf, fmt, args); va_end(args); OutputDebugStringA(buf); } void pbreak(int code) { #if defined(DEBUG) && defined(plat_x86) __debugbreak(); (void)code; #else exit(code); #endif } #endif