-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.cpp
153 lines (130 loc) · 3.29 KB
/
Logger.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "Logger.h"
#include <assert.h>
#include <pthread.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#define SIZE 17280 // Enough for 24 hrs of readigns taken every 5 seconds
#define LOG_INTR 60 // Each 5 minutes if readings taken every 5 seconds
Reading ouroboros[SIZE];
int pos; // Current position in Ouroboros
int ints_to_log = LOG_INTR; // Intervals to log in file
FILE* rws_db;
pthread_mutex_t rws_db_lock;
pthread_mutex_t msg_log_lock;
void time2str(time_t t, char* out, bool fname);
void initLogger()
{
pthread_mutex_init(&msg_log_lock, NULL);
pthread_mutex_init(&rws_db_lock, NULL);
// Critical Section Beg
pthread_mutex_lock(&rws_db_lock);
rws_db = fopen("./log/readings.rws", "a+b");
int res = fseek(rws_db, -SIZE * sizeof(Reading), SEEK_END);
res = fread(ouroboros, sizeof(Reading), SIZE, rws_db);
pos = res % SIZE;
pthread_mutex_unlock(&rws_db_lock);
// Critical Section End
}
void deinitLogger()
{
pthread_mutex_destroy(&rws_db_lock);
pthread_mutex_destroy(&msg_log_lock);
// Critical Section Beg
pthread_mutex_lock(&rws_db_lock);
fclose(rws_db);
pthread_mutex_unlock(&rws_db_lock);
// Critical Section End
}
void logReading(Reading rd)
{
ouroboros[pos] = rd;
pos = (pos + 1) % SIZE;
if(pos % LOG_INTR == 0)
{
// Critical Section Beg
if(pthread_mutex_trylock(&rws_db_lock) == 0)
{
int oft = ints_to_log - 1;
while(oft >= 0)
{
Reading tmp = getReading((unsigned int)oft--);
fwrite(&tmp, sizeof(Reading), 1, rws_db);
}
pthread_mutex_unlock(&rws_db_lock);
// Critical Section End
ints_to_log = LOG_INTR;
}
else // While file is being downloaded, just increase next logging amount to compensate
{
ints_to_log += LOG_INTR;
}
}
}
Reading getReading(unsigned int offset)
{
assert(offset < SIZE);
if((int)offset <= pos)
{
return ouroboros[pos - offset - 1];
}
else
{
return ouroboros[SIZE - offset + pos - 1];
}
}
FILE* getDBfileAndLock()
{
// Critical Section Beg
pthread_mutex_lock(&rws_db_lock);
return rws_db;
}
void unlockDBfile()
{
pthread_mutex_unlock(&rws_db_lock);
// Critical Section End
}
void deleteDBfile()
{
// Critical Section Beg
pthread_mutex_lock(&rws_db_lock);
fclose(rws_db);
remove("./log/readings.rws");
rws_db = fopen("./log/readings.rws", "a+b");
pthread_mutex_unlock(&rws_db_lock);
// Critical Section End
}
void formDBfilename(char* buff)
{
time_t beg, end;
fseek(rws_db, 0, SEEK_SET);
fread(&beg, 4, 1, rws_db);
fseek(rws_db, -sizeof(Reading), SEEK_END);
fread(&end, 4, 1, rws_db);
char begs[20], ends[20];
time2str(beg, begs, 1);
time2str(end, ends, 1);
sprintf(buff, "%s__%s.rws", begs, ends);
}
void logError(const char* descript, int err_num)
{
#ifndef NO_ERR_LOGGING
// Critical Section Beg
pthread_mutex_lock(&msg_log_lock);
FILE* err_log = fopen("./log/err.log", "a");
char tstr[20];
time2str(time(NULL), tstr, 0);
fprintf(err_log, "%s: %s - Error: %s\n", tstr, descript, strerror(err_num));
fclose(err_log);
pthread_mutex_unlock(&msg_log_lock);
// Critical Section End
#endif
}
void time2str(time_t t, char* out, bool fname)
{
struct tm s = *localtime(&t);
char ts = fname ? '.' : ':';
char sep = fname ? '_' : ' ';
sprintf(out, "%d.%02d.%02d%c%02d%c%02d%c%02d", s.tm_year + 1900, s.tm_mon + 1,
s.tm_mday, sep, s.tm_hour, ts, s.tm_min, ts, s.tm_sec);
}