%% options copyright owner = Dirk Krause copyright year = 2011-2014 license = bsd %% header #include "dk3conf.h" #include "dk3types.h" #include #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 DK3_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) ((x) ? (x) : L"(NULL)") /** Show pointer. */ #define TR_PTR(x) ((x) ? L"PTR" : L"(NULL)") /** Show long string. */ #define TR_LSTR(x) ((x) ? (x) : L"(NULL)") #else /* if TRACE_WIDE */ /** Show string. */ #define TR_STR(x) ((x) ? (x) : "(NULL)") /** Show pointer. */ #define TR_PTR(x) ((x) ? "PTR" : "(NULL)") /** Show long string. */ #define TR_LSTR(x) ((x) ? (x) : L"(NULL)") #endif /* if TRACE_WIDE */ %% module #include "dk3all.h" #include #if DK3_TIME_WITH_SYS_TIME #include #include #else #if DK3_HAVE_SYS_TIME_H #include #else #if DK3_HAVE_TIME_H #include #endif #endif #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; if(filename) { #if DK3_HAVE_LARGEFILE64_SOURCE && DK3_HAVE_FOPEN64 x = fopen64(filename, "w"); #else x = fopen(filename, "w"); #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) /* {{{ */ { #if DK3_HAVE_TIME_H || DK3_HAVE_SYS_TIME_H time_t timer; struct tm *tm; if(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 } /* }}} */ #if DK3_ON_WINDOWS /** Write time to output file. @param f Output file. */ static void dk3trace_i_wtime(FILE *f) /* {{{ */ { #if DK3_HAVE_TIME_H || DK3_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 : */