1 | /********************************************************************\
|
---|
2 |
|
---|
3 | SlowData.cc
|
---|
4 |
|
---|
5 | Class handling the writing of slow data. Strings to be written in the
|
---|
6 | slow data file are checked that they do not contain control characters.
|
---|
7 |
|
---|
8 | 1. Call constructor with desired identification and directory
|
---|
9 | for slow data file. The public ErrorCode variable will be zero
|
---|
10 | if successful, otherwise contain the relevant errno.
|
---|
11 | 2. Use NewEntry() to start a new entry with given variable name. A text
|
---|
12 | as second argument is optional.
|
---|
13 | 3. If text should be added to an open entry, use AddToEntry(). It can
|
---|
14 | process printf()-style formatting.
|
---|
15 | 4. The slow data file will be closed upon destruction of the instance.
|
---|
16 |
|
---|
17 | Oliver Grimm
|
---|
18 |
|
---|
19 | \********************************************************************/
|
---|
20 |
|
---|
21 | #include "SlowData.h"
|
---|
22 |
|
---|
23 | //
|
---|
24 | // Constructor: Open slow data file (filename generate using current date)
|
---|
25 | //
|
---|
26 | SlowData::SlowData(const char* IssuerName, const char* Direcory) {
|
---|
27 |
|
---|
28 | time_t rawtime;
|
---|
29 | struct tm *timeinfo;
|
---|
30 | char Filename[MAX_PATH];
|
---|
31 |
|
---|
32 | Issuer = IssuerName;
|
---|
33 | NewEntryCalled = false;
|
---|
34 | InternalCall = false;
|
---|
35 |
|
---|
36 | time(&rawtime);
|
---|
37 | timeinfo = gmtime(&rawtime);
|
---|
38 | if(timeinfo->tm_hour>=13) rawtime += 12*60*60;
|
---|
39 | timeinfo = gmtime(&rawtime);
|
---|
40 | snprintf(Filename, sizeof(Filename), "%s/%s_%d%02d%02d.slow", Direcory, Issuer, timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday);
|
---|
41 |
|
---|
42 | if ((SlowdataFile = fopen(Filename, "a")) == NULL) ErrorCode = errno;
|
---|
43 | else ErrorCode = 0;
|
---|
44 | }
|
---|
45 |
|
---|
46 | //
|
---|
47 | // Destructor
|
---|
48 | //
|
---|
49 | SlowData::~SlowData() {
|
---|
50 | if(SlowdataFile!=NULL) fclose(SlowdataFile);
|
---|
51 | }
|
---|
52 |
|
---|
53 | //
|
---|
54 | // Add a new entry to slow data file
|
---|
55 | //
|
---|
56 | bool SlowData::NewEntry(const char *Variable, const char *Text) {
|
---|
57 |
|
---|
58 | time_t RawTime;
|
---|
59 | struct tm *TM;
|
---|
60 | struct timeval Time;
|
---|
61 |
|
---|
62 | time(&RawTime);
|
---|
63 | TM = localtime(&RawTime);
|
---|
64 | gettimeofday(&Time, NULL);
|
---|
65 | InternalCall = true;
|
---|
66 | NewEntryCalled = AddToEntry("\n%s %s %d %d %d %d %d %d %d %lu ", Issuer, Variable, TM->tm_year+1900,TM->tm_mon+1,TM->tm_mday,TM->tm_hour,TM->tm_min,TM->tm_sec, Time.tv_usec/1000, Time.tv_sec);
|
---|
67 | InternalCall = false;
|
---|
68 | if(Text != NULL && NewEntryCalled == true) NewEntryCalled = AddToEntry("%s", Text);
|
---|
69 |
|
---|
70 | return NewEntryCalled;
|
---|
71 | }
|
---|
72 |
|
---|
73 | //
|
---|
74 | // Add data to an open entry
|
---|
75 | //
|
---|
76 | bool SlowData::AddToEntry(const char *Format, ...) {
|
---|
77 |
|
---|
78 | char Textbuffer[MAX_ENTRY_SIZE];
|
---|
79 | va_list ArgumentPointer;
|
---|
80 |
|
---|
81 | if(SlowdataFile==NULL || (!NewEntryCalled && !InternalCall)) return false;
|
---|
82 |
|
---|
83 | va_start(ArgumentPointer, Format);
|
---|
84 | vsnprintf(Textbuffer, sizeof(Textbuffer), Format, ArgumentPointer);
|
---|
85 | va_end(ArgumentPointer);
|
---|
86 |
|
---|
87 | if(!InternalCall)
|
---|
88 | for(unsigned int i=0; i<strlen(Textbuffer); i++)
|
---|
89 | if (iscntrl(Textbuffer[i])) return false;
|
---|
90 |
|
---|
91 | if(fprintf(SlowdataFile, "%s", Textbuffer) == -1) {
|
---|
92 | ErrorCode = errno;
|
---|
93 | fclose(SlowdataFile);
|
---|
94 | SlowdataFile = NULL;
|
---|
95 | return false;
|
---|
96 | }
|
---|
97 | fflush(SlowdataFile);
|
---|
98 | ErrorCode = 0;
|
---|
99 | return true;
|
---|
100 | }
|
---|