-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.h
74 lines (58 loc) · 1.89 KB
/
log.h
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
#ifndef _LOG_H
#define _LOG_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
#include <stdio.h>
FILE *new_log(const char *name, long id);
FILE *open_log(const char *name, long id);
// enum { LOG_BUFFER_SIZE = 4 * 1024 * 1024 }; // For testing, make it small to make enlarge necessary.
enum { LOG_BUFFER_SIZE = 4 * 1024 }; // For testing, make it small to make enlarge necessary.
struct mapped_log {
char *start;
char *buf; // buf can be changed
char *end;
int fd;
};
int new_mapped_log(const char *name, int id, struct mapped_log *log);
int new_mapped_log_path(const char *name, struct mapped_log *log);
int enlarge_mapped_log(struct mapped_log *log);
// The following two function return 0 on success.
int open_mapped_log(const char *name, int id, struct mapped_log *log);
int open_mapped_log_path(const char *path, struct mapped_log *log);
int truncate_log(struct mapped_log *log);
int unmap_log(struct mapped_log *log);
int unmap_truncate_log(struct mapped_log *log);
void *create_mapped_file(const char *name, unsigned long size);
static inline char *next_log_entry(struct mapped_log *log, int entry_size) {
if ((log->buf + entry_size) > log->end) {
enlarge_mapped_log(log);
}
char *start = log->buf;
log->buf += entry_size;
return start;
}
static inline char *read_log_entry(struct mapped_log *log, int entry_size) {
if ((log->buf + entry_size) > log->end) {
return NULL;
}
char *start = log->buf;
log->buf += entry_size;
return start;
}
static inline int log_end(struct mapped_log *log) {
return log->buf >= log->end;
}
#define LOGDIR "replay-log/"
#define MAX_PATH_LEN 256
static inline void logpath(char *buf, const char *name, long id) {
if (snprintf(buf, MAX_PATH_LEN, LOGDIR"%s-%ld", name, id) >= MAX_PATH_LEN) {
printf("Path name too long\n");
exit(1);
}
}
#ifdef __cplusplus
}
#endif
#endif