1.Add spdlog to save log file
2.Add program options to connect specific broadcast code and decide whether to save log file
3.Add comments in readme for windows 64-bit project compilation
This commit is contained in:
Livox-SDK
2019-04-12 10:09:56 +08:00
parent 12c954d175
commit b65ec710ce
70 changed files with 15690 additions and 89 deletions
+3 -3
View File
@@ -48,7 +48,7 @@ bool IOLoop::Init() {
#endif
if (rv != APR_SUCCESS) {
LOG_ERROR << PrintAPRStatus(rv) << std::endl;
LOG_ERROR(PrintAPRStatus(rv));
return false;
}
@@ -141,14 +141,14 @@ void IOLoop::Loop() {
}
if (rv != APR_SUCCESS && !APR_STATUS_IS_TIMEUP(rv) && !APR_STATUS_IS_EINTR(rv)) {
LOG_ERROR << PrintAPRStatus(rv) << std::endl;
LOG_ERROR(PrintAPRStatus(rv));
}
}
bool IOLoop::Wakeup() {
apr_status_t rv = apr_pollset_wakeup(pollset_);
if (rv != APR_SUCCESS) {
LOG_ERROR << PrintAPRStatus(rv) << std::endl;
LOG_ERROR(PrintAPRStatus(rv));
return false;
}
return true;
+50
View File
@@ -0,0 +1,50 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2019 Livox. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#include "logging.h"
std::shared_ptr<spdlog::logger> logger = NULL;
bool is_save_log_file = false;
void InitLogger() {
std::vector<spdlog::sink_ptr> sinkList;
auto consoleSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
consoleSink->set_level(spdlog::level::debug);
sinkList.push_back(consoleSink);
if (is_save_log_file) {
auto rotateSink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("livox_log.txt", 1024 * 1024 * 5, 2);
rotateSink->set_level(spdlog::level::debug);
sinkList.push_back(rotateSink);
}
logger = std::make_shared<spdlog::logger>("console", begin(sinkList), end(sinkList));
spdlog::register_logger(logger);
logger->set_level(spdlog::level::debug);
logger->flush_on(spdlog::level::debug);
}
void UninitLogger() {
spdlog::drop_all();
}
+35 -9
View File
@@ -24,16 +24,42 @@
#ifndef LIVOX_LOGGING_H_
#define LIVOX_LOGGING_H_
#include <iomanip>
#include <iostream>
#include "../include/third_party/spdlog/spdlog/spdlog.h"
#include "../include/third_party/spdlog/spdlog/sinks/stdout_color_sinks.h"
#include "../include/third_party/spdlog/spdlog/sinks/rotating_file_sink.h"
#define __FILENAME__ (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1) : __FILE__)
#ifdef _WIN32
#define __FILENAME__ (strrchr(__FILE__, '\\') ? (strrchr(__FILE__, '\\') + 1):__FILE__)
#else
#define __FILENAME__ (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1):__FILE__)
#endif
#define LOG_TRACE std::cout << "TRACE " << __FILENAME__ << ":" << __LINE__ << " " << __FUNCTION__ << " "
#define LOG_DEBUG std::cout << "DEBUG " << __FILENAME__ << ":" << __LINE__ << " " << __FUNCTION__ << " "
#define LOG_INFO std::cout << "INFO " << __FILENAME__ << ":" << __LINE__ << " " << __FUNCTION__ << " "
#define LOG_WARN std::cout << "WARN " << __FILENAME__ << ":" << __LINE__ << " " << __FUNCTION__ << " "
#define LOG_ERROR std::cout << "ERROR " << __FILENAME__ << ":" << __LINE__ << " " << __FUNCTION__ << " "
#define LOG_FATAL std::cout << "FATAL " << __FILENAME__ << ":" << __LINE__ << " " << __FUNCTION__ << " "
#ifndef suffix
#define suffix(msg) std::string(msg).append(" [")\
.append(__FILENAME__).append("] [").append(__func__)\
.append("] [").append(std::to_string(__LINE__))\
.append("]").c_str()
#endif
#ifndef SPDLOG_TRACE_ON
#define SPDLOG_TRACE_ON
#endif
#ifndef SPDLOG_DEBUG_ON
#define SPDLOG_DEBUG_ON
#endif
extern std::shared_ptr<spdlog::logger> logger;
extern bool is_save_log_file;
void InitLogger();
void UninitLogger();
#define LOG_TRACE(msg, ...) logger->trace(suffix(msg), ##__VA_ARGS__)
#define LOG_DEBUG(msg, ...) logger->debug(suffix(msg), ##__VA_ARGS__)
#define LOG_INFO(msg, ...) logger->info(suffix(msg), ##__VA_ARGS__)
#define LOG_WARN(msg, ...) logger->warn(suffix(msg), ##__VA_ARGS__)
#define LOG_ERROR(msg, ...) logger->error(suffix(msg), ##__VA_ARGS__)
#define LOG_FATAL(msg, ...) logger->critical(suffix(msg), ##__VA_ARGS__)
#endif // LIVOX_LOGGING_H_
@@ -78,7 +78,7 @@ void CommandChannel::OnData(apr_socket_t *, void *) {
apr_status_t rv = apr_socket_recvfrom(&addr, sock_, 0, reinterpret_cast<char *>(cache_buf), &size);
comm_port_->UpdateCacheWrIdx(size);
if (rv != APR_SUCCESS) {
LOG_ERROR << PrintAPRStatus(rv) << std::endl;
LOG_ERROR(PrintAPRStatus(rv));
return;
}
@@ -132,8 +132,8 @@ void CommandChannel::OnTimer(apr_time_t now) {
}
for (list<Command>::iterator ite = timeout_commands.begin(); ite != timeout_commands.end(); ++ite) {
LOG_WARN << PrintAPRTime(apr_time_now()) << " Command Timeout: Set " << (uint16_t)ite->packet.cmd_set << " Id "
<< ite->packet.cmd_code << " Seq " << ite->packet.seq_num << std::endl;
LOG_WARN("Apr time now: {}, Command Timeout: Set {}, Id {}, Seq {}", PrintAPRTime(apr_time_now()),
(uint16_t)ite->packet.cmd_set, ite->packet.cmd_code, ite->packet.seq_num);
if (callback_) {
ite->packet.packet_type = kCommandTypeAck;
callback_->OnCommand(handle_, *ite);
@@ -230,6 +230,7 @@ void CommandChannel::DeviceDisconnect(uint8_t handle) {
}
void CommandChannel::Send(const Command &command) {
LOG_INFO(" Send Command: Set {} Id {} Seq {}", (uint16_t)command.packet.cmd_set, command.packet.cmd_code, command.packet.seq_num);
SendInternal(command);
commands_[command.packet.seq_num] = make_pair(command, apr_time_now() + apr_time_from_msec(command.time_out));
Command &cmd = commands_[command.packet.seq_num].first;
@@ -25,6 +25,7 @@
#include "command_handler.h"
#include <boost/thread/lock_guard.hpp>
#include <boost/thread/locks.hpp>
#include "base/logging.h"
#include "command_impl.h"
#include "device_manager.h"
#include "hub_command_handler.h"
@@ -100,8 +101,10 @@ void CommandHandler::Uninit() {
void CommandHandler::OnCommand(uint8_t handle, const Command &command) {
if (command.packet.packet_type == kCommandTypeAck) {
LOG_INFO(" Recieve Ack: Set {} Id {} Seq {}", (uint16_t)command.packet.cmd_set, command.packet.cmd_code, command.packet.seq_num);
OnCommandAck(handle, command);
} else if (command.packet.packet_type == kCommandTypeMsg) {
LOG_INFO(" Recieve Message: Set {} Id {} Seq {}", (uint16_t)command.packet.cmd_set, command.packet.cmd_code, command.packet.seq_num);
OnCommandMsg(handle, command);
}
}
+9 -13
View File
@@ -38,10 +38,6 @@ DataHandler &data_handler() {
return handler;
}
#pragma pack(1)
#pragma pack()
bool DataHandler::AddDataListener(uint8_t handle, const DataCallback &cb) {
if (handle >= callbacks_.size()) {
return false;
@@ -102,15 +98,15 @@ void DataHandler::OnDataCallback(uint8_t handle, void *data, uint16_t size) {
return;
}
if (cb) {
// LOG_INFO << " device_sn: " << device_sn << std::endl;
// LOG_INFO << " version: " << (uint32_t) lidar_data_cb->version << std::endl;
// LOG_INFO << " slot: " << (uint32_t) lidar_data_cb->slot << std::endl;
// LOG_INFO << " resverd : " << (uint32_t) lidar_data_cb->reserved << std::endl;
// LOG_INFO << " errorCode: " << (uint32_t) lidar_data_cb->err_code << std::endl;
// LOG_INFO << " lidarID: " << (uint16_t) lidar_data_cb->id << std::endl;
// LOG_INFO << " timeStampType: " << (uint32_t) (lidar_data_cb->timestamp_type) << std::endl;
// LOG_INFO << " timeStamp: " << (uint32_t) *(lidar_data_cb->time_stamp) << std::endl;
// LOG_INFO << " dataType: " << (uint16_t) lidar_data->data_type << std::endl << std::endl;
//LOG_INFO(" device_sn: {}", device_sn);
//LOG_INFO(" version: {}", (uint32_t)lidar_data->version);
//LOG_INFO(" slot: {}", (uint32_t)lidar_data->slot);
//LOG_INFO(" resverd : {}", (uint32_t)lidar_data->reserved);
//LOG_INFO(" errorCode: {}", (uint32_t)lidar_data->err_code);
//LOG_INFO(" lidarID: {}", (uint16_t)lidar_data->id);
//LOG_INFO(" timeStampType: {}", (uint32_t) (lidar_data->timestamp_type));
//LOG_INFO(" timeStamp: {}", (uint32_t) *(lidar_data->time_stamp));
//LOG_INFO(" dataType: {}", (uint16_t) lidar_data->data_type);
cb(handle, lidar_data, size);
}
}
+13 -14
View File
@@ -52,7 +52,7 @@ uint16_t DeviceDiscovery::port_count = 0;
bool DeviceDiscovery::Init() {
apr_status_t rv = apr_pool_create(&mem_pool_, NULL);
if (rv != APR_SUCCESS) {
LOG_ERROR << PrintAPRStatus(rv) << std::endl;
LOG_ERROR(PrintAPRStatus(rv));
return false;
}
if (comm_port_ == NULL) {
@@ -68,7 +68,7 @@ bool DeviceDiscovery::Start(IOLoop *loop) {
loop_ = loop;
sock_ = util::CreateBindSocket(kListenPort, mem_pool_);
if (sock_ == NULL) {
LOG_ERROR << "DeviceDiscovery Create Socket Failed" << std::endl;
LOG_ERROR("DeviceDiscovery Create Socket Failed");
return false;
}
loop_->AddDelegate(sock_, this);
@@ -84,7 +84,7 @@ void DeviceDiscovery::OnData(apr_socket_t *sock, void *) {
comm_port_->UpdateCacheWrIdx(size);
if (rv != APR_SUCCESS) {
LOG_ERROR << " Receive Failed " << PrintAPRStatus(rv) << std::endl;
LOG_ERROR(" Receive Failed {}", PrintAPRStatus(rv));
return;
}
CommPacket packet;
@@ -107,14 +107,13 @@ void DeviceDiscovery::OnData(apr_socket_t *sock, void *) {
continue;
}
if (*(uint8_t *)packet.data == 0) {
LOG_INFO << "New Device " << std::endl;
LOG_INFO << "Handle: " << static_cast<uint16_t>(info.handle) << std::endl;
LOG_INFO << "Broadcast Code: " << info.broadcast_code << std::endl;
LOG_INFO << "Type: " << info.type << std::endl;
LOG_INFO << "IP: " << info.ip << std::endl;
LOG_INFO << "Command Port: " << info.cmd_port << std::endl;
LOG_INFO << "Data Port: " << info.data_port << std::endl;
LOG_INFO << std::endl;
LOG_INFO("New Device");
LOG_INFO("Handle: {}", static_cast<uint16_t>(info.handle));
LOG_INFO("Broadcast Code: {}", info.broadcast_code);
LOG_INFO("Type: {}", info.type);
LOG_INFO("IP: {}", info.ip);
LOG_INFO("Command Port: {}", info.cmd_port);
LOG_INFO("Data Port: {}", info.data_port);
DeviceFound(info);
}
}
@@ -159,7 +158,7 @@ void DeviceDiscovery::OnBroadcast(const CommPacket &packet, apr_sockaddr_t *addr
BroadcastDeviceInfo *device_info = (BroadcastDeviceInfo *)packet.data;
string broadcast_code = device_info->broadcast_code;
//LOG_INFO << " Boradcast broadcast code: " << broadcast_code << std::endl;
LOG_INFO(" Boradcast broadcast code: {}", broadcast_code);
device_manager().BroadcastDevices(device_info);
DeviceInfo lidar_info;
bool found = device_manager().FindDevice(broadcast_code, lidar_info);
@@ -180,14 +179,14 @@ void DeviceDiscovery::OnBroadcast(const CommPacket &packet, apr_sockaddr_t *addr
memset(&ip, 0, sizeof(ip));
apr_status_t rv = apr_sockaddr_ip_getbuf(ip, sizeof(ip), addr);
if (rv != APR_SUCCESS) {
LOG_ERROR << PrintAPRStatus(rv) << std::endl;
LOG_ERROR(PrintAPRStatus(rv));
return;
}
strncpy(lidar_info.ip, ip, sizeof(lidar_info.ip));
apr_pool_t *pool = NULL;
rv = apr_pool_create(&pool, mem_pool_);
if (rv != APR_SUCCESS) {
LOG_ERROR << PrintAPRStatus(rv) << std::endl;
LOG_ERROR(PrintAPRStatus(rv));
return;
}
+7 -4
View File
@@ -96,6 +96,7 @@ void DeviceManager::RemoveDevice(uint8_t handle) {
if (handle < devices_.size()) {
devices_[handle].connected = false;
}
LOG_INFO(" Device {} removed ", (uint16_t)handle);
}
void DeviceManager::BroadcastDevices(const BroadcastDeviceInfo *info) {
@@ -110,7 +111,7 @@ void DeviceManager::UpdateDevices(const DeviceInfo &device, DeviceEvent type) {
}
if ((device_mode_ == kDeviceModeHub) && (type == kEventConnect)) {
LOG_DEBUG << "Send Query lidars command" << std::endl;
LOG_DEBUG("Send Query lidars command");
command_handler().SendCommand(kHubDefaultHandle,
kCommandSetHub,
kCommandIDHubQueryLidarInformation,
@@ -140,7 +141,7 @@ void DeviceManager::HubLidarInfomationCallback(uint8_t status, uint8_t, HubQuery
connected_cb_(&(devices_[kHubDefaultHandle].info), kEventConnect);
}
} else {
LOG_ERROR << "Failed to query lidars information connected to hub." << std::endl;
LOG_ERROR("Failed to query lidars information connected to hub.");
}
}
@@ -214,19 +215,21 @@ void DeviceManager::UpdateDeviceState(uint8_t handle, const HeartbeatResponse &r
return;
}
// LOG_INFO << " Update State " << (uint16_t) response.state << " connect " << devices_[handle].connected <<
// std::endl;
//LOG_INFO(" Update State {}, connect {}", (uint16_t)response.state, devices_[handle].connected);
bool update = false;
DeviceInfo &info = devices_[handle].info;
if (info.state != response.state) {
LOG_INFO(" Update State to {}, device connect {}", (uint16_t)response.state, devices_[handle].connected);
info.state = static_cast<LidarState>(response.state);
update = true;
}
if (info.feature != response.feature) {
LOG_INFO(" Update feature to {}, device connect {}", (uint16_t)response.feature, devices_[handle].connected);
info.feature = static_cast<LidarFeature>(response.feature);
update = true;
}
if (info.status.progress != response.error_union.progress) {
LOG_INFO(" Update progress {}, device connect {}", (uint16_t)response.error_union.progress, devices_[handle].connected);
info.status.progress = response.error_union.progress;
update = true;
}
+9
View File
@@ -26,6 +26,7 @@
#include "apr_general.h"
#include "command_handler/command_handler.h"
#include "data_handler/data_handler.h"
#include "base/logging.h"
#include "device_manager.h"
using namespace livox;
IOThread *g_thread = NULL;
@@ -41,6 +42,8 @@ void GetLivoxSdkVersion(LivoxSdkVersion *version) {
bool Init() {
bool result = false;
do {
InitLogger();
if (apr_initialize() != APR_SUCCESS) {
result = false;
break;
@@ -67,6 +70,7 @@ bool Init() {
result = false;
break;
}
result = true;
} while (0);
@@ -94,6 +98,7 @@ void Uninit() {
g_thread = NULL;
}
UninitLogger();
apr_terminate();
}
@@ -103,3 +108,7 @@ bool Start() {
}
return false;
}
void SaveLoggerFile() {
is_save_log_file = true;
}