/* Copyright (C) 2011-2020, Dirk Krause SPDX-License-Identifier: BSD-3-Clause */ /* WARNING: This file was generated by the dkct program (see http://dktools.sourceforge.net/ for details). Changes you make here will be lost if dkct is run again! You should modify the original source and run dkct on it. Original source: dk3trace.ctr */ /** @file dk3trace.c The dk3trace module. */ #line 170 "dk3trace.ctr" #include "dk4conf.h" #if _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) { #if _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) /* {{{ */ { #if _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 _WIN32 /** 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 _WIN32 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 : */