Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logging conflict #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion handy/conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ struct Conf {
std::string filename;
};

} // namespace handy
} // namespace handy
60 changes: 30 additions & 30 deletions handy/conn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,43 @@ void handyUnregisterIdle(EventBase *base, const IdleId &idle);
void handyUpdateIdle(EventBase *base, const IdleId &idle);

void TcpConn::attach(EventBase *base, int fd, Ip4Addr local, Ip4Addr peer) {
fatalif((destPort_ <= 0 && state_ != State::Invalid) || (destPort_ >= 0 && state_ != State::Handshaking),
hfatalif((destPort_ <= 0 && state_ != State::Invalid) || (destPort_ >= 0 && state_ != State::Handshaking),
"you should use a new TcpConn to attach. state: %d", state_);
base_ = base;
state_ = State::Handshaking;
local_ = local;
peer_ = peer;
delete channel_;
channel_ = new Channel(base, fd, kWriteEvent | kReadEvent);
trace("tcp constructed %s - %s fd: %d", local_.toString().c_str(), peer_.toString().c_str(), fd);
htrace("tcp constructed %s - %s fd: %d", local_.toString().c_str(), peer_.toString().c_str(), fd);
TcpConnPtr con = shared_from_this();
con->channel_->onRead([=] { con->handleRead(con); });
con->channel_->onWrite([=] { con->handleWrite(con); });
}

void TcpConn::connect(EventBase *base, const string &host, unsigned short port, int timeout, const string &localip) {
fatalif(state_ != State::Invalid && state_ != State::Closed && state_ != State::Failed, "current state is bad state to connect. state: %d", state_);
hfatalif(state_ != State::Invalid && state_ != State::Closed && state_ != State::Failed, "current state is bad state to connect. state: %d", state_);
destHost_ = host;
destPort_ = port;
connectTimeout_ = timeout;
connectedTime_ = util::timeMilli();
localIp_ = localip;
Ip4Addr addr(host, port);
int fd = socket(AF_INET, SOCK_STREAM, 0);
fatalif(fd < 0, "socket failed %d %s", errno, strerror(errno));
hfatalif(fd < 0, "socket failed %d %s", errno, strerror(errno));
net::setNonBlock(fd);
int t = util::addFdFlag(fd, FD_CLOEXEC);
fatalif(t, "addFdFlag FD_CLOEXEC failed %d %s", t, strerror(t));
hfatalif(t, "addFdFlag FD_CLOEXEC failed %d %s", t, strerror(t));
int r = 0;
if (localip.size()) {
Ip4Addr addr(localip, 0);
r = ::bind(fd, (struct sockaddr *) &addr.getAddr(), sizeof(struct sockaddr));
error("bind to %s failed error %d %s", addr.toString().c_str(), errno, strerror(errno));
herror("bind to %s failed error %d %s", addr.toString().c_str(), errno, strerror(errno));
}
if (r == 0) {
r = ::connect(fd, (sockaddr *) &addr.getAddr(), sizeof(sockaddr_in));
if (r != 0 && errno != EINPROGRESS) {
error("connect to %s error %d %s", addr.toString().c_str(), errno, strerror(errno));
herror("connect to %s error %d %s", addr.toString().c_str(), errno, strerror(errno));
}
}

Expand All @@ -56,7 +56,7 @@ void TcpConn::connect(EventBase *base, const string &host, unsigned short port,
if (r == 0) {
r = getsockname(fd, (sockaddr *) &local, &alen);
if (r < 0) {
error("getsockname failed %d %s", errno, strerror(errno));
herror("getsockname failed %d %s", errno, strerror(errno));
}
}
state_ = State::Handshaking;
Expand Down Expand Up @@ -90,7 +90,7 @@ void TcpConn::cleanup(const TcpConnPtr &con) {
} else {
state_ = State::Closed;
}
trace("tcp closing %s - %s fd %d %d", local_.toString().c_str(), peer_.toString().c_str(), channel_ ? channel_->fd() : -1, errno);
htrace("tcp closing %s - %s fd %d %d", local_.toString().c_str(), peer_.toString().c_str(), channel_ ? channel_->fd() : -1, errno);
getBase()->cancel(timeoutId_);
if (statecb_) {
statecb_(con);
Expand Down Expand Up @@ -118,7 +118,7 @@ void TcpConn::handleRead(const TcpConnPtr &con) {
int rd = 0;
if (channel_->fd() >= 0) {
rd = readImp(channel_->fd(), input_.end(), input_.space());
trace("channel %lld fd %d readed %d bytes", (long long) channel_->id(), channel_->fd(), rd);
htrace("channel %lld fd %d readed %d bytes", (long long) channel_->id(), channel_->fd(), rd);
}
if (rd == -1 && errno == EINTR) {
continue;
Expand All @@ -140,7 +140,7 @@ void TcpConn::handleRead(const TcpConnPtr &con) {
}

int TcpConn::handleHandshake(const TcpConnPtr &con) {
fatalif(state_ != Handshaking, "handleHandshaking called when state_=%d", state_);
hfatalif(state_ != Handshaking, "handleHandshaking called when state_=%d", state_);
struct pollfd pfd;
pfd.fd = channel_->fd();
pfd.events = POLLOUT | POLLERR;
Expand All @@ -150,13 +150,13 @@ int TcpConn::handleHandshake(const TcpConnPtr &con) {
state_ = State::Connected;
if (state_ == State::Connected) {
connectedTime_ = util::timeMilli();
trace("tcp connected %s - %s fd %d", local_.toString().c_str(), peer_.toString().c_str(), channel_->fd());
htrace("tcp connected %s - %s fd %d", local_.toString().c_str(), peer_.toString().c_str(), channel_->fd());
if (statecb_) {
statecb_(con);
}
}
} else {
trace("poll fd %d return %d revents %d", channel_->fd(), r, pfd.revents);
htrace("poll fd %d return %d revents %d", channel_->fd(), r, pfd.revents);
cleanup(con);
return -1;
}
Expand All @@ -176,15 +176,15 @@ void TcpConn::handleWrite(const TcpConnPtr &con) {
channel_->enableWrite(false);
}
} else {
error("handle write unexpected");
herror("handle write unexpected");
}
}

ssize_t TcpConn::isend(const char *buf, size_t len) {
size_t sended = 0;
while (len > sended) {
ssize_t wd = writeImp(channel_->fd(), buf + sended, len - sended);
trace("channel %lld fd %d write %ld bytes", (long long) channel_->id(), channel_->fd(), wd);
htrace("channel %lld fd %d write %ld bytes", (long long) channel_->id(), channel_->fd(), wd);
if (wd > 0) {
sended += wd;
continue;
Expand All @@ -196,7 +196,7 @@ ssize_t TcpConn::isend(const char *buf, size_t len) {
}
break;
} else {
error("write error: channel %lld fd %d wd %ld %d %s", (long long) channel_->id(), channel_->fd(), wd, errno, strerror(errno));
herror("write error: channel %lld fd %d wd %ld %d %s", (long long) channel_->id(), channel_->fd(), wd, errno, strerror(errno));
break;
}
}
Expand All @@ -219,7 +219,7 @@ void TcpConn::send(Buffer &buf) {
}
}
} else {
warn("connection %s - %s closed, but still writing %lu bytes", local_.toString().c_str(), peer_.toString().c_str(), buf.size());
hwarn("connection %s - %s closed, but still writing %lu bytes", local_.toString().c_str(), peer_.toString().c_str(), buf.size());
}
}

Expand All @@ -234,7 +234,7 @@ void TcpConn::send(const char *buf, size_t len) {
output_.append(buf, len);
}
} else {
warn("connection %s - %s closed, but still writing %lu bytes", local_.toString().c_str(), peer_.toString().c_str(), len);
hwarn("connection %s - %s closed, but still writing %lu bytes", local_.toString().c_str(), peer_.toString().c_str(), len);
}
}

Expand All @@ -250,7 +250,7 @@ void TcpConn::onMsg(CodecBase *codec, const MsgCallBack &cb) {
con->channel_->close();
break;
} else if (r > 0) {
trace("a msg decoded. origin len %d msg len %ld", r, msg.size());
htrace("a msg decoded. origin len %d msg len %ld", r, msg.size());
cb(con, msg);
con->getInput().consume(r);
}
Expand All @@ -269,20 +269,20 @@ int TcpServer::bind(const std::string &host, unsigned short port, bool reusePort
addr_ = Ip4Addr(host, port);
int fd = socket(AF_INET, SOCK_STREAM, 0);
int r = net::setReuseAddr(fd);
fatalif(r, "set socket reuse option failed");
hfatalif(r, "set socket reuse option failed");
r = net::setReusePort(fd, reusePort);
fatalif(r, "set socket reuse port option failed");
hfatalif(r, "set socket reuse port option failed");
r = util::addFdFlag(fd, FD_CLOEXEC);
fatalif(r, "addFdFlag FD_CLOEXEC failed");
hfatalif(r, "addFdFlag FD_CLOEXEC failed");
r = ::bind(fd, (struct sockaddr *) &addr_.getAddr(), sizeof(struct sockaddr));
if (r) {
close(fd);
error("bind to %s failed %d %s", addr_.toString().c_str(), errno, strerror(errno));
herror("bind to %s failed %d %s", addr_.toString().c_str(), errno, strerror(errno));
return errno;
}
r = listen(fd, 20);
fatalif(r, "listen failed %d %s", errno, strerror(errno));
info("fd %d listening at %s", fd, addr_.toString().c_str());
hfatalif(r, "listen failed %d %s", errno, strerror(errno));
hinfo("fd %d listening at %s", fd, addr_.toString().c_str());
listen_channel_ = new Channel(base_, fd, kReadEvent);
listen_channel_->onRead([this] { handleAccept(); });
return 0;
Expand All @@ -292,7 +292,7 @@ TcpServerPtr TcpServer::startServer(EventBases *bases, const std::string &host,
TcpServerPtr p(new TcpServer(bases));
int r = p->bind(host, port, reusePort);
if (r) {
error("bind to %s:%d failed %d %s", host.c_str(), port, errno, strerror(errno));
herror("bind to %s:%d failed %d %s", host.c_str(), port, errno, strerror(errno));
}
return r == 0 ? p : NULL;
}
Expand All @@ -307,16 +307,16 @@ void TcpServer::handleAccept() {
socklen_t alen = sizeof(peer);
int r = getpeername(cfd, (sockaddr *) &peer, &alen);
if (r < 0) {
error("get peer name failed %d %s", errno, strerror(errno));
herror("get peer name failed %d %s", errno, strerror(errno));
continue;
}
r = getsockname(cfd, (sockaddr *) &local, &alen);
if (r < 0) {
error("getsockname failed %d %s", errno, strerror(errno));
herror("getsockname failed %d %s", errno, strerror(errno));
continue;
}
r = util::addFdFlag(cfd, FD_CLOEXEC);
fatalif(r, "addFdFlag FD_CLOEXEC failed");
hfatalif(r, "addFdFlag FD_CLOEXEC failed");
EventBase *b = bases_->allocBase();
auto addcon = [=] {
TcpConnPtr con = createcb_();
Expand All @@ -338,7 +338,7 @@ void TcpServer::handleAccept() {
}
}
if (lfd >= 0 && errno != EAGAIN && errno != EINTR) {
warn("accept return %d %d %s", cfd, errno, strerror(errno));
hwarn("accept return %d %d %s", cfd, errno, strerror(errno));
}
}

Expand Down
28 changes: 14 additions & 14 deletions handy/event_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ struct EventsImp {
}
void wakeup() {
int r = write(wakeupFds_[1], "", 1);
fatalif(r <= 0, "write error wd %d %d %s", r, errno, strerror(errno));
hfatalif(r <= 0, "write error wd %d %d %s", r, errno, strerror(errno));
}

bool cancel(TimerId timerid);
Expand Down Expand Up @@ -143,12 +143,12 @@ void EventsImp::loop() {

void EventsImp::init() {
int r = pipe(wakeupFds_);
fatalif(r, "pipe failed %d %s", errno, strerror(errno));
hfatalif(r, "pipe failed %d %s", errno, strerror(errno));
r = util::addFdFlag(wakeupFds_[0], FD_CLOEXEC);
fatalif(r, "addFdFlag failed %d %s", errno, strerror(errno));
hfatalif(r, "addFdFlag failed %d %s", errno, strerror(errno));
r = util::addFdFlag(wakeupFds_[1], FD_CLOEXEC);
fatalif(r, "addFdFlag failed %d %s", errno, strerror(errno));
trace("wakeup pipe created %d %d", wakeupFds_[0], wakeupFds_[1]);
hfatalif(r, "addFdFlag failed %d %s", errno, strerror(errno));
htrace("wakeup pipe created %d %d", wakeupFds_[0], wakeupFds_[1]);
Channel *ch = new Channel(base_, wakeupFds_[0], kReadEvent);
ch->onRead([=] {
char buf[1024];
Expand All @@ -162,7 +162,7 @@ void EventsImp::init() {
delete ch;
} else if (errno == EINTR) {
} else {
fatal("wakeup channel read error %d %d %s", r, errno, strerror(errno));
hfatal("wakeup channel read error %d %d %s", r, errno, strerror(errno));
}
});
}
Expand Down Expand Up @@ -207,17 +207,17 @@ IdleId EventsImp::registerIdle(int idle, const TcpConnPtr &con, const TcpCallBac
}
auto &lst = idleConns_[idle];
lst.push_back(IdleNode{con, util::timeMilli() / 1000, move(cb)});
trace("register idle");
htrace("register idle");
return IdleId(new IdleIdImp(&lst, --lst.end()));
}

void EventsImp::unregisterIdle(const IdleId &id) {
trace("unregister idle");
htrace("unregister idle");
id->lst_->erase(id->iter_);
}

void EventsImp::updateIdle(const IdleId &id) {
trace("update idle");
htrace("update idle");
id->iter_->updated_ = util::timeMilli() / 1000;
id->lst_->splice(id->lst_->end(), *id->lst_, id->iter_);
}
Expand Down Expand Up @@ -293,7 +293,7 @@ void MultiBase::loop() {
}

Channel::Channel(EventBase *base, int fd, int events) : base_(base), fd_(fd), events_(events) {
fatalif(net::setNonBlock(fd_) < 0, "channel set non block failed");
hfatalif(net::setNonBlock(fd_) < 0, "channel set non block failed");
static atomic<int64_t> id(0);
id_ = ++id;
poller_ = base_->imp_->poller_;
Expand Down Expand Up @@ -338,7 +338,7 @@ void Channel::enableReadWrite(bool readable, bool writable) {

void Channel::close() {
if (fd_ >= 0) {
trace("close channel %ld fd %d", (long) id_, fd_);
htrace("close channel %ld fd %d", (long) id_, fd_);
poller_->removeChannel(this);
::close(fd_);
fd_ = -1;
Expand All @@ -365,7 +365,7 @@ TcpConn::TcpConn()
: base_(NULL), channel_(NULL), state_(State::Invalid), destPort_(-1), connectTimeout_(0), reconnectInterval_(-1), connectedTime_(util::timeMilli()) {}

TcpConn::~TcpConn() {
trace("tcp destroyed %s - %s", local_.toString().c_str(), peer_.toString().c_str());
htrace("tcp destroyed %s - %s", local_.toString().c_str(), peer_.toString().c_str());
delete channel_;
}

Expand All @@ -380,7 +380,7 @@ void TcpConn::reconnect() {
getBase()->imp_->reconnectConns_.insert(con);
long long interval = reconnectInterval_ - (util::timeMilli() - connectedTime_);
interval = interval > 0 ? interval : 0;
info("reconnect interval: %d will reconnect after %lld ms", reconnectInterval_, interval);
hinfo("reconnect interval: %d will reconnect after %lld ms", reconnectInterval_, interval);
getBase()->runAfter(interval, [this, con]() {
getBase()->imp_->reconnectConns_.erase(con);
connect(getBase(), destHost_, (unsigned short) destPort_, connectTimeout_, localIp_);
Expand All @@ -389,4 +389,4 @@ void TcpConn::reconnect() {
channel_ = NULL;
}

} // namespace handy
} // namespace handy
14 changes: 7 additions & 7 deletions handy/http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ HttpMsg::Result HttpMsg::tryDecode_(Slice buf, bool copyBody, Slice *line1) {
} else if (k.empty() && ln.empty() && req.empty()) {
break;
} else {
error("bad http line: %.*s %.*s", (int) k.size(), k.data(), (int) ln.size(), ln.data());
herror("bad http line: %.*s %.*s", (int) k.size(), k.data(), (int) ln.size(), ln.data());
return Error;
}
}
Expand Down Expand Up @@ -100,7 +100,7 @@ HttpMsg::Result HttpRequest::tryDecode(Slice buf, bool copyBody) {
query_uri = ln1.eatWord();
version = ln1.eatWord();
if (query_uri.size() == 0 || query_uri[0] != '/') {
error("query uri '%.*s' should begin with /", (int) query_uri.size(), query_uri.data());
herror("query uri '%.*s' should begin with /", (int) query_uri.size(), query_uri.data());
return Error;
}
for (size_t i = 0; i < query_uri.size(); i++) {
Expand Down Expand Up @@ -194,8 +194,8 @@ void HttpConnPtr::handleRead(const HttpCallBack &cb) const {
if (r == HttpMsg::Continue100) {
tcp->send("HTTP/1.1 100 Continue\n\r\n");
} else if (r == HttpMsg::Complete) {
info("http request: %s %s %s", req.method.c_str(), req.query_uri.c_str(), req.version.c_str());
trace("http request:\n%.*s", (int) tcp->input_.size(), tcp->input_.data());
hinfo("http request: %s %s %s", req.method.c_str(), req.query_uri.c_str(), req.version.c_str());
htrace("http request:\n%.*s", (int) tcp->input_.size(), tcp->input_.data());
cb(*this);
}
} else {
Expand All @@ -206,8 +206,8 @@ void HttpConnPtr::handleRead(const HttpCallBack &cb) const {
return;
}
if (r == HttpMsg::Complete) {
info("http response: %d %s", resp.status, resp.statusWord.c_str());
trace("http response:\n%.*s", (int) tcp->input_.size(), tcp->input_.data());
hinfo("http response: %d %s", resp.status, resp.statusWord.c_str());
htrace("http response:\n%.*s", (int) tcp->input_.size(), tcp->input_.data());
cb(tcp);
}
}
Expand All @@ -225,7 +225,7 @@ void HttpConnPtr::clearData() const {

void HttpConnPtr::logOutput(const char *title) const {
Buffer &o = tcp->getOutput();
trace("%s:\n%.*s", title, (int) o.size(), o.data());
htrace("%s:\n%.*s", title, (int) o.size(), o.data());
}

HttpServer::HttpServer(EventBases *bases) : TcpServer(bases) {
Expand Down
4 changes: 2 additions & 2 deletions handy/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void Logger::setFileName(const string &filename) {
fd_ = fd;
} else {
int r = dup2(fd, fd_);
fatalif(r < 0, "dup2 failed");
hfatalif(r < 0, "dup2 failed");
close(fd);
}
}
Expand Down Expand Up @@ -142,4 +142,4 @@ void Logger::logv(int level, const char *file, int line, const char *func, const
}
}

} // namespace handy
} // namespace handy
Loading