Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b52dabe

Browse files
committed
日志模块的日志记录可以了,但是dump还是不行,我先放着
1 parent eba95a6 commit b52dabe

File tree

12 files changed

+172
-110
lines changed

12 files changed

+172
-110
lines changed

PlayServer/Epoll.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class EpollData
1818
explicit EpollData(uint32_t u32) { m_data.u32 = u32; }
1919
EpollData(const EpollData& epoll)
2020
{
21-
m_data.u64 = epoll.m_data.u64;//联合体的赋值,直接给最大的赋值
21+
m_data.u64 = epoll.m_data.u64;//联合体的赋值,直接给最大的赋值
2222
}
2323
~EpollData(){}
2424

@@ -51,7 +51,7 @@ class EpollData
5151
m_data.u64 = data;
5252
return *this;
5353
}
54-
operator epoll_data_t() const { return m_data; }//一个函数体被定义为const后,一个const的对象才能调用这个函数
54+
operator epoll_data_t() const { return m_data; }//一个函数体被定义为const后,一个const的对象才能调用这个函数
5555
operator epoll_data_t() { return m_data; }
5656
operator epoll_data_t*() { return &m_data; }
5757
operator const epoll_data_t*() const { return &m_data; }
@@ -77,7 +77,7 @@ class CEpoll
7777
public:
7878
operator int() const { return m_epoll; }
7979
public:
80-
//创建epoll
80+
//创建epoll
8181
int Create(unsigned count)
8282
{
8383
if (m_epoll < 0)
@@ -92,7 +92,7 @@ class CEpoll
9292
return SUCCESS;
9393
}
9494

95-
//设置epoll等待
95+
//设置epoll等待
9696
ssize_t WaitEvents(EPEvents& events, int timeout = 0)
9797
{
9898
if (m_epoll < 0) return m_epoll;
@@ -116,7 +116,7 @@ class CEpoll
116116

117117
}
118118

119-
//添加eopoll事件
119+
//添加eopoll事件
120120
int Add(int fd, const EpollData& data = EpollData((void*)0), uint32_t events = EPOLLIN)
121121
{
122122
if (m_epoll < 0) return m_epoll;

PlayServer/Function.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ class CFunctionBase
1313
};
1414
/**
1515
16-
std::_Bindres_helper<int, _FUNCTION, _ARGS...>::type 是 C++ 标准库中的一个模板类型,通常用于实现绑定(bind)操作。这个类型的目的是在绑定操作中保存绑定的参数和函数,以便在稍后调用。
16+
std::_Bindres_helper<int, _FUNCTION, _ARGS...>::type 是 C++ 标准库中的一个模板类型,通常用于实现绑定(bind)操作。这个类型的目的是在绑定操作中保存绑定的参数和函数,以便在稍后调用。
1717
18-
让我们拆解这个类型的各个部分:
18+
让我们拆解这个类型的各个部分:
1919
20-
int: 这表示绑定操作的返回类型。在这里,它是一个整数类型,但实际上,这可以是任何你期望的返回类型,具体取决于你绑定的函数。
20+
int: 这表示绑定操作的返回类型。在这里,它是一个整数类型,但实际上,这可以是任何你期望的返回类型,具体取决于你绑定的函数。
2121
22-
_FUNCTION: 这是绑定的函数或可调用对象。可以是一个函数指针、函数对象或者是一个lambda表达式。
22+
_FUNCTION: 这是绑定的函数或可调用对象。可以是一个函数指针、函数对象或者是一个lambda表达式。
2323
24-
_ARGS...: 这是绑定操作的参数。它表示可以有零个或多个参数,具体取决于你绑定的函数需要的参数数量。
24+
_ARGS...: 这是绑定操作的参数。它表示可以有零个或多个参数,具体取决于你绑定的函数需要的参数数量。
2525
26-
std::_Bindres_helper<...>::type: 这是一个辅助类型,用于保存和管理绑定操作的信息,包括返回类型、函数或可调用对象以及参数。
26+
std::_Bindres_helper<...>::type: 这是一个辅助类型,用于保存和管理绑定操作的信息,包括返回类型、函数或可调用对象以及参数。
2727
28-
综合起来,std::_Bindres_helper<int, _FUNCTION, _ARGS...>::type 表示一个绑定操作的类型,其中包含了返回类型、绑定的函数或可调用对象以及相关的参数信息。这通常是在使用std::bind函数进行函数绑定时,用于表示绑定的结果类型。
28+
综合起来,std::_Bindres_helper<int, _FUNCTION, _ARGS...>::type 表示一个绑定操作的类型,其中包含了返回类型、绑定的函数或可调用对象以及相关的参数信息。这通常是在使用std::bind函数进行函数绑定时,用于表示绑定的结果类型。
2929
*/
3030

3131
template<typename _FUNCTION, typename... _ARGS>

PlayServer/Loggere.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ LogInfo::LogInfo(const char* file, int line, const char* func, pid_t pid, pthrea
3232
LogInfo::LogInfo(const char* file, int line, const char* func, pid_t pid, pthread_t tid, int level)
3333
{
3434
bAuto = true;
35-
//自己主动记录日志
35+
//自己主动记录日志
3636
const char sLevel[][8] = { "INFO","DEBUG","WARNING","ERROR","FATAL" };
3737
char* buf = NULL;
3838
bAuto = false;
@@ -73,13 +73,21 @@ LogInfo::LogInfo(const char* file, int line, const char* func, pid_t pid, pthrea
7373
for (; i < nSize; i++)
7474
{
7575
char buf[16] = "";
76-
snprintf(buf, sizeof(buf), "02X ", Data[i] & 0xFF);
76+
snprintf(buf, sizeof(buf), "%02X ", Data[i] & 0xFF);
7777
m_buf += buf;
7878
if (0 == ((i + 1) % 16))
7979
{
8080
m_buf = "\t;";
81-
for (size_t j = i - 15; j <= i; j++)
81+
char buf[17] = "";
82+
memcpy(buf, Data + i - 15, 16);
83+
for (int j = 0; j < 16; j++)
8284
{
85+
if ((unsigned)buf[j] < 32 && buf[j] >= 0) buf[j] = '.';
86+
}
87+
m_buf += buf;
88+
/*for (size_t j = i - 15; j <= i; j++)
89+
{
90+
8391
if ((Data[j] & 0xFF) > 31 && (Data[j] & 0xFF) < 0x7F)
8492
{
8593
m_buf += Data[i];
@@ -88,12 +96,12 @@ LogInfo::LogInfo(const char* file, int line, const char* func, pid_t pid, pthrea
8896
{
8997
m_buf == ".";
9098
}
91-
}
99+
}*/
92100
m_buf == "\n";
93101
}
94102
}
95103

96-
//处理数据尾部
104+
//处理数据尾部
97105
size_t k = i % 16;
98106
if (k != 0)
99107
{
@@ -107,7 +115,7 @@ LogInfo::LogInfo(const char* file, int line, const char* func, pid_t pid, pthrea
107115
}
108116
else
109117
{
110-
m_buf == ".";
118+
m_buf += ".";
111119
}
112120
}
113121
m_buf == "\n";

PlayServer/Loggere.h

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,16 @@ class LogInfo
3535
}
3636

3737
template<typename T>
38-
LogInfo& operator<< (const T& data);
38+
LogInfo& operator<< (const T& data)
39+
{
40+
std::stringstream stream;
41+
stream << data;
42+
43+
m_buf += stream.str().c_str();
44+
printf(m_buf);
45+
return *this;
46+
}
47+
3948

4049
private:
4150
bool bAuto = false;
@@ -115,7 +124,7 @@ class CLoggerServer
115124
return 0;
116125
}
117126

118-
//给其他非日志进程和线程使用的
127+
//给其他非日志进程和线程使用的
119128
static void Trace(const LogInfo& info)
120129
{
121130
static thread_local CLocalSocket client;
@@ -138,7 +147,7 @@ class CLoggerServer
138147
}
139148
}
140149
ret = client.Send(info);
141-
printf("%s(%d):<%s> pid = %d errno = %d msg:%s ret = %d\n", __FILE__, __LINE__, __FUNCTION__, getpid(), errno, strerror(errno),ret);
150+
printf("%s(%d):<%s> pid = %d errno = %d msg:%s ret = %d\n", __FILE__, __LINE__, __FUNCTION__, getpid(), errno, strerror(errno),ret);//这里ret零
142151
}
143152
static Buffer GetTimeStr()
144153
{
@@ -176,6 +185,7 @@ class CLoggerServer
176185
}
177186
if (ret > 0)
178187
{
188+
//我在给你跑一边,我把连接和发送设置一个延时你看清楚一些
179189
printf("%s(%d):<%s> pid = %d errno = %d msg:%s \n", __FILE__, __LINE__, __FUNCTION__, getpid(), errno, strerror(errno));
180190
size_t i = 0;
181191
for (; i < ret; i++)
@@ -187,9 +197,11 @@ class CLoggerServer
187197
}
188198
if (events[i].events & EPOLLIN)
189199
{
190-
printf("%s(%d):<%s> pid = %d errno = %d msg:%s epollin =%s\n", __FILE__, __LINE__, __FUNCTION__, getpid(), errno, strerror(errno), "============检测到epollin事件");
200+
char buff[] = "============检测到epollin事件=============================\n";
201+
printf("%s", buff);
191202
if (events[i].data.ptr == m_pServer)
192203
{
204+
//创建的新的接收数据的服务端
193205
CSocketBase* pClient = NULL;
194206
int r = m_pServer->Link(&pClient);
195207
printf("%s(%d):<%s> pid = %d errno = %d msg:%s ret = %d\n", __FILE__, __LINE__, __FUNCTION__, getpid(), errno, strerror(errno), r);
@@ -203,23 +215,32 @@ class CLoggerServer
203215
delete pClient;
204216
continue;
205217
}
218+
printf("%s(%d):<%s> pid = %d errno = %d msg:%s \n", __FILE__, __LINE__, __FUNCTION__, getpid(), errno, strerror(errno));
206219
auto it = mapClients.find(*pClient);
207-
if (it->second != NULL)
220+
printf("%s(%d):<%s> pid = %d errno = %d msg:%s \n", __FILE__, __LINE__, __FUNCTION__, getpid(), errno, strerror(errno));
221+
if (it->second != NULL && it != mapClients.end())
208222
{
223+
printf("%s(%d):<%s> pid = %d errno = %d msg:%s \n", __FILE__, __LINE__, __FUNCTION__, getpid(), errno, strerror(errno));
209224
delete it->second;
225+
210226
}
211227
mapClients[*pClient] = pClient;
212-
228+
printf("%s(%d):<%s> pid = %d errno = %d msg:%s \n", __FILE__, __LINE__, __FUNCTION__, getpid(), errno, strerror(errno));
229+
//我看一下走没走玩这个if语句
230+
//我好像找到问题了
231+
//这个if好像卡在那里了,都没有走到这里是的,
232+
//我加个日志看看哪里死了还是卡住了
233+
printf("%s(%d):<%s> pid = %d errno = %d msg:%s \n", __FILE__, __LINE__, __FUNCTION__, getpid(), errno, strerror(errno));
213234
}
214235
else
215236
{
237+
//没有进入到else中 也就是没有收到send,send的位置在哪
216238
printf("%s(%d):<%s> pid = %d errno = %d msg:%s ret = %d\n", __FILE__, __LINE__, __FUNCTION__, getpid(), errno, strerror(errno), ret);
217239
CSocketBase* pClient = (CSocketBase*)events[i].data.ptr;
218240
if (pClient != NULL)
219241
{
220242
Buffer data(1024 * 1024);
221-
int r = pClient->Recv(data);
222-
printf("%s(%d):<%s> pid = %d errno = %d msg:%s \n", __FILE__, __LINE__, __FUNCTION__, getpid(), errno, strerror(errno));
243+
int r = pClient->Recv(data);
223244
if (r <= 0)
224245
{
225246
printf("%s(%d):<%s> pid = %d errno = %d msg:%s ret = %d\n", __FILE__, __LINE__, __FUNCTION__, getpid(), errno, strerror(errno), ret);
@@ -262,9 +283,13 @@ class CLoggerServer
262283
return;
263284
}
264285
FILE* pfile = m_file;
265-
fwrite((char*)data, 1, data.size(), pfile);
286+
std::string str;
287+
str.resize(data.size());
288+
memcpy((char*)str.c_str(), data.c_str(), data.size());
289+
printf("我要开始写入日志了 ==================================================== msg:%s\n", str.c_str());
290+
fwrite((char*)str.c_str(), 1, str.size(), pfile);
266291
fflush(pfile);
267-
printf("%s", (char*)data);
292+
printf("%s", data);
268293
}
269294

270295

@@ -293,18 +318,9 @@ class CLoggerServer
293318
#define LOGF LogInfo(__FILE__, __LINE__,__FUNCTION__, getpid(), pthread_self(),LOG_FATAL)
294319

295320
//01 02 03 A1 ... ...
296-
#define DUMPI(data, size) LogInfo(__FILE__,__LINE__, __FUNCTION__, getpid(),pthread_self(), LOG_INFO, data, size)
297-
#define DUMPD(data, size) LogInfo(__FILE__,__LINE__, __FUNCTION__, getpid(),pthread_self(), LOG_DEBUG, data, size)
298-
#define DUMPW(data, size) LogInfo(__FILE__,__LINE__, __FUNCTION__, getpid(),pthread_self(), LOG_WARNING, data, size)
299-
#define DUMPE(data, size) LogInfo(__FILE__,__LINE__, __FUNCTION__, getpid(),pthread_self(), LOG_ERROR, data, size)
300-
#define DUMPF(data, size) LogInfo(__FILE__,__LINE__, __FUNCTION__, getpid(),pthread_self(), LOG_FATAL, data, size)
321+
#define DUMPI(data, size) CLoggerServer::Trace(LogInfo(__FILE__,__LINE__, __FUNCTION__, getpid(),pthread_self(), LOG_INFO, data, size))
322+
#define DUMPD(data, size) CLoggerServer::Trace(LogInfo(__FILE__,__LINE__, __FUNCTION__, getpid(),pthread_self(), LOG_DEBUG, data, size))
323+
#define DUMPW(data, size) CLoggerServer::Trace(LogInfo(__FILE__,__LINE__, __FUNCTION__, getpid(),pthread_self(), LOG_WARNING, data, size))
324+
#define DUMPE(data, size) CLoggerServer::Trace(LogInfo(__FILE__,__LINE__, __FUNCTION__, getpid(),pthread_self(), LOG_ERROR, data, size))
325+
#define DUMPF(data, size) CLoggerServer::Trace(LogInfo(__FILE__,__LINE__, __FUNCTION__, getpid(),pthread_self(), LOG_FATAL, data, size))
301326
#endif
302-
303-
template<typename T>
304-
inline LogInfo& LogInfo::operator<<(const T& data)
305-
{
306-
std::stringstream stream;
307-
stream << data;
308-
m_buf += stream.str();
309-
return *this;
310-
}

PlayServer/PlayServer.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<ClCompile Include="Loggere.cpp" />
7979
<ClCompile Include="main.cpp" />
8080
<ClCompile Include="Thread.cpp" />
81+
<ClCompile Include="ThreadPool.cpp" />
8182
</ItemGroup>
8283
<ItemGroup>
8384
<ClInclude Include="Loggere.h" />
@@ -86,6 +87,7 @@
8687
<ClInclude Include="Function.h" />
8788
<ClInclude Include="Process.h" />
8889
<ClInclude Include="Thread.h" />
90+
<ClInclude Include="ThreadPool.h" />
8991
</ItemGroup>
9092
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
9193
<Link>

PlayServer/Process.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ class CProcess
3333
if (pid == -1) return -3;
3434
if (pid == 0)
3535
{
36-
//子进程
37-
close(pipes[1]);//关闭写管道
36+
//子进程
37+
close(pipes[1]);//关闭写管道
3838
pipes[1] = 0;
3939
ret = (*m_func)();
4040
exit(0);
4141
}
4242
else
4343
{
44-
//主进程
45-
close(pipes[0]);//关闭读管道
44+
//主进程
45+
close(pipes[0]);//关闭读管道
4646
pipes[0] = 0;
4747
m_pid = pid;
4848
return 0;
@@ -51,7 +51,7 @@ class CProcess
5151

5252
int SendFD(int fd)
5353
{
54-
//在主进程完成
54+
//在主进程完成
5555
msghdr msg;
5656
iovec iov[2];
5757
char buf[2][20] = { "send hello","send other" };
@@ -114,21 +114,21 @@ class CProcess
114114
}
115115
if (ret > 0)
116116
{
117-
exit(0);//主进程直接退出
117+
exit(0);//主进程直接退出
118118
}
119-
//子进程的内容
119+
//子进程的内容
120120
ret = setsid();
121121
if (ret == -1) return -2;
122122
ret = fork();
123123
if (ret == -1) return-3;
124-
if (ret > 0) exit(0);//子进程的使命也完成了,退出
125-
//现在只剩下了孙进城了,就进入了守护状态了
124+
if (ret > 0) exit(0);//子进程的使命也完成了,退出
125+
//现在只剩下了孙进城了,就进入了守护状态了//找下epoll的位置
126126
for (size_t i = 0; i < 3; i++)
127127
{
128128
//close(i);
129129
}
130130
umask(0);
131-
signal(SIGCHLD, SIG_IGN); // 防止子进程变成僵尸进程
131+
signal(SIGCHLD, SIG_IGN); // 防止子进程变成僵尸进程
132132
printf("%s(%d):<%s> pid = %d\n", __FILE__, __LINE__, __FUNCTION__, getpid());
133133
return 0;
134134
}

0 commit comments

Comments
 (0)