Set up logging system for CarlaServer

This commit is contained in:
nsubiron 2017-06-01 12:57:10 +02:00
parent 663ab17924
commit 7c5881ac13
8 changed files with 145 additions and 49 deletions

View File

@ -7,6 +7,7 @@
#include <iostream>
#include <carla/Logging.h>
#include <carla/server/CarlaCommunication.h>
namespace carla {
@ -142,7 +143,7 @@ namespace carla {
if (str.size() < std::numeric_limits<uint32_t>::max()) {
init_file.copyContents(str.data(), static_cast<uint32_t>(str.size()));
} else {
std::cerr << "Received string is too big!" << std::endl;
log_error("Received string is too big!");
}
return true;

View File

@ -0,0 +1,87 @@
// CARLA, Copyright (C) 2017 Computer Vision Center (CVC)
#pragma once
#define LOG_LEVEL_DEBUG 10
#define LOG_LEVEL_INFO 20
#define LOG_LEVEL_ERROR 30
#define LOG_LEVEL_CRITICAL 40
#define LOG_LEVEL_NONE 100
// Choose here the log level.
#define LOG_LEVEL LOG_LEVEL_ERROR
#include <iostream>
namespace carla {
namespace detail {
// https://stackoverflow.com/a/27375675
template <typename Arg, typename ... Args>
void print_args(std::ostream &out, Arg &&arg, Args &&... args) {
out << std::forward<Arg>(arg);
using expander = int[];
(void)expander{0, (void(out << ' ' << std::forward<Args>(args)),0)...};
}
} // namespace detail
#if LOG_LEVEL <= LOG_LEVEL_DEBUG
template <typename ... Args>
static inline void log_debug(Args &&... args) {
detail::print_args(std::cout, "DEBUG:", std::forward<Args>(args)..., '\n');
}
#else
template <typename ... Args>
static inline void log_debug(Args &&...) {}
#endif
#if LOG_LEVEL <= LOG_LEVEL_INFO
template <typename ... Args>
static inline void log_info(Args &&... args) {
detail::print_args(std::cout, "INFO:", std::forward<Args>(args)..., '\n');
}
#else
template <typename ... Args>
static inline void log_info(Args &&...) {}
#endif
#if LOG_LEVEL <= LOG_LEVEL_ERROR
template <typename ... Args>
static inline void log_error(Args &&... args) {
detail::print_args(std::cerr, "ERROR:", std::forward<Args>(args)..., '\n');
}
#else
template <typename ... Args>
static inline void log_error(Args &&...) {}
#endif
#if LOG_LEVEL <= LOG_LEVEL_CRITICAL
template <typename ... Args>
static inline void log_critical(Args &&... args) {
detail::print_args(std::cerr, "CRITICAL:", std::forward<Args>(args)..., '\n');
}
#else
template <typename ... Args>
static inline void log_critical(Args &&...) {}
#endif
} // namespace carla

View File

@ -3,6 +3,8 @@
#include "carla_protocol.pb.h"
#include "../CarlaServer.h"
#include <carla/Logging.h>
#include <iostream>
namespace carla {
@ -12,7 +14,7 @@ namespace server {
template<typename ERROR_CODE>
static void logTCPError(const std::string &text, const ERROR_CODE &errorCode) {
std::cerr << "CarlaConnection - TCP Server: " << text << ": " << errorCode.message() << std::endl;
log_error("CarlaConnection - TCP Server:", text, ':', errorCode.message());
}
@ -43,8 +45,7 @@ namespace server {
double elapsed_secs = double(end- start) / CLOCKS_PER_SEC;
cout << "Send time: " << elapsed_secs << " (s)" << endl;
log_info("Send time:", elapsed_secs, "(s)");
bool correctSerialize = reward.SerializeToString(&message);
@ -102,7 +103,7 @@ namespace server {
static std::unique_ptr<std::string> worldReceiveThread(TCPServer &server, thread::AsyncReadWriteJobQueue<std::string, std::string> &thr) {
auto message = std::make_unique<std::string>();
bool success = false;
if (!thr.getRestart()) {
TCPServer::error_code error;
@ -144,7 +145,7 @@ namespace server {
}
static void Connect(TCPServer &server, CarlaCommunication &communication) {
std::cout << "Waiting... port: " << server.port << std::endl;
log_info("Waiting... port:", server.port);
server.AcceptSocket();
communication.checkRestart();
@ -157,22 +158,22 @@ namespace server {
if (!communication.needsRestart()) {
std::cout << " ---- RECONNECT ALL ...." << std::endl;
log_debug("---- RECONNECT ALL ....");
if (!communication.getWorldThread().getRestart()) {
std::cout << " ---- RESTART WORLD ...." << std::endl;
log_debug("---- RESTART WORLD ....");
communication.restartWorld();
communication.getWorldThread().restart();
}
if (!communication.getServerThread().getRestart()) {
std::cout << " ---- RESTART SERVER ...." << std::endl;
log_debug("---- RESTART SERVER ....");
communication.restartServer();
communication.getServerThread().restart();
}
if (!communication.getClientThread().getRestart()) {
std::cout << " ---- RESTART CLIENT ...." << std::endl;
log_debug("---- RESTART CLIENT ....");
communication.restartClient();
communication.getClientThread().restart();
}
@ -310,8 +311,8 @@ namespace server {
bool CarlaCommunication::tryReadRequestNewEpisode(std::string &init_file) {
std::unique_ptr<std::string> request = _worldThread.tryPop();
if (request == nullptr) {
return false;
if (request == nullptr) {
return false;
}
RequestNewEpisode reqEpisode;
@ -321,13 +322,13 @@ namespace server {
_worldThread.undoPop(std::move(request));
return false;
} else {
} else {
init_file = reqEpisode.init_file();
std::cout << init_file << std::endl;
log_debug("Received init file:\n", init_file);
return true;
return true;
}
}

View File

@ -3,6 +3,8 @@
#include "CarlaCommunication.h"
#include "carla/CarlaServer.h"
#include <carla/Logging.h>
#include "carla_protocol.pb.h"
#include <iostream>
@ -122,7 +124,7 @@ namespace server {
if (image_info.empty())
return false;
if (image_info.size() != image_info.width() * image_info.height()) {
std::cerr << "Invalid image size" << std::endl;
log_error("Invalid image size:", image_info.size(), "!=", image_info.width(), '*', image_info.height());
return false;
}
@ -138,7 +140,7 @@ namespace server {
png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png) {
std::cerr << "Cannot create png_struct" << std::endl;
log_error("Cannot create png_struct");
return false;
}
@ -146,7 +148,7 @@ namespace server {
info = png_create_info_struct(png);
if (!info){
std::cerr << "Cannot create info_struct" << std::endl;
log_error("Cannot create info_struct");
return false;
}
@ -212,7 +214,7 @@ namespace server {
if (!GetImage(img, compressedImage)) {
std::cerr << "Error while encoding image" << std::endl;
log_error("Error while encoding image");
return false;
}
@ -271,12 +273,12 @@ namespace server {
static bool getBitMapImage(const Image &image_info, char* image){
if (image_info.empty()){
std::cerr << "Error: Empty image" << std::endl;
log_error("Empty image");
return false;
}
if (image_info.size() != image_info.width() * image_info.height()) {
std::cerr << "Invalid image size" << std::endl;
log_error("Invalid image size:", image_info.size(), "!=", image_info.width(), '*', image_info.height());
return false;
}
@ -305,7 +307,7 @@ static bool getBitMapImages(const Collection<Image> &images, Reward &rwd) {
char image[image_size];
if (!getBitMapImage(img, image)){
std::cerr << "Error while encoding image" << std::endl;
log_error("Error while encoding image");
return false;
}
@ -385,13 +387,13 @@ static bool getBitMapImages(const Collection<Image> &images, Reward &rwd) {
#ifdef CARLA_WITH_PNG_COMPRESSION
if (!getPNGImages(values.images, reward)) {
std::cerr << "Error compressing image to PNG" << std::endl;
log_error("Error compressing image to PNG");
}
#else
if (!getBitMapImages(values.images, reward)) {
std::cerr << "Error encoding bitmap image" << std::endl;
log_error("Error encoding bitmap image");
}
#endif // CARLA_WITH_PNG_COMPRESSION
@ -405,7 +407,7 @@ static bool getBitMapImages(const Collection<Image> &images, Reward &rwd) {
std::string positions_bytes = "";
std::cout << "------- POSITIONS--------" << std::endl;
log_debug("------- POSITIONS--------");
for (auto i = 0u; i < values.possible_positions.size(); ++i) {
@ -415,13 +417,13 @@ static bool getBitMapImages(const Collection<Image> &images, Reward &rwd) {
positions_bytes += GetBytes(x) + GetBytes(y);
std::cout << "x: " << x << " byte size: " << sizeof(float) << " bytes: " << GetBytes(x) << std::endl;
std::cout << "y: " << y << " byte size: " << sizeof(float) << " bytes: " << GetBytes(y) << std::endl;
log_debug("x:", x, "byte size:", sizeof(float), "bytes:", GetBytes(x));
log_debug("y:", y, "byte size:", sizeof(float), "bytes:", GetBytes(y));
}
std::cout << "---------------" << std::endl;
std::cout << "Final string: "<< positions_bytes << std::endl;
std::cout << "---------------" << std::endl;
log_debug("---------------");
log_debug("Final string:", positions_bytes);
log_debug("---------------");
scene.set_positions(positions_bytes);

View File

@ -5,6 +5,8 @@
#include <fstream>
#include <iostream>
#include <carla/Logging.h>
namespace carla {
namespace server {
@ -55,11 +57,13 @@ namespace carla {
boost::asio::ip::tcp::acceptor acceptor(_service, tcp::endpoint(tcp::v4(), port));
acceptor.accept(_socket);
_service.run();
std::cout << "Connected port " << port << std::endl;
log_info("Connected port", port);;
_connected = true;
}
catch (boost::system::system_error) {std::cerr<<"Socket System error"<<std::endl;};
catch (const boost::system::system_error &e) {
log_error("Socket System error: ", e.what());
};
}
bool TCPServer::Connected() {
@ -73,7 +77,7 @@ namespace carla {
if (error)
{
std::cout << "DESCONECTED port " << port << std::endl;
log_info("DESCONECTED port", port);
_connected = false;
}
}
@ -83,7 +87,7 @@ namespace carla {
bool end = false, readedBytes = false;
int readedSize = 0, sizeToRead = -1;
std::cout << "Try to read " << std::endl;
log_debug("Try to read");
//if (_socket.available() > 0){
do {
@ -94,7 +98,7 @@ namespace carla {
if (error)
{
std::cout << "DESCONECTED port " << port << std::endl;
log_info("DESCONECTED port", port);
_connected = false;
}
else if (!error){
@ -115,7 +119,7 @@ namespace carla {
} while ((!readedBytes || sizeToRead > readedSize) && _connected);
std::cout << "End read" << std::endl;
log_debug("End read");
return true;
//}

View File

@ -2,6 +2,7 @@
#pragma once
#include <carla/Logging.h>
#include <carla/thread/ThreadSafeQueue.h>
#include <carla/thread/ThreadUniquePointer.h>
@ -38,7 +39,7 @@ namespace thread {
_thread(new std::thread(&AsyncReadWriteJobQueue::workerThread, this)) {}
~AsyncReadWriteJobQueue() {
std::cout << "Destroyed thread world"<< std::endl;
log_debug("Destroyed thread world");
done = true;
}
@ -47,11 +48,11 @@ namespace thread {
}
void undoPop(std::unique_ptr<W> value){
_writeQueue.push(std::move(value));
_writeQueue.push(std::move(value));
}
void push(std::unique_ptr<R> item) {
if (item != nullptr) _readQueue.push(std::move(item));
}
@ -83,7 +84,7 @@ namespace thread {
void workerThread() {
while(!done){
_connectJob();
_restart = false;
_restart = false;
_readQueue.canWait(true);
while (!_restart && !done) {
/* auto value = _readQueue.wait_and_pop();
@ -105,7 +106,7 @@ namespace thread {
}
}
std::atomic_bool _restart;
WritingJob _writeJob;

View File

@ -35,7 +35,7 @@ namespace thread {
_thread(new std::thread(&AsyncReaderJobQueue::workerThread, this)) {}
~AsyncReaderJobQueue() {
std::cout << "Destroyed thread client"<< std::endl;
log_debug("Destroyed thread client");
done = true;
}
@ -62,7 +62,7 @@ namespace thread {
}
std::atomic_bool done;
private:
void workerThread() {
while (!done){
@ -90,7 +90,7 @@ namespace thread {
}
}
std::atomic_bool _restart;
Job _job;

View File

@ -32,7 +32,7 @@ namespace thread {
{}
~AsyncWriterJobQueue() {
std::cout << "Destroyed thread server"<< std::endl;
log_debug("Destroyed thread server");
done = true;
}
@ -56,7 +56,7 @@ namespace thread {
void clear(){
_queue.clear();
}
std::atomic_bool done;
@ -70,13 +70,13 @@ namespace thread {
while (!_restart && !done) {
//_queue.wait_and_push(_job);
_queue.push(std::move(_job()));
_queue.push(std::move(_job()));
//Sleep(10);
}
}
}
std::atomic_bool _restart;
Job _job;