Skip to content

Commit

Permalink
log修改
Browse files Browse the repository at this point in the history
  • Loading branch information
wlgq2 committed Sep 30, 2019
1 parent 9b830ec commit 3e7f53d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 35 deletions.
4 changes: 2 additions & 2 deletions examples/all_example/Clinet.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ class Client : public uv::TcpClient
}
}

void newMessage(const char* buf,unsigned int size)
void newMessage(const char* buf,ssize_t size)
{
write(buf,size,nullptr);
write(buf, (unsigned)size, nullptr);
}

private:
Expand Down
2 changes: 1 addition & 1 deletion examples/all_example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ int main(int argc, char** args)
//定时器测试
#if TEST_TIMER
Timer timer(loop, 1000, 1000,
[&client](Timer*)
[](Timer*)
{
std::cout << "timer callback test..." << std::endl;
});
Expand Down
6 changes: 3 additions & 3 deletions examples/pingpang/Clinet.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ class Client : public uv::TcpClient
}
}

void newMessage(const char* buf,unsigned int size)
void newMessage(const char* buf,ssize_t size)
{
#if USED_NO_PACKET
write(data,1024);
write(data, (unsigned int)size);
#else
uv::Packet packet;
appendToBuffer(buf, size);
appendToBuffer(buf, (int)size);
while (0 == readFromBuffer(packet))
{
write(packet.Buffer(), packet.BufferSize(), nullptr);
Expand Down
35 changes: 32 additions & 3 deletions uv/LogWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Description: https://github.com/wlgq2/uv-cpp

#include "LogWriter.h"

#include <iostream>


using namespace uv;

LogWriter* LogWriter::Instance()
Expand All @@ -29,15 +32,24 @@ void uv::LogWriter::ToHex(std::string& message, const char* data, unsigned int s
{
char buf[8];
std::sprintf(buf, " 0x%x ", (unsigned char)data[i]);
message += buf;
message.append(buf);
}
}

void uv::LogWriter::write(Level level, const std::string& data)
{
if ((callback_) && (level <= Error) && (level >= level_))
if ((level <= Error) && (level >= level_) && (level >= Debug))
{
callback_(level, data);
if (callback_)
{
callback_(level, data);
}
else
{
#if USED_STD_OUT
std::cout << getLevelName(level) << " :" << data << std::endl;
#endif
}
}
}

Expand Down Expand Up @@ -106,8 +118,25 @@ int uv::LogWriter::getLevel()
return level_;
}

const std::string& uv::LogWriter::getLevelName(int level)
{
if (level >= 0 && level <= levelStr_.size())
{
return levelStr_[level];
}
return nullLevel_;
}

LogWriter::LogWriter()
:callback_(nullptr),
level_(0)
{
levelStr_.resize(Level::LevelSize);
levelStr_[Level::Debug] = "Debug";
levelStr_[Level::Info] = "Info";
levelStr_[Level::Warn] = "Warn";
levelStr_[Level::Error] = "Error";
levelStr_[Level::Fatal] = "Fatal";

nullLevel_ = "NullLevel";
}
13 changes: 10 additions & 3 deletions uv/LogWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ Description: https://github.com/wlgq2/uv-cpp
#ifndef UV_LOG_INTERFACE_H
#define UV_LOG_INTERFACE_H

#include <string>
#include <functional>
#include <string>
#include <functional>
#include <vector>

#define USED_STD_OUT 1

namespace uv
{
Expand All @@ -27,7 +30,8 @@ class LogWriter
Info,
Warn,
Error,
Fatal
Fatal,
LevelSize
};
static LogWriter* Instance();
static void ToHex(std::string& message, const char* data, unsigned int size);
Expand All @@ -51,12 +55,15 @@ class LogWriter

void setLevel(int level);
int getLevel();
const std::string& getLevelName(int level);

private:
LogWriter();

WriteLogCallback callback_;
int level_;
std::vector<std::string> levelStr_;
std::string nullLevel_;
};
}
#endif // ! UV_LOG_INTERFACE_H
Expand Down
29 changes: 6 additions & 23 deletions uv/TcpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ TcpServer::TcpServer(EventLoop* loop, SocketAddr& addr, bool tcpNoDealy)
accetper_->setNewConnectinonCallback( [this] (EventLoop* loop,uv_tcp_t* client)
{
string key;
SocketAddr::AddrToStr(client,key, ipv_);
SocketAddr::AddrToStr(client, key, ipv_);

uv::LogWriter::Instance()->info("new connect "+key);

shared_ptr<TcpConnection> connection(new TcpConnection(loop,key,client));
shared_ptr<TcpConnection> connection(new TcpConnection(loop, key, client));
if(connection)
{
connection->setMessageCallback(std::bind(&TcpServer::onMessage,this,placeholders::_1,placeholders::_2,placeholders::_3));
Expand Down Expand Up @@ -134,7 +134,7 @@ void TcpServer::setMessageCallback(OnMessageCallback callback)

void TcpServer::write(shared_ptr<TcpConnection> connection,const char* buf,unsigned int size, AfterWriteCallback callback)
{
if(connection)
if(nullptr != connection)
{
connection->write(buf,size, callback);
}
Expand All @@ -148,20 +148,12 @@ void TcpServer::write(shared_ptr<TcpConnection> connection,const char* buf,unsig
void TcpServer::write(string& name,const char* buf,unsigned int size,AfterWriteCallback callback)
{
auto connection = getConnnection(name);
if(connection)
{
connection->write(buf,size, callback);
}
else if (callback)
{
WriteInfo info = { WriteInfo::Disconnected,const_cast<char*>(buf),size };
callback(info);
}
write(connection, buf, size, callback);
}

void TcpServer::writeInLoop(shared_ptr<TcpConnection> connection,const char* buf,unsigned int size,AfterWriteCallback callback)
{
if(connection)
if(nullptr != connection)
{
connection->writeInLoop(buf,size,callback);
}
Expand All @@ -176,16 +168,7 @@ void TcpServer::writeInLoop(shared_ptr<TcpConnection> connection,const char* buf
void TcpServer::writeInLoop(string& name,const char* buf,unsigned int size,AfterWriteCallback callback)
{
auto connection = getConnnection(name);
if(connection)
{
connection->writeInLoop(buf,size,callback);
}
else if (callback)
{
uv::LogWriter::Instance()->warn(std::string("try write a disconnect connection.")+name);
WriteInfo info = { WriteInfo::Disconnected,const_cast<char*>(buf),size };
callback(info);
}
writeInLoop(connection, buf, size, callback);
}

void TcpServer::setNewConnectCallback(OnConnectionStatusCallback callback)
Expand Down

0 comments on commit 3e7f53d

Please sign in to comment.