From ada9342240f8a1b53bd92953f83034724302971b Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sun, 6 Oct 2024 22:15:43 +0100 Subject: [PATCH] Debug: Use fmt for assert_msg/assert_msg_tile --- src/CMakeLists.txt | 1 + src/blitter/32bpp_base.cpp | 5 +++-- src/cachecheck.cpp | 8 ++++---- src/cargopacket.cpp | 17 +++++++++++++++++ src/cargopacket.h | 17 +++++++---------- src/command.cpp | 4 ++-- src/core/pool_type.hpp | 7 ++++--- src/debug.cpp | 27 +++++++++++++++++++++++++++ src/debug.h | 21 +++++++++++++++++++++ src/debug_dbg_assert.h | 22 ++++++++++++++++++++++ src/network/core/udp.cpp | 2 +- src/newgrf.cpp | 2 +- src/newgrf_text.cpp | 5 ----- src/openttd.cpp | 38 -------------------------------------- src/order_cmd.cpp | 12 ++++++------ src/pbs.cpp | 6 +++--- src/rail.h | 3 ++- src/roadstop.cpp | 4 ++-- src/settings.cpp | 6 +++--- src/settings_gui.cpp | 4 ++-- src/signal.cpp | 2 +- src/stdafx.h | 11 +---------- src/timetable_cmd.cpp | 5 +++-- src/train_cmd.cpp | 5 +++-- src/tunnel_map.cpp | 2 +- src/tunnelbridge_cmd.cpp | 2 +- 26 files changed, 138 insertions(+), 100 deletions(-) create mode 100644 src/debug_dbg_assert.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5926f525232..cbc20cb43cd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -126,6 +126,7 @@ add_files( date_type.h debug.cpp debug.h + debug_dbg_assert.h debug_desync.h debug_settings.h debug_tictoc.h diff --git a/src/blitter/32bpp_base.cpp b/src/blitter/32bpp_base.cpp index 040720ec329..61a5360913d 100644 --- a/src/blitter/32bpp_base.cpp +++ b/src/blitter/32bpp_base.cpp @@ -8,6 +8,7 @@ /** @file 32bpp_base.cpp Implementation of base for 32 bpp blitters. */ #include "../stdafx.h" +#include "../debug.h" #include "32bpp_base.hpp" #include "common.hpp" @@ -144,7 +145,7 @@ void Blitter_32bppBase::ScrollBuffer(void *video, int left, int top, int width, /* Decrease height and increase top */ top += scroll_y; height -= scroll_y; - assert_msg(height > 0, "%d, %d, %d, %d, %d, %d", left, top, width, height, scroll_x, scroll_y); + assert_msg(height > 0, "{}, {}, {}, {}, {}, {}", left, top, width, height, scroll_x, scroll_y); /* Adjust left & width */ if (scroll_x >= 0) { @@ -168,7 +169,7 @@ void Blitter_32bppBase::ScrollBuffer(void *video, int left, int top, int width, /* Decrease height. (scroll_y is <=0). */ height += scroll_y; - assert_msg(height > 0, "%d, %d, %d, %d, %d, %d", left, top, width, height, scroll_x, scroll_y); + assert_msg(height > 0, "{}, {}, {}, {}, {}, {}", left, top, width, height, scroll_x, scroll_y); /* Adjust left & width */ if (scroll_x >= 0) { diff --git a/src/cachecheck.cpp b/src/cachecheck.cpp index 2baafcd5b74..3350c335505 100644 --- a/src/cachecheck.cpp +++ b/src/cachecheck.cpp @@ -529,12 +529,12 @@ void CheckCaches(bool force_check, std::function log, Ch ValidateVehicleTickCaches(); for (Vehicle *v : Vehicle::Iterate()) { - if (v->Previous()) assert_msg(v->Previous()->Next() == v, "%u", v->index); - if (v->Next()) assert_msg(v->Next()->Previous() == v, "%u", v->index); + if (v->Previous()) assert_msg(v->Previous()->Next() == v, "{}", v->index); + if (v->Next()) assert_msg(v->Next()->Previous() == v, "{}", v->index); } for (const TemplateVehicle *tv : TemplateVehicle::Iterate()) { - if (tv->Prev()) assert_msg(tv->Prev()->Next() == tv, "%u", tv->index); - if (tv->Next()) assert_msg(tv->Next()->Prev() == tv, "%u", tv->index); + if (tv->Prev()) assert_msg(tv->Prev()->Next() == tv, "{}", tv->index); + if (tv->Next()) assert_msg(tv->Next()->Prev() == tv, "{}", tv->index); } { diff --git a/src/cargopacket.cpp b/src/cargopacket.cpp index 4322c7bfe77..e7029b60e8e 100644 --- a/src/cargopacket.cpp +++ b/src/cargopacket.cpp @@ -8,6 +8,7 @@ /** @file cargopacket.cpp Implementation of the cargo packets. */ #include "stdafx.h" +#include "debug.h" #include "station_base.h" #include "core/pool_func.hpp" #include "core/random_func.hpp" @@ -524,6 +525,22 @@ void VehicleCargoList::PopCargo(Taction action) } } +void VehicleCargoList::AssertCountConsistencyError() const +{ + assert_msg(this->action_counts[MTA_KEEP] + + this->action_counts[MTA_DELIVER] + + this->action_counts[MTA_TRANSFER] + + this->action_counts[MTA_LOAD] == this->count, + "{} + {} + {} + {} != {}, ({} in {} packets)", + this->action_counts[MTA_KEEP], + this->action_counts[MTA_DELIVER], + this->action_counts[MTA_TRANSFER], + this->action_counts[MTA_LOAD], + this->count, + this->RecalculateCargoTotal(), + this->packets.size()); +} + /** * Update the cached values to reflect the removal of this packet or part of it. * Decreases count, feeder share and periods_in_transit. diff --git a/src/cargopacket.h b/src/cargopacket.h index d0593cdc466..5771765eac7 100644 --- a/src/cargopacket.h +++ b/src/cargopacket.h @@ -403,6 +403,7 @@ class VehicleCargoList : public CargoList { return total; } + void AssertCountConsistencyError() const; public: /** @@ -410,18 +411,14 @@ class VehicleCargoList : public CargoList { */ inline void AssertCountConsistency() const { - assert_msg(this->action_counts[MTA_KEEP] + +#ifdef WITH_ASSERT + if (unlikely(this->action_counts[MTA_KEEP] + this->action_counts[MTA_DELIVER] + this->action_counts[MTA_TRANSFER] + - this->action_counts[MTA_LOAD] == this->count, - "%u + %u + %u + %u != %u, (%u in %u packets)", - this->action_counts[MTA_KEEP], - this->action_counts[MTA_DELIVER], - this->action_counts[MTA_TRANSFER], - this->action_counts[MTA_LOAD], - this->count, - this->RecalculateCargoTotal(), - (uint) this->packets.size()); + this->action_counts[MTA_LOAD] != this->count)) { + this->AssertCountConsistencyError(); + } +#endif } protected: diff --git a/src/command.cpp b/src/command.cpp index 038073948a2..26896858e15 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -1268,8 +1268,8 @@ CommandCost DoCommandPInternal(TileIndex tile, uint32_t p1, uint32_t p2, uint64_ * i.e. cost and error state are the same. */ if (!test_and_exec_can_differ) { assert_msg(res.GetCost() == res2.GetCost() && res.Failed() == res2.Failed(), - "Command: cmd: 0x%X (%s), Test: %s, Exec: %s", cmd, GetCommandName(cmd), - res.SummaryMessage(GB(cmd, 16, 16)).c_str(), res2.SummaryMessage(GB(cmd, 16, 16)).c_str()); // sanity check + "Command: cmd: 0x{:X} ({}), Test: {}, Exec: {}", cmd, GetCommandName(cmd), + res.SummaryMessage(GB(cmd, 16, 16)), res2.SummaryMessage(GB(cmd, 16, 16))); // sanity check } else if (res2.Failed()) { return_dcpi(res2); } diff --git a/src/core/pool_type.hpp b/src/core/pool_type.hpp index 12bbcd6ce8d..a2c36fb0cc2 100644 --- a/src/core/pool_type.hpp +++ b/src/core/pool_type.hpp @@ -11,6 +11,7 @@ #define POOL_TYPE_HPP #include "enum_type.hpp" +#include "debug_dbg_assert.h" #include /** Various types of a pool. */ @@ -119,7 +120,7 @@ struct Pool : PoolBase { inline PtrType &GetRawRef(size_t index) { - dbg_assert_msg(index < this->first_unused, "index: " PRINTF_SIZE ", first_unused: " PRINTF_SIZE ", name: %s", index, this->first_unused, this->name); + dbg_assert_msg(index < this->first_unused, "index: {}, first_unused: {}, name: {}", index, this->first_unused, this->name); return this->data[index]; } @@ -296,7 +297,7 @@ struct Pool : PoolBase { { if (p == nullptr) return; Titem *pn = static_cast(p); - dbg_assert_msg(pn == Tpool->Get(pn->index), "name: %s", Tpool->name); + dbg_assert_msg(pn == Tpool->Get(pn->index), "name: {}", Tpool->name); Tpool->FreeItem(pn->index); } @@ -329,7 +330,7 @@ struct Pool : PoolBase { * memory are the same (because of possible inheritance). * Use { size_t index = item->index; delete item; new (index) item; } * instead to make sure destructor is called and no memory leaks. */ - dbg_assert_msg(ptr != Tpool->data[i], "name: %s", Tpool->name); + dbg_assert_msg(ptr != Tpool->data[i], "name: {}", Tpool->name); } return ptr; } diff --git a/src/debug.cpp b/src/debug.cpp index 7c3ad097dcf..c6762ed5793 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -18,6 +18,7 @@ #include "settings_type.h" #include "date_func.h" #include "thread.h" +#include "map_func.h" #include #include @@ -449,3 +450,29 @@ void TicToc::PrintAndReset() this->state.count = 0; this->state.chrono_sum = 0; } + +[[noreturn]] void AssertMsgErrorVFmt(int line, const char *file, const char *expr, fmt::string_view msg, fmt::format_args args) +{ + format_buffer out; + out.vformat(msg, args); + + assert_str_error(line, file, expr, out); +} + +[[noreturn]] void AssertMsgTileErrorVFmt(int line, const char *file, const char *expr, uint32_t tile, fmt::string_view msg, fmt::format_args args) +{ + format_buffer out; + DumpTileInfo(out, tile); + out.append(", "); + out.vformat(msg, args); + + assert_str_error(line, file, expr, out); +} + +void assert_tile_error(int line, const char *file, const char *expr, uint32_t tile) +{ + format_buffer out; + DumpTileInfo(out, tile); + + assert_str_error(line, file, expr, out); +} diff --git a/src/debug.h b/src/debug.h index 7d6200e8343..d7be4ccd5a7 100644 --- a/src/debug.h +++ b/src/debug.h @@ -116,4 +116,25 @@ void DumpDesyncMsgLog(struct format_target &buffer); void DebugSendRemoteMessages(); void DebugReconsiderSendRemoteMessages(); +template +[[noreturn]] void AssertMsgError(int line, const char *file, const char *expr, fmt::format_string msg, T&&... args) +{ + [[noreturn]] extern void AssertMsgErrorVFmt(int line, const char *file, const char *expr, fmt::string_view msg, fmt::format_args args); + AssertMsgErrorVFmt(line, file, expr, msg, fmt::make_format_args(args...)); +} +template +[[noreturn]] void AssertMsgTileError(int line, const char *file, const char *expr, uint32_t tile, fmt::format_string msg, T&&... args) +{ + [[noreturn]] extern void AssertMsgTileErrorVFmt(int line, const char *file, const char *expr, uint32_t tile, fmt::string_view msg, fmt::format_args args); + AssertMsgTileErrorVFmt(line, file, expr, tile, msg, fmt::make_format_args(args...)); +} + +#if !defined(NDEBUG) || defined(WITH_ASSERT) +# define assert_msg(expression, ...) do { if (unlikely(!(expression))) AssertMsgError(__LINE__, __FILE__, #expression, __VA_ARGS__); } while (false) +# define assert_msg_tile(expression, tile, ...) do { if (unlikely(!(expression))) AssertMsgTileError(__LINE__, __FILE__, #expression, tile, __VA_ARGS__); } while (false) +#else +# define assert_msg(expression, ...) +# define assert_msg_tile(expression, tile, ...) +#endif + #endif /* DEBUG_H */ diff --git a/src/debug_dbg_assert.h b/src/debug_dbg_assert.h new file mode 100644 index 00000000000..f440b906c50 --- /dev/null +++ b/src/debug_dbg_assert.h @@ -0,0 +1,22 @@ +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file debug_dbg_assert.h Macros for normally-off extended debug asserts. */ + +#ifndef DEBUG_DBG_ASSERT_H +#define DEBUG_DBG_ASSERT_H + +#ifdef WITH_FULL_ASSERTS +# include "debug.h" +# define dbg_assert_msg(expression, ...) assert_msg(expression, __VA_ARGS__) +# define dbg_assert_msg_tile(expression, tile, ...) assert_msg_tile(expression, tile, __VA_ARGS__) +#else +# define dbg_assert_msg(expression, ...) +# define dbg_assert_msg_tile(expression, tile, ...) +#endif + +#endif /* DEBUG_DBG_ASSERT_H */ diff --git a/src/network/core/udp.cpp b/src/network/core/udp.cpp index 0cd34182f4c..5d4ff9c3f71 100644 --- a/src/network/core/udp.cpp +++ b/src/network/core/udp.cpp @@ -103,7 +103,7 @@ void NetworkUDPSocketHandler::SendPacket(Packet &p, NetworkAddress &recv, bool a this->SendPacket(frag, recv, all, broadcast, short_mtu); frag.ResetState(PACKET_UDP_EX_MULTI); } - assert_msg(current_frag == frag_count, "%u, %u", current_frag, frag_count); + assert_msg(current_frag == frag_count, "{}, {}", current_frag, frag_count); return; } diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 35ec612b387..43cf22ef4b0 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -11872,7 +11872,7 @@ void LoadNewGRF(uint load_index, uint num_baseset) SetBit(c->flags, GCF_RESERVED); } else if (stage == GLS_ACTIVATION) { ClrBit(c->flags, GCF_RESERVED); - assert_msg(GetFileByGRFID(c->ident.grfid) == _cur.grffile, "%08X", BSWAP32(c->ident.grfid)); + assert_msg(GetFileByGRFID(c->ident.grfid) == _cur.grffile, "{:08X}", BSWAP32(c->ident.grfid)); ClearTemporaryNewGRFData(_cur.grffile); BuildCargoTranslationMap(); HandleVarAction2OptimisationPasses(); diff --git a/src/newgrf_text.cpp b/src/newgrf_text.cpp index d6284b927a5..8cbdfed717f 100644 --- a/src/newgrf_text.cpp +++ b/src/newgrf_text.cpp @@ -695,11 +695,6 @@ const char *GetDefaultLangGRFStringFromGRFText(const GRFTextWrapper &text) */ const char *GetGRFStringPtr(uint32_t stringid) { -#if 0 - assert_msg(stringid < _grf_text.size(), "stringid: %u, size: %u", stringid, (uint)_grf_text.size()); - assert_msg(_grf_text[stringid].grfid != 0, "stringid: %u", stringid); -#endif - if (stringid >= _grf_text.size() || _grf_text[stringid].grfid == 0) { Debug(misc, 0, "Invalid NewGRF string ID: {}", stringid); return "(invalid StringID)"; diff --git a/src/openttd.cpp b/src/openttd.cpp index 81280dbe7ce..130521ea785 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -186,44 +186,6 @@ void FatalErrorI(const std::string &str) fatalerror_common(str.c_str()); } -void CDECL assert_msg_error(int line, const char *file, const char *expr, const char *str, ...) -{ - if (CrashLog::HaveAlreadyCrashed()) DoOSAbort(); - - char buf[2048]; - - va_list va; - va_start(va, str); - vseprintf(buf, lastof(buf), str, va); - va_end(va); - - assert_str_error(line, file, expr, buf); -} - -void CDECL assert_msg_tile_error(int line, const char *file, const char *expr, uint32_t tile, const char *str, ...) -{ - char buf[2048]; - - format_to_fixed_z out(buf, lastof(buf)); - DumpTileInfo(out, tile); - out.append(", "); - - va_list va; - va_start(va, str); - vseprintf(out.finalise(), lastof(buf), str, va); - va_end(va); - - assert_str_error(line, file, expr, buf); -} - -void assert_tile_error(int line, const char *file, const char *expr, uint32_t tile) -{ - format_buffer out; - DumpTileInfo(out, tile); - - assert_str_error(line, file, expr, out); -} - /** * Show the help message when someone passed a wrong parameter. */ diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp index ee157c13dc3..4713472672a 100644 --- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -888,16 +888,16 @@ void OrderList::DebugCheckSanity() const check_total_duration += o->GetWaitTime() + o->GetTravelTime(); } } - assert_msg(this->GetNumOrders() == check_num_orders, "%u, %u", (uint) this->GetNumOrders(), check_num_orders); - assert_msg(this->num_manual_orders == check_num_manual_orders, "%u, %u", this->num_manual_orders, check_num_manual_orders); - assert_msg(this->timetable_duration == check_timetable_duration, "%u, %u", this->timetable_duration, check_timetable_duration); - assert_msg(this->total_duration == check_total_duration, "%u, %u", this->total_duration, check_total_duration); + assert_msg(this->GetNumOrders() == check_num_orders, "{}, {}", this->GetNumOrders(), check_num_orders); + assert_msg(this->num_manual_orders == check_num_manual_orders, "{}, {}", this->num_manual_orders, check_num_manual_orders); + assert_msg(this->timetable_duration == check_timetable_duration, "{}, {}", this->timetable_duration, check_timetable_duration); + assert_msg(this->total_duration == check_total_duration, "{}, {}", this->total_duration, check_total_duration); for (const Vehicle *v = this->first_shared; v != nullptr; v = v->NextShared()) { ++check_num_vehicles; - assert_msg(v->orders == this, "%p, %p", v->orders, this); + assert_msg(v->orders == this, "{}, {}", fmt::ptr(v->orders), fmt::ptr(this)); } - assert_msg(this->num_vehicles == check_num_vehicles, "%u, %u", this->num_vehicles, check_num_vehicles); + assert_msg(this->num_vehicles == check_num_vehicles, "{}, {}", this->num_vehicles, check_num_vehicles); Debug(misc, 6, "... detected {} orders ({} manual), {} vehicles, {} timetabled, {} total", this->GetNumOrders(), this->num_manual_orders, this->num_vehicles, this->timetable_duration, this->total_duration); diff --git a/src/pbs.cpp b/src/pbs.cpp index 4a046da6f45..53564d1899d 100644 --- a/src/pbs.cpp +++ b/src/pbs.cpp @@ -107,7 +107,7 @@ bool TryReserveRailTrackdir(const Train *v, TileIndex tile, Trackdir td, bool tr bool TryReserveRailTrack(TileIndex tile, Track track, bool trigger_stations) { assert_msg_tile((TrackdirBitsToTrackBits(GetTileTrackdirBits(tile, TRANSPORT_RAIL, 0)) & TrackToTrackBits(track)) != 0, tile, - "%X, %X, %X", TrackdirBitsToTrackBits(GetTileTrackdirBits(tile, TRANSPORT_RAIL, 0)), track, TrackToTrackBits(track)); + "{:X}, {:X}, {:X}", TrackdirBitsToTrackBits(GetTileTrackdirBits(tile, TRANSPORT_RAIL, 0)), track, TrackToTrackBits(track)); if (_settings_client.gui.show_track_reservation) { /* show the reserved rail if needed */ @@ -203,7 +203,7 @@ void UnreserveRailTrackdir(TileIndex tile, Trackdir td) */ void UnreserveRailTrack(TileIndex tile, Track t) { - assert_msg_tile(TrackdirBitsToTrackBits(GetTileTrackdirBits(tile, TRANSPORT_RAIL, 0)) & TrackToTrackBits(t), tile, "track: %u", t); + assert_msg_tile(TrackdirBitsToTrackBits(GetTileTrackdirBits(tile, TRANSPORT_RAIL, 0)) & TrackToTrackBits(t), tile, "track: {:X}", t); if (_settings_client.gui.show_track_reservation) { if (IsTileType(tile, MP_TUNNELBRIDGE)) { @@ -1310,7 +1310,7 @@ void FillTrainReservationLookAhead(Train *v) */ Train *GetTrainForReservation(TileIndex tile, Track track) { - assert_msg_tile(HasReservedTracks(tile, TrackToTrackBits(track)), tile, "track: %u", track); + assert_msg_tile(HasReservedTracks(tile, TrackToTrackBits(track)), tile, "track: {:X}", track); Trackdir trackdir = TrackToTrackdir(track); RailTypes rts = GetRailTypeInfo(GetTileRailTypeByTrack(tile, track))->all_compatible_railtypes; diff --git a/src/rail.h b/src/rail.h index a4b40dddd5e..f36eb3c5be6 100644 --- a/src/rail.h +++ b/src/rail.h @@ -21,6 +21,7 @@ #include "signal_type.h" #include "rail_map.h" #include "settings_type.h" +#include "debug_dbg_assert.h" #include /** Railtype flag bit numbers. */ @@ -331,7 +332,7 @@ class RailTypeInfo { inline const RailTypeInfo *GetRailTypeInfo(RailType railtype) { extern RailTypeInfo _railtypes[RAILTYPE_END]; - dbg_assert_msg(railtype < RAILTYPE_END, "%u", railtype); + dbg_assert_msg(railtype < RAILTYPE_END, "{}", railtype); return &_railtypes[railtype]; } diff --git a/src/roadstop.cpp b/src/roadstop.cpp index 5dbdeaa4c39..1024ef71ec3 100644 --- a/src/roadstop.cpp +++ b/src/roadstop.cpp @@ -497,10 +497,10 @@ void RoadStop::Entry::CheckIntegrity(const RoadStop *rs) const if (!HasBit(rs->status, RSSFB_BASE_ENTRY)) return; /* The tile 'before' the road stop must not be part of this 'line' */ - assert_msg(!IsDriveThroughRoadStopContinuation(rs->xy, rs->xy - abs(TileOffsByDiagDir(GetRoadStopDir(rs->xy)))), "xy: %X, index: %u", rs->xy, rs->index); + assert_msg(!IsDriveThroughRoadStopContinuation(rs->xy, rs->xy - abs(TileOffsByDiagDir(GetRoadStopDir(rs->xy)))), "xy: {:X}, index: {}", rs->xy, rs->index); Entry temp; temp.Rebuild(rs, rs->east == this); - assert_msg(temp.length == this->length && temp.occupied == this->occupied, "length: %u == %u, occupied: %u == %u, xy: %X, index: %u", + assert_msg(temp.length == this->length && temp.occupied == this->occupied, "length: {} == {}, occupied: {} == {}, xy: {:X}, index: {}", temp.length, this->length, temp.occupied, this->occupied, rs->xy, rs->index); } diff --git a/src/settings.cpp b/src/settings.cpp index a741a2b669f..d4b8fa206bd 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1052,7 +1052,7 @@ SettingType SettingDesc::GetType() const */ const IntSettingDesc *SettingDesc::AsIntSetting() const { - assert_msg(this->IsIntSetting(), "name: %s", this->name); + assert_msg(this->IsIntSetting(), "name: {}", this->name); return static_cast(this); } @@ -1062,7 +1062,7 @@ const IntSettingDesc *SettingDesc::AsIntSetting() const */ const StringSettingDesc *SettingDesc::AsStringSetting() const { - assert_msg(this->IsStringSetting(), "name: %s", this->name); + assert_msg(this->IsStringSetting(), "name: {}", this->name); return static_cast(this); } @@ -3585,7 +3585,7 @@ static void LoadSettings(std::initializer_list settings, std::init for (const SettingsCompat &c : compat) { if (c.type == SettingsCompatType::Setting || c.type == SettingsCompatType::Xref) { auto iters = names.equal_range(c.name); - assert_msg(iters.first != iters.second, "Setting: %s", c.name.c_str()); + assert_msg(iters.first != iters.second, "Setting: {}", c.name); for (auto it = iters.first; it != iters.second; ++it) { items.push_back({ c, it->second }); } diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index 4c3f9721ab9..3af72ba02e1 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -1572,7 +1572,7 @@ void SettingEntry::Init(uint8_t level) { BaseSettingEntry::Init(level); const SettingDesc *st = GetSettingFromName(this->name); - assert_msg(st != nullptr, "name: %s", this->name); + assert_msg(st != nullptr, "name: {}", this->name); this->setting = st->AsIntSetting(); } @@ -3146,7 +3146,7 @@ struct GameSettingsWindow : Window { val = data.val; } } - assert_msg(val >= sd->min && val <= (int)sd->max, "min: %d, max: %d, val: %d", sd->min, sd->max, val); + assert_msg(val >= sd->min && val <= (int)sd->max, "min: {}, max: {}, val: {}", sd->min, sd->max, val); sd->SetValueDParams(0, val); list.push_back(MakeDropDownListStringItem(STR_JUST_STRING2, val, false)); } diff --git a/src/signal.cpp b/src/signal.cpp index c8b1a5e8def..42f23aba905 100644 --- a/src/signal.cpp +++ b/src/signal.cpp @@ -832,7 +832,7 @@ static void UpdateSignalsAroundSegment(SigInfo info) continue; } - assert_msg_tile(HasSignalOnTrackdir(tile, trackdir), tile, "trackdir: %u", trackdir); + assert_msg_tile(HasSignalOnTrackdir(tile, trackdir), tile, "trackdir: {}", trackdir); track = TrackdirToTrack(trackdir); SignalType sig = GetSignalType(tile, track); diff --git a/src/stdafx.h b/src/stdafx.h index 58ff63bf1f1..df815cb650e 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -389,11 +389,9 @@ struct fmt_formattable{}; /* cpp-btree: Don't include IO stream headers, dump support */ #define BTREE_NO_IOSTREAM -[[noreturn]] void CDECL assert_msg_error(int line, const char *file, const char *expr, const char *str, ...) WARN_FORMAT(4, 5); [[noreturn]] void assert_str_error(int line, const char *file, const char *expr, std::string_view str); [[noreturn]] void assert_str_error(int line, const char *file, const char *expr, const char *str); [[noreturn]] void assert_str_error(int line, const char *file, const char *expr); -[[noreturn]] void CDECL assert_msg_tile_error(int line, const char *file, const char *expr, uint32_t tile, const char *str, ...) WARN_FORMAT(5, 6); [[noreturn]] void assert_tile_error(int line, const char *file, const char *expr, uint32_t tile); [[noreturn]] void not_reached_error(int line, const char *file); #define NOT_REACHED() not_reached_error(__LINE__, __FILE__); @@ -402,31 +400,24 @@ struct fmt_formattable{}; #if !defined(NDEBUG) || defined(WITH_ASSERT) # undef assert # define assert(expression) do { if (unlikely(!(expression))) assert_str_error(__LINE__, __FILE__, #expression); } while (false) -# define assert_msg(expression, ...) do { if (unlikely(!(expression))) assert_msg_error(__LINE__, __FILE__, #expression, __VA_ARGS__); } while (false) -# define assert_msg_tile(expression, tile, ...) do { if (unlikely(!(expression))) assert_msg_tile_error(__LINE__, __FILE__, #expression, tile, __VA_ARGS__); } while (false) # define assert_tile(expression, tile) do { if (unlikely(!(expression))) assert_tile_error(__LINE__, __FILE__, #expression, tile); } while (false) # define assert_str(expression, str) do { if (unlikely(!(expression))) assert_str_error(__LINE__, __FILE__, #expression, str); } while (false) #else # undef assert # define assert(expression) -# define assert_msg(expression, ...) -# define assert_msg_tile(expression, tile, ...) # define assert_tile(expression, tile) # define assert_str(expression, str) #endif #if (!defined(NDEBUG) || defined(WITH_ASSERT)) && !defined(FEWER_ASSERTS) # define WITH_FULL_ASSERTS # define dbg_assert(expression) assert(expression) -# define dbg_assert_msg(expression, ...) assert_msg(expression, __VA_ARGS__) -# define dbg_assert_msg_tile(expression, tile, ...) assert_msg_tile(expression, tile, __VA_ARGS__) # define dbg_assert_tile(expression, tile) assert_tile(expression, tile) #else # define dbg_assert(expression) -# define dbg_assert_msg(expression, ...) -# define dbg_assert_msg_tile(expression, tile, ...) # define dbg_assert_tile(expression, tile) #endif + /* Define JSON_ASSERT, which is used by nlohmann-json. Otherwise the header-file * will re-include assert.h, and reset the assert macro. */ #define JSON_ASSERT(x) assert(x) diff --git a/src/timetable_cmd.cpp b/src/timetable_cmd.cpp index e89d8da2c10..24a23131f0c 100644 --- a/src/timetable_cmd.cpp +++ b/src/timetable_cmd.cpp @@ -12,6 +12,7 @@ #include "company_func.h" #include "date_func.h" #include "date_type.h" +#include "debug.h" #include "window_func.h" #include "vehicle_base.h" #include "settings_type.h" @@ -66,7 +67,7 @@ static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint32_t va total_delta = val - order->GetTravelTime(); timetable_delta = (timetabled ? val : 0) - order->GetTimetabledTravel(); } - if (order->IsType(OT_CONDITIONAL)) assert_msg(val == order->GetTravelTime(), "%u == %u", val, order->GetTravelTime()); + if (order->IsType(OT_CONDITIONAL)) assert_msg(val == order->GetTravelTime(), "{} == {}", val, order->GetTravelTime()); order->SetTravelTime(val); order->SetTravelTimetabled(timetabled); break; @@ -987,7 +988,7 @@ void UpdateVehicleTimetable(Vehicle *v, bool travelling) return; } } else { - assert_msg(real_timetable_order == real_current_order, "%u, %u", v->cur_real_order_index, v->cur_timetable_order_index); + assert_msg(real_timetable_order == real_current_order, "{}, {}", v->cur_real_order_index, v->cur_timetable_order_index); } if (just_started) return; diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 2a6929fa208..83a8f07f6dd 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -42,6 +42,7 @@ #include "scope_info.h" #include "scope.h" #include "core/checksum_func.hpp" +#include "debug_dbg_assert.h" #include "debug_settings.h" #include "train_speed_adaptation.h" #include "event_logs.h" @@ -280,7 +281,7 @@ void Train::ConsistChanged(ConsistChangeFlags allowed_changes) const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type); /* Check the this->first cache. */ - dbg_assert_msg(u->First() == this, "u: %s, this: %s", + dbg_assert_msg(u->First() == this, "u: {}, this: {}", scope_dumper().VehicleInfo(u), scope_dumper().VehicleInfo(this)); /* update the 'first engine' */ @@ -5696,7 +5697,7 @@ bool TrainController(Train *v, Vehicle *nomove, bool reverse) ChooseTrainTrackResult result = ChooseTrainTrack(v, gp.new_tile, enterdir, bits, CTTF_MARK_STUCK | CTTF_NON_LOOKAHEAD); chosen_track = TrackToTrackBits(result.track); reverse_at_signal = (result.ctt_flags & CTTRF_REVERSE_AT_SIGNAL); - dbg_assert_msg_tile(chosen_track & (bits | GetReservedTrackbits(gp.new_tile)), gp.new_tile, "0x%X, 0x%X, 0x%X", chosen_track, bits, GetReservedTrackbits(gp.new_tile)); + dbg_assert_msg_tile(chosen_track & (bits | GetReservedTrackbits(gp.new_tile)), gp.new_tile, "0x{:X}, 0x{:X}, 0x{:X}", chosen_track, bits, GetReservedTrackbits(gp.new_tile)); if (v->force_proceed != TFP_NONE && IsPlainRailTile(gp.new_tile) && HasSignals(gp.new_tile)) { /* For each signal we find decrease the counter by one. diff --git a/src/tunnel_map.cpp b/src/tunnel_map.cpp index c613d981d30..94955d65244 100644 --- a/src/tunnel_map.cpp +++ b/src/tunnel_map.cpp @@ -86,7 +86,7 @@ void Tunnel::PreCleanPool() TunnelID GetTunnelIndexByLookup(TileIndex t) { auto iter = tunnel_tile_index_map.find(t); - assert_msg(iter != tunnel_tile_index_map.end(), "tile: 0x%X", t); + assert_tile(iter != tunnel_tile_index_map.end(), t); return iter->second; } diff --git a/src/tunnelbridge_cmd.cpp b/src/tunnelbridge_cmd.cpp index e4d6336b2d2..3785e7d55c3 100644 --- a/src/tunnelbridge_cmd.cpp +++ b/src/tunnelbridge_cmd.cpp @@ -3209,7 +3209,7 @@ static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex ti if (frame == _tunnel_visibility_frame[dir]) { /* Frame should be equal to the next frame number in the RV's movement */ assert_msg(frame == rv->frame + 1 || rv->frame == _tunnel_turnaround_pre_visibility_frame[dir], - "frame: %u, rv->frame: %u, dir: %u, _tunnel_turnaround_pre_visibility_frame[dir]: %u", frame, rv->frame, dir, _tunnel_turnaround_pre_visibility_frame[dir]); + "frame: {}, rv->frame: {}, dir: {}, _tunnel_turnaround_pre_visibility_frame[dir]: {}", frame, rv->frame, dir, _tunnel_turnaround_pre_visibility_frame[dir]); rv->tile = tile; rv->InvalidateImageCache(); rv->state = RVSB_WORMHOLE;