Skip to content

Commit 659e528

Browse files
committed
add examples/ace/logging
1 parent 9696d34 commit 659e528

3 files changed

Lines changed: 137 additions & 10 deletions

File tree

‎examples/ace/logging/CMakeLists.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ target_link_libraries(ace_logging_proto protobuf pthread)
1212

1313
add_executable(ace_logging_client client.cc)
1414
set_target_properties(ace_logging_client PROPERTIES COMPILE_FLAGS "-Wno-error=shadow")
15-
target_link_libraries(ace_logging_client protobuf_codec ace_logging_proto)
15+
target_link_libraries(ace_logging_client muduo_protobuf_codec ace_logging_proto)
1616

1717
add_executable(ace_logging_server server.cc)
1818
set_target_properties(ace_logging_server PROPERTIES COMPILE_FLAGS "-Wno-error=shadow")
19-
target_link_libraries(ace_logging_server protobuf_codec ace_logging_proto)
19+
target_link_libraries(ace_logging_server muduo_protobuf_codec ace_logging_proto)

‎examples/ace/logging/logrecord.proto‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package logging;
22

33
message LogRecord {
44
// must present in first message
5-
optional string hostname = 1;
6-
optional string process_name = 2;
7-
optional int32 process_id = 3;
8-
optional int64 process_start_time = 4; // microseconds sinch epoch
5+
message Heartbeat {
6+
required string hostname = 1;
7+
required string process_name = 2;
8+
required int32 process_id = 3;
9+
required int64 process_start_time = 4; // microseconds sinch epoch
10+
}
911

12+
optional Heartbeat heartbeat = 1;
1013
// muduo/base/Logging.h
1114
// enum LogLevel
1215
// {
@@ -17,9 +20,9 @@ message LogRecord {
1720
// ERROR, // 4
1821
// FATAL, // 5
1922
// };
20-
required int32 level = 10;
21-
required int32 thread_id = 11;
22-
required int64 timestamp = 12; // microseconds sinch epoch
23-
required string message = 13;
23+
required int32 level = 2;
24+
required int32 thread_id = 3;
25+
required int64 timestamp = 4; // microseconds sinch epoch
26+
required string message = 5;
2427
// optional: source file, source line, function name
2528
}

‎examples/ace/logging/server.cc‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,129 @@
11
#include <examples/ace/logging/logrecord.pb.h>
22

3+
#include <muduo/base/Atomic.h>
4+
#include <muduo/base/FileUtil.h>
5+
#include <muduo/base/Logging.h>
6+
#include <muduo/net/EventLoop.h>
7+
#include <muduo/net/TcpServer.h>
8+
#include <muduo/net/protobuf/codec.h>
9+
10+
#include <boost/bind.hpp>
11+
12+
#include <stdio.h>
13+
14+
using namespace muduo;
15+
using namespace muduo::net;
16+
17+
namespace logging
18+
{
19+
20+
class Session : boost::noncopyable
21+
{
22+
public:
23+
explicit Session(const TcpConnectionPtr& conn)
24+
: codec_(&LogRecord::default_instance(),
25+
"LOG0",
26+
boost::bind(&Session::onMessage, this, _1, _2, _3)),
27+
file_(getFileName(conn))
28+
{
29+
conn->setMessageCallback(
30+
boost::bind(&ProtobufCodec::onMessage, &codec_, _1, _2, _3));
31+
}
32+
33+
private:
34+
35+
string getFileName(const TcpConnectionPtr& conn)
36+
{
37+
string filename;
38+
filename += conn->peerAddress().toIp();
39+
40+
char timebuf[32];
41+
struct tm tm;
42+
time_t now = time(NULL);
43+
gmtime_r(&now, &tm); // FIXME: localtime_r ?
44+
strftime(timebuf, sizeof timebuf, ".%Y%m%d-%H%M%S.", &tm);
45+
filename += timebuf;
46+
47+
char buf[32];
48+
snprintf(buf, sizeof buf, "%d", count_.incrementAndGet());
49+
filename += buf;
50+
51+
filename += ".log";
52+
LOG_INFO << "Session of " << conn->name() << " file " << filename;
53+
return filename;
54+
}
55+
56+
void onMessage(const TcpConnectionPtr& conn,
57+
const MessagePtr& message,
58+
Timestamp time)
59+
{
60+
LogRecord* logRecord = muduo::down_cast<LogRecord*>(message.get());
61+
if (logRecord->has_heartbeat())
62+
{
63+
// FIXME ?
64+
}
65+
std::string str = logRecord->DebugString();
66+
file_.append(str.c_str(), str.size());
67+
file_.append("\n", 1);
68+
}
69+
70+
ProtobufCodec codec_;
71+
FileUtil::AppendFile file_;
72+
static AtomicInt32 count_;
73+
};
74+
typedef boost::shared_ptr<Session> SessionPtr;
75+
76+
AtomicInt32 Session::count_;
77+
78+
class LogServer : boost::noncopyable
79+
{
80+
public:
81+
LogServer(EventLoop* loop, const InetAddress& listenAddr, int numThreads)
82+
: loop_(loop),
83+
server_(loop_, listenAddr, "AceLoggingServer")
84+
{
85+
server_.setConnectionCallback(
86+
boost::bind(&LogServer::onConnection, this, _1));
87+
if (numThreads > 1)
88+
{
89+
server_.setThreadNum(numThreads);
90+
}
91+
}
92+
93+
void start()
94+
{
95+
server_.start();
96+
}
97+
98+
private:
99+
void onConnection(const TcpConnectionPtr& conn)
100+
{
101+
if (conn->connected())
102+
{
103+
SessionPtr session(new Session(conn));
104+
conn->setContext(session);
105+
}
106+
else
107+
{
108+
conn->setContext(SessionPtr());
109+
}
110+
}
111+
112+
EventLoop* loop_;
113+
TcpServer server_;
114+
};
115+
116+
}
117+
3118
int main(int argc, char* argv[])
4119
{
120+
EventLoop loop;
121+
int port = argc > 1 ? atoi(argv[1]) : 50000;
122+
LOG_INFO << "Listen on port " << port;
123+
InetAddress listenAddr(static_cast<uint16_t>(port));
124+
int numThreads = argc > 2 ? atoi(argv[2]) : 1;
125+
logging::LogServer server(&loop, listenAddr, numThreads);
126+
server.start();
127+
loop.loop();
128+
5129
}

0 commit comments

Comments
 (0)