132
|
1 /*******************************************************************************
|
|
2
|
|
3 copyright: Copyright (c) 2004 Kris Bell. All rights reserved
|
|
4
|
|
5 license: BSD style: $(LICENSE)
|
|
6
|
|
7 version: Initial release: May 2004
|
|
8
|
|
9 author: Kris
|
|
10
|
|
11 *******************************************************************************/
|
|
12
|
|
13 module tango.util.log.SocketAppender;
|
|
14
|
|
15 private import tango.util.log.Appender;
|
|
16
|
|
17 private import tango.io.Buffer,
|
|
18 tango.io.Console;
|
|
19
|
|
20 private import tango.net.SocketConduit,
|
|
21 tango.net.InternetAddress;
|
|
22
|
|
23 /*******************************************************************************
|
|
24
|
|
25 Appender for sending formatted output to a Socket.
|
|
26
|
|
27 *******************************************************************************/
|
|
28
|
|
29 public class SocketAppender : Appender
|
|
30 {
|
|
31 private Mask mask;
|
|
32 private IBuffer buffer;
|
|
33 private SocketConduit conduit;
|
|
34 private InternetAddress address;
|
|
35 private bool connected;
|
|
36
|
|
37 /***********************************************************************
|
|
38
|
|
39 Create with the given Layout and address
|
|
40
|
|
41 ***********************************************************************/
|
|
42
|
|
43 this (InternetAddress address, EventLayout layout = null)
|
|
44 {
|
|
45 setLayout (layout);
|
|
46
|
|
47 this.address = address;
|
|
48 this.conduit = new SocketConduit;
|
|
49 this.buffer = new Buffer (conduit);
|
|
50
|
|
51 // Get a unique fingerprint for this class
|
|
52 this.mask = register (address.toString);
|
|
53 }
|
|
54
|
|
55 /***********************************************************************
|
|
56
|
|
57 Return the fingerprint for this class
|
|
58
|
|
59 ***********************************************************************/
|
|
60
|
|
61 Mask getMask ()
|
|
62 {
|
|
63 return mask;
|
|
64 }
|
|
65
|
|
66 /***********************************************************************
|
|
67
|
|
68 Return the name of this class
|
|
69
|
|
70 ***********************************************************************/
|
|
71
|
|
72 char[] getName ()
|
|
73 {
|
|
74 return this.classinfo.name;
|
|
75 }
|
|
76
|
|
77 /***********************************************************************
|
|
78
|
|
79 Append an event to the output. If the operations fails
|
|
80 we have to revert to an alternative logging strategy,
|
|
81 which will probably require a backup Appender specified
|
|
82 during construction. For now we simply echo to Cerr if
|
|
83 the socket has become unavailable.
|
|
84
|
|
85 ***********************************************************************/
|
|
86
|
|
87 void append (Event event)
|
|
88 {
|
|
89 auto layout = getLayout();
|
|
90
|
|
91 if (buffer)
|
|
92 {
|
|
93 try {
|
|
94 if (! connected)
|
|
95 {
|
|
96 conduit.connect (address);
|
|
97 connected = true;
|
|
98 }
|
|
99
|
|
100 buffer (layout.header (event));
|
|
101 buffer (layout.content (event));
|
|
102 buffer (layout.footer (event)) ();
|
|
103 return;
|
|
104 } catch (Exception e)
|
|
105 {
|
|
106 connected = false;
|
|
107 Cerr ("SocketAppender.append :: "~e.toString).newline;
|
|
108 }
|
|
109 }
|
|
110
|
|
111 Cerr (layout.content(event)).newline;
|
|
112 }
|
|
113
|
|
114 /***********************************************************************
|
|
115
|
|
116 Close the socket associated with this Appender
|
|
117
|
|
118 ***********************************************************************/
|
|
119
|
|
120 void close ()
|
|
121 {
|
|
122 if (conduit)
|
|
123 conduit.detach;
|
|
124 conduit = null;
|
|
125 }
|
|
126 }
|