1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
/**
* General purpose logger class.
*
* Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>
*
* This file is subject to the terms and conditions of the GNU General
* Public License, version 2.
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include "Logger.h"
const char *Logger::_LOGGER_DATE_FMT = "%b %d %Y %H:%M:%S";
int Logger::_log_vfprintf(FILE *f, const char *prefix, const char *fmt, va_list ap)
{
struct timeval now;
char buf[64];
int ret;
if (gettimeofday(&now, NULL))
return -EINVAL;
strftime(buf, sizeof(buf), _LOGGER_DATE_FMT, localtime(&now.tv_sec));
ret = fprintf(f, "[%s.%03lu] %s%s", buf, now.tv_usec / 1000,
prefix ? prefix : "", prefix ? ": " : "");
return vfprintf(f, fmt, ap);
}
int Logger::log(const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = _log_vfprintf(_f_out, NULL, fmt, ap);
va_end(ap);
return ret;
}
int Logger::err(const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = _log_vfprintf(_f_err, "Error", fmt, ap);
va_end(ap);
return ret;
}
|