LogWindow::LogWindow(QWidget *parent) : QDockWidget(parent), ui(new Ui::LogWindow) { QMutexLocker lock(&sMutex); if(sInstance != 0) { qFatal("Log Window cannot be instantiated more than once"); } ui->setupUi(this); mLogFile.setFileName(QCoreApplication::applicationDirPath() + "/logfile.log"); mLogFile.open(QFile::Append | QFile::Text | QIODevice::Unbuffered); mLogStream.setDevice(&mLogFile); ui->plainTextEdit->setMaximumBlockCount(200); mHighlighter = new LogSyntaxHighlighter(ui->plainTextEdit->document()); addLogType(QtDebugMsg, tr("Debug"), "DBG", Qt::blue); addLogType(QtWarningMsg, tr("Warning"), "WRN", Qt::darkYellow); addLogType(QtCriticalMsg, tr("Error"), "ERR", Qt::red); addLogType(QtInfoMsg, tr("Info"), "INF", QColor(64, 64, 64)); sInstance = this; mPreviousHandler = qInstallMessageHandler(&LogWindow::logMessage); mTimer.setSingleShot(true); mTimer.start(300); connect(&mTimer,SIGNAL(timeout()), SLOT(onTimeout())); }
/** * Reads in the configuration values stored in the configuration file and * stores each value in its corresponding global variables. */ void parseConfFile() { FILE *fd; char buf[512]; char *param; char *value; // These variables reused for each service section char service[512]; char log[512]; int maxAttempts = 0; char pattern[512]; int enableSync = 1; bzero(log, sizeof(char) * 512); bzero(pattern, sizeof(char) * 512); bzero(service, sizeof(char) * 512); strncpy(centralServer, UPDATE_SERVER, 256); DBG("Parsing conf file %s\n", CONF_FILE); if (!(fd = fopen(CONF_FILE, "r"))) err(1, "Failed to open configuration file %s", CONF_FILE); while (fgets(buf, 512, fd)) { // Ignore comments and empty lines if (buf[0] == '#' || strcmp(buf, "\n") == 0) continue; // Start of the next service if (buf[0] == '[') { // Record info for previous service if (service[0] != '\0') addLogType(service, log, pattern, maxAttempts); bzero(log, sizeof(char) * 512); bzero(pattern, sizeof(char) * 512); maxAttempts = 0; if ((value = strtok(&buf[1], "]")) == NULL) errx(1, "Missing terminating ']' for service"); strncpy(service, value, 512); } else { // Parse the values from lines of the form "parameter=value" if ((param = strtok(buf, "=")) == NULL) errx(1, "Invalid line in configuration file: %s", buf); if ((value = strtok(NULL, "\n")) == NULL ) errx(1, "Must enter a value in conf file for %s\n", param); DBG("Config Param: %s=%s\n", param, value); rightTrimWhitespace(value); // Store the value of the parameter in the corresponsing variable if (strncmp(param, "KEY", 512) == 0) strncpy(key, value, 512); else if (strncmp(param, "MAX_ATTEMPT", 512) == 0) maxAttempts = atoi(value); else if (strncmp(param, "FIREWALL", 512) == 0) { // Store the firewall as an integer so that we can use it in a // switch instead of doing string comparisons all the time if (strncmp(value, "iptables", 512) == 0) firewall = IPTABLES; } else if (strncmp(param, "CHAIN_NAME", 512) == 0) strncpy(chainName, value, 512); else if (strncmp(param, "SYNC_INTERVAL", 512) == 0) syncInterval = atoi(value); else if (strncmp(param, "MAINT_INTERVAL", 512) == 0) maintInterval = atoi(value); else if (strncmp(param, "LOG_ROTATE_INTERVAL", 512) == 0) logRotateInterval = atoi(value); else if (strncmp(param, "LOG", 512) == 0) strncpy(log, value, 512); else if (strncmp(param, "PATTERN", 512) == 0) strncpy(pattern, value, 512); else if (strncmp(param, "ENABLE_SYNC", 512) == 0) enableSync = atoi(value); else if (strncmp(param, "EMAIL", 512) == 0) { if (strncmp(value, "", 2) != 0) { strncpy(alertEmail, value, 512); } } else if (strncmp(param, "TEST_MODE", 512) == 0) { if (atoi(value) == 1) { strncpy(centralServer, TEST_SERVER, 256); } } else errx(1, "Invalid parameter in conf file: %s\n", param); } } if (enableSync == 0) syncInterval = 0; // Write info for the last service if (service[0] != '\0') addLogType(service, log, pattern, maxAttempts); // Make sure they provided a value for everything that is required if (!firewall || !strcmp(key, "") || !strcmp(chainName, "")) errx(1, "Please provide all configuration values"); DBG("Using central server: %s\n", centralServer); fclose(fd); }