From 0922dd1fd6e9e242663b6e4169a95213261531e4 Mon Sep 17 00:00:00 2001 From: tangxinfa Date: Wed, 30 Jul 2025 16:19:36 +0800 Subject: [PATCH] fix: build and run on windows with MinGW --- src/event.c | 3 ++- src/fmacros.h | 6 ++++++ src/spec.c | 16 ++++++++++------ src/zc_xplatform.h | 2 +- src/zlog-chk-conf.c | 4 ++++ 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/event.c b/src/event.c index 304b7e35..ee779a00 100644 --- a/src/event.c +++ b/src/event.c @@ -28,8 +28,9 @@ #include #include +#if defined __linux__ || __APPLE__ #include - +#endif #include "zc_defs.h" #include "event.h" #ifdef _WIN32 diff --git a/src/fmacros.h b/src/fmacros.h index 18b01cb5..68d276f4 100644 --- a/src/fmacros.h +++ b/src/fmacros.h @@ -28,6 +28,12 @@ #endif #endif +#ifdef _WIN32 +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200809L +#endif +#endif + #ifndef _LARGEFILE_SOURCE #define _LARGEFILE_SOURCE #endif diff --git a/src/spec.c b/src/spec.c index 24895f9d..1a9dbc35 100644 --- a/src/spec.c +++ b/src/spec.c @@ -62,16 +62,12 @@ static int zlog_spec_write_time_internal(zlog_spec_t * a_spec, zlog_thread_t * a time_t now_sec = a_thread->event->time_stamp.tv_sec; struct tm *time; time_t *time_sec; - typedef struct tm *(*zlog_spec_time_fn) (const time_t*, struct tm *); - zlog_spec_time_fn time_stamp_convert_function; if( use_utc ) { time = &(a_thread->event->time_utc); time_sec = &(a_thread->event->time_utc_sec); - time_stamp_convert_function = gmtime_r; } else { time = &(a_thread->event->time_local); time_sec = &(a_thread->event->time_local_sec); - time_stamp_convert_function = localtime_r; } /* the event meet the 1st time_spec in his life cycle */ @@ -82,7 +78,11 @@ static int zlog_spec_write_time_internal(zlog_spec_t * a_spec, zlog_thread_t * a /* When this event's last cached time_local is not now */ if (*time_sec != now_sec) { - time_stamp_convert_function(&(now_sec), time); + if (use_utc) { + gmtime_r(&(now_sec), time); + } else { + localtime_r(&(now_sec), time); + } *time_sec = now_sec; } @@ -241,7 +241,7 @@ static int zlog_spec_write_pid(zlog_spec_t * a_spec, zlog_thread_t * a_thread, z if (a_thread->event->pid != a_thread->event->last_pid) { a_thread->event->last_pid = a_thread->event->pid; a_thread->event->pid_str_len - = sprintf(a_thread->event->pid_str, "%u", a_thread->event->pid); + = sprintf(a_thread->event->pid_str, "%ld", (long)a_thread->event->pid); } } @@ -264,6 +264,7 @@ static int zlog_spec_write_tid_long(zlog_spec_t * a_spec, zlog_thread_t * a_thre return zlog_buf_append(a_buf, a_thread->event->tid_str, a_thread->event->tid_str_len); } +#if defined __linux__ || __APPLE__ static int zlog_spec_write_ktid(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { @@ -271,6 +272,7 @@ static int zlog_spec_write_ktid(zlog_spec_t * a_spec, zlog_thread_t * a_thread, /* and fork not change tid */ return zlog_buf_append(a_buf, a_thread->event->ktid_str, a_thread->event->ktid_str_len); } +#endif static int zlog_spec_write_level_lowercase(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { @@ -647,9 +649,11 @@ zlog_spec_t *zlog_spec_new(char *pattern_start, char **pattern_next, int *time_c case 'H': a_spec->write_buf = zlog_spec_write_hostname; break; +#if defined __linux__ || __APPLE__ case 'k': a_spec->write_buf = zlog_spec_write_ktid; break; +#endif case 'L': a_spec->write_buf = zlog_spec_write_srcline; break; diff --git a/src/zc_xplatform.h b/src/zc_xplatform.h index 5e62fe0d..8d56c160 100644 --- a/src/zc_xplatform.h +++ b/src/zc_xplatform.h @@ -52,7 +52,7 @@ #if defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_6) #define zlog_fstat fstat64 #define zlog_stat stat64 -#elif defined(_WIN32) +#elif defined(_WIN32) && !defined(__MINGW32__) #define zlog_fstat _fstat #define zlog_stat _stat #else diff --git a/src/zlog-chk-conf.c b/src/zlog-chk-conf.c index d4cf1238..992396e1 100644 --- a/src/zlog-chk-conf.c +++ b/src/zlog-chk-conf.c @@ -20,7 +20,11 @@ #include #include +#ifndef _WIN32 #include +#else +#include "zlog_win.h" +#endif #include "zlog.h" #include "version.h"