4 * Copyright 2015 Eddie Berrisford-Lynch <dev@fun2be.me>.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 package uk.ac.ntu.n0521366.wsyd.libs.logging;
26 import java.io.IOException;
27 import java.util.logging.LogRecord;
28 import java.util.logging.Handler;
29 import java.util.logging.ErrorManager;
30 import java.net.InetSocketAddress;
31 import java.net.DatagramPacket;
32 import java.net.DatagramSocket;
33 import uk.ac.ntu.n0521366.wsyd.libs.message.MessageLogRecord;
34 import uk.ac.ntu.n0521366.wsyd.libs.net.NetworkMessage;
37 * Publish log records over UDP encapsulated in a NetworkMessage.
39 * Sends the records to a remote NetworkServerUDP service.
41 * @see java.util.logging.Handler
42 * @see uk.ac.ntu.n0521366.wsyd.libs.net.NetworkMessage
43 * @author Eddie Berrisford-Lynch <dev@fun2be.me>
45 public class PacketHandler extends Handler {
48 * Where to send the log record
50 private final InetSocketAddress _socketAddress;
61 * Send the record to the log service.
63 * Wraps the record in a Message, and the Message in a NetworkMessage
68 public void publish(LogRecord record) {
72 if (!this.isLoggable(record)) {
75 MessageLogRecord m = new MessageLogRecord(record);
76 NetworkMessage nm = NetworkMessage.createNetworkMessage("Log", "ServerLog", m);
78 byte[] data = NetworkMessage.serialize(nm);
79 DatagramPacket packetMessage = new DatagramPacket(data, data.length);
81 // set remote log server host address and port
82 packetMessage.setSocketAddress(this._socketAddress);
85 DatagramSocket datagramSocket = new DatagramSocket(_socketAddress.getPort(), _socketAddress.getAddress())
88 datagramSocket.send(packetMessage);
89 } catch (IOException e ) {
90 reportError("Unable to create UDP socket", e, ErrorManager.WRITE_FAILURE);
92 } catch (IOException e) {
93 reportError("Unable to serialize NetworkMessage", e, ErrorManager.WRITE_FAILURE);
98 * Create a handler that sends log records to a UDP log service.
100 * @param logService address of the target log service
102 public PacketHandler(InetSocketAddress logService) {
103 this._socketAddress = logService;