-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogFiles.c
More file actions
317 lines (292 loc) · 12.7 KB
/
logFiles.c
File metadata and controls
317 lines (292 loc) · 12.7 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//=========================================================================
// logFiles.c
//
// Log and data files for multiMag magnetometer utility.
//
// Author: David M. Witten II, KD0EAG
// Date: May 3, 2021
// License: GPL 3.0
//=========================================================================
#include "logFiles.h"
//extern char version[MAXVERSIONLEN];
extern char cityState[MAXPATHBUFLEN];
extern char callSign[MAXPATHBUFLEN];
extern char latLonElv[MAXPATHBUFLEN];
extern char freqStd[MAXPATHBUFLEN];
extern char nodeNum[MAXPATHBUFLEN];
extern char sysTem[MAXPATHBUFLEN];
extern char metadata[MAXPATHBUFLEN];
extern char dataTyp[MAXPATHBUFLEN];
extern char gridSqr[GRIDSQRLEN];
extern char rollOverTime[UTCBUFLEN];
extern char sitePrefix[SITEPREFIXLEN];
extern char baseFileFolder[FOLDERNAMELEN];
extern char baseFilePath[MAXPATHBUFLEN];
extern char outFilePath[MAXPATHBUFLEN];
extern char outFileName[MAXPATHBUFLEN];
const char *fdata[][3] =
{
{"/PSWS/Srawdata/", "CityState.txt", ""},
{"/PSWS/Sinfo/", "CallSign.txt", ""},
{"/PSWS/Sstat/", "LatLonElv.txt", ""},
{"/PSWS/Scmd/", "FreqStd.txt", ""},
{"/PSWS/Scode/", "NodeNum.txt", ""},
{"/PSWS/Stemp/", "GridSqr.txt", ""},
{"/PSWS/Sdata/", "System.txt", ""},
{"/PSWS/Sdata/", "Metadata.txt", ""},
{"/PSWS/Splot/", "", ""},
{NULL, "", ""}
};
//------------------------------------------
// logHeader()
// show settings in use (-P)
//------------------------------------------
char *logHeader(int argc, char **argv, pList *p)
{
char outLine[1024] = "";
char outStr[4096] = "";
//char pathStr[128] = "";
snprintf(outFileName, sizeof(outFileName), "/dev/i2c-%i", p->i2cBusNumber);
strncat(outStr, "\n", strlen("\n")+1);
snprintf(outLine, 1023, "#=================================================================\n");
strncat(outStr, outLine, strlen(outLine));
snprintf(outLine, 1023, "#\n# Current Parameters:\n");
strncat(outStr, outLine, strlen(outLine));
snprintf(outLine, 1023, "# \n");
strncat(outStr, outLine, strlen(outLine));
snprintf(outLine, 1023, "# Command line: ");
strncat(outStr, outLine, strlen(outLine));
snprintf(outLine, 1023, " %s ", argv[0]);
strncat(outStr, outLine, strlen(outLine));
for(int i = 1; i < argc; i++)
{
snprintf(outLine,1023, " %s", argv[i]);
strncat(outStr, outLine, strlen(outLine));
}
snprintf(outLine, 1023, "\n");
//if(!p->magRevId)
//{
// getMagRev(p);
//}
snprintf(outLine, 1023, "\n# Software Version: %s\n", MULTIMAG_VERSION);
strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Magnetometer rev. ID detected: %i (dec)\n", p->magRevId);
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# I2C bus number as integer: %i (dec)\n", p->i2cBusNumber);
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# I2C bus path as string: %s\n", pathStr);
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Log output format: %s\n", getOutputMode(p));
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Log file path: %s\n", p->outputFilePath);
//strncat(outStr, outLine, strlen(outLine));
////snprintf(outLine, 1023, "# Log output path: %s\n", p->buildLogPath ? "TRUE" : "FALSE" );
////strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Write Log output: %s\n", p->logOutput ? "TRUE" : "FALSE" );
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Site prefix/NodID string: %s\n", p->sitePrefix);
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Timestamp format: %s\n", p->tsMilliseconds ? "RAW" : "UTCSTRING");
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Show total field: %s\n", p->showTotal ? "TRUE" : "FALSE");
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Local temperature address: %02X (hex)\n", p->localTempAddr);
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Remote temperature address: %02X (hex)\n", p->remoteTempAddr);
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Magnetometer address: %02X {hex)\n", p->magnetometerAddr);
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# NOS Register value: %02X (hex)\n", p->NOSRegValue);
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Post DRDY delay: %i (dec)\n", p->DRDYdelay);
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Device sampling mode: %s\n", p->samplingMode ? "CONTINUOUS" : "POLL");
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Cycle counts by vector: X: %3i (dec), Y: %3i (dec), Z: %3i (dec)\n", p->cc_x, p->cc_y, p->cc_z);
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Gain by vector: X: %3i (dec), Y: %3i (dec), Z: %3i (dec)\n", p->x_gain, p->y_gain, p->z_gain);
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Read back CC Regs after set: %s\n", p->readBackCCRegs ? "TRUE" : "FALSE" );
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Software Loop Delay (uSec): %i (dec)\n", p->outDelay);
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# CMM sample rate: %2X (hex)\n", p->CMMSampleRate);
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# TMRC reg value: %2X (hex)\n", p->TMRCRate);
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Read local temperature only: %s\n", p->localTempOnly ? "TRUE" : "FALSE");
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Read remote temperature only: %s\n", p->remoteTempOnly ? "TRUE" : "FALSE");
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Read magnetometer only: %s\n", p->magnetometerOnly ? "TRUE" : "FALSE");
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Show parameters: %s\n", p->showParameters ? "TRUE" : "FALSE" );
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Hide raw measurements: %s\n", p->hideRaw ? "TRUE" : "FALSE" );
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Return single reading: %s\n", p->singleRead ? "TRUE" : "FALSE");
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Axis Translation: X -> %s\n", (p->xTranslate == 0) ? "X" : ((p->xTranslate == 1) ? "Y" : "Z"));
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Y -> %s\n", (p->yTranslate == 0) ? "X" : ((p->yTranslate == 1) ? "Y" : "Z"));
//strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "# Z -> %s\n", (p->zTranslate == 0) ? "X" : ((p->zTranslate == 1) ? "Y" : "Z"));
//strncat(outStr, outLine, strlen(outLine));
////strncat(outStr, outLine, strlen(outLine));
//snprintf(outLine, 1023, "#\n#=================================================================\n\n");
//strncat(outStr, outLine, strlen(outLine));
return outFileName;
}
//------------------------------------------
// openLogs()
//------------------------------------------
int openLogs(pList *p)
{
int rv = 0;
if(p->outfp == 0)
{
buildOutputFileName(p);
if((p->outfp = fopen(p->outputFilePath, "a+"))!= NULL)
{
printf("Log File: %s\n", p->outputFilePath);
}
else
{
perror("Log File: ");
}
rv = 0;
}
return rv;
}
//------------------------------------------
// openLogs()
//------------------------------------------
void closeLogs(pList *p)
{
if(p->outfp != stdout)
{
fclose(p->outfp);
}
}
//------------------------------------------
// buildOutputFileName()
//------------------------------------------
void buildOutputFileName(pList *p)
{
struct tm *utcTime = getUTC();
char utcStr[UTCBUFLEN] = "";
char *penv = getenv("HOME");
strncpy(baseFilePath, "", 1);
strncpy(baseFilePath, penv, MAXPATHBUFLEN - 1);
strncat(baseFilePath, baseFileFolder, FOLDERNAMELEN - 1);
strftime(utcStr, UTCBUFLEN, "%Y-%m-%dT%H%M%S", utcTime);
strncpy((char *)outFileName, utcStr, UTCBUFLEN);
strncat((char *)outFileName, "Z_", 3);
strncat((char *)outFileName, p->sitePrefix, SITEPREFIXLEN);
strncat((char *)outFileName, "_N_", 4);
strncat((char *)outFileName, p->gridSqr, 7);
strncat((char *)outFileName, "_MGT.log", 9);
strncat((char *)baseFilePath, "/", 2);
strncat((char *)baseFilePath, outFileName, MAXPATHBUFLEN);
p->outputFileName = outFileName;
p->outputFilePath = baseFilePath;
}
//------------------------------------------
// buildGrapeOutputFileName()
//------------------------------------------
void buildGrapeOutputFileName(pList *p)
{
struct tm *utcTime = getUTC();
char utcStr[UTCBUFLEN] = "";
char *penv = getenv("HOME");
strncpy(baseFilePath, "", 1);
strncpy(baseFilePath, penv, MAXPATHBUFLEN - 1);
strncat(baseFilePath, baseFileFolder, FOLDERNAMELEN - 1);
strftime(utcStr, UTCBUFLEN, "%Y-%m-%dT%H%M%S", utcTime);
strncpy((char *)outFileName, utcStr, UTCBUFLEN);
strncat((char *)outFileName, "Z_", 3);
strncat((char *)outFileName, p->sitePrefix, SITEPREFIXLEN);
strncat((char *)outFileName, "_N_", 4);
strncat((char *)outFileName, p->gridSqr, 7);
strncat((char *)outFileName, "_MGT.log", 9);
strncat((char *)baseFilePath, "/", 2);
strncat((char *)baseFilePath, outFileName, MAXPATHBUFLEN);
p->outputFileName = outFileName;
p->outputFilePath = baseFilePath;
}
////------------------------------------------
//// buildOutputFilePath()
////------------------------------------------
//int buildOutputFilePath(pList *p)
//{
// int rv = 0;
// char *penv = getenv("HOME");
// strncpy(baseFilePath, "", 1);
// strncpy(baseFilePath, penv, MAXPATHBUFLEN - 1);
// strncat(baseFilePath, baseFileFolder, FOLDERNAMELEN - 1);
// p->baseFilePath = baseFilePath;
//fprintf(stdout, "buildOutputFilePath(): [%s]\n", p->baseFilePath);
//fflush(stdout);
// return rv;
//}
//
////------------------------------------------
//// setOutputFileRoot()
////------------------------------------------
//int setOutputFileRoot(pList *p, char *outPath)
//{
// int rv = 0;
//
// if(strlen(outPath) > MAXPATHBUFLEN - 1)
// {
// fprintf(stderr, "\nOutput path length exceeds maximum allowed length (%i)\n", MAXPATHBUFLEN - 1);
// rv = 1;
// }
// else
// {
// strncpy(outFilePath, outPath, strlen(outPath));
// p->outputFilePath = outFilePath;
// }
// return rv;
//}
//------------------------------------------
// openUIPipes()
//------------------------------------------
int openUIPipes(pList *p)
{
int rv = 0;
if(p->useOutputPipe = TRUE)
{
// Note that fdPipeOut and fdPipeIn are intentionally reversed.
if(!(p->fdPipeOut = open(p->pipeInPath, O_WRONLY | O_CREAT)))
{
perror("Open PIPE Out failed: ");
fprintf(stderr, "%s\n", p->pipeInPath);
exit(1);
}
if(!(p->fdPipeIn = open(p->pipeOutPath, O_RDONLY | O_CREAT)))
{
perror("Open PIPE In failed: ");
fprintf(stderr, "%s\n", p->pipeOutPath);
exit(1);
}
}
return rv;
}
//------------------------------------------
// closeUIPipes()
//------------------------------------------
void closeUIPipes(pList *p)
{
// Note that fdPipeOut and fdPipeIn are intentionally reversed.
if(p->fdPipeOut != 0)
{
close(p->fdPipeOut);
p->fdPipeOut = 0;
}
if(p->fdPipeIn != 0)
{
close(p->fdPipeIn);
p->fdPipeIn = 0;
}
}