embox

Форк
0
/
loop_file_logger_lib.c 
136 строк · 2.7 Кб
1
/**
2
 * @file
3
 *
4
 * @date Aug 3, 2021
5
 * @author Anton Bondarev
6
 */
7

8
#include <assert.h>
9
#include <sys/stat.h>
10
#include <fcntl.h>
11
#include <unistd.h>
12
#include <stdio.h>
13
#include <string.h>
14
#include <stdlib.h>
15

16
#include <lib/loop_file_logger.h>
17

18
#include <framework/mod/options.h>
19

20
#define LOGGER_FILE_NAME OPTION_STRING_GET(file_name)
21
#define RECORD_SIZE      OPTION_GET(NUMBER,record_size)
22
#define RECORD_QUANTITY  OPTION_GET(NUMBER,record_quantity)
23

24
static_assert(RECORD_QUANTITY <= 256, "");
25

26
static char loop_logger_buf[RECORD_SIZE];
27

28
static int find_current_record(int fd, int *label) {
29
	int res;
30
	int i;
31
	char cur_label;
32

33
	res = read(fd, loop_logger_buf, RECORD_SIZE);
34
	if (res != RECORD_SIZE) {
35
		*label = 0;
36
		return 0;
37
	}
38

39
	cur_label = loop_logger_buf[0];
40

41
	for(i = 1; i < RECORD_QUANTITY; i++) {
42
		res = read(fd, loop_logger_buf, RECORD_SIZE);
43
		if (res != RECORD_SIZE) {
44
			break;
45
		}
46
		if (loop_logger_buf[0] != cur_label) {
47
			*label = (loop_logger_buf[0] == '0') ? 1 : 0;
48
			return i;
49
		}
50
	}
51

52
	*label = (cur_label == '0') ? 1 : 0;
53
	return 0;
54
}
55

56
int loop_logger_write(char *message) {
57
	int fd;
58
	int start_idx = -1;
59
	int flip;
60
	char tmp_buf[4];
61
	int len;
62

63
	if (loop_logger_size() < RECORD_QUANTITY) {
64
		start_idx = 0;
65
	}
66
	fd = open(LOGGER_FILE_NAME, O_RDWR);
67
	if (fd < 0) {
68
		return -1;
69
	}
70
	if (start_idx == -1) {
71
		start_idx = find_current_record(fd, &flip);
72
	} else {
73
		start_idx = loop_logger_size();
74
		flip = 0;
75
	}
76
	lseek(fd, start_idx * RECORD_SIZE, SEEK_SET);
77
	memset(loop_logger_buf, 0, RECORD_SIZE);
78
	//snprintf(loop_logger_buf, "%d%02X:%s\n",flip, start_idx,message);
79
	itoa(flip, loop_logger_buf, 10);
80
	itoa(start_idx, tmp_buf, 16);
81
	if (strlen(tmp_buf) == 1) {
82
		strcat(loop_logger_buf,"0");
83
	}
84
	strcat(loop_logger_buf, tmp_buf);
85
	strcat(loop_logger_buf,":");
86
	strncat(loop_logger_buf, message, RECORD_SIZE - 1);
87

88
	len = strlen(loop_logger_buf);
89
	if (len < (RECORD_SIZE - 1)) {
90
		memset(&loop_logger_buf[len], ' ', (RECORD_SIZE - 1) - len);
91
	}
92
	loop_logger_buf[RECORD_SIZE - 1] = '\n';
93
	write(fd, loop_logger_buf, RECORD_SIZE);
94

95
	return 0;
96
}
97

98
int loop_logger_read(int idx_mes, char *out_mes, int buf_size) {
99
	int fd;
100
	int start_idx = -1;
101

102
	if (loop_logger_size() < RECORD_QUANTITY) {
103
		start_idx = 0;
104
	}
105
	fd = open(LOGGER_FILE_NAME, O_RDONLY);
106
	if (fd < 0) {
107
		return -1;
108
	}
109
	if (start_idx == -1) {
110
		int tmp;
111
		start_idx = find_current_record(fd, &tmp);
112
	}
113
	start_idx += idx_mes;
114
	start_idx %= RECORD_QUANTITY;
115
	lseek(fd, start_idx * RECORD_SIZE + 4, SEEK_SET);
116

117
	read(fd, out_mes, buf_size);
118
	close(fd);
119

120
	return 0;
121
}
122

123
int loop_logger_size(void) {
124
	struct stat st;
125

126
	stat(LOGGER_FILE_NAME, &st);
127
	if (st.st_size < RECORD_SIZE * RECORD_QUANTITY) {
128
		return st.st_size / RECORD_SIZE;
129
	}
130

131
	return RECORD_QUANTITY;
132
}
133

134
int loop_logger_message_size(void) {
135
	return RECORD_SIZE - (4 + 1);
136
}
137

138

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.