forked from openbmc/phosphor-logging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_streamer.cpp
137 lines (118 loc) · 3.4 KB
/
log_streamer.cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* SPDX-FileCopyrightText: Copyright (c) 2023-2024 NVIDIA CORPORATION &
* AFFILIATES. All rights reserved. SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "log_streamer.hpp"
#include <phosphor-logging/lg2.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
#include <csignal>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <iostream>
namespace phosphor
{
namespace logging
{
bool LogStreamer::start()
{
sockfd = createSocket();
return sockfd != -1;
}
void LogStreamer::stop()
{
if (sockfd != -1)
{
closeSocket();
lg2::info("Socket closed gracefully");
}
}
bool LogStreamer::sendMessage(const std::vector<uint8_t>& message)
{
/* Maximum binary size 64K */
constexpr std::size_t MAX_MESSAGE_SIZE = 65536;
if (sockfd == -1)
{
lg2::error("Socket is not connected");
return false;
}
if (message.size() > MAX_MESSAGE_SIZE)
{
lg2::error("Message size exceeds 64K limit: {SIZE}", "SIZE",
message.size());
return false;
}
ssize_t bytesSent = sendto(sockfd, message.data(), message.size(), 0,
(struct sockaddr*)&serverAddr,
sizeof(serverAddr));
if (bytesSent == -1)
{
lg2::error("Failed to send message: {ERROR}", "ERROR", strerror(errno));
return false;
}
lg2::info("Sent {BYTES} bytes", "BYTES", bytesSent);
return true;
}
bool LogStreamer::sendFile(const std::string& filePath)
{
/* Opens the file in binary mode and position the file pointer at the end */
std::ifstream file(filePath, std::ios::binary | std::ios::ate);
if (!file.is_open())
{
lg2::error("Failed to open file: {ERROR}", "ERROR", strerror(errno));
return false;
}
/* Gets the size of the file */
std::streamsize size = file.tellg();
if (size == 0)
{
lg2::error("File is empty");
return false;
}
file.seekg(0, std::ios::beg);
/* Reads the entire file into a buffer */
std::vector<uint8_t> buffer(size);
if (!file.read(reinterpret_cast<char*>(buffer.data()), size))
{
lg2::error("Failed to read file: {ERROR}", "ERROR", strerror(errno));
return false;
}
return sendMessage(buffer);
}
int LogStreamer::createSocket()
{
int sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (sockfd == -1)
{
lg2::error("Failed to create socket: {ERROR}", "ERROR",
strerror(errno));
return -1;
}
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sun_family = AF_UNIX;
strncpy(serverAddr.sun_path, socketPath.c_str(),
sizeof(serverAddr.sun_path) - 1);
return sockfd;
}
void LogStreamer::closeSocket()
{
if (sockfd != -1)
{
close(sockfd);
sockfd = -1;
}
}
} // namespace logging
} // namespace phosphor