A simple UDP logger that lets you send messages via UDP to a remote host. It's a cheap implementation for logging to a remove host via UDP. Note, the logger is uses UDP sockets provided by NetServices (https://mbed.org/cookbook/NetServices). After you are ready with the network setup (eth.setup()...), just call Log::init(), so the logger would create its local socket, and then you can log any text via Log::log(); Also note that the logger won't do any net polls (eth.poll()) to save computing time, so it's up to you to handle it.

Committer:
screamer
Date:
Wed Oct 23 10:55:18 2013 +0000
Revision:
2:acc2aad1d2d5
Parent:
1:1130e7abfc09
remove legacy .lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 1:1130e7abfc09 1
screamer 1:1130e7abfc09 2 /*
screamer 1:1130e7abfc09 3 Copyright (c) 2012 Mihail Stoyanov (wm [at] screamer [dot] org)
screamer 1:1130e7abfc09 4
screamer 1:1130e7abfc09 5 Permission is hereby granted, free of charge, to any person obtaining a copy
screamer 1:1130e7abfc09 6 of this software and associated documentation files (the "Software"), to deal
screamer 1:1130e7abfc09 7 in the Software without restriction, including without limitation the rights
screamer 1:1130e7abfc09 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
screamer 1:1130e7abfc09 9 copies of the Software, and to permit persons to whom the Software is
screamer 1:1130e7abfc09 10 furnished to do so, subject to the following conditions:
screamer 1:1130e7abfc09 11
screamer 1:1130e7abfc09 12 The above copyright notice and this permission notice shall be included in
screamer 1:1130e7abfc09 13 all copies or substantial portions of the Software.
screamer 1:1130e7abfc09 14
screamer 1:1130e7abfc09 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
screamer 1:1130e7abfc09 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
screamer 1:1130e7abfc09 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
screamer 1:1130e7abfc09 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
screamer 1:1130e7abfc09 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
screamer 1:1130e7abfc09 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
screamer 1:1130e7abfc09 21 THE SOFTWARE.
screamer 1:1130e7abfc09 22 */
screamer 1:1130e7abfc09 23
screamer 1:1130e7abfc09 24
screamer 1:1130e7abfc09 25 #ifndef LOG_H
screamer 1:1130e7abfc09 26 #define LOG_H
screamer 1:1130e7abfc09 27
screamer 1:1130e7abfc09 28 #include "mbed.h"
screamer 1:1130e7abfc09 29 #include "UDPSocket.h"
screamer 1:1130e7abfc09 30
screamer 1:1130e7abfc09 31
screamer 1:1130e7abfc09 32 class Log {
screamer 1:1130e7abfc09 33 private:
screamer 1:1130e7abfc09 34 UDPSocket sock;
screamer 1:1130e7abfc09 35 int lPort;
screamer 1:1130e7abfc09 36 Host lHost;
screamer 1:1130e7abfc09 37 Host dHost;
screamer 1:1130e7abfc09 38
screamer 1:1130e7abfc09 39 bool is_init;
screamer 1:1130e7abfc09 40
screamer 1:1130e7abfc09 41 public:
screamer 1:1130e7abfc09 42 ///Constructor
screamer 1:1130e7abfc09 43 /**
screamer 1:1130e7abfc09 44 @param port : local UDP bind port even though the logger doesn't acccept messages
screamer 1:1130e7abfc09 45 */
screamer 1:1130e7abfc09 46 Log (int port = 10000) {
screamer 1:1130e7abfc09 47 lPort = port;
screamer 1:1130e7abfc09 48 };
screamer 1:1130e7abfc09 49
screamer 1:1130e7abfc09 50 ///The actual init method, which should be called after the ethernet is initialized
screamer 1:1130e7abfc09 51 /**
screamer 1:1130e7abfc09 52 @param pHost : the destination UDP server - Host dHost(IpAddr(1,2,3,4), port)
screamer 1:1130e7abfc09 53
screamer 1:1130e7abfc09 54 Can be called multiple times to switch destination UDP servers
screamer 1:1130e7abfc09 55 */
screamer 1:1130e7abfc09 56 inline bool init(Host pHost) {
screamer 1:1130e7abfc09 57 Host lHost(IpAddr(), lPort);
screamer 1:1130e7abfc09 58
screamer 1:1130e7abfc09 59 UDPSocketErr udpErr = sock.bind(lHost);
screamer 1:1130e7abfc09 60 if (udpErr != UDPSOCKET_OK) {
screamer 1:1130e7abfc09 61 printf("error %d\n", udpErr);
screamer 1:1130e7abfc09 62 return false;
screamer 1:1130e7abfc09 63 };
screamer 1:1130e7abfc09 64
screamer 1:1130e7abfc09 65 dHost = pHost;
screamer 1:1130e7abfc09 66 is_init = true;
screamer 1:1130e7abfc09 67
screamer 1:1130e7abfc09 68 return true;
screamer 1:1130e7abfc09 69 };
screamer 1:1130e7abfc09 70
screamer 1:1130e7abfc09 71 ///The logging method.
screamer 1:1130e7abfc09 72 inline bool log(const char *msg) {
screamer 1:1130e7abfc09 73 if (!is_init) return false;
screamer 1:1130e7abfc09 74
screamer 1:1130e7abfc09 75 int sent = sock.sendto(msg, strlen(msg), &dHost);
screamer 1:1130e7abfc09 76 if (sent < 0) {
screamer 1:1130e7abfc09 77 printf("error %d\n", (UDPSocketErr)sent);
screamer 1:1130e7abfc09 78 };
screamer 1:1130e7abfc09 79
screamer 1:1130e7abfc09 80 return true;
screamer 1:1130e7abfc09 81 };
screamer 1:1130e7abfc09 82 };
screamer 1:1130e7abfc09 83
screamer 1:1130e7abfc09 84 #endif