Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

use high-level io for logging

git-svn-id: http://kazuho.31tools.com/svn/incline/trunk@260 4d3e2a30-9d6d-0410-bc8c-dac56cff10b3
  • Loading branch information...
commit d7e2f75766356112ae7e2d0c14ee71dedc223416 1 parent e5f7de2
kazuho authored
17 src/incline.cc
View
@@ -1,7 +1,9 @@
extern "C" {
+#include <errno.h>
#include <fcntl.h>
#include <signal.h>
}
+#include <cstdio>
#include <fstream>
#include <iostream>
#include <iterator>
@@ -173,21 +175,20 @@ main(int argc, char** argv)
vector<string> stmt(aq_driver()->drop_table_all(true));
run_all_stmt(dbh(), stmt);
} else if (command == "forward") {
- int log_fd = -1;
+ FILE* log_fh = NULL;
if (*opt_forwarder_log_file == "-") {
- log_fd = 1;
+ log_fh = stdout;
} else if (! opt_forwarder_log_file->empty()) {
- log_fd = open(opt_forwarder_log_file->c_str(),
- O_WRONLY | O_APPEND | O_CREAT, 0666);
- if (log_fd == -1) {
- fprintf(stderr, "failed to open log file:%s\n",
- opt_forwarder_log_file->c_str());
+ if ((log_fh = fopen(opt_forwarder_log_file->c_str(), "w+")) == NULL) {
+ fprintf(stderr, "failed to open log file:%s:%s\n",
+ opt_forwarder_log_file->c_str(), strerror(errno));
exit(3);
}
}
signal(SIGHUP, shutdown_forwarder);
signal(SIGTERM, shutdown_forwarder);
- aq_driver()->run_forwarder(1, log_fd);
+ aq_driver()->run_forwarder(1, log_fh);
+ // TODO close log_fh
} else {
fprintf(stderr, "unknown command: %s\n", command.c_str());
exit(1);
4 src/incline_driver_async_qtable.cc
View
@@ -62,9 +62,9 @@ incline_driver_async_qtable::drop_table_of(const incline_def* _def,
}
void
-incline_driver_async_qtable::run_forwarder(int poll_interval, int log_fd)
+incline_driver_async_qtable::run_forwarder(int poll_interval, FILE* log_fh)
{
- incline_fw_async_qtable::manager manager(this, poll_interval, log_fd);
+ incline_fw_async_qtable::manager manager(this, poll_interval, log_fh);
vector<pthread_t> threads;
{ // create and start forwarders
2  src/incline_driver_async_qtable.h
View
@@ -17,7 +17,7 @@ class incline_driver_async_qtable : public incline_driver_async {
virtual std::vector<std::string> drop_table_all(bool if_exists) const;
std::string create_table_of(const incline_def* def, bool if_not_exists, incline_dbms* dbh) const;
std::string drop_table_of(const incline_def* def, bool if_exists) const;
- virtual void run_forwarder(int poll_interval, int log_fd);
+ virtual void run_forwarder(int poll_interval, FILE* log_fh);
virtual bool should_exit_loop() const { return should_exit_loop_; }
virtual void should_exit_loop(bool f) { should_exit_loop_ = f; }
protected:
6 src/incline_driver_sharded.cc
View
@@ -480,10 +480,10 @@ incline_driver_sharded::drop_table_all(bool if_exists)
}
void
-incline_driver_sharded::run_forwarder(int poll_interval, int log_fd)
+incline_driver_sharded::run_forwarder(int poll_interval, FILE* log_fh)
{
- incline_fw_sharded::manager shard_mgr(this, poll_interval, log_fd);
- incline_fw_replicator::manager repl_mgr(this, poll_interval, log_fd);
+ incline_fw_sharded::manager shard_mgr(this, poll_interval, log_fh);
+ incline_fw_replicator::manager repl_mgr(this, poll_interval, log_fh);
vector<pthread_t> threads;
// create forwarders and writers
2  src/incline_driver_sharded.h
View
@@ -70,7 +70,7 @@ class incline_driver_sharded : public incline_driver_async_qtable {
virtual incline_def* create_def() const;
virtual std::vector<std::string> create_table_all(bool if_not_exists, incline_dbms* dbh) const;
virtual std::vector<std::string> drop_table_all(bool if_exists) const;
- virtual void run_forwarder(int poll_interval, int log_fd);
+ virtual void run_forwarder(int poll_interval, FILE* log_fh);
virtual bool should_exit_loop() const;
const rule* rule_of(const std::string& file) const;
std::pair<std::string, unsigned short> get_hostport() const {
18 src/incline_fw.cc
View
@@ -1,7 +1,3 @@
-extern "C" {
-#include <sys/uio.h>
-#include <unistd.h>
-}
#include "incline_dbms.h"
#include "incline_def_async_qtable.h"
#include "incline_driver_async_qtable.h"
@@ -13,17 +9,9 @@ using namespace std;
void
incline_fw::manager::log_sql(const incline_dbms* dbms, const string& sql)
{
- if (log_fd_ != -1) {
- struct iovec vec[3];
- char buf[1024];
- snprintf(buf, sizeof(buf), "%s:%hu:", dbms->host().c_str(), dbms->port());
- vec[0].iov_base = buf;
- vec[0].iov_len = strlen(buf);
- vec[1].iov_base = const_cast<char*>(sql.c_str());
- vec[1].iov_len = sql.size();
- vec[2].iov_base = const_cast<char*>(";\n");
- vec[2].iov_len = 2;
- writev(log_fd_, vec, sizeof(vec) / sizeof(vec[0]));
+ if (log_fh_ != NULL) {
+ fprintf(log_fh_, "%s:%hu:%s;\n", dbms->host().c_str(), dbms->port(),
+ sql.c_str());
}
}
5 src/incline_fw.h
View
@@ -1,6 +1,7 @@
#ifndef incline_fw_h
#define incline_fw_h
+#include <cstdio>
#include <string>
#include <vector>
@@ -15,9 +16,9 @@ class incline_fw {
protected:
incline_driver_async_qtable* driver_;
int poll_interval_;
- int log_fd_;
+ FILE* log_fh_;
public:
- manager(incline_driver_async_qtable* driver, int poll_interval, int log_fd) : driver_(driver), poll_interval_(poll_interval), log_fd_(log_fd) {}
+ manager(incline_driver_async_qtable* driver, int poll_interval, FILE* log_fh) : driver_(driver), poll_interval_(poll_interval), log_fh_(log_fh) {}
virtual ~manager() {}
const incline_driver_async_qtable* driver() const { return driver_; }
incline_driver_async_qtable* driver() { return driver_; }
2  src/incline_fw_replicator.h
View
@@ -12,7 +12,7 @@ class incline_fw_replicator : public incline_fw {
public:
typedef super::manager super;
public:
- manager(incline_driver_sharded* driver, int poll_interval, int log_fd) : super(driver, poll_interval, log_fd) {}
+ manager(incline_driver_sharded* driver, int poll_interval, FILE* log_fh) : super(driver, poll_interval, log_fh) {}
const incline_driver_sharded* driver() const { return static_cast<const incline_driver_sharded*>(super::driver()); }
incline_driver_sharded* driver() { return static_cast<incline_driver_sharded*>(super::driver()); }
void start(std::vector<pthread_t>& threads);
2  src/incline_fw_sharded.h
View
@@ -39,7 +39,7 @@ class incline_fw_sharded : public incline_fw_async_qtable {
protected:
std::vector<std::pair<incline_driver_sharded::connect_params, writer*> > writers_;
public:
- manager(incline_driver_sharded* driver, int poll_interval, int log_fd) : super(driver, poll_interval, log_fd), writers_() {}
+ manager(incline_driver_sharded* driver, int poll_interval, FILE* log_fh) : super(driver, poll_interval, log_fh), writers_() {}
virtual ~manager();
const incline_driver_sharded* driver() const { return static_cast<const incline_driver_sharded*>(super::driver()); }
incline_driver_sharded* driver() { return static_cast<incline_driver_sharded*>(super::driver()); }
Please sign in to comment.
Something went wrong with that request. Please try again.