From 12e030784df09f010e54ce7d98a6b3c493168fe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Szczepaniak?= Date: Sat, 19 Aug 2023 12:24:16 +0200 Subject: [PATCH] Change logging defines to not collide with normal functions for example in qt --- handy/conf.h | 2 +- handy/conn.cc | 60 ++++++++++++++++++++++----------------------- handy/event_base.cc | 28 ++++++++++----------- handy/http.cc | 14 +++++------ handy/logging.cc | 4 +-- handy/logging.h | 18 +++++++------- handy/net.cc | 4 +-- handy/poller.cc | 58 +++++++++++++++++++++---------------------- handy/stat-svr.cc | 6 ++--- handy/udp.cc | 42 +++++++++++++++---------------- 10 files changed, 118 insertions(+), 118 deletions(-) diff --git a/handy/conf.h b/handy/conf.h index 1704237..c92827d 100644 --- a/handy/conf.h +++ b/handy/conf.h @@ -36,4 +36,4 @@ struct Conf { std::string filename; }; -} // namespace handy \ No newline at end of file +} // namespace handy diff --git a/handy/conn.cc b/handy/conn.cc index b934308..53ab2ea 100644 --- a/handy/conn.cc +++ b/handy/conn.cc @@ -11,7 +11,7 @@ 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; @@ -19,14 +19,14 @@ void TcpConn::attach(EventBase *base, int fd, Ip4Addr local, Ip4Addr peer) { 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; @@ -34,20 +34,20 @@ void TcpConn::connect(EventBase *base, const string &host, unsigned short port, 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)); } } @@ -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; @@ -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); @@ -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; @@ -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; @@ -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; } @@ -176,7 +176,7 @@ void TcpConn::handleWrite(const TcpConnPtr &con) { channel_->enableWrite(false); } } else { - error("handle write unexpected"); + herror("handle write unexpected"); } } @@ -184,7 +184,7 @@ 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; @@ -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; } } @@ -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()); } } @@ -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); } } @@ -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); } @@ -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; @@ -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; } @@ -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_(); @@ -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)); } } diff --git a/handy/event_base.cc b/handy/event_base.cc index e60ae64..5f4451d 100644 --- a/handy/event_base.cc +++ b/handy/event_base.cc @@ -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); @@ -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]; @@ -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)); } }); } @@ -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_); } @@ -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 id(0); id_ = ++id; poller_ = base_->imp_->poller_; @@ -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; @@ -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_; } @@ -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_); @@ -389,4 +389,4 @@ void TcpConn::reconnect() { channel_ = NULL; } -} // namespace handy \ No newline at end of file +} // namespace handy diff --git a/handy/http.cc b/handy/http.cc index de667df..25cb239 100644 --- a/handy/http.cc +++ b/handy/http.cc @@ -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; } } @@ -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++) { @@ -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 { @@ -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); } } @@ -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) { diff --git a/handy/logging.cc b/handy/logging.cc index 0a9dcd6..45529e8 100644 --- a/handy/logging.cc +++ b/handy/logging.cc @@ -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); } } @@ -142,4 +142,4 @@ void Logger::logv(int level, const char *file, int line, const char *func, const } } -} // namespace handy \ No newline at end of file +} // namespace handy diff --git a/handy/logging.h b/handy/logging.h index 8f7904e..81eb308 100644 --- a/handy/logging.h +++ b/handy/logging.h @@ -22,25 +22,25 @@ #endif -#define trace(...) hlog(Logger::LTRACE, __VA_ARGS__) -#define debug(...) hlog(Logger::LDEBUG, __VA_ARGS__) -#define info(...) hlog(Logger::LINFO, __VA_ARGS__) -#define warn(...) hlog(Logger::LWARN, __VA_ARGS__) -#define error(...) hlog(Logger::LERROR, __VA_ARGS__) -#define fatal(...) hlog(Logger::LFATAL, __VA_ARGS__) -#define fatalif(b, ...) \ +#define htrace(...) hlog(Logger::LTRACE, __VA_ARGS__) +#define hdebug(...) hlog(Logger::LDEBUG, __VA_ARGS__) +#define hinfo(...) hlog(Logger::LINFO, __VA_ARGS__) +#define hwarn(...) hlog(Logger::LWARN, __VA_ARGS__) +#define herror(...) hlog(Logger::LERROR, __VA_ARGS__) +#define hfatal(...) hlog(Logger::LFATAL, __VA_ARGS__) +#define hfatalif(b, ...) \ do { \ if ((b)) { \ hlog(Logger::LFATAL, __VA_ARGS__); \ } \ } while (0) -#define check(b, ...) \ +#define hcheck(b, ...) \ do { \ if ((b)) { \ hlog(Logger::LFATAL, __VA_ARGS__); \ } \ } while (0) -#define exitif(b, ...) \ +#define hexitif(b, ...) \ do { \ if ((b)) { \ hlog(Logger::LERROR, __VA_ARGS__); \ diff --git a/handy/net.cc b/handy/net.cc index e25116b..61ed9d9 100644 --- a/handy/net.cc +++ b/handy/net.cc @@ -29,7 +29,7 @@ int net::setReuseAddr(int fd, bool value) { int net::setReusePort(int fd, bool value) { #ifndef SO_REUSEPORT - fatalif(value, "SO_REUSEPORT not supported"); + hfatalif(value, "SO_REUSEPORT not supported"); return 0; #else int flag = value; @@ -54,7 +54,7 @@ Ip4Addr::Ip4Addr(const string &host, unsigned short port) { addr_.sin_addr.s_addr = INADDR_ANY; } if (addr_.sin_addr.s_addr == INADDR_NONE) { - error("cannot resove %s to ip", host.c_str()); + herror("cannot resove %s to ip", host.c_str()); } } diff --git a/handy/poller.cc b/handy/poller.cc index 8bf190f..4e8231f 100644 --- a/handy/poller.cc +++ b/handy/poller.cc @@ -35,17 +35,17 @@ PollerBase *createPoller() { PollerEpoll::PollerEpoll() { fd_ = epoll_create1(EPOLL_CLOEXEC); - fatalif(fd_ < 0, "epoll_create error %d %s", errno, strerror(errno)); - info("poller epoll %d created", fd_); + hfatalif(fd_ < 0, "epoll_create error %d %s", errno, strerror(errno)); + hinfo("poller epoll %d created", fd_); } PollerEpoll::~PollerEpoll() { - info("destroying poller %d", fd_); + hinfo("destroying poller %d", fd_); while (liveChannels_.size()) { (*liveChannels_.begin())->close(); } ::close(fd_); - info("poller %d destroyed", fd_); + hinfo("poller %d destroyed", fd_); } void PollerEpoll::addChannel(Channel *ch) { @@ -53,9 +53,9 @@ void PollerEpoll::addChannel(Channel *ch) { memset(&ev, 0, sizeof(ev)); ev.events = ch->events(); ev.data.ptr = ch; - trace("adding channel %lld fd %d events %d epoll %d", (long long) ch->id(), ch->fd(), ev.events, fd_); + htrace("adding channel %lld fd %d events %d epoll %d", (long long) ch->id(), ch->fd(), ev.events, fd_); int r = epoll_ctl(fd_, EPOLL_CTL_ADD, ch->fd(), &ev); - fatalif(r, "epoll_ctl add failed %d %s", errno, strerror(errno)); + hfatalif(r, "epoll_ctl add failed %d %s", errno, strerror(errno)); liveChannels_.insert(ch); } @@ -64,13 +64,13 @@ void PollerEpoll::updateChannel(Channel *ch) { memset(&ev, 0, sizeof(ev)); ev.events = ch->events(); ev.data.ptr = ch; - trace("modifying channel %lld fd %d events read %d write %d epoll %d", (long long) ch->id(), ch->fd(), ev.events & POLLIN, ev.events & POLLOUT, fd_); + htrace("modifying channel %lld fd %d events read %d write %d epoll %d", (long long) ch->id(), ch->fd(), ev.events & POLLIN, ev.events & POLLOUT, fd_); int r = epoll_ctl(fd_, EPOLL_CTL_MOD, ch->fd(), &ev); - fatalif(r, "epoll_ctl mod failed %d %s", errno, strerror(errno)); + hfatalif(r, "epoll_ctl mod failed %d %s", errno, strerror(errno)); } void PollerEpoll::removeChannel(Channel *ch) { - trace("deleting channel %lld fd %d epoll %d", (long long) ch->id(), ch->fd(), fd_); + htrace("deleting channel %lld fd %d epoll %d", (long long) ch->id(), ch->fd(), fd_); liveChannels_.erase(ch); for (int i = lastActive_; i >= 0; i--) { if (ch == activeEvs_[i].data.ptr) { @@ -84,21 +84,21 @@ void PollerEpoll::loop_once(int waitMs) { int64_t ticks = util::timeMilli(); lastActive_ = epoll_wait(fd_, activeEvs_, kMaxEvents, waitMs); int64_t used = util::timeMilli() - ticks; - trace("epoll wait %d return %d errno %d used %lld millsecond", waitMs, lastActive_, errno, (long long) used); - fatalif(lastActive_ == -1 && errno != EINTR, "epoll return error %d %s", errno, strerror(errno)); + htrace("epoll wait %d return %d errno %d used %lld millsecond", waitMs, lastActive_, errno, (long long) used); + hfatalif(lastActive_ == -1 && errno != EINTR, "epoll return error %d %s", errno, strerror(errno)); while (--lastActive_ >= 0) { int i = lastActive_; Channel *ch = (Channel *) activeEvs_[i].data.ptr; int events = activeEvs_[i].events; if (ch) { if (events & (kReadEvent | POLLERR)) { - trace("channel %lld fd %d handle read", (long long) ch->id(), ch->fd()); + htrace("channel %lld fd %d handle read", (long long) ch->id(), ch->fd()); ch->handleRead(); } else if (events & kWriteEvent) { - trace("channel %lld fd %d handle write", (long long) ch->id(), ch->fd()); + htrace("channel %lld fd %d handle write", (long long) ch->id(), ch->fd()); ch->handleWrite(); } else { - fatal("unexpected poller events"); + hfatal("unexpected poller events"); } } } @@ -125,17 +125,17 @@ PollerBase *createPoller() { PollerKqueue::PollerKqueue() { fd_ = kqueue(); - fatalif(fd_ < 0, "kqueue error %d %s", errno, strerror(errno)); - info("poller kqueue %d created", fd_); + hfatalif(fd_ < 0, "kqueue error %d %s", errno, strerror(errno)); + hinfo("poller kqueue %d created", fd_); } PollerKqueue::~PollerKqueue() { - info("destroying poller %d", fd_); + hinfo("destroying poller %d", fd_); while (liveChannels_.size()) { (*liveChannels_.begin())->close(); } ::close(fd_); - info("poller %d destroyed", fd_); + hinfo("poller %d destroyed", fd_); } void PollerKqueue::addChannel(Channel *ch) { @@ -150,9 +150,9 @@ void PollerKqueue::addChannel(Channel *ch) { if (ch->writeEnabled()) { EV_SET(&ev[n++], ch->fd(), EVFILT_WRITE, EV_ADD | EV_ENABLE, 0, 0, ch); } - trace("adding channel %lld fd %d events read %d write %d epoll %d", (long long) ch->id(), ch->fd(), ch->events() & POLLIN, ch->events() & POLLOUT, fd_); + htrace("adding channel %lld fd %d events read %d write %d epoll %d", (long long) ch->id(), ch->fd(), ch->events() & POLLIN, ch->events() & POLLOUT, fd_); int r = kevent(fd_, ev, n, NULL, 0, &now); - fatalif(r, "kevent add failed %d %s", errno, strerror(errno)); + hfatalif(r, "kevent add failed %d %s", errno, strerror(errno)); liveChannels_.insert(ch); } @@ -172,13 +172,13 @@ void PollerKqueue::updateChannel(Channel *ch) { } else { EV_SET(&ev[n++], ch->fd(), EVFILT_WRITE, EV_DELETE, 0, 0, ch); } - trace("modifying channel %lld fd %d events read %d write %d epoll %d", (long long) ch->id(), ch->fd(), ch->events() & POLLIN, ch->events() & POLLOUT, fd_); + htrace("modifying channel %lld fd %d events read %d write %d epoll %d", (long long) ch->id(), ch->fd(), ch->events() & POLLIN, ch->events() & POLLOUT, fd_); int r = kevent(fd_, ev, n, NULL, 0, &now); - fatalif(r, "kevent mod failed %d %s", errno, strerror(errno)); + hfatalif(r, "kevent mod failed %d %s", errno, strerror(errno)); } void PollerKqueue::removeChannel(Channel *ch) { - trace("deleting channel %lld fd %d epoll %d", (long long) ch->id(), ch->fd(), fd_); + htrace("deleting channel %lld fd %d epoll %d", (long long) ch->id(), ch->fd(), fd_); liveChannels_.erase(ch); // remove channel if in ready stat for (int i = lastActive_; i >= 0; i--) { @@ -195,8 +195,8 @@ void PollerKqueue::loop_once(int waitMs) { timeout.tv_nsec = (waitMs % 1000) * 1000 * 1000; long ticks = util::timeMilli(); lastActive_ = kevent(fd_, NULL, 0, activeEvs_, kMaxEvents, &timeout); - trace("kevent wait %d return %d errno %d used %lld millsecond", waitMs, lastActive_, errno, util::timeMilli() - ticks); - fatalif(lastActive_ == -1 && errno != EINTR, "kevent return error %d %s", errno, strerror(errno)); + htrace("kevent wait %d return %d errno %d used %lld millsecond", waitMs, lastActive_, errno, util::timeMilli() - ticks); + hfatalif(lastActive_ == -1 && errno != EINTR, "kevent return error %d %s", errno, strerror(errno)); while (--lastActive_ >= 0) { int i = lastActive_; Channel *ch = (Channel *) activeEvs_[i].udata; @@ -204,17 +204,17 @@ void PollerKqueue::loop_once(int waitMs) { if (ch) { // only handle write if read and write are enabled if (!(ke.flags & EV_EOF) && ch->writeEnabled()) { - trace("channel %lld fd %d handle write", (long long) ch->id(), ch->fd()); + htrace("channel %lld fd %d handle write", (long long) ch->id(), ch->fd()); ch->handleWrite(); } else if ((ke.flags & EV_EOF) || ch->readEnabled()) { - trace("channel %lld fd %d handle read", (long long) ch->id(), ch->fd()); + htrace("channel %lld fd %d handle read", (long long) ch->id(), ch->fd()); ch->handleRead(); } else { - fatal("unexpected epoll events %d", ch->events()); + hfatal("unexpected epoll events %d", ch->events()); } } } } #endif -} // namespace handy \ No newline at end of file +} // namespace handy diff --git a/handy/stat-svr.cc b/handy/stat-svr.cc index 2ea2211..56f282a 100644 --- a/handy/stat-svr.cc +++ b/handy/stat-svr.cc @@ -62,7 +62,7 @@ StatServer::StatServer(EventBase *base) : server_(base) { } resp.body = Slice(buf.data(), buf.size()); } - info("response is: %d \n%.*s", resp.status, (int) resp.body.size(), resp.body.data()); + hinfo("response is: %d \n%.*s", resp.status, (int) resp.body.size(), resp.body.data()); con.sendResponse(); }); } @@ -75,7 +75,7 @@ void StatServer::onRequest(StatType type, const string &key, const string &desc, } else if (type == CMD) { cmdcbs_[key] = {desc, cb}; } else { - error("unknow state type: %d", type); + herror("unknow state type: %d", type); return; } allcbs_[key] = cb; @@ -89,7 +89,7 @@ void StatServer::onPageFile(const string &page, const string &desc, const string return onRequest(PAGE, page, desc, [file](const HttpRequest &req, HttpResponse &resp) { Status st = file::getContent(file, resp.body); if (!st.ok()) { - error("get file %s failed %s", file.c_str(), st.toString().c_str()); + herror("get file %s failed %s", file.c_str(), st.toString().c_str()); resp.setNotFound(); } else { resp.headers["Content-Type"] = "text/plain; charset=utf-8"; diff --git a/handy/udp.cc b/handy/udp.cc index a0a895f..6b8ab02 100644 --- a/handy/udp.cc +++ b/handy/udp.cc @@ -11,19 +11,19 @@ int UdpServer::bind(const std::string &host, unsigned short port, bool reusePort addr_ = Ip4Addr(host, port); int fd = socket(AF_INET, SOCK_DGRAM, 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; } net::setNonBlock(fd); - info("udp fd %d bind to %s", fd, addr_.toString().c_str()); + hinfo("udp fd %d bind to %s", fd, addr_.toString().c_str()); channel_ = new Channel(base_, fd, kReadEvent); channel_->onRead([this] { Buffer buf; @@ -35,11 +35,11 @@ int UdpServer::bind(const std::string &host, unsigned short port, bool reusePort int fd = channel_->fd(); ssize_t rn = recvfrom(fd, buf.makeRoom(kUdpPacketSize), kUdpPacketSize, 0, (sockaddr *) &raddr, &rsz); if (rn < 0) { - error("udp %d recv failed: %d %s", fd, errno, strerror(errno)); + herror("udp %d recv failed: %d %s", fd, errno, strerror(errno)); return; } buf.addSize(rn); - trace("udp %d recv %ld bytes from %s", fd, rn, Ip4Addr(raddr).toString().data()); + htrace("udp %d recv %ld bytes from %s", fd, rn, Ip4Addr(raddr).toString().data()); this->msgcb_(shared_from_this(), buf, raddr); }); return 0; @@ -49,38 +49,38 @@ UdpServerPtr UdpServer::startServer(EventBases *bases, const std::string &host, UdpServerPtr p(new UdpServer(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; } void UdpServer::sendTo(const char *buf, size_t len, Ip4Addr addr) { if (!channel_ || channel_->fd() < 0) { - warn("udp sending %lu bytes to %s after channel closed", len, addr.toString().data()); + hwarn("udp sending %lu bytes to %s after channel closed", len, addr.toString().data()); return; } int fd = channel_->fd(); int wn = ::sendto(fd, buf, len, 0, (sockaddr *) &addr.getAddr(), sizeof(sockaddr)); if (wn < 0) { - error("udp %d sendto %s error: %d %s", fd, addr.toString().c_str(), errno, strerror(errno)); + herror("udp %d sendto %s error: %d %s", fd, addr.toString().c_str(), errno, strerror(errno)); return; } - trace("udp %d sendto %s %d bytes", fd, addr.toString().c_str(), wn); + htrace("udp %d sendto %s %d bytes", fd, addr.toString().c_str(), wn); } UdpConnPtr UdpConn::createConnection(EventBase *base, const string &host, unsigned short port) { Ip4Addr addr(host, port); int fd = socket(AF_INET, SOCK_DGRAM, 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 = ::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)); return NULL; } - trace("udp fd %d connecting to %s ok", fd, addr.toString().data()); + htrace("udp fd %d connecting to %s ok", fd, addr.toString().data()); UdpConnPtr con(new UdpConn); con->destHost_ = host; con->destPort_ = port; @@ -96,10 +96,10 @@ UdpConnPtr UdpConn::createConnection(EventBase *base, const string &host, unsign int fd = con->channel_->fd(); int rn = ::read(fd, input.makeRoom(kUdpPacketSize), kUdpPacketSize); if (rn < 0) { - error("udp read from %d error %d %s", fd, errno, strerror(errno)); + herror("udp read from %d error %d %s", fd, errno, strerror(errno)); return; } - trace("udp %d read %d bytes", fd, rn); + htrace("udp %d read %d bytes", fd, rn); input.addSize(rn); con->cb_(con, input); }); @@ -116,16 +116,16 @@ void UdpConn::close() { void UdpConn::send(const char *buf, size_t len) { if (!channel_ || channel_->fd() < 0) { - warn("udp sending %lu bytes to %s after channel closed", len, peer_.toString().data()); + hwarn("udp sending %lu bytes to %s after channel closed", len, peer_.toString().data()); return; } int fd = channel_->fd(); int wn = ::write(fd, buf, len); if (wn < 0) { - error("udp %d write error %d %s", fd, errno, strerror(errno)); + herror("udp %d write error %d %s", fd, errno, strerror(errno)); return; } - trace("udp %d write %d bytes", fd, wn); + htrace("udp %d write %d bytes", fd, wn); } HSHAUPtr HSHAU::startServer(EventBase *base, const std::string &host, unsigned short port, int threads) { @@ -147,4 +147,4 @@ void HSHAU::onMsg(const RetMsgUdpCallBack &cb) { }); } -} // namespace handy \ No newline at end of file +} // namespace handy