summaryrefslogtreecommitdiff
path: root/cs/src/Ice/SysLoggerI.cs
blob: 0ef1ebf88e8ec34c3e6f67d924bf1b992617970c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// **********************************************************************
//
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

using System.Net.Sockets;

namespace Ice
{

    public sealed class SysLoggerI :  Logger
    {
        public SysLoggerI(string ident)
        {
            _ident = ident;
            
            //
            // Open a datagram socket to communicate with the localhost
            // syslog daemon.
            // 
            try
            {
                _host = IceInternal.Network.getAddress(System.Net.Dns.GetHostName(), _port, 
                                                       IceInternal.Network.EnableBoth).Address;
                _socket = new UdpClient();
                _socket.Connect(_host, _port);
            }
            catch(System.Exception ex)
            {
                throw new Ice.DNSException(ex);
            }
        }
        
        public void print(string message)
        {
            log(LOG_INFO, message);
        }

        public void trace(string category, string message)
        {
            log(LOG_INFO, category + ": " + message);
        }
        
        public void warning(string message)
        {
            log(LOG_WARNING, message);
        }
        
        public void error(string message)
        {
            log(LOG_ERR, message);
        }
        
        private void log(int severity, string message)
        {
            try
            {
                //
                // Create a syslog message as defined by the RFC 3164:
                // <PRI>HEADER MSG. PRI is the priority and is calculated
                // from the facility and the severity. We don't specify
                // the HEADER. MSG contains the identifier followed by a
                // colon character and the message.
                //
                
                int priority = (LOG_USER << 3) | severity;
                
                string msg = '<' + priority + '>' + _ident + ": " + message;
                
                byte[] buf = new byte[msg.Length];
                for(int i = 0; i < msg.Length; i++)
                {
                    buf[i] = (byte)msg[i];
                }
                _socket.Send(buf, buf.Length);
            }
            catch(System.IO.IOException ex)
            {
                Ice.SocketException se = new Ice.SocketException(ex);
                throw se;
            }
        }
        
        private string _ident;
        private UdpClient _socket;
        private System.Net.IPAddress _host;
        private static int _port = 514;
        
        //
        // Syslog facilities facilities (as defined in syslog.h)
        // 
        private static readonly int LOG_USER = 1;
        
        //
        // Syslog priorities (as defined in syslog.h)
        // 
        private static readonly int LOG_ERR = 3;
        private static readonly int LOG_WARNING = 4;
        private static readonly int LOG_INFO = 6;
    }

}