enhance Exception logging

This commit is contained in:
falsycat 2022-08-25 11:55:29 +09:00
parent 0f655315de
commit 8e00af8dab
5 changed files with 57 additions and 5 deletions

View File

@ -1,5 +1,6 @@
#pragma once
#include <exception>
#include <memory>
#include <string>
#include <string_view>
@ -44,6 +45,8 @@ struct Logger::Item final {
File::Id file;
std::source_location srcloc;
std::exception_ptr ex;
};
} // namespace nf7

View File

@ -1,6 +1,7 @@
#pragma once
#include <cassert>
#include <exception>
#include <memory>
#include <mutex>
#include <source_location>
@ -53,16 +54,26 @@ class LoggerRef final : public nf7::FileBase::Feature {
void Info(std::string_view msg, std::source_location s = std::source_location::current()) noexcept {
Write({nf7::Logger::kInfo, msg, 0, s});
}
void Info(nf7::Exception& e, std::source_location s = std::source_location::current()) noexcept {
Info(e.StringifyRecursive(), s);
}
void Warn(std::string_view msg, std::source_location s = std::source_location::current()) noexcept {
Write({nf7::Logger::kWarn, msg, 0, s});
}
void Warn(nf7::Exception& e, std::source_location s = std::source_location::current()) noexcept {
Warn(e.StringifyRecursive(), s);
}
void Error(std::string_view msg, std::source_location s = std::source_location::current()) noexcept {
Write({nf7::Logger::kError, msg, 0, s});
}
void Error(nf7::Exception& e, std::source_location s = std::source_location::current()) noexcept {
Error(e.StringifyRecursive(), s);
}
void Write(nf7::Logger::Item&& item) noexcept {
std::unique_lock<std::mutex> k(mtx_);
if (!id_ || !logger_) return;
item.file = id_;
item.ex = std::current_exception();
logger_->Write(std::move(item));
}

View File

@ -1,6 +1,7 @@
#include <atomic>
#include <cinttypes>
#include <deque>
#include <exception>
#include <memory>
#include <mutex>
#include <typeinfo>
@ -47,6 +48,7 @@ class Logger final : public nf7::File,
std::string msg;
std::string path;
std::string location;
std::exception_ptr ex;
std::string Stringify() const noexcept {
std::stringstream st;
@ -136,7 +138,7 @@ class Logger final : public nf7::File,
}
}
static std::string GetLocationString(const std::source_location loc) noexcept {
return loc.file_name()+":"s+loc.function_name()+":"s+std::to_string(loc.line());
return loc.file_name()+":"s+std::to_string(loc.line());
}
};
@ -186,6 +188,7 @@ class Logger::ItemStore final : public nf7::Context,
.msg = std::move(itr->msg),
.path = owner.GetPathString(itr->file),
.location = GetLocationString(itr->srcloc),
.ex = itr->ex,
};
rows.push_back(std::move(row));
}
@ -322,10 +325,19 @@ void Logger::Update() noexcept {
ImGui::TextUnformatted(row.location.c_str());
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
ImGui::Text("file: %s", row.srcloc.file_name());
ImGui::Text("func: %s", row.srcloc.function_name());
ImGui::Text("line: %zu", static_cast<size_t>(row.srcloc.line()));
ImGui::Text("col : %zu", static_cast<size_t>(row.srcloc.column()));
ImGui::Text(row.location.c_str());
for (auto ptr = row.ex; ptr;)
try {
ImGui::Bullet();
std::rethrow_exception(ptr);
} catch (Exception& e) {
e.UpdatePanic();
ImGui::Spacing();
ptr = e.reason();
} catch (std::exception& e) {
ImGui::Text("std::exception (%s)", e.what());
ptr = nullptr;
}
ImGui::EndTooltip();
}
}

23
nf7.cc
View File

@ -3,6 +3,7 @@
#include <algorithm>
#include <cassert>
#include <map>
#include <sstream>
#include <imgui.h>
#include <yas/serialize.hpp>
@ -27,10 +28,32 @@ static inline auto& registry_() noexcept {
void Exception::UpdatePanic() const noexcept {
ImGui::BeginGroup();
ImGui::TextUnformatted(msg_.c_str());
ImGui::Indent();
ImGui::Text("from %s:%d", srcloc_.file_name(), srcloc_.line());
ImGui::Unindent();
ImGui::EndGroup();
}
std::string Exception::Stringify() const noexcept {
return msg() + "\n "+srcloc_.file_name()+":"+std::to_string(srcloc_.line());
}
std::string Exception::StringifyRecursive() const noexcept {
std::stringstream st;
st << Stringify() << "\n";
auto ptr = reason();
while (ptr)
try {
std::rethrow_exception(ptr);
} catch (nf7::Exception& e) {
st << e.Stringify() << "\n";
ptr = e.reason();
} catch (std::exception& e) {
st << e.what() << "\n";
ptr = nullptr;
}
return st.str();
}
const std::map<std::string, const File::TypeInfo*>& File::registry() noexcept {

3
nf7.hh
View File

@ -41,6 +41,9 @@ class Exception : public std::nested_exception {
Exception& operator=(Exception&&) = delete;
virtual void UpdatePanic() const noexcept;
virtual std::string Stringify() const noexcept;
std::string StringifyRecursive() const noexcept;
const std::string& msg() const noexcept { return msg_; }
const std::source_location& srcloc() const noexcept { return srcloc_; }