-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsimlog.cpp
More file actions
299 lines (272 loc) · 6.47 KB
/
Copy pathsimlog.cpp
File metadata and controls
299 lines (272 loc) · 6.47 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* sim-log.c
*
* Handle the simmgr logging system
*
* This file is part of the sim-mgr distribution (https://github.com/OpenVetSimDevelopers/sim-mgr).
*
* Copyright (c) 2019-2026 VetSim, Cornell University College of Veterinary Medicine Ithaca, NY
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
*
* The log file is created in the user's temporary directory
* The filename is created from the scenario name and start time
*/
#include "vetsim.h"
using namespace std;
#define SIMLOG_NAME_LENGTH 128
#define MAX_TIME_STR 24
#define MAX_LINE_LEN 512
/*
* FUNCTION:
* simlog_create
*
* ARGUMENTS:
*
*
* RETURNS:
* On success, returns 0. On fail, returns -1.
*/
char simlog_file[SIMLOG_NAME_LENGTH] = { 0, };
int simlog_initialized = 0;
FILE* simlog_fd;
int simlog_line; // Last line written
int lock_held = 0;
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <filesystem>
namespace fs = std::filesystem;
int
simlog_create()
{
char timeStr[MAX_TIME_STR];
char msgBuf[MAX_LINE_LEN];
int rval = 0;
fs::current_path(localConfig.html_path);
fs::create_directory("simlogs");
fs::create_directory("simlogs/video");
// Format from status.scenario.start: "2021-02-22_09.31.53"
strftime(timeStr, MAX_TIME_STR, "%Y-%m-%d_%H.%M.%S", &simmgr_shm->status.scenario.tmStart);
sprintf_s(simlog_file, SIMLOG_NAME_LENGTH, "%s/simlogs/%s_%s.log", localConfig.html_path, timeStr, simmgr_shm->status.scenario.active);
printf("simlog_file is %s\n", simlog_file);
(void)simlog_open(SIMLOG_MODE_CREATE);
if (simlog_fd)
{
printf("simlog_open succeeds\n");
simlog_line = 0;
simmgr_shm->logfile.active = 1;
snprintf(msgBuf, MAX_LINE_LEN, "Scenario: '%s' Date: %s", simmgr_shm->status.scenario.active, simmgr_shm->status.scenario.start);
simlog_write((char*)"Start");
simlog_close();
sprintf_s(simmgr_shm->logfile.filename, FILENAME_SIZE, "%s_%s.log", timeStr, simmgr_shm->status.scenario.active);
sprintf_s(simmgr_shm->logfile.vfilename, FILENAME_SIZE, "%s_%s.mp4", timeStr, simmgr_shm->status.scenario.active);
}
else
{
printf("simlog_open fails\n");
simmgr_shm->logfile.active = 0;
sprintf_s(simmgr_shm->logfile.filename, FILENAME_SIZE, "%s", "");
simmgr_shm->logfile.lines_written = 0;
rval = -1;
}
return (rval);
}
void
simlog_entry(char* msg)
{
int sts;
if (strlen(msg) == 0)
{
log_message("", "simlog_entry with null message");
}
else
{
if ((strlen(simlog_file) > 0) && (simmgr_shm->logfile.active))
{
sts = simlog_open(SIMLOG_MODE_WRITE);
if (sts == 0)
{
(void)simlog_write(msg);
(void)simlog_close();
}
}
}
}
int
simlog_open(int rw)
{
int sts;
char buffer[512];
char errBuffer[256];
if (simlog_fd)
{
log_message("", "simlog_open called with simlog_fd already set");
printf("simlog_open called with simlog_fd already set\n");
return (-1);
}
if (lock_held)
{
log_message("", "simlog_open called with lock_held set");
printf("simlog_open called with lock_held set\n");
return (-2);
}
if (rw == SIMLOG_MODE_WRITE || rw == SIMLOG_MODE_CREATE)
{
// Take the lock
sts = WaitForSingleObject(simmgr_shm->logfile.sema, 1000);
if (sts != WAIT_OBJECT_0)
{
// Could not get lock. Try again next time.
log_message("", "simlog_open failed to take logfile mutex");
printf("simlog_open failed to take logfile mutex\n");
return (-1);
}
lock_held = 1;
if (rw == SIMLOG_MODE_CREATE)
{
sts = fopen_s(&simlog_fd, simlog_file, "w");
}
else
{
sts = fopen_s(&simlog_fd, simlog_file, "a");
}
if (sts || !simlog_fd)
{
strerror_s(errBuffer, 256, errno);
sprintf_s(buffer, 512, "simlog_open failed to open for write: %s : %s",
simlog_file, errBuffer);
log_message("", buffer);
printf("simlog_open failed to open for write: %s : %s\n",
simlog_file, errBuffer);
ReleaseMutex(simmgr_shm->logfile.sema);
lock_held = 0;
}
}
else
{
sts = fopen_s(&simlog_fd, simlog_file, "r");
if (sts || !simlog_fd)
{
sprintf_s(errBuffer, 256, "simlog_open failed to open for read: %s", simlog_file);
printf("simlog_open failed to open for read:%s : %s\n",
simlog_file, errBuffer);
log_message("", errBuffer);
}
}
if (!simlog_fd)
{
printf("simlog_open failed\n");
return (-1);
}
return (0);
}
int
simlog_write(char* msg)
{
if (!simlog_fd)
{
log_message("", "simlog_write called with closed file");
return (-1);
}
if (!lock_held)
{
log_message("", "simlog_write called without lock held");
return (-1);
}
if (strlen(msg) > MAX_LINE_LEN)
{
log_message("", "simlog_write overlength string");
return (-1);
}
if (strlen(msg) <= 0)
{
log_message("", "simlog_write empty string");
return (-1);
}
fprintf(simlog_fd, "%s %s %s %s\n",
simmgr_shm->status.scenario.runtimeAbsolute,
simmgr_shm->status.scenario.runtimeScenario,
simmgr_shm->status.scenario.runtimeScene,
msg);
simlog_line++;
simmgr_shm->logfile.lines_written = simlog_line;
return (simlog_line);
}
size_t
simlog_read(char* rbuf)
{
sprintf_s(rbuf, MAX_LINE_LEN, "%s", "");
if (!simlog_fd)
{
log_message("", "simlog_read called with closed file");
return (-1);
}
fgets(rbuf, MAX_LINE_LEN, simlog_fd);
return (strlen(rbuf));
}
size_t
simlog_read_line(char* rbuf, int lineno)
{
int line;
char* sts;
sprintf_s(rbuf, MAX_LINE_LEN, "%s", "");
if (!simlog_fd)
{
log_message("", "simlog_read_line called with closed file");
return (-1);
}
fseek(simlog_fd, 0, SEEK_SET);
for (line = 1; line <= lineno; line++)
{
sts = fgets(rbuf, MAX_LINE_LEN, simlog_fd);
if (!sts)
{
return (-1);
}
}
return (strlen(rbuf));
}
void
simlog_close()
{
// Close the file
if (simlog_fd)
{
fclose(simlog_fd);
simlog_fd = NULL;
}
// Release the MUTEX
if (lock_held)
{
ReleaseMutex(simmgr_shm->logfile.sema);
lock_held = 0;
}
}
void
simlog_end()
{
int sts;
if ((simlog_fd == NULL) && (simmgr_shm->logfile.active))
{
sts = simlog_open(SIMLOG_MODE_WRITE);
if (sts == 0)
{
simlog_write((char*)"End");
simlog_close();
}
}
simmgr_shm->logfile.active = 0;
}