Beispiel #1
0
func verifyLogSettings(lt constants.LogType) error {
	// too many stale logs
	if logDirNeedsCleaning(lt) {
		// delete stale logs
		if err := deleteLogs(lt); err != nil {
			return fmt.Errorf("verifyLogSettings error: %s\n", err)
		}
		// re-create default logfile
		if err := util.CreateEmptyFile(getLogPath(lt), true); err != nil {
			return fmt.Errorf("verifyLogSettings error: %s\n", err)
		}
	}
	// log file to be written to is too large
	if isMaxLogSizeExceeded(lt) {
		latestlog, _, err := getLatestAndEarliestLog(lt)
		if err != nil {
			return fmt.Errorf("verifyLogSettings error: %s\n", err)
		}
		// only app|web.log exists, rename it to app|web.log.1 & re-create
		if strings.EqualFold(latestlog, getLogFilenameFromType(lt)) {
			if err := os.Rename(getLogPath(lt), path.Join(constants.LogDirectory,
				fmt.Sprintf("%s.1", getLogFilenameFromType(lt)))); err != nil {
				return fmt.Errorf("verifyLogSettings error: %s\n", err)
			}
			// re-create default app|web.log
			if err := util.CreateEmptyFile(getLogPath(lt), true); err != nil {
				return fmt.Errorf("verifyLogSettings error: %s\n", err)
			}
		} else {
			// other logs exist, rename current as app|web.log.largestnum+1 & re-create
			latestlognum, _, err := getLatestAndEarliestLogNum(lt)
			if err != nil {
				return fmt.Errorf("verifyLogSettings error: %s\n", err)
			}
			if err := os.Rename(getLogPath(lt), path.Join(constants.LogDirectory,
				fmt.Sprintf("%s.%d", getLogFilenameFromType(lt),
					latestlognum+1))); err != nil {
				return fmt.Errorf("verifyLogSettings error: %s\n", err)
			}
			// re-create
			if err := util.CreateEmptyFile(getLogPath(lt), true); err != nil {
				return fmt.Errorf("verifyLogSettings error: %s\n", err)
			}
		}
	}
	return nil
}
Beispiel #2
0
func verifyLogPaths(lt constants.LogType) error {
	if err := util.CreateDirectory(constants.LogDirectory); err != nil {
		return err
	}
	if !util.FileExists(getLogPath(lt)) {
		if err := util.CreateEmptyFile(getLogPath(lt), false); err != nil {
			return fmt.Errorf("verifyLogPaths error: %s", err)
		}
	}
	return nil
}
Beispiel #3
0
func createServerDBtable(dbfile string) error {
	create := `CREATE TABLE servers (
	server_id INTEGER NOT NULL,
	host TEXT NOT NULL,
	game TEXT NOT NULL,
	PRIMARY KEY(server_id)
	)`

	if util.FileExists(dbfile) {
		// already exists, so verify integrity
		db, err := sql.Open("sqlite3", dbfile)
		if err != nil {
			return logger.LogAppErrorf(
				"Unable to open server DB file for verification: %s", err)
		}
		defer db.Close()
		var name string
		err = db.QueryRow(
			"SELECT name from sqlite_master where type='table' and name='servers'").Scan(&name)
		switch {
		case err == sql.ErrNoRows:
			if _, err = db.Exec(create); err != nil {
				return logger.LogAppErrorf("Unable to create servers table in DB: %s", err)
			}
		case err != nil:
			return logger.LogAppErrorf("Server DB table verification error: %s", err)
		}
		return nil
	}

	err := util.CreateEmptyFile(dbfile, true)
	if err != nil {
		return logger.LogAppErrorf("Unable to create server DB: %s", err)
	}

	db, err := sql.Open("sqlite3", dbfile)
	if err != nil {
		return logger.LogAppErrorf(
			"Unable to open server DB file for table creation: %s", err)
	}
	defer db.Close()
	_, err = db.Exec(create)
	if err != nil {
		return logger.LogAppErrorf("Unable to create servers table in servers DB: %s",
			err)
	}
	return nil
}