00001
00002
00003
00004
00005
00006
00007
00008
00010
00011 #include "StMessageCounter.h"
00012 #include "StMessageStream.h"
00013 #include <cstring>
00014
00015 StMessageCounter* StMessageCounter::mInstance = 0;
00016
00017
00018 StMessageCounter::StMessageCounter() : ostrstream(),
00019 limitMessage(" - COUNT LIMIT REACHED!\n") {
00020 messTypeList = StMessTypeList::Instance();
00021 yesLimits = 0;
00022 noLimits = 0;
00023
00024 *this << ch64 << ch64 << ch64 << ch64;
00025 }
00026
00027 StMessageCounter::~StMessageCounter() {
00028 }
00029
00030 StMessageCounter* StMessageCounter::Instance() {
00031 if (!mInstance) {
00032 mInstance = new StMessageCounter;
00033 }
00034 return mInstance;
00035 }
00036
00037 void StMessageCounter::SetLimit(const char* str, int n) {
00038 if (noLimits) return;
00039 if (!yesLimits && (n >= 0)) yesLimits = 1;
00040 const size_t len = strlen(str);
00041 char* temp;
00042 if (len==1) {
00043 int typeN = messTypeList->FindTypeNum(str);
00044 if (typeN) {
00045 if (limitTList[typeN] != -5)
00046 limitTList[typeN] = n;
00047 } else {
00048 if (limitWList.size()) {
00049 index = 0;
00050 for (curString=limitWList.begin(); curString!=limitWList.end();
00051 curString++) {
00052 if (*str == *(*curString)) {
00053 if (limitWNList[index] != -5)
00054 limitWNList[index] = n;
00055 return;
00056 }
00057 index++;
00058 }
00059 }
00060 temp = new char[len+1];
00061 temp[len] = 0;
00062 limitWList.push_back(strncpy(temp,str,len));
00063 limitWNList.push_back(n);
00064 }
00065 } else {
00066 index=0;
00067 for (curString=limitList.begin(); curString!=limitList.end();
00068 curString++) {
00069 if (!strcmp(str,(*curString))) {
00070 if (limitNList[index] == -5) return;
00071 if ((n < 0) && (n != -5)) {
00072 limitList.erase(curString);
00073 limitNList.erase(limitNList.begin()+index);
00074 limitNCountList.erase(limitNCountList.begin()+index);
00075 } else {
00076 limitNList[index] = n;
00077 }
00078 return;
00079 }
00080 index++;
00081 }
00082 if ((n < 0) && (n != -5)) return;
00083 temp = new char[len+1];
00084 temp[len] = 0;
00085 limitList.push_back(strncpy(temp,str,len));
00086 limitNList.push_back(n);
00087 limitNCountList.push_back(0);
00088 }
00089 return;
00090 }
00091
00092 int StMessageCounter::GetLimit(const char* str) {
00093 const size_t len = strlen(str);
00094 if (len==1) {
00095 int typeN = messTypeList->FindTypeNum(str);
00096 if (typeN) {
00097 return limitTList[typeN];
00098 } else {
00099 if (limitWList.size()) {
00100 index = 0;
00101 for (curString=limitWList.begin(); curString!=limitWList.end();
00102 curString++) {
00103 if (*str == *(*curString)) return limitWNList[index];
00104 index++;
00105 }
00106 }
00107 }
00108 } else {
00109 index=0;
00110 for (curString=limitList.begin(); curString!=limitList.end();
00111 curString++) {
00112 if (!strcmp(str,(*curString))) return limitNList[index];
00113 index++;
00114 }
00115 }
00116 return -1;
00117 }
00118
00119 void StMessageCounter::ListLimits() {
00120 if (yesLimits) {
00121 myout << "StMessage Limits: negative limit means no limit, ";
00122 myout << "-5 means fixed with no limit\n";
00123 myout << " Limits : counts : on message types" << endl;
00124 for (index = 1; index < limitTList.size(); index++) {
00125 myout.width(8);
00126 myout << limitTList[index] << " : ";
00127 myout.width(8);
00128 myout << limitTCountList[index] << " : ";
00129 myout << messTypeList->FindNumType(index) << " - ";
00130 myout << messTypeList->FindNumText(index) << endl;
00131 }
00132 for (index = 0; index < limitWList.size(); index++) {
00133 myout.width(8);
00134 myout << limitWNList[index] << " : ";
00135 myout.width(8);
00136 myout << 0 << " : ";
00137 myout << limitWList[index] << " - ";
00138 myout << "???" << endl;
00139 }
00140 myout << " Limits : counts : on message strings" << endl;
00141 index=0;
00142 for (curString=limitList.begin(); curString!=limitList.end();
00143 curString++) {
00144 myout.width(8);
00145 myout << limitNList[index] << " : ";
00146 myout.width(8);
00147 myout << limitNCountList[index++] << " : ";
00148 myout << (*curString) << endl;
00149 }
00150 } else {
00151 myout << "No limits have been set on messages." << endl;
00152 }
00153 return;
00154 }
00155
00156 int StMessageCounter::CheckLimit(char* mess, const char* type) {
00157 static const char* leader="St";
00158 static const char* colon =": ";
00159 static const char* stmess="StMessage: ";
00160 int printIt = 1;
00161 int typeN = messTypeList->FindTypeNum(type);
00162 int typeNewSize = limitTCountList[typeN] + 1;
00163 limitTCountList[typeN] = typeNewSize;
00164
00165 if (yesLimits && (! noLimits)) {
00166 seekp(0);
00167 int limit = limitTList[typeN];
00168 if (typeNewSize == limit) {
00169 *this << leader;
00170 *this << (messTypeList->FindType(type)->Text());
00171 *this << colon << limitMessage;
00172 } else if ((limit >= 0) && (typeNewSize > limit)) {
00173 printIt = 0;
00174 }
00175
00176 index=0;
00177 for (curString=limitList.begin(); curString!=limitList.end();
00178 curString++) {
00179 if (strstr(mess,(*curString))) {
00180 int counts = limitNCountList[index] + 1;
00181 limitNCountList[index] = counts;
00182 limit = limitNList[index];
00183 if (counts==limit) {
00184 *this << stmess << (*curString) << limitMessage;
00185 } else if ((limit >= 0) && (counts > limit)) {
00186 printIt = 0;
00187 }
00188 }
00189 index++;
00190 }
00191 *this << ends;
00192 }
00193 return printIt;
00194 }
00195
00196 void StMessageCounter::AddType(const char* type) {
00197 limitTList.push_back(-1);
00198 limitTCountList.push_back(0);
00199 if (limitWList.size()) {
00200 index=0;
00201 for (curString=limitWList.begin(); curString!=limitWList.end();
00202 curString++) {
00203 if (*type == *(*curString)) {
00204 SetLimit((*curString),limitWNList[index]);
00205 limitWList.erase(curString);
00206 limitWNList.erase(limitWNList.begin()+index);
00207 return;
00208 }
00209 index++;
00210 }
00211 }
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273