libdc/src/utils.c
Jef Driesen e65025b501 Separate private and public headers.
The public header files are moved to a new subdirectory, to separate
the definition of the public interface from the actual implementation.
Using an identical directory layout as the final installation has the
advantage that the example code can be build outside the project tree
without any modifications to the #include statements.
2012-06-21 21:39:24 +02:00

95 lines
2.2 KiB
C

/*
* libdivecomputer
*
* Copyright (C) 2008 Jef Driesen
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <libdivecomputer/utils.h>
static FILE* g_logfile = NULL;
static unsigned char g_lastchar = '\n';
#ifdef _WIN32
#include <windows.h>
static unsigned long g_timestamp;
#else
#include <sys/time.h>
static struct timeval g_timestamp;
#endif
int message (const char* fmt, ...)
{
va_list ap;
if (g_logfile) {
if (g_lastchar == '\n') {
#ifdef _WIN32
unsigned long timestamp = GetTickCount () - g_timestamp;
unsigned long sec = timestamp / 1000L, msec = timestamp % 1000L;
fprintf (g_logfile, "[%li.%03li] ", sec, msec);
#else
struct timeval now, timestamp;
gettimeofday (&now, NULL);
timersub (&now, &g_timestamp, &timestamp);
fprintf (g_logfile, "[%lli.%06lli] ", (long long)timestamp.tv_sec, (long long)timestamp.tv_usec);
#endif
}
size_t len = strlen (fmt);
if (len > 0)
g_lastchar = fmt[len - 1];
else
g_lastchar = 0;
va_start (ap, fmt);
vfprintf (g_logfile, fmt, ap);
va_end (ap);
}
va_start (ap, fmt);
int rc = vfprintf (stderr, fmt, ap);
va_end (ap);
return rc;
}
void message_set_logfile (const char* filename)
{
if (g_logfile) {
fclose (g_logfile);
g_logfile = NULL;
}
if (filename)
g_logfile = fopen (filename, "w");
if (g_logfile) {
g_lastchar = '\n';
#ifdef _WIN32
g_timestamp = GetTickCount ();
#else
gettimeofday (&g_timestamp, NULL);
#endif
}
}