summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuo Jinghua <sunmoon1997@gmail.com>2011-11-19 20:59:14 +0800
committerLuo Jinghua <sunmoon1997@gmail.com>2011-11-19 20:59:14 +0800
commita15610f26641db79c5585fcec493a8287de80af5 (patch)
tree883319a684220b09d7b6e945b123fe962beb3973
parent7dfaab23ccb4f2d6ec025cd3c133cc1a7fc2ea04 (diff)
SexyAppFramework: Added a udp log listener
-rw-r--r--osframework/source/SexyAppFramework/SimpleUdpLogListener.cpp50
-rw-r--r--osframework/source/SexyAppFramework/SimpleUdpLogListener.h26
2 files changed, 76 insertions, 0 deletions
diff --git a/osframework/source/SexyAppFramework/SimpleUdpLogListener.cpp b/osframework/source/SexyAppFramework/SimpleUdpLogListener.cpp
new file mode 100644
index 0000000..f00d01f
--- /dev/null
+++ b/osframework/source/SexyAppFramework/SimpleUdpLogListener.cpp
@@ -0,0 +1,50 @@
+#include "SimpleUdpLogListener.h"
+#include "SexyLogManager.h"
+#include "Common.h"
+
+#include <stdio.h>
+
+using namespace Sexy;
+
+SimpleUdpLogListener::SimpleUdpLogListener(const std::string& target) :
+ mHost("localhost"), mPort("11035")
+{
+ std::string s = target;
+
+ if (s.substr(0, 6) != "udp://")
+ return;
+
+ s = s.substr(6);
+ std::vector<std::string> tuple;
+ Split(s, ":", tuple);
+
+ if (tuple.size() > 1)
+ mPort = tuple[1];
+ if (tuple.size() > 0)
+ mHost = tuple[0];
+}
+
+void SimpleUdpLogListener::log(LogLevel lvl, const std::string& tag, const std::string& s)
+{
+ if (mHost.empty() || mPort.empty() || s.empty())
+ return;
+
+ std::string msg = formatMessage(lvl, tag, s);
+ if (msg.empty())
+ return;
+
+ inlineRTrim(msg);
+
+ msg += "\n";
+ int left = msg.length();
+ const char* data = msg.data();
+ int port = atoi(mPort.c_str());
+ while (left)
+ {
+ size_t bytes = std::min(left, 1500);
+ if (mSock.sendTo(data, bytes, mHost, port) < 0)
+ return;
+ data += bytes;
+ left -= bytes;
+ }
+}
diff --git a/osframework/source/SexyAppFramework/SimpleUdpLogListener.h b/osframework/source/SexyAppFramework/SimpleUdpLogListener.h
new file mode 100644
index 0000000..6b10614
--- /dev/null
+++ b/osframework/source/SexyAppFramework/SimpleUdpLogListener.h
@@ -0,0 +1,26 @@
+#ifndef __SIMPLE_UDP_LOG_LISTENER_H__
+#define __SIMPLE_UDP_LOG_LISTENER_H__
+
+#include "SexyLogListener.h"
+#include "SexySocket.h"
+
+#include <string>
+
+namespace Sexy {
+
+ class SimpleUdpLogListener : public LogListener {
+ public:
+ SimpleUdpLogListener(const std::string& target = "");
+ ~SimpleUdpLogListener() {}
+
+ virtual void log(LogLevel lvl, const std::string& tag, const std::string& s);
+
+ private:
+ UDPSocket mSock;
+ std::string mHost;
+ std::string mPort;
+ };
+
+}
+
+#endif