|
1 | 1 | #include <examples/ace/logging/logrecord.pb.h> |
2 | 2 |
|
| 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 | + |
3 | 118 | int main(int argc, char* argv[]) |
4 | 119 | { |
| 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 | + |
5 | 129 | } |
0 commit comments