%% options copyright owner = Dirk Krause copyright year = 2011-xxxx license = bsd %% header #ifndef STDIO_H_INCLUDED #include #define STDIO_H_INCLUDED 1 #endif #if defined(__cplusplus) extern "C" { #endif /** Close trace output file. */ void dktrace_end(void); /** Start tracing. @param filename File name to write trace output or NULL to use standard output. */ void dktrace_init( char const *filename ); /** Get trace file. @return Pointer to open file used for tracing. */ FILE * dktrace_file(void); /** Close trace output file. */ void dk_trace_end(void); /** Start tracing. @param filename File name to write trace output or NULL to use standard output. */ void dk_trace_init( char const *filename ); /** Get trace file. @return Pointer to open file used for tracing. */ FILE * dk_trace_file(void); /** Write time to trace output. */ void dktrace_time(void); /** Write time to trace output. */ void dk_trace_time(void); /** Write time to standard output. */ void dktrace_stdout_time(void); #if DK4_ON_WINDOWS /** Write time to trace output. */ void dk_trace_wtime(void); /** Write time to standard output. */ void dktrace_stdout_wtime(void); #endif #if defined(__cplusplus) } #endif /* ifdef __cplusplus */ #if TRACE_WIDE /** Show string. */ #define TR_STR(x) ((NULL != x) ? (x) : L"(NULL)") /** Show pointer. */ #define TR_PTR(x) ((NULL != x) ? L"PTR" : L"(NULL)") #else /* if TRACE_WIDE */ /** Show string. */ #define TR_STR(x) ((NULL != x) ? (x) : "(NULL)") /** Show pointer. */ #define TR_PTR(x) ((NULL != x) ? "PTR" : "(NULL)") #endif /* if TRACE_WIDE */ /** Show 8 bit string. */ #define TR_8STR(x) ((NULL != x) ? (x) : "(NULL)") /** Show pointer as 8 bit string. */ #define TR_8PTR(x) ((NULL != x) ? "PTR" : "(NULL)") /** Show long string. */ #define TR_LSTR(x) ((NULL != x) ? (x) : L"(NULL)") /** Show pointer as long string. */ #define TR_LPTR(x) ((NULL != x) ? L"PTR" : L"(NULL)") /** Show string as dkChar. */ #define TR_DKSTR(x) ((NULL != x) ? (x) : dkT("NULL")) /** Show pointer as dkChar. */ #define TR_DKPTR(x) ((NULL != x) ? dkT("PTR") : dkT("(NULL)")) /** Show string as wxChar. */ #define TR_WXSTR(x) ((NULL != x) ? (x) : wxT("(NULL)")) /** Show pointer as wxChar. */ #define TR_WXPTR(x) ((NULL != x) ? wxT("PTR") : wxT("(NULL)")) /** Show pointer as integer. */ #define TR_IPTR(x) ((NULL != x) ? 1 : 0) %% module #include "dk4conf.h" #ifdef _WIN32 #ifndef WINDOWS_H_INCLUDED #include #define WINDOWS_H_INCLUDED 1 #endif #endif #ifndef STDIO_H_INCLUDED #include #define STDIO_H_INCLUDED 1 #endif #ifndef TIME_H_INCLUDED #include #define TIME_H_INCLUDED 1 #endif /** Inside the dk3trace module. */ #define DK3_TRACE_C 1 #include "dk3trace.h" /** Output file. */ static FILE *trace_file = NULL; /** Timestamp of previous trace message. */ static time_t dk3trace_last_time = (time_t)0UL; void dktrace_end(void) /* {{{ */ { if(trace_file) { (void)fclose(trace_file); trace_file = NULL; } } /* }}} */ void dk_trace_end(void) /* {{{*/ { dktrace_end(); } /* }}} */ void dktrace_init(char const *filename) /* {{{ */ { FILE *x = NULL; if(filename) { #ifdef _WIN32 if (0 != fopen_s(&x, filename, "w")) { x = NULL; } #else #if DK4_HAVE_LARGEFILE64_SOURCE && DK4_HAVE_FOPEN64 x = fopen64(filename, "w"); #else x = fopen(filename, "w"); #endif #endif if(x) { dktrace_end(); trace_file = x; } } } /* }}} */ void dk_trace_init(char const *filename) /* {{{ */ { dktrace_init(filename); } /* }}} */ FILE * dktrace_file(void) /* {{{ */ { return trace_file; } /* }}} */ FILE * dk_trace_file(void) /* {{{ */ { FILE *back; back = dktrace_file(); return back; } /* }}} */ /** Write time to output file. @param f Output file. */ static void dk3trace_i_time(FILE *f) /* {{{ */ { #ifdef _WIN32 SYSTEMTIME st; if (NULL != f) { GetLocalTime(&st); fprintf( f, "%04d/%02d/%02d %02d:%02d:%02d\n", (int)(st.wYear), (int)(st.wMonth), (int)(st.wDay), (int)(st.wHour), (int)(st.wMinute), (int)(st.wSecond) ); } #else #if DK4_HAVE_TIME_H || DK4_HAVE_SYS_TIME_H time_t timer; struct tm *tm; if(NULL != f) { (void)time(&timer); if(dk3trace_last_time != timer) { dk3trace_last_time = timer; tm = localtime(&timer); if(tm) { fprintf( f, "# %04d/%02d/%02d %02d:%02d:%02d\n", (1900 + tm->tm_year), (1 + tm->tm_mon), tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec ); } } } #endif #endif } /* }}} */ #if DK3_ON_WINDOWS /** Write time to output file. @param f Output file. */ static void dk3trace_i_wtime(FILE *f) /* {{{ */ { #if DK4_HAVE_TIME_H || DK4_HAVE_SYS_TIME_H time_t timer; struct tm *tm; if(f) { time(&timer); if(dk3trace_last_time != timer) { dk3trace_last_time = timer; tm = localtime(&timer); if(tm) { fwprintf(f, L"# %04d/%02d/%02d %02d:%02d:%02d\n", (1900 + tm->tm_year), (1 + tm->tm_mon), tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec ); } } } #endif } /* }}} */ #endif void dktrace_time(void) /* {{{ */ { if(trace_file) { dk3trace_i_time(trace_file); } } /* }}} */ void dktrace_stdout_time(void) /* {{{ */ { dk3trace_i_time(stdout); } /* }}} */ #if DK3_ON_WINDOWS void dktrace_wtime(void) /* {{{ */ { if(trace_file) { dk3trace_i_wtime(trace_file); } } /* }}} */ void dktrace_stdout_wtime(void) /* {{{ */ { dk3trace_i_wtime(stdout); } /* }}} */ #endif void dk_trace_time(void) /* {{{ */ { dktrace_time(); } /* }}} */ /* vim: set ai sw=2 filetype=c foldmethod=marker foldopen=all : */