-
Notifications
You must be signed in to change notification settings - Fork 0
/
PM_LOG.CPP
141 lines (123 loc) · 4.13 KB
/
PM_LOG.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
#include <stdio.h>
#include <time.h>
#include <memory.h>
#include "pm_log.h"
//////////////////////////////////////////////////////////////////
// FUNCTION: PM_LOG()
// Purpose: Constructor of the PM_LOG Class
// RETURNS:
// HISTORY: 980605: Created this function.
//////////////////////////////////////////////////////////////////
PM_LOG::PM_LOG()
{
// Initializations
filename[0] = '\0';
logfile = NULL;
}
//////////////////////////////////////////////////////////////////
// FUNCTION: CloseLogFile()
// Purpose: Close open logfile
// RETURNS: NULL Pointer
// HISTORY: 980605: Created this function.
//////////////////////////////////////////////////////////////////
FILE *
PM_LOG::CloseLogFile()
{
if (logfile) {
fclose(logfile);
logfile = NULL;
}
return(NULL);
}
//////////////////////////////////////////////////////////////////
// FUNCTION: OpenLogFile()
// Purpose: Open logfile by name.
// Filename will be saved in class, together with the
// pointer to the logfile.
// RETURNS: Pointer to file opened.
// HISTORY: 980605: Created this function.
//////////////////////////////////////////////////////////////////
FILE *
PM_LOG::OpenLogFile(char *s)
{
if (!logfile) {
logfile = fopen(s,"r+b");
}
return(logfile);
}
//////////////////////////////////////////////////////////////////
// FUNCTION: ReadLogData()
// Purpose: Read line from logfile
// Returns: pointer to string of data read in case of success
// NULL pointer in case of failure.
// HISTORY: 980530 V0.2 - Changed way file header is read.
// 980605 V0.3 - Moved this function to PM_LOG.C and
// created header file PM_LOG.H
//////////////////////////////////////////////////////////////////
char *PM_LOG::ReadLogData()
{
static char s[MAXLOGDATASIZE]; // String to contain store logdata in.
static long fp; // Pointer to start of header
unsigned char version; // Header revision
unsigned int headerlen; // Length of this header
unsigned int datalen; // Length of data after this header
// check if logfile is opened
if (!logfile)
return(NULL);
// If end of input, close file and return
if (feof(logfile)) {
CloseLogFile();
return(NULL);
}
memset(s,'\0',MAXLOGDATASIZE); // Initialize string
// Read header, then read the data
// No special case for differnt types of headers build in yet.
fp = ftell(logfile); // Get current position
fread(&version,sizeof(unsigned char),1, logfile);
fread(&headerlen,sizeof(unsigned int),1, logfile);
fread(&datalen,sizeof(unsigned int),1, logfile);
// Go back to first start of header and read complete header now
fseek(logfile,fp,SEEK_SET);
fread(&logheader,headerlen,1,logfile);
// Now read the data part
fread(s,datalen,1,logfile);
return(s);
}
///////////////////////////////////////////////////////////////////
// FUNCTION: LogData
// PURPOSE: Log data in a log file.
// This function opens the log file, write the header
// then writes the data part and closes the file again.
// HISTORY: 970605 V0.2 - Moved this function to 'pm_log.c'
///////////////////////////////////////////////////////////////////
void PM_LOG::WriteLogData(char *dataptr, int size)
{
FILE *f_out = NULL;
// Open the log file
if (!f_out) {
f_out = fopen("PM.LOG","a+b");
}
// If logfile could not be opened, return
if (!f_out)
return;
// Fill in header of the log record
logheader.version = 2; // Version 2
logheader.headerlen = sizeof(LOGHEADER); // version 2)
logheader.datalen = size; // Version 1
logheader.timestamp = time(NULL); // Save current time (version: 2)
// Write log header and data to the log file
fwrite(&logheader,sizeof(LOGHEADER),1,f_out);
fwrite(dataptr,sizeof(char),size,f_out);
// Close the logfile
CloseLogFile();
}
//////////////////////////////////////////////////////////////////
// FUNCTION: TimeStamp()
// Purpose: Return timestamp of header
// Returns: Timestamp of the header
// HISTORY: 980614 V0.1 - Initial version
//////////////////////////////////////////////////////////////////
time_t PM_LOG::TimeStamp()
{
return(logheader.timestamp);
}