Skip to content

Commit 9b1f2a6

Browse files
Change logging defines to not collide with normal functions for example in qt
1 parent e58eb50 commit 9b1f2a6

10 files changed

Lines changed: 118 additions & 118 deletions

File tree

handy/conf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ struct Conf {
3636
std::string filename;
3737
};
3838

39-
} // namespace handy
39+
} // namespace handy

handy/conn.cc

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,43 @@ void handyUnregisterIdle(EventBase *base, const IdleId &idle);
1111
void handyUpdateIdle(EventBase *base, const IdleId &idle);
1212

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

2828
void TcpConn::connect(EventBase *base, const string &host, unsigned short port, int timeout, const string &localip) {
29-
fatalif(state_ != State::Invalid && state_ != State::Closed && state_ != State::Failed, "current state is bad state to connect. state: %d", state_);
29+
hfatalif(state_ != State::Invalid && state_ != State::Closed && state_ != State::Failed, "current state is bad state to connect. state: %d", state_);
3030
destHost_ = host;
3131
destPort_ = port;
3232
connectTimeout_ = timeout;
3333
connectedTime_ = util::timeMilli();
3434
localIp_ = localip;
3535
Ip4Addr addr(host, port);
3636
int fd = socket(AF_INET, SOCK_STREAM, 0);
37-
fatalif(fd < 0, "socket failed %d %s", errno, strerror(errno));
37+
hfatalif(fd < 0, "socket failed %d %s", errno, strerror(errno));
3838
net::setNonBlock(fd);
3939
int t = util::addFdFlag(fd, FD_CLOEXEC);
40-
fatalif(t, "addFdFlag FD_CLOEXEC failed %d %s", t, strerror(t));
40+
hfatalif(t, "addFdFlag FD_CLOEXEC failed %d %s", t, strerror(t));
4141
int r = 0;
4242
if (localip.size()) {
4343
Ip4Addr addr(localip, 0);
4444
r = ::bind(fd, (struct sockaddr *) &addr.getAddr(), sizeof(struct sockaddr));
45-
error("bind to %s failed error %d %s", addr.toString().c_str(), errno, strerror(errno));
45+
herror("bind to %s failed error %d %s", addr.toString().c_str(), errno, strerror(errno));
4646
}
4747
if (r == 0) {
4848
r = ::connect(fd, (sockaddr *) &addr.getAddr(), sizeof(sockaddr_in));
4949
if (r != 0 && errno != EINPROGRESS) {
50-
error("connect to %s error %d %s", addr.toString().c_str(), errno, strerror(errno));
50+
herror("connect to %s error %d %s", addr.toString().c_str(), errno, strerror(errno));
5151
}
5252
}
5353

@@ -56,7 +56,7 @@ void TcpConn::connect(EventBase *base, const string &host, unsigned short port,
5656
if (r == 0) {
5757
r = getsockname(fd, (sockaddr *) &local, &alen);
5858
if (r < 0) {
59-
error("getsockname failed %d %s", errno, strerror(errno));
59+
herror("getsockname failed %d %s", errno, strerror(errno));
6060
}
6161
}
6262
state_ = State::Handshaking;
@@ -90,7 +90,7 @@ void TcpConn::cleanup(const TcpConnPtr &con) {
9090
} else {
9191
state_ = State::Closed;
9292
}
93-
trace("tcp closing %s - %s fd %d %d", local_.toString().c_str(), peer_.toString().c_str(), channel_ ? channel_->fd() : -1, errno);
93+
htrace("tcp closing %s - %s fd %d %d", local_.toString().c_str(), peer_.toString().c_str(), channel_ ? channel_->fd() : -1, errno);
9494
getBase()->cancel(timeoutId_);
9595
if (statecb_) {
9696
statecb_(con);
@@ -118,7 +118,7 @@ void TcpConn::handleRead(const TcpConnPtr &con) {
118118
int rd = 0;
119119
if (channel_->fd() >= 0) {
120120
rd = readImp(channel_->fd(), input_.end(), input_.space());
121-
trace("channel %lld fd %d readed %d bytes", (long long) channel_->id(), channel_->fd(), rd);
121+
htrace("channel %lld fd %d readed %d bytes", (long long) channel_->id(), channel_->fd(), rd);
122122
}
123123
if (rd == -1 && errno == EINTR) {
124124
continue;
@@ -140,7 +140,7 @@ void TcpConn::handleRead(const TcpConnPtr &con) {
140140
}
141141

142142
int TcpConn::handleHandshake(const TcpConnPtr &con) {
143-
fatalif(state_ != Handshaking, "handleHandshaking called when state_=%d", state_);
143+
hfatalif(state_ != Handshaking, "handleHandshaking called when state_=%d", state_);
144144
struct pollfd pfd;
145145
pfd.fd = channel_->fd();
146146
pfd.events = POLLOUT | POLLERR;
@@ -150,13 +150,13 @@ int TcpConn::handleHandshake(const TcpConnPtr &con) {
150150
state_ = State::Connected;
151151
if (state_ == State::Connected) {
152152
connectedTime_ = util::timeMilli();
153-
trace("tcp connected %s - %s fd %d", local_.toString().c_str(), peer_.toString().c_str(), channel_->fd());
153+
htrace("tcp connected %s - %s fd %d", local_.toString().c_str(), peer_.toString().c_str(), channel_->fd());
154154
if (statecb_) {
155155
statecb_(con);
156156
}
157157
}
158158
} else {
159-
trace("poll fd %d return %d revents %d", channel_->fd(), r, pfd.revents);
159+
htrace("poll fd %d return %d revents %d", channel_->fd(), r, pfd.revents);
160160
cleanup(con);
161161
return -1;
162162
}
@@ -176,15 +176,15 @@ void TcpConn::handleWrite(const TcpConnPtr &con) {
176176
channel_->enableWrite(false);
177177
}
178178
} else {
179-
error("handle write unexpected");
179+
herror("handle write unexpected");
180180
}
181181
}
182182

183183
ssize_t TcpConn::isend(const char *buf, size_t len) {
184184
size_t sended = 0;
185185
while (len > sended) {
186186
ssize_t wd = writeImp(channel_->fd(), buf + sended, len - sended);
187-
trace("channel %lld fd %d write %ld bytes", (long long) channel_->id(), channel_->fd(), wd);
187+
htrace("channel %lld fd %d write %ld bytes", (long long) channel_->id(), channel_->fd(), wd);
188188
if (wd > 0) {
189189
sended += wd;
190190
continue;
@@ -196,7 +196,7 @@ ssize_t TcpConn::isend(const char *buf, size_t len) {
196196
}
197197
break;
198198
} else {
199-
error("write error: channel %lld fd %d wd %ld %d %s", (long long) channel_->id(), channel_->fd(), wd, errno, strerror(errno));
199+
herror("write error: channel %lld fd %d wd %ld %d %s", (long long) channel_->id(), channel_->fd(), wd, errno, strerror(errno));
200200
break;
201201
}
202202
}
@@ -219,7 +219,7 @@ void TcpConn::send(Buffer &buf) {
219219
}
220220
}
221221
} else {
222-
warn("connection %s - %s closed, but still writing %lu bytes", local_.toString().c_str(), peer_.toString().c_str(), buf.size());
222+
hwarn("connection %s - %s closed, but still writing %lu bytes", local_.toString().c_str(), peer_.toString().c_str(), buf.size());
223223
}
224224
}
225225

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

@@ -250,7 +250,7 @@ void TcpConn::onMsg(CodecBase *codec, const MsgCallBack &cb) {
250250
con->channel_->close();
251251
break;
252252
} else if (r > 0) {
253-
trace("a msg decoded. origin len %d msg len %ld", r, msg.size());
253+
htrace("a msg decoded. origin len %d msg len %ld", r, msg.size());
254254
cb(con, msg);
255255
con->getInput().consume(r);
256256
}
@@ -269,20 +269,20 @@ int TcpServer::bind(const std::string &host, unsigned short port, bool reusePort
269269
addr_ = Ip4Addr(host, port);
270270
int fd = socket(AF_INET, SOCK_STREAM, 0);
271271
int r = net::setReuseAddr(fd);
272-
fatalif(r, "set socket reuse option failed");
272+
hfatalif(r, "set socket reuse option failed");
273273
r = net::setReusePort(fd, reusePort);
274-
fatalif(r, "set socket reuse port option failed");
274+
hfatalif(r, "set socket reuse port option failed");
275275
r = util::addFdFlag(fd, FD_CLOEXEC);
276-
fatalif(r, "addFdFlag FD_CLOEXEC failed");
276+
hfatalif(r, "addFdFlag FD_CLOEXEC failed");
277277
r = ::bind(fd, (struct sockaddr *) &addr_.getAddr(), sizeof(struct sockaddr));
278278
if (r) {
279279
close(fd);
280-
error("bind to %s failed %d %s", addr_.toString().c_str(), errno, strerror(errno));
280+
herror("bind to %s failed %d %s", addr_.toString().c_str(), errno, strerror(errno));
281281
return errno;
282282
}
283283
r = listen(fd, 20);
284-
fatalif(r, "listen failed %d %s", errno, strerror(errno));
285-
info("fd %d listening at %s", fd, addr_.toString().c_str());
284+
hfatalif(r, "listen failed %d %s", errno, strerror(errno));
285+
hinfo("fd %d listening at %s", fd, addr_.toString().c_str());
286286
listen_channel_ = new Channel(base_, fd, kReadEvent);
287287
listen_channel_->onRead([this] { handleAccept(); });
288288
return 0;
@@ -292,7 +292,7 @@ TcpServerPtr TcpServer::startServer(EventBases *bases, const std::string &host,
292292
TcpServerPtr p(new TcpServer(bases));
293293
int r = p->bind(host, port, reusePort);
294294
if (r) {
295-
error("bind to %s:%d failed %d %s", host.c_str(), port, errno, strerror(errno));
295+
herror("bind to %s:%d failed %d %s", host.c_str(), port, errno, strerror(errno));
296296
}
297297
return r == 0 ? p : NULL;
298298
}
@@ -307,16 +307,16 @@ void TcpServer::handleAccept() {
307307
socklen_t alen = sizeof(peer);
308308
int r = getpeername(cfd, (sockaddr *) &peer, &alen);
309309
if (r < 0) {
310-
error("get peer name failed %d %s", errno, strerror(errno));
310+
herror("get peer name failed %d %s", errno, strerror(errno));
311311
continue;
312312
}
313313
r = getsockname(cfd, (sockaddr *) &local, &alen);
314314
if (r < 0) {
315-
error("getsockname failed %d %s", errno, strerror(errno));
315+
herror("getsockname failed %d %s", errno, strerror(errno));
316316
continue;
317317
}
318318
r = util::addFdFlag(cfd, FD_CLOEXEC);
319-
fatalif(r, "addFdFlag FD_CLOEXEC failed");
319+
hfatalif(r, "addFdFlag FD_CLOEXEC failed");
320320
EventBase *b = bases_->allocBase();
321321
auto addcon = [=] {
322322
TcpConnPtr con = createcb_();
@@ -338,7 +338,7 @@ void TcpServer::handleAccept() {
338338
}
339339
}
340340
if (lfd >= 0 && errno != EAGAIN && errno != EINTR) {
341-
warn("accept return %d %d %s", cfd, errno, strerror(errno));
341+
hwarn("accept return %d %d %s", cfd, errno, strerror(errno));
342342
}
343343
}
344344

handy/event_base.cc

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ struct EventsImp {
8080
}
8181
void wakeup() {
8282
int r = write(wakeupFds_[1], "", 1);
83-
fatalif(r <= 0, "write error wd %d %d %s", r, errno, strerror(errno));
83+
hfatalif(r <= 0, "write error wd %d %d %s", r, errno, strerror(errno));
8484
}
8585

8686
bool cancel(TimerId timerid);
@@ -143,12 +143,12 @@ void EventsImp::loop() {
143143

144144
void EventsImp::init() {
145145
int r = pipe(wakeupFds_);
146-
fatalif(r, "pipe failed %d %s", errno, strerror(errno));
146+
hfatalif(r, "pipe failed %d %s", errno, strerror(errno));
147147
r = util::addFdFlag(wakeupFds_[0], FD_CLOEXEC);
148-
fatalif(r, "addFdFlag failed %d %s", errno, strerror(errno));
148+
hfatalif(r, "addFdFlag failed %d %s", errno, strerror(errno));
149149
r = util::addFdFlag(wakeupFds_[1], FD_CLOEXEC);
150-
fatalif(r, "addFdFlag failed %d %s", errno, strerror(errno));
151-
trace("wakeup pipe created %d %d", wakeupFds_[0], wakeupFds_[1]);
150+
hfatalif(r, "addFdFlag failed %d %s", errno, strerror(errno));
151+
htrace("wakeup pipe created %d %d", wakeupFds_[0], wakeupFds_[1]);
152152
Channel *ch = new Channel(base_, wakeupFds_[0], kReadEvent);
153153
ch->onRead([=] {
154154
char buf[1024];
@@ -162,7 +162,7 @@ void EventsImp::init() {
162162
delete ch;
163163
} else if (errno == EINTR) {
164164
} else {
165-
fatal("wakeup channel read error %d %d %s", r, errno, strerror(errno));
165+
hfatal("wakeup channel read error %d %d %s", r, errno, strerror(errno));
166166
}
167167
});
168168
}
@@ -207,17 +207,17 @@ IdleId EventsImp::registerIdle(int idle, const TcpConnPtr &con, const TcpCallBac
207207
}
208208
auto &lst = idleConns_[idle];
209209
lst.push_back(IdleNode{con, util::timeMilli() / 1000, move(cb)});
210-
trace("register idle");
210+
htrace("register idle");
211211
return IdleId(new IdleIdImp(&lst, --lst.end()));
212212
}
213213

214214
void EventsImp::unregisterIdle(const IdleId &id) {
215-
trace("unregister idle");
215+
htrace("unregister idle");
216216
id->lst_->erase(id->iter_);
217217
}
218218

219219
void EventsImp::updateIdle(const IdleId &id) {
220-
trace("update idle");
220+
htrace("update idle");
221221
id->iter_->updated_ = util::timeMilli() / 1000;
222222
id->lst_->splice(id->lst_->end(), *id->lst_, id->iter_);
223223
}
@@ -293,7 +293,7 @@ void MultiBase::loop() {
293293
}
294294

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

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

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

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

392-
} // namespace handy
392+
} // namespace handy

handy/http.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ HttpMsg::Result HttpMsg::tryDecode_(Slice buf, bool copyBody, Slice *line1) {
5555
} else if (k.empty() && ln.empty() && req.empty()) {
5656
break;
5757
} else {
58-
error("bad http line: %.*s %.*s", (int) k.size(), k.data(), (int) ln.size(), ln.data());
58+
herror("bad http line: %.*s %.*s", (int) k.size(), k.data(), (int) ln.size(), ln.data());
5959
return Error;
6060
}
6161
}
@@ -100,7 +100,7 @@ HttpMsg::Result HttpRequest::tryDecode(Slice buf, bool copyBody) {
100100
query_uri = ln1.eatWord();
101101
version = ln1.eatWord();
102102
if (query_uri.size() == 0 || query_uri[0] != '/') {
103-
error("query uri '%.*s' should begin with /", (int) query_uri.size(), query_uri.data());
103+
herror("query uri '%.*s' should begin with /", (int) query_uri.size(), query_uri.data());
104104
return Error;
105105
}
106106
for (size_t i = 0; i < query_uri.size(); i++) {
@@ -194,8 +194,8 @@ void HttpConnPtr::handleRead(const HttpCallBack &cb) const {
194194
if (r == HttpMsg::Continue100) {
195195
tcp->send("HTTP/1.1 100 Continue\n\r\n");
196196
} else if (r == HttpMsg::Complete) {
197-
info("http request: %s %s %s", req.method.c_str(), req.query_uri.c_str(), req.version.c_str());
198-
trace("http request:\n%.*s", (int) tcp->input_.size(), tcp->input_.data());
197+
hinfo("http request: %s %s %s", req.method.c_str(), req.query_uri.c_str(), req.version.c_str());
198+
htrace("http request:\n%.*s", (int) tcp->input_.size(), tcp->input_.data());
199199
cb(*this);
200200
}
201201
} else {
@@ -206,8 +206,8 @@ void HttpConnPtr::handleRead(const HttpCallBack &cb) const {
206206
return;
207207
}
208208
if (r == HttpMsg::Complete) {
209-
info("http response: %d %s", resp.status, resp.statusWord.c_str());
210-
trace("http response:\n%.*s", (int) tcp->input_.size(), tcp->input_.data());
209+
hinfo("http response: %d %s", resp.status, resp.statusWord.c_str());
210+
htrace("http response:\n%.*s", (int) tcp->input_.size(), tcp->input_.data());
211211
cb(tcp);
212212
}
213213
}
@@ -225,7 +225,7 @@ void HttpConnPtr::clearData() const {
225225

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

231231
HttpServer::HttpServer(EventBases *bases) : TcpServer(bases) {

handy/logging.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void Logger::setFileName(const string &filename) {
5959
fd_ = fd;
6060
} else {
6161
int r = dup2(fd, fd_);
62-
fatalif(r < 0, "dup2 failed");
62+
hfatalif(r < 0, "dup2 failed");
6363
close(fd);
6464
}
6565
}
@@ -142,4 +142,4 @@ void Logger::logv(int level, const char *file, int line, const char *func, const
142142
}
143143
}
144144

145-
} // namespace handy
145+
} // namespace handy

0 commit comments

Comments
 (0)