Changeset 152 for Evidence/Evidence.cc
- Timestamp:
- 01/27/10 15:28:06 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Evidence/Evidence.cc
r144 r152 26 26 27 27 bool EvidenceServer::ExitRequest = false; 28 string __StatusName; 28 29 29 30 // Constructor starts server with given name … … 34 35 ConfigList = NULL; 35 36 ConfigNum = 0; 37 __StatusName = string(Name) + "/Status"; 36 38 37 39 // Catch some signals … … 41 43 signal(SIGHUP, &SignalHandler); // Terminal closed 42 44 43 // Create server name 44 if (MakeString(&StatusName, "%s/Status", Name) == -1) { 45 State(FATAL, "Could not generate service name, asprintf() failed"); 46 } 45 // Catch C++ unhandled exceptions 46 set_terminate(Terminate); 47 47 48 48 // Start server 49 Status = new DimService( StatusName, (char *) "Server started");49 Status = new DimService(__StatusName.c_str(), (char *) "Server started"); 50 50 51 51 start(Name); … … 56 56 EvidenceServer::~EvidenceServer() { 57 57 58 free(StatusName);59 60 58 for (unsigned int i=0; i<ConfigNum; i++) { 61 59 delete[] ConfigList[i].Name; … … 95 93 va_end(ArgumentPointer); 96 94 97 snprintf(TBuf, sizeof(TBuf), "%s (%s): %s", StatusName, StateString[Severity], Tmp); // Normal string95 snprintf(TBuf, sizeof(TBuf), "%s (%s): %s", __StatusName.c_str(), StateString[Severity], Tmp); // Normal string 98 96 snprintf(SBuf, sizeof(SBuf), "%s*%c", Tmp, (char) Severity); 99 97 *(strrchr(SBuf, '*')) = '\0'; // String with severity encoding … … 115 113 // The memory allocated by all calls to this function will be freed by 116 114 // the destructor. 117 char* EvidenceServer::GetConfig(const char *Item ) {115 char* EvidenceServer::GetConfig(const char *Item, const char *Default) { 118 116 119 117 // Determine configuration file update time … … 138 136 DimRpcInfo Config((char *) "ConfigRequest", (char *) ""); 139 137 Config.setData((char *) Item); 140 138 char *Result = Config.getString(); 139 141 140 // Terminate if not successful 142 if (strlen(Config.getString()) == 0) { 143 State(FATAL, "Missing configuration data '%s'", Item); 141 if (strlen(Result) == 0) { 142 if (Default == NULL) State(FATAL, "Missing configuration data '%s'", Item); 143 Result = (char *) Default; 144 144 } 145 145 … … 157 157 158 158 // Allocate memory for strings, and copy data to this memory 159 ConfigList[ItemNo].Value = new char [strlen( Config.getString())+1];159 ConfigList[ItemNo].Value = new char [strlen(Result)+1]; 160 160 ConfigList[ItemNo].Name = new char [strlen(Item)+1]; 161 161 strcpy(ConfigList[ItemNo].Name, Item); 162 strcpy(ConfigList[ItemNo].Value, Config.getString());162 strcpy(ConfigList[ItemNo].Value, Result); 163 163 164 164 ConfigList[ItemNo].Time = Time; … … 175 175 176 176 EvidenceServer::ExitRequest = true; 177 } 178 179 // C++ exception handler 180 void EvidenceServer::Terminate() { 181 182 string Msg = __StatusName + ": Caught unhandled exception"; 183 printf("%s\n", Msg.c_str()); 184 DimClient::sendCommand("DColl/Log", Msg.c_str()); 185 186 abort(); 177 187 } 178 188 … … 182 192 183 193 char *Text; 194 int R; 184 195 185 196 if (strlen(Item->getFormat()) != 1) return NULL; 186 197 187 198 switch (*(Item->getFormat())) { 188 case 'I': MakeString(&Text, "%d", Item->getInt()); break;189 case 'C': MakeString(&Text, "%s", Item->getString()); break;190 case 'S': MakeString(&Text, "%hd", Item->getShort()); break;191 case 'F': MakeString(&Text, "%.5f", Item->getFloat()); break;192 case 'D': MakeString(&Text, "%.5f", Item->getDouble()); break;193 case 'X': MakeString(&Text, "%lld", Item->getLonglong()); break;199 case 'I': R = asprintf(&Text, "%d", Item->getInt()); break; 200 case 'C': R = asprintf(&Text, "%s", Item->getString()); break; 201 case 'S': R = asprintf(&Text, "%hd", Item->getShort()); break; 202 case 'F': R = asprintf(&Text, "%.5f", Item->getFloat()); break; 203 case 'D': R = asprintf(&Text, "%.5f", Item->getDouble()); break; 204 case 'X': R = asprintf(&Text, "%lld", Item->getLonglong()); break; 194 205 default: return NULL; 195 206 } 196 207 197 return Text; 198 } 199 200 // 201 // Generate string with vasprintf() 202 // 203 // The pointer will be set to NULL in case of error, so can always safely passed to free(). 204 // In case vasprintf() is not available on a particular system, the functionality can 205 // be manually implemented in this routine. 206 // 207 int EvidenceServer::MakeString(char **Pointer, const char *Format, ...) { 208 209 int Ret; 210 va_list ArgumentPointer; 211 212 va_start(ArgumentPointer, Format); 213 if ((Ret = vasprintf(Pointer, Format, ArgumentPointer)) == -1) Pointer = NULL; 214 va_end(ArgumentPointer); 215 216 return Ret; 217 } 208 return (R == -1) ? NULL : Text; 209 }
Note:
See TracChangeset
for help on using the changeset viewer.