-
Notifications
You must be signed in to change notification settings - Fork 1
/
logfile.c
66 lines (52 loc) · 1.39 KB
/
logfile.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* logfile.c
*
* Created on: 10.05.2012
* Author: YaroslavLitvinov
*/
#include "logfile.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdarg.h>
#include <sys/time.h>
#include <time.h>
static char __prefix[40];
static FILE *__log_file = NULL;
static char *NaClTimeStampString(char *buffer, size_t buffer_size) {
struct timeval tv;
struct tm bdt; /* broken down time */
if (-1 == gettimeofday(&tv, (struct timezone *) NULL)) {
snprintf(buffer, buffer_size, "-NA-");
return buffer;
}
(void) localtime_r(&tv.tv_sec, &bdt);
/* suseconds_t holds at most 10**6 < 16**6 = (2**4)**6 = 2**24 < 2**32 */
snprintf(buffer, buffer_size, "%02d:%02d:%02d.%06d",
bdt.tm_hour, bdt.tm_min, bdt.tm_sec, (int) tv.tv_usec);
return buffer;
}
/*@return 0 succes, -1 error*/
int log_open_stream(const char *log_file, const char* prefix, int id) {
sprintf(__prefix+10, "[%s:%d]", prefix, id);
int fd;
fd = open(log_file, O_WRONLY | O_APPEND | O_CREAT, 0777);
if (-1 == fd) {
perror("log_open_stream::open\n");
}
__log_file = fdopen(fd, "a");
if (NULL == __log_file) {
perror("log_open_stream::fdopen\n");
}
return !__log_file? -1: 0;
}
const char *log_prefix(){
memset(__prefix, ' ', 10);
NaClTimeStampString( __prefix, 10);
return __prefix;
}
FILE *log_file(){
return __log_file;
}