forked from google/xsecurelock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.c
40 lines (36 loc) · 929 Bytes
/
logging.c
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
#include "logging.h"
#include <errno.h> // for errno
#include <stdarg.h> // for va_end, va_list, va_start
#include <stdio.h> // for fputs, stderr, vfprintf, perror, NULL
#include <time.h>
#include <unistd.h>
static void PrintLogPrefix(void) {
time_t t = time(NULL);
struct tm tm_buf;
struct tm *tm = gmtime_r(&t, &tm_buf);
char s[32];
if (!strftime(s, sizeof(s), "%Y-%m-%dT%H:%M:%SZ ", tm)) {
*s = 0;
}
fprintf(stderr, "%s%ld xsecurelock: ", s, (long)getpid());
}
void Log(const char *format, ...) {
va_list args;
va_start(args, format);
PrintLogPrefix();
vfprintf(stderr, format, args);
fputs(".\n", stderr);
va_end(args);
}
void LogErrno(const char *format, ...) {
int errno_save = errno;
va_list args;
va_start(args, format);
PrintLogPrefix();
vfprintf(stderr, format, args);
fputs(": ", stderr);
errno = errno_save;
perror(NULL);
va_end(args);
errno = errno_save;
}