/src/logging-log4cxx/src/main/cpp/syslogwriter.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | | * contributor license agreements. See the NOTICE file distributed with |
4 | | * this work for additional information regarding copyright ownership. |
5 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | | * (the "License"); you may not use this file except in compliance with |
7 | | * the License. You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | #include <log4cxx/logstring.h> |
19 | | #include <log4cxx/helpers/syslogwriter.h> |
20 | | #include <log4cxx/helpers/loglog.h> |
21 | | #include <log4cxx/helpers/inetaddress.h> |
22 | | #include <log4cxx/helpers/datagramsocket.h> |
23 | | #include <log4cxx/helpers/datagrampacket.h> |
24 | | #include <log4cxx/helpers/transcoder.h> |
25 | | |
26 | | using namespace LOG4CXX_NS; |
27 | | using namespace LOG4CXX_NS::helpers; |
28 | | |
29 | | struct SyslogWriter::SyslogWriterPrivate { |
30 | | SyslogWriterPrivate(const LogString& syslogHost1, int syslogHostPort1) |
31 | 0 | : syslogHost(syslogHost1), syslogHostPort(syslogHostPort1){} |
32 | | |
33 | | LogString syslogHost; |
34 | | int syslogHostPort; |
35 | | InetAddressPtr address; |
36 | | DatagramSocketPtr ds; |
37 | | }; |
38 | | |
39 | | SyslogWriter::SyslogWriter(const LogString& syslogHost1, int syslogHostPort1) |
40 | 0 | : m_priv(std::make_unique<SyslogWriterPrivate>(syslogHost1, syslogHostPort1)) |
41 | 0 | { |
42 | 0 | try |
43 | 0 | { |
44 | 0 | m_priv->address = InetAddress::getByName(syslogHost1); |
45 | 0 | } |
46 | 0 | catch (UnknownHostException& e) |
47 | 0 | { |
48 | 0 | LogLog::error(((LogString) LOG4CXX_STR("Could not find ")) + syslogHost1 + |
49 | 0 | LOG4CXX_STR(". All logging will FAIL."), e); |
50 | 0 | } |
51 | |
|
52 | 0 | try |
53 | 0 | { |
54 | 0 | m_priv->ds = DatagramSocket::create(); |
55 | 0 | } |
56 | 0 | catch (SocketException& e) |
57 | 0 | { |
58 | 0 | LogLog::error(((LogString) LOG4CXX_STR("Could not instantiate DatagramSocket to ")) + syslogHost1 + |
59 | 0 | LOG4CXX_STR(". All logging will FAIL."), e); |
60 | 0 | } |
61 | 0 | } |
62 | | |
63 | 0 | SyslogWriter::~SyslogWriter(){} |
64 | | |
65 | | void SyslogWriter::write(const LogString& source) |
66 | 0 | { |
67 | 0 | if (m_priv->ds != 0 && m_priv->address != 0) |
68 | 0 | { |
69 | 0 | LOG4CXX_ENCODE_CHAR(data, source); |
70 | |
|
71 | 0 | auto packet = std::make_shared<DatagramPacket>( |
72 | 0 | (void*) data.data(), (int)data.length(), |
73 | 0 | m_priv->address, m_priv->syslogHostPort); |
74 | |
|
75 | 0 | m_priv->ds->send(packet); |
76 | 0 | } |
77 | 0 | } |