Skip to content

Commit 1caa868

Browse files
committed
Issue #21: Add glog to all src files
1 parent b1442e6 commit 1caa868

File tree

8 files changed

+120
-67
lines changed

8 files changed

+120
-67
lines changed

src/kpi_rover_ecu/src/IMUController.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <iostream>
88
#include <vector>
99

10+
#include "loggingIncludes.h"
11+
1012
IMUController::IMUController() : data_{}, isStarted_(false), configuration_(rc_mpu_default_config()), actualData_({}) {
1113
configuration_.i2c_bus = kI2cBus;
1214
configuration_.gpio_interrupt_pin_chip = kGpioIntPinChip;
@@ -32,20 +34,23 @@ std::vector<float> IMUController::GetData() {
3234
if (!isStarted_) {
3335
return {};
3436
}
35-
37+
LOG_DEBUG << "Read data from accel and gyro";
3638
rc_mpu_read_accel(&data_);
3739
rc_mpu_read_gyro(&data_);
3840

3941
actualData_.resize(kActualDataSize);
4042
const std::vector<float> kAccelData = GetAccel();
43+
LOG_DEBUG << "Get data from accelerometr";
4144
const int kAccelDataSize = static_cast<int>(kAccelData.size());
4245
std::copy(kAccelData.begin(), kAccelData.begin() + kAccelDataSize, actualData_.begin());
4346

4447
const std::vector<float> kGyroData = GetGyro();
48+
LOG_DEBUG << "Get data from gyroscope";
4549
const int kGyroDataSize = static_cast<int>(kGyroData.size());
4650
std::copy(kGyroData.begin(), kGyroData.begin() + kGyroDataSize, actualData_.begin() + kAccelDataSize);
4751

4852
const std::vector<float> kQaternionData = GetQaternion();
53+
LOG_DEBUG << "Get data from magnetometer (qaternion)";
4954
const int kQaternionDataSize = static_cast<int>(kQaternionData.size());
5055
std::copy(kQaternionData.begin(), kQaternionData.begin() + kQaternionDataSize,
5156
actualData_.begin() + kAccelDataSize + kGyroDataSize);

src/kpi_rover_ecu/src/KPIRoverECU.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <vector>
1212

1313
#include "IMUController.h"
14+
#include "loggingIncludes.h"
1415
#include "TCPTransport.h"
1516
#include "UDPClient.h"
1617
#include "protocolHandler.h"
@@ -27,12 +28,14 @@ KPIRoverECU::KPIRoverECU(ProtocolHanlder *_protocolHandler, TCPTransport *_tcpTr
2728

2829
bool KPIRoverECU::Start() {
2930
tcp_transport_->Start();
31+
LOG_DEBUG << "Starting all thread in KPIRoverECU";
3032
timerThread_ = std::thread([this] { TimerThreadFuction(this->protocol_handler_); });
3133
processingThread_ = std::thread([this] { ProcessingThreadFunction(); });
3234
imuThread_ = std::thread([this] { IMUThreadFucntion(this->imu_controller_); });
35+
LOG_DEBUG << "All thread in KPIRoverECU started";
3336

3437
if (!timerThread_.joinable() || !processingThread_.joinable() || !imuThread_.joinable()) {
35-
std::cout << "Error creating thread" << '\n';
38+
LOG_ERROR << "Error creating thread";
3639
return false;
3740
}
3841

@@ -47,9 +50,11 @@ void KPIRoverECU::IMUThreadFucntion(IMUController *workClass) {
4750

4851
while (runningProcess_) {
4952
if (destination_address.empty()) {
53+
LOG_DEBUG << "Udp server address is unknown, get address from tcp server clients";
5054
destination_address = tcp_transport_->GetClientIp();
5155
destination_port = tcp_transport_->GetClientPort();
5256
if (!destination_address.empty()) {
57+
LOG_DEBUG << "Get UDP server address, initializing UDPClient";
5358
udp_client_->Init(destination_address, destination_port);
5459
}
5560

@@ -58,9 +63,11 @@ void KPIRoverECU::IMUThreadFucntion(IMUController *workClass) {
5863
if (!kImuData.empty()) {
5964
if (packet_number == k16MaxCount) {
6065
packet_number = 0;
66+
LOG_DEBUG << "set UDP packet number to 0";
6167
}
6268
packet_number += 1;
6369

70+
LOG_DEBUG << "build UDP packet " << packet_number << " for UDP server";
6471
std::vector<uint8_t> send_val;
6572
send_val.push_back(workClass->GetId());
6673

@@ -83,6 +90,8 @@ void KPIRoverECU::IMUThreadFucntion(IMUController *workClass) {
8390
send_val.push_back(bytes[j]);
8491
}
8592
}
93+
94+
LOG_DEBUG << "use UDP client to send message";
8695
udp_client_->Send(send_val);
8796
}
8897
}
@@ -97,13 +106,15 @@ void KPIRoverECU::TimerThreadFuction(ProtocolHanlder *workClass) {
97106
if (counter_ > 0) {
98107
rc_usleep(kTimerPrecision);
99108
counter_--;
109+
LOG_DEBUG << "counter decrement: " << counter_;
100110

101111
if (counter_ == 0) {
102-
std::cout << "[INFO] Motor set to stop" << '\n';
112+
LOG_INFO << "Motor set to stop";
103113
}
104114

105115
} else if (counter_ == 0) {
106116
// command to stop all motors
117+
LOG_DEBUG << "Send command to stop all motors";
107118
workClass->HandleMessage(kStopVector);
108119
imu_controller_->SetDisable();
109120
rc_usleep(kTimeStop * kOneSecondMicro);
@@ -112,34 +123,42 @@ void KPIRoverECU::TimerThreadFuction(ProtocolHanlder *workClass) {
112123
}
113124

114125
void KPIRoverECU::ProcessingThreadFunction() {
126+
LOG_DEBUG << "start cycle in main thread";
115127
while (runningProcess_) {
116128
std::vector<uint8_t> message;
117129
if (tcp_transport_->Receive(message)) {
130+
LOG_DEBUG << "TCP packet received, processing it";
118131
imu_controller_->SetEnable();
119132

120133
counter_.store(GetCounter());
134+
LOG_DEBUG << "set timer counter to GetCounter()";
121135
const std::vector<uint8_t> kReturnMessage = protocol_handler_->HandleMessage(message);
136+
LOG_DEBUG << "get TCP response";
122137
tcp_transport_->Send(kReturnMessage);
138+
LOG_DEBUG << "Send TCP response";
123139
}
124140
}
125141
}
126142

127143
void KPIRoverECU::Stop() {
128-
std::cout << "END of program" << '\n';
129-
std::cout << "joining threads" << '\n';
144+
LOG_INFO << "END of program";
145+
LOG_INFO << "joining threads";
130146
runningState_ = true;
131147
if (timerThread_.joinable()) {
132148
timerThread_.join();
149+
LOG_DEBUG << "timerThread_ joined";
133150
}
134151
runningProcess_ = false;
135152
if (processingThread_.joinable()) {
136153
processingThread_.join();
154+
LOG_DEBUG << "main thread joined";
137155
}
138156
if (imuThread_.joinable()) {
139157
imuThread_.join();
158+
LOG_DEBUG << "IMUThread joined";
140159
}
141160

142-
std::cout << "destroying drivers" << '\n';
161+
LOG_INFO << "destroying drivers";
143162
// tcp_transport_->Destroy();
144163
}
145164

src/kpi_rover_ecu/src/TCPTransport.cpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <string>
1414
#include <vector>
1515

16+
#include "loggingIncludes.h"
17+
1618
constexpr std::size_t kBufferSize = 1024;
1719
constexpr int kTimeoutMs = 50;
1820

@@ -29,9 +31,9 @@ TCPTransport::TCPTransport(const char *ip_address, int port)
2931
TCPTransport::~TCPTransport() {
3032
running_ = false;
3133
shutdown(sockfd_, SHUT_RDWR);
32-
std::cout << "joining thread ... " << '\n';
34+
LOG_INFO << "joining thread ... ";
3335
acceptThread_.join();
34-
std::cout << "closing socket" << '\n';
36+
LOG_INFO << "closing socket";
3537
close(client_sockfd_);
3638
close(sockfd_);
3739
delete[] server_address_;
@@ -43,33 +45,38 @@ int TCPTransport::Init() {
4345
source_address_[0] = '\0';
4446

4547
sockfd_ = socket(AF_INET, SOCK_STREAM, 0);
48+
4649

4750
if (sockfd_ < 0) {
48-
perror("[ERROR] Socket()");
51+
LOG_ERROR << "Socket()";
4952
return -1;
5053
}
54+
LOG_DEBUG << "Created socket";
5155

5256
if (setsockopt(sockfd_, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) != 0) {
53-
perror("[ERROR] setsockopt()");
57+
LOG_ERROR << "setsockopt()";
5458
return -1;
5559
}
5660

5761
memset(&serv_addr, 0, sizeof(serv_addr));
5862
serv_addr.sin_family = AF_INET;
5963
serv_addr.sin_addr.s_addr = inet_addr(server_address_);
6064
serv_addr.sin_port = htons(server_portnum_);
65+
LOG_DEBUG << "Set socket parameters";
6166

6267
if (bind(sockfd_, reinterpret_cast<struct sockaddr *>(&serv_addr), sizeof(serv_addr)) != 0) {
63-
perror("[ERROR] bind()");
68+
LOG_ERROR << "bind()";
6469
return -1;
6570
}
71+
LOG_DEBUG << "Socket binded";
6672

6773
if (listen(sockfd_, TCPTransport::kNumSlots) != 0) {
68-
perror("[ERROR] listen");
74+
LOG_ERROR << "listen()";
6975
return -1;
7076
}
77+
LOG_DEBUG << "Start listening";
7178

72-
std::cout << "[INFO] Started server on " << server_address_ << ":" << server_portnum_ << '\n';
79+
LOG_INFO << " Started server on " << server_address_ << ":" << server_portnum_;
7380

7481
return sockfd_;
7582
}
@@ -80,19 +87,21 @@ void TCPTransport::Start() {
8087
socklen_t client_add_size = sizeof(client_addr);
8188

8289
while (running_) {
83-
std::cout << "Waiting for connection..." << '\n';
90+
LOG_INFO << "Waiting for connection...";
8491
client_sockfd_ = accept(sockfd_, reinterpret_cast<sockaddr *>(&client_addr), &client_add_size);
8592
if (client_sockfd_ >= 0) {
8693
inet_ntop(AF_INET, &client_addr.sin_addr, source_address_, INET_ADDRSTRLEN);
8794
source_port_ = static_cast<int>(ntohs(client_addr.sin_port));
95+
LOG_INFO << "Client connected, source address: " << std::string(source_address_) << " source port: " << source_port_;
8896
while (true) { // Use true instead of 1
8997
std::uint8_t buffer[kBufferSize];
9098
const ssize_t kBytesReceived = recv(client_sockfd_, buffer, sizeof(buffer), 0);
9199
if (kBytesReceived > 0) {
92100
const std::vector<std::uint8_t> kMessage(buffer, buffer + kBytesReceived);
93101
messageQueue_.Push(kMessage);
102+
LOG_DEBUG << "Get message, put in MessageQueue";
94103
} else {
95-
std::cout << "Client disconnected." << '\n';
104+
LOG_INFO << "Client disconnected.";
96105
close(client_sockfd_);
97106
client_sockfd_ = -1;
98107
break;
@@ -107,6 +116,8 @@ bool TCPTransport::Send(const std::vector<std::uint8_t> &data) {
107116
if (client_sockfd_ < 0) {
108117
return false;
109118
}
119+
LOG_DEBUG << "Send TCP packet to TCP client";
120+
110121
return ::send(client_sockfd_, data.data(), data.size(), 0) != -1;
111122
}
112123

@@ -119,9 +130,9 @@ int TCPTransport::GetClientPort() { return source_port_; }
119130
void TCPTransport::Destroy() {
120131
running_ = false;
121132
shutdown(sockfd_, SHUT_RDWR);
122-
std::cout << "joining thread ... " << '\n';
133+
LOG_INFO << "joining thread ... ";
123134
acceptThread_.join();
124-
std::cout << "closing socket" << '\n';
135+
LOG_INFO << "closing socket";
125136
close(client_sockfd_);
126137
close(sockfd_);
127138
delete[] server_address_;

src/kpi_rover_ecu/src/UDPClient.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <string>
1414
#include <vector>
1515

16+
#include "loggingIncludes.h"
17+
1618
UDPClient::UDPClient() : sockfd_(-1), server_portnum_(0), server_address_{} {}
1719

1820
int UDPClient::Init(std::string ip_address, int port) {
@@ -21,10 +23,10 @@ int UDPClient::Init(std::string ip_address, int port) {
2123

2224
server_portnum_ = port;
2325

24-
std::cout << "initialize socket" << '\n';
26+
LOG_DEBUG << "initialize socket";
2527
sockfd_ = socket(AF_INET, SOCK_DGRAM, 0);
2628
if (sockfd_ == -1) {
27-
std::cout << "Error while creating socket" << '\n';
29+
LOG_ERROR << "Error while creating socket";
2830
return -1;
2931
}
3032

@@ -40,7 +42,7 @@ bool UDPClient::Send(const std::vector<std::uint8_t> &data) {
4042
if (sockfd_ < 0) {
4143
return false;
4244
}
43-
45+
LOG_DEBUG << "send UDP packet to UDP server , addr: " << server_address_ << "port: " << server_portnum_;
4446
return sendto(sockfd_, data.data(), data.size(), 0, reinterpret_cast<sockaddr *>(&serverStruct_),
4547
sizeof(serverStruct_)) != -1;
4648
}
@@ -51,12 +53,12 @@ void UDPClient::Destroy() {
5153
shutdown(sockfd_, SHUT_WR);
5254

5355
close(sockfd_);
54-
std::cout << "joining udp client socket ..." << '\n';
56+
LOG_INFO << "joining udp client socket ...";
5557
}
5658

5759
UDPClient::~UDPClient() {
5860
shutdown(sockfd_, SHUT_WR);
5961

6062
close(sockfd_);
61-
std::cout << "joining udp client socket ..." << '\n';
63+
LOG_INFO << "joining udp client socket ...";
6264
}

src/kpi_rover_ecu/src/main.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ int main(int argc, char* argv[]) {
5252
log_level = strtol(optarg, nullptr, kBase);
5353
break;
5454
default:
55-
std::cout << "Usage: " << argv[0] << '\n';
56-
std::cout << " [-a server_address] " << '\n';
57-
std::cout << " [-p server_portnum]" << '\n';
58-
std::cout << " [-l log level]" << '\n';
55+
std::cout << "Usage: " << argv[0];
56+
std::cout << " [-a server_address] ";
57+
std::cout << " [-p server_portnum]";
58+
std::cout << " [-l log level]";
5959
return EXIT_FAILURE;
6060
}
6161
}
@@ -71,7 +71,7 @@ int main(int argc, char* argv[]) {
7171
FLAGS_stderrthreshold = 0;
7272
}
7373
FLAGS_log_dir = "./log";
74-
LOG_INFO << "Logger was set up";
74+
LOG_INFO << "Logger was set up." << "Directory to log: " << FLAGS_log_dir;
7575

7676
MotorController motors_processor;
7777
const uint8_t kMotorNumber = 4;
@@ -88,7 +88,7 @@ int main(int argc, char* argv[]) {
8888
IMUController imu_controller;
8989

9090
if (imu_controller.Init() == -1) {
91-
std::cout << "[ERROR] Error initializing IMU controller" << '\n';
91+
LOG_ERROR << "Error initializing IMU controller";
9292
imu_controller.Stop();
9393
return 1;
9494
}
@@ -97,28 +97,28 @@ int main(int argc, char* argv[]) {
9797
UDPClient udp_client;
9898

9999
if (tcp_transport.Init() == -1) {
100-
std::cout << "[ERROR] Error creating socket" << '\n';
100+
LOG_ERROR << "Error creating socket";
101101
tcp_transport.Destroy();
102102
udp_client.Destroy();
103103
return 1;
104104
}
105105

106-
std::cout << "start ..." << '\n';
106+
LOG_INFO << "start ...";
107107

108108
KPIRoverECU kpi_rover_ecu(&protocol_handler, &tcp_transport, &udp_client, &imu_controller);
109109

110110
if (!kpi_rover_ecu.Start()) {
111-
std::cout << "Error In intitalizing main class" << '\n';
111+
LOG_ERROR << "Error In intitalizing main class";
112112
return 1;
113113
}
114114

115115
if (std::signal(SIGINT, InterruptSignalHandler) == SIG_ERR) {
116-
std::cerr << "Error: Unable to set signal handler for SIGINT" << '\n';
116+
LOG_ERROR << "Unable to set signal handler for SIGINT";
117117
return EXIT_FAILURE;
118118
}
119119

120120
if (std::signal(SIGTERM, InterruptSignalHandler) == SIG_ERR) {
121-
std::cerr << "Error: Unable to set signal handler for SIGTERM" << '\n';
121+
LOG_ERROR << "Unable to set signal handler for SIGTERM";
122122
return EXIT_FAILURE;
123123
}
124124

0 commit comments

Comments
 (0)