1 /*
2 * SNOOPY LOGGER
3 *
4 * File: snoopy/datasource/timestamp_ms.c
5 *
6 * Copyright (c) 2015 Bostjan Skufca <bostjan@a2o.si>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23
24
25 /*
26 * Includes order: from local to global
27 */
28 #include "timestamp_ms.h"
29
30 #include "snoopy.h"
31
32 #include <errno.h>
33 #include <stdio.h>
34 #include <sys/time.h>
35
36
37
38 /*
39 * SNOOPY DATA SOURCE: timestamp_ms
40 *
41 * Description:
42 * Returns milliseconds part of current Unix timestamp.
43 *
44 * Params:
45 * result: pointer to string, to write result into
46 * arg: (ignored)
47 *
48 * Return:
49 * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE
50 */
snoopy_datasource_timestamp_ms(char * const result,char const * const arg)51 int snoopy_datasource_timestamp_ms (char * const result, char const * const arg)
52 {
53 struct timeval tv;
54 int retVal;
55
56 retVal = gettimeofday(&tv, NULL);
57 if (0 == retVal) {
58 return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%03d", (int) tv.tv_usec/1000);
59 } else {
60 return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(error: %d)", errno);
61 }
62 }
63