| 108 | } |
| 109 | |
| 110 | uint32_t NewGRFProfiler::Finish() |
| 111 | { |
| 112 | if (!this->active) return 0; |
| 113 | |
| 114 | if (this->calls.empty()) { |
| 115 | IConsolePrint(CC_DEBUG, "Finished profile of NewGRF [{:08X}], no events collected, not writing a file.", std::byteswap(this->grffile->grfid)); |
| 116 | |
| 117 | this->Abort(); |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | std::string filename = this->GetOutputFilename(); |
| 122 | IConsolePrint(CC_DEBUG, "Finished profile of NewGRF [{:08X}], writing {} events to '{}'.", std::byteswap(this->grffile->grfid), this->calls.size(), filename); |
| 123 | |
| 124 | uint32_t total_microseconds = 0; |
| 125 | |
| 126 | auto f = FioFOpenFile(filename, "wt", Subdirectory::NO_DIRECTORY); |
| 127 | |
| 128 | if (!f.has_value()) { |
| 129 | IConsolePrint(CC_ERROR, "Failed to open '{}' for writing.", filename); |
| 130 | } else { |
| 131 | fmt::print(*f, "Tick,Sprite,Feature,Item,CallbackID,Microseconds,Depth,Result\n"); |
| 132 | for (const Call &c : this->calls) { |
| 133 | fmt::print(*f, "{},{},0x{:X},{},0x{:X},{},{},{}\n", c.tick, c.root_sprite, c.feat, c.item, (uint)c.cb, c.time, c.subs, c.result); |
| 134 | total_microseconds += c.time; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | this->Abort(); |
| 139 | return total_microseconds; |
| 140 | } |
| 141 | |
| 142 | void NewGRFProfiler::Abort() |
| 143 | { |
no test coverage detected