From d1204393463d9754bb8ccada9a8e7dd4242d22d0 Mon Sep 17 00:00:00 2001 From: nsubiron Date: Tue, 20 Jun 2017 15:15:57 +0100 Subject: [PATCH] Improve logging --- Source/Carla/AI/VehicleSpawnerBase.cpp | 6 ++-- Source/Carla/AI/WalkerAIController.cpp | 13 +++++-- Source/Carla/AI/WalkerSpawnerBase.cpp | 4 ++- Source/CarlaServer/source/carla/Logging.h | 34 ++++++++++++++++++- .../source/carla/thread/AsyncReaderJobQueue.h | 21 +++++------- 5 files changed, 58 insertions(+), 20 deletions(-) diff --git a/Source/Carla/AI/VehicleSpawnerBase.cpp b/Source/Carla/AI/VehicleSpawnerBase.cpp index 88dce39b3..66d0e7506 100644 --- a/Source/Carla/AI/VehicleSpawnerBase.cpp +++ b/Source/Carla/AI/VehicleSpawnerBase.cpp @@ -43,10 +43,10 @@ void AVehicleSpawnerBase::BeginPlay() TryToSpawnRandomVehicle(); ++NumberOfAttempts; } - } - if (NumberOfVehicles > Vehicles.Num()) { - UE_LOG(LogCarla, Error, TEXT("Requested %d vehicles, but we were only able to spawn %d"), NumberOfVehicles, Vehicles.Num()); + if (NumberOfVehicles > Vehicles.Num()) { + UE_LOG(LogCarla, Error, TEXT("Requested %d vehicles, but we were only able to spawn %d"), NumberOfVehicles, Vehicles.Num()); + } } } diff --git a/Source/Carla/AI/WalkerAIController.cpp b/Source/Carla/AI/WalkerAIController.cpp index 0db69f343..3887253e5 100644 --- a/Source/Carla/AI/WalkerAIController.cpp +++ b/Source/Carla/AI/WalkerAIController.cpp @@ -187,7 +187,12 @@ FPathFollowingRequestResult AWalkerAIController::MoveTo( const FAIMoveRequest& MoveRequest, FNavPathSharedPtr* OutPath) { - LOG_AI_WALKER(Log, "requested to move"); +#ifdef CARLA_AI_WALKERS_EXTRA_LOG + UE_LOG(LogCarla, Log, TEXT("Walker %s requested move from (%s) to (%s)"), + *GetPawn()->GetName(), + *GetPawn()->GetActorLocation().ToString(), + *MoveRequest.GetGoalLocation().ToString()); +#endif // CARLA_AI_WALKERS_EXTRA_LOG Status = EWalkerStatus::Moving; return Super::MoveTo(MoveRequest, OutPath); } @@ -197,7 +202,11 @@ void AWalkerAIController::OnMoveCompleted( const FPathFollowingResult &Result) { Super::OnMoveCompleted(RequestID, Result); - LOG_AI_WALKER(Log, "completed move"); +#ifdef CARLA_AI_WALKERS_EXTRA_LOG + UE_LOG(LogCarla, Log, TEXT("Walker %s completed move at (%s)"), + *GetPawn()->GetName(), + *GetPawn()->GetActorLocation().ToString()); +#endif // CARLA_AI_WALKERS_EXTRA_LOG Status = EWalkerStatus::MoveCompleted; } diff --git a/Source/Carla/AI/WalkerSpawnerBase.cpp b/Source/Carla/AI/WalkerSpawnerBase.cpp index b721893ee..1626fa042 100644 --- a/Source/Carla/AI/WalkerSpawnerBase.cpp +++ b/Source/Carla/AI/WalkerSpawnerBase.cpp @@ -162,7 +162,9 @@ void AWalkerSpawnerBase::SetNumberOfWalkers(const int32 Count) const AWalkerSpawnPointBase &AWalkerSpawnerBase::GetRandomSpawnPoint() { check(SpawnPoints.Num() > 0); - return *GetRandomEngine()->PickOne(SpawnPoints); + const auto *SpawnPoint = GetRandomEngine()->PickOne(SpawnPoints); + check(SpawnPoint != nullptr); + return *SpawnPoint; } bool AWalkerSpawnerBase::TryGetValidDestination(const FVector &Origin, FVector &Destination) diff --git a/Source/CarlaServer/source/carla/Logging.h b/Source/CarlaServer/source/carla/Logging.h index 48550fdb7..89b380e67 100644 --- a/Source/CarlaServer/source/carla/Logging.h +++ b/Source/CarlaServer/source/carla/Logging.h @@ -11,6 +11,22 @@ // Choose here the log level. #define LOG_LEVEL LOG_LEVEL_ERROR +// The following log functions are available, they are only active if LOG_LEVEL +// is greater equal the function's log level. +// +// * log_debug +// * log_info +// * log_error +// * log_critical +// +// And macros +// +// * LOG_DEBUG_ONLY(/* code here */) +// * LOG_INFO_ONLY(/* code here */) + +// ============================================================================= +// -- Implementation of log functions ------------------------------------------ +// ============================================================================= #include @@ -20,7 +36,7 @@ namespace detail { // https://stackoverflow.com/a/27375675 template - void print_args(std::ostream &out, Arg &&arg, Args &&... args) { + static void print_args(std::ostream &out, Arg &&arg, Args &&... args) { out << std::forward(arg); using expander = int[]; (void)expander{0, (void(out << ' ' << std::forward(args)),0)...}; @@ -85,3 +101,19 @@ namespace detail { #endif } // namespace carla + +// ============================================================================= +// -- Implementation of macros ------------------------------------------------- +// ============================================================================= + +#if LOG_LEVEL <= LOG_LEVEL_DEBUG +# define LOG_DEBUG_ONLY(code) code +#else +# define LOG_DEBUG_ONLY(code) +#endif + +#if LOG_LEVEL <= LOG_LEVEL_INFO +# define LOG_INFO_ONLY(code) code +#else +# define LOG_INFO_ONLY(code) +#endif diff --git a/Source/CarlaServer/source/carla/thread/AsyncReaderJobQueue.h b/Source/CarlaServer/source/carla/thread/AsyncReaderJobQueue.h index 69dfb834f..bceacfb4b 100644 --- a/Source/CarlaServer/source/carla/thread/AsyncReaderJobQueue.h +++ b/Source/CarlaServer/source/carla/thread/AsyncReaderJobQueue.h @@ -70,23 +70,18 @@ namespace thread { _restart = false; _queue.canWait(true); while (!_restart && !done) { - { - using namespace std; - clock_t start = clock(); + using namespace std; + LOG_DEBUG_ONLY(clock_t start = clock();) - auto value = _queue.wait_and_pop(); - if (value != nullptr) { - _job(*value); - - clock_t end = clock(); - double elapsed_secs = double(end- start) / CLOCKS_PER_SEC; - cout<< "Send Thread: " << elapsed_secs << endl; + auto value = _queue.wait_and_pop(); + if (value != nullptr) { + _job(*value); + LOG_DEBUG_ONLY(clock_t end = clock();) + LOG_DEBUG_ONLY(double elapsed_secs = double(end - start) / CLOCKS_PER_SEC;) + LOG_DEBUG_ONLY(log_debug("Send Thread:", elapsed_secs);) } - - }//Sleep(10); } - } }