-
Notifications
You must be signed in to change notification settings - Fork 5
/
logger.c
128 lines (90 loc) · 1.99 KB
/
logger.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/param.h>
#include <time.h>
#include "compat.h"
#include "logger.h"
#define DEV_EVENT "/dev/input/event"
struct log
{
FILE *file;
int dev;
};
static void logger_set_devname(char *buf, size_t len, const char *devid)
{
snprintf(buf, len, "%s%s", DEV_EVENT, devid);
}
static void logger_write_time(struct log *log, const char *str)
{
time_t t;
const char *timing;
t = time(NULL);
timing = ctime(&t);
fprintf(log->file, "[%.*s] %s", (int)strlen(str)-1, timing, str);
}
static void logger_start(struct log *log)
{
logger_write_time(log, "start");
}
static void logger_stop(struct log *log)
{
logger_write_time(log, "stop");
}
struct log *logger_open(const char *logname, const char *devid)
{
struct log *log;
char devname[sizeof(DEV_EVENT "1234")];
if ( logname == NULL || devid == NULL )
return NULL;
if ( (log = malloc(sizeof(struct log))) == NULL )
return NULL;
/* Dash stands for stdout
*/
if ( strlen(logname) == 1 && *logname == '-' )
{
log->file = stdout;
}
else if ( (log->file = fopen(logname, "a")) == NULL )
{
logger_close(log);
return NULL;
}
logger_set_devname(devname, sizeof(devname), devid);
if ( (log->dev = open(devname, O_RDONLY)) < 0 )
{
logger_close(log);
return NULL;
}
logger_start(log);
return log;
}
void logger_close(struct log *log)
{
if ( log == NULL )
return;
logger_stop(log);
if ( log->file != NULL && log->file != stdout )
fclose(log->file);
if ( log->dev > 0 )
close(log->dev);
free(log);
}
int logger_get_event(struct input_event *ev, struct log *log)
{
int ret;
if ( ev == NULL || log == NULL )
return -1;
ret = read(log->dev, ev, sizeof(struct input_event));
return ret == sizeof(struct input_event) ? 0 : 1;
}
int logger_save(const char *key, struct log *log)
{
int ret;
if ( key == NULL || log == NULL )
return -1;
ret = fprintf(log->file, "%s", key);
return ret == strlen(key) ? 0 : 1;
}