Skip to content

Commit

Permalink
add output macro
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexStocks committed Apr 22, 2023
1 parent 068a065 commit af131bd
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/net/include/net_conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "net/include/net_define.h"
#include "net/include/server_thread.h"
#include "net/src/net_multiplexer.h"
#include "pstd/include/testutil.h"

namespace net {

Expand Down Expand Up @@ -64,7 +65,7 @@ class NetConn : public std::enable_shared_from_this<NetConn> {
void set_name(std::string name) { name_ = std::move(name); }

bool IsClose() { return close_; }
void SetClose(bool close) { close_ = close; }
void SetClose(bool close);

void set_last_interaction(const struct timeval& now) { last_interaction_ = now; }

Expand Down
3 changes: 2 additions & 1 deletion src/net/src/dispatch_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <vector>

#include "pstd/include/testutil.h"
#include "net/src/dispatch_thread.h"

#include "net/src/net_item.h"
Expand Down Expand Up @@ -147,7 +148,7 @@ bool DispatchThread::KillConn(const std::string& ip_port) {
void DispatchThread::KillAllConns() { KillConn(kKillAllConnsTask); }

void DispatchThread::HandleNewConn(const int connfd, const std::string& ip_port) {
printf("accept new conn %d, ip_port: %s\n", connfd, ip_port.c_str());
output("accept new conn %d, ip_port: %s", connfd, ip_port.c_str());
// Slow workers may consume many fds.
// We simply loop to find next legal worker.
NetItem ti(connfd, ip_port);
Expand Down
5 changes: 5 additions & 0 deletions src/net/src/net_conn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ NetConn::~NetConn() {
#endif
}

void NetConn::SetClose(bool close) {
output("close fd %d, addr %s, close %d", this->fd_, this->ip_port().c_str(), close);
close_ = close;
}

bool NetConn::SetNonblock() {
flags_ = Setnonblocking(fd());
if (flags_ == -1) {
Expand Down
4 changes: 3 additions & 1 deletion src/net/src/worker_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <vector>

#include "pstd/include/testutil.h"
#include "net/src/worker_thread.h"

#include "net/include/net_conn.h"
Expand Down Expand Up @@ -171,7 +172,8 @@ void* WorkerThread::ThreadMain() {
in_conn->set_is_reply(false);
if (in_conn->IsClose()) {
should_close = 1;
std::cout << "will close client connection " << in_conn->ip_port() << std::endl;
// std::cout << "will close client connection " << in_conn->ip_port() << std::endl;
output("will close client connection fd %d, addr %s", in_conn->fd(), in_conn->ip_port().c_str());
}
} else if (write_status == kWriteHalf) {
continue;
Expand Down
1 change: 1 addition & 0 deletions src/pika_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2289,6 +2289,7 @@ void QuitCmd::DoInitial() {

void QuitCmd::Do(std::shared_ptr<Partition> partition) {
res_.SetRes(CmdRes::kOk);
output("QutCmd will close %d connection", GetConn()->fd());
GetConn()->SetClose(true);
}

Expand Down
2 changes: 1 addition & 1 deletion src/pstd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ project (pstd)

aux_source_directory(./src DIR_SRCS)

add_library(pstd STATIC ${DIR_SRCS} )
add_library(pstd STATIC ${DIR_SRCS})

target_include_directories(pstd PUBLIC ${PROJECT_SOURCE_DIR}/..)
2 changes: 1 addition & 1 deletion src/pstd/include/pstd_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ namespace pstd {
std::string md5(const std::string& str, bool raw = false);
std::string sha256(const std::string& input, bool raw = false);

} // namespace pstd
}

#endif // __PSTD_HASH_H__
10 changes: 10 additions & 0 deletions src/pstd/include/testutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@
#define __PSTD_INCLUDE_TESTUTIL_H__

#include <string>
#include <iostream>

namespace pstd {

extern std::string RandomString(const int len);
extern int RandomSeed();
extern int GetTestDirectory(std::string* result);

extern char* get_date_time();

void current_time_str(char * str, size_t max_len);
#define output(fmt, args...) do { \
char __time_str__[64];\
pstd::current_time_str(__time_str__, sizeof(__time_str__)); \
printf("[%s] [%s] [%d]" fmt "\n", __time_str__, __FILE__, __LINE__, ##args); \
} while (0)

}; // namespace pstd

#endif // __PSTD_INCLUDE_TESTUTIL_H__
2 changes: 1 addition & 1 deletion src/pstd/include/xdebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
# define clean_errno() (errno == 0 ? "None" : strerror(errno))
# define log_err(M, ...) \
{ \
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__); \
fprintf(stderr, "[ERROR] (%s:%d %s errno: %s) " M "\n", __FILE__, __LINE__, get_date_time().c_str(), clean_errno(), ##__VA_ARGS__); \
exit(-1); \
}
# define log_warn(M, ...) \
Expand Down
19 changes: 19 additions & 0 deletions src/pstd/src/testutil.cc
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
#include "pstd/include/testutil.h"

#include <sys/time.h>
#include <string>

#include "pstd/include/env.h"
#include "pstd/include/random.h"

namespace pstd {

void current_time_str(char * str, size_t max_len)
{
time_t t = time(NULL);
struct timeval tv;
struct tm tmm;

gettimeofday(&tv, 0);

localtime_r(&(tv.tv_sec), &tmm);
snprintf(str, max_len, "%04d%02d%02d-%02d%02d%02d-%06d",
tmm.tm_year + 1900,
tmm.tm_mon+1,
tmm.tm_mday,
tmm.tm_hour,
tmm.tm_min,
tmm.tm_sec,
tv.tv_usec);
}
std::string RandomString(const int len) {
char buf[len];
for (int i = 0; i < len; i++) {
Expand Down

0 comments on commit af131bd

Please sign in to comment.