Beispiel #1
0
void errorReporterInitialize(ErrorReporter self) {
  CharString infoText = newCharStringWithCString(kErrorReportInfoText);
  CharString wrappedInfoText;
  time_t now;
  size_t length;
  size_t i;

  printf("=== Starting error report ===\n");
  wrappedInfoText = charStringWrap(infoText, 0);
  // The second newline here is intentional
  printf("%s\n", wrappedInfoText->data);

  time(&now);
  self->started = true;

  snprintf(self->reportName->data, self->reportName->capacity,
    "MrsWatson Report %s", ctime(&now));
  // Trim the final newline character from this string if it exists
  length = strlen(self->reportName->data);
  if(self->reportName->data[length - 1] == '\n') {
    self->reportName->data[length - 1] = '\0';
    length--;
  }
  for(i = 0; i < length; i++) {
    if(!charStringIsLetter(self->reportName, i) ||
        charStringIsNumber(self->reportName, i)) {
      self->reportName->data[i] = '-';
    }
  }

 #if UNIX
  snprintf(self->desktopPath->data, self->desktopPath->capacity,
    "%s/Desktop", getenv("HOME"));
#elif WINDOWS
  SHGetFolderPathA(NULL, CSIDL_DESKTOPDIRECTORY, NULL, 0, self->desktopPath->data);
#endif
  snprintf(self->reportDirPath->data, self->reportDirPath->capacity,
    "%s%c%s", self->desktopPath->data, PATH_DELIMITER, self->reportName->data);
  makeDirectory(self->reportDirPath);

  freeCharString(wrappedInfoText);
  freeCharString(infoText);
}
Beispiel #2
0
void errorReporterInitialize(ErrorReporter self)
{
    CharString infoText = newCharStringWithCString(kErrorReportInfoText);
    CharString wrappedInfoText;
    time_t now;
    size_t length;
    size_t i;

    printf("=== Starting error report ===\n");
    wrappedInfoText = charStringWrap(infoText, 0);
    // The second newline here is intentional
    printf("%s\n", wrappedInfoText->data);

    time(&now);
    self->started = true;

    snprintf(self->reportName->data, self->reportName->capacity,
             "MrsWatson Report %s", ctime(&now));
    // Trim the final newline character from this string if it exists
    length = strlen(self->reportName->data);

    if (self->reportName->data[length - 1] == '\n') {
        self->reportName->data[length - 1] = '\0';
        length--;
    }

    for (i = 0; i < length; i++) {
        if (!(charStringIsLetter(self->reportName, i) ||
                charStringIsNumber(self->reportName, i))) {
            self->reportName->data[i] = '-';
        }
    }

#if UNIX
    snprintf(self->desktopPath->data, self->desktopPath->capacity,
             "%s/Desktop", getenv("HOME"));
#elif WINDOWS
    SHGetFolderPathA(NULL, CSIDL_DESKTOPDIRECTORY, NULL, 0, self->desktopPath->data);
#endif

    // Try to place the report on the user's desktop. However, if we cannot find
    // the desktop (which may occur with localized Linux installations, for instance),
    // then just dump it in the current directory instead.
    File desktopPath = newFileWithPath(self->desktopPath);
    File reportPath;

    if (!fileExists(desktopPath)) {
        logWarn("Could not find desktop location, placing error report in current directory instead");
        CharString currentDirString = fileGetCurrentDirectory();
        File currentDir = newFileWithPath(currentDirString);
        reportPath = newFileWithParent(currentDir, self->reportName);
        freeFile(currentDir);
        freeCharString(currentDirString);
    } else {
        reportPath = newFileWithParent(desktopPath, self->reportName);
        freeFile(desktopPath);
    }

    if (fileExists(reportPath)) {
        logCritical("The path '%s' already contains a previous error report. Please remove the report data and try again.");
    } else {
        fileCreate(reportPath, kFileTypeDirectory);
    }

    // Now we should have a real error report path
    charStringCopy(self->reportDirPath, reportPath->absolutePath);

    freeFile(reportPath);
    freeCharString(wrappedInfoText);
    freeCharString(infoText);
}