Changeset 152 for Evidence/Evidence.cc


Ignore:
Timestamp:
01/27/10 15:28:06 (12 years ago)
Author:
ogrimm
Message:
Updates
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Evidence/Evidence.cc

    r144 r152  
    2626
    2727bool EvidenceServer::ExitRequest = false;
     28string __StatusName;
    2829
    2930// Constructor starts server with given name
     
    3435  ConfigList = NULL;
    3536  ConfigNum = 0;
     37  __StatusName = string(Name) + "/Status";
    3638 
    3739  // Catch some signals
     
    4143  signal(SIGHUP, &SignalHandler);   // Terminal closed
    4244 
    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);
    4747 
    4848  // Start server
    49   Status = new DimService(StatusName, (char *) "Server started");
     49  Status = new DimService(__StatusName.c_str(), (char *) "Server started");
    5050
    5151  start(Name);
     
    5656EvidenceServer::~EvidenceServer() {
    5757
    58   free(StatusName);
    59  
    6058  for (unsigned int i=0; i<ConfigNum; i++) {
    6159        delete[] ConfigList[i].Name;
     
    9593  va_end(ArgumentPointer);
    9694
    97   snprintf(TBuf, sizeof(TBuf), "%s (%s): %s", StatusName, StateString[Severity], Tmp); // Normal string
     95  snprintf(TBuf, sizeof(TBuf), "%s (%s): %s", __StatusName.c_str(), StateString[Severity], Tmp); // Normal string
    9896  snprintf(SBuf, sizeof(SBuf), "%s*%c", Tmp, (char) Severity);
    9997  *(strrchr(SBuf, '*')) = '\0';  // String with severity encoding
     
    115113// The memory allocated by all calls to this function will be freed by
    116114// the destructor.
    117 char* EvidenceServer::GetConfig(const char *Item) {
     115char* EvidenceServer::GetConfig(const char *Item, const char *Default) {
    118116 
    119117  // Determine configuration file update time
     
    138136  DimRpcInfo Config((char *) "ConfigRequest", (char *) "");
    139137  Config.setData((char *) Item);
    140 
     138  char *Result = Config.getString();
     139 
    141140  // 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;
    144144  }
    145145
     
    157157 
    158158  // 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];
    160160  ConfigList[ItemNo].Name = new char [strlen(Item)+1];
    161161  strcpy(ConfigList[ItemNo].Name, Item);
    162   strcpy(ConfigList[ItemNo].Value, Config.getString());
     162  strcpy(ConfigList[ItemNo].Value, Result);
    163163
    164164  ConfigList[ItemNo].Time = Time;
     
    175175
    176176  EvidenceServer::ExitRequest = true;
     177}
     178
     179// C++ exception handler
     180void 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();
    177187}
    178188
     
    182192
    183193  char *Text;
     194  int R;
    184195 
    185196  if (strlen(Item->getFormat()) != 1) return NULL;
    186197 
    187198  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;
    194205    default: return NULL;
    195206  }
    196207
    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.