Exemplo n.º 1
0
compressed_data
persistence_sqlite::retrieve (data_type type, chunk_coordinates xyz)
{
    boost::mutex::scoped_lock l (lock);

    int idx (static_cast<int>(type));
    auto& query (read_[idx]);

    arm_timer();
    query.reset();

    query.bind(1, xyz.x);
    query.bind(2, xyz.y);
    query.bind(3, xyz.z);

    auto rc (query.step());
    if (rc != SQLITE_DONE && rc != SQLITE_ROW)
    {
        std::stringstream msg;
        msg << "data type " << (int)type << " at " << xyz << ": " << err_str_(rc);
        throw not_in_storage_error(msg.str());
    }

    return deserialize_as<compressed_data>(query.get_blob(0));
}
Exemplo n.º 2
0
void
persistence_sqlite::store (map_coordinates xy, chunk_height z)
{
    boost::mutex::scoped_lock l (lock);

    auto& query (write_height_);

    arm_timer();
    query.reset();

    query.bind(1, xy.x);
    query.bind(2, xy.y);
    query.bind(3, z);

    auto rc (query.step());
    if (rc != SQLITE_DONE && rc != SQLITE_OK)
        throw std::runtime_error(std::string("cannot store data : ") + err_str_(rc));
}
Exemplo n.º 3
0
void
persistence_sqlite::store (data_type type, chunk_coordinates xyz,
                           const compressed_data& data)
{
    boost::mutex::scoped_lock l (lock);

    unsigned int idx (static_cast<unsigned int>(type));
    assert(idx < write_.size());
    auto& query (write_[idx]);

    arm_timer();
    query.reset();

    query.bind(1, xyz.x);
    query.bind(2, xyz.y);
    query.bind(3, xyz.z);
    query.bind(4, serialize(data));

    auto rc (query.step());
    if (rc != SQLITE_DONE && rc != SQLITE_OK)
        throw std::runtime_error(std::string("cannot store data : ") + err_str_(rc));
}
Exemplo n.º 4
0
chunk_height
persistence_sqlite::retrieve (map_coordinates xy)
{
    boost::mutex::scoped_lock l (lock);

    auto& query (read_height_);

    arm_timer();
    query.reset();

    query.bind(1, xy.x);
    query.bind(2, xy.y);

    auto rc (query.step());
    if (rc != SQLITE_DONE && rc != SQLITE_ROW)
    {
        std::stringstream msg;
        msg << "coarse map height at " << xy << ": " << err_str_(rc);
        throw not_in_storage_error(msg.str());
    }

    return query.get_uint(0);
}
Exemplo n.º 5
0
void
ls_syslog (int level, const char *fmt, ...)
{
    int save_errno = errno;
    va_list ap;
    static char lastMsg[16384];
    static int  counter = 0;

    va_start(ap, fmt);

    if (log_dest == LOGTO_STDERR) {

        if ((logmask & LOG_MASK(level)) != 0) {
            errno = save_errno;
            verrlog_(level, stderr, fmt, ap);
        }

    } else if (logfile[0]) {

        if ((logmask & LOG_MASK(level)) != 0) {
            FILE *lfp;
            struct stat st;

            if (lstat(logfile, &st) < 0) {
                if (errno == ENOENT) {
                    if ((lfp = fopen(logfile, "a")) == NULL) {

                        if (log_dest == LOGTO_FILE) {
                            log_dest = LOGTO_SYS;
                            openlog(logident, LOG_PID, LOG_DAEMON);
                            setlogmask(logmask);
                        }
                        goto use_syslog;
                    }
                } else {
                    if (log_dest == LOGTO_FILE) {
                        log_dest = LOGTO_SYS;
                        openlog(logident, LOG_PID, LOG_DAEMON);
                        setlogmask(logmask);
                    }
                    goto use_syslog;
                }
            } else if (!(S_ISREG(st.st_mode) && st.st_nlink == 1)) {

                if (log_dest == LOGTO_FILE) {
                    log_dest = LOGTO_SYS;
                    openlog(logident, LOG_PID, LOG_DAEMON);
                    setlogmask(logmask);
                }
                goto use_syslog;
            } else {
                if ((lfp = fopen(logfile, "a")) == NULL) {
                    if (log_dest == LOGTO_FILE) {
                        log_dest = LOGTO_SYS;
                        openlog(logident, LOG_PID, LOG_DAEMON);
                        setlogmask(logmask);
                    }
                    goto use_syslog;
                }
            }

            if (log_dest == LOGTO_SYS) {

                closelog();
                log_dest = LOGTO_FILE;
            }
            errno = save_errno;
            verrlog_(level, lfp, fmt, ap);
            fclose(lfp);
        }
    }
    else if ((logmask & LOG_MASK(level)) != 0)
    {
        char buf[1024];
#ifndef HAS_VSYSLOG
        char otherbuf[16384];
#endif
    use_syslog:

        if (level > LOG_DEBUG)
            level = LOG_DEBUG;
#ifdef HAS_VSYSLOG
        vsyslog(level, err_str_(save_errno, fmt, buf), ap);
#else

#if defined(HAS_VSNPRINTF)
        vsnprintf(otherbuf, sizeof(otherbuf), err_str_(save_errno, fmt, buf), ap);
#else
        vsprintf(otherbuf, err_str_(save_errno, fmt, buf), ap);
#endif

        if (!strcmp(otherbuf, lastMsg)) {
            counter++;
            if (counter > 10) {
                syslog(level, otherbuf);
                counter = 0;
            }
        } else {
            syslog(level, otherbuf);
            strcpy(lastMsg, otherbuf);
            counter = 0;
        }
#endif
        closelog();
    }

    va_end(ap);
}