LogViewer
A modern log viewer with AI-assisted analysis (Qt 6)
Loading...
Searching...
No Matches
Error.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include "UiServices.hpp"
9#include "Logger.hpp"
10
11#include <atomic>
12#include <stdexcept>
13#include <string>
14#include <string_view>
15
16namespace error
17{
18
32
33// Global toggle for showing dialogs (default: true)
34inline std::atomic<bool>& DialogsEnabledFlag()
35{
36 static std::atomic<bool> flag {true};
37 return flag;
38}
39inline void SetShowDialogs(bool enabled)
40{
41 DialogsEnabledFlag().store(enabled);
42}
43inline bool GetShowDialogs()
44{
45 return DialogsEnabledFlag().load();
46}
47inline bool CanShowDialogs()
48{
50 return GetShowDialogs() && presenter && presenter->CanShowDialogs();
51}
52
53inline void ShowError(std::string_view message)
54{
56 if (presenter && CanShowDialogs()) {
57 presenter->ShowError(message);
58 }
59}
60
61class Error : public std::runtime_error
62{
63 private:
65
66 public:
67 explicit Error(ErrorCode code, const char* message, bool showMsgBox = true)
68 : Error(code, std::string(message), showMsgBox)
69 {
70 }
71
72 explicit Error(ErrorCode code, const std::string& message, bool showMsgBox = true)
73 : std::runtime_error(message)
74 , m_code(code)
75 {
76 if (showMsgBox && CanShowDialogs())
77 ShowError(message);
78 util::Logger::Error("Application Error [{}]: {}",
79 static_cast<int>(code), message);
80 }
81
82 // Legacy constructors without ErrorCode (defaults to Unknown)
83 explicit Error(const char* message, bool showMsgBox = true)
84 : Error(ErrorCode::Unknown, std::string(message), showMsgBox)
85 {
86 }
87
88 explicit Error(const std::string& message, bool showMsgBox = true)
89 : Error(ErrorCode::Unknown, message, showMsgBox)
90 {
91 }
92
93 ErrorCode code() const { return m_code; }
94};
95
96} // namespace error
Logging facade for application-wide logging.
Definition Error.hpp:62
Error(const std::string &message, bool showMsgBox=true)
Definition Error.hpp:88
Error(const char *message, bool showMsgBox=true)
Definition Error.hpp:83
ErrorCode code() const
Definition Error.hpp:93
ErrorCode m_code
Definition Error.hpp:64
Error(ErrorCode code, const char *message, bool showMsgBox=true)
Definition Error.hpp:67
Error(ErrorCode code, const std::string &message, bool showMsgBox=true)
Definition Error.hpp:72
static UiServices & Instance()
Definition UiServices.hpp:16
std::shared_ptr< IErrorPresenter > GetErrorPresenter() const
Definition UiServices.hpp:35
static void Error(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logger.hpp:303
Definition Error.hpp:17
bool GetShowDialogs()
Definition Error.hpp:43
bool CanShowDialogs()
Definition Error.hpp:47
void ShowError(std::string_view message)
Definition Error.hpp:53
std::atomic< bool > & DialogsEnabledFlag()
Definition Error.hpp:34
ErrorCode
Error code enumeration for categorizing errors.
Definition Error.hpp:23
@ InvalidArgument
Invalid argument provided.
@ FileNotFound
File not found.
@ IOError
Input/output error.
@ Unknown
Unknown or unspecified error.
@ NotImplemented
Feature not yet implemented.
@ RuntimeError
Runtime error occurred.
@ ParseError
Parsing error.
void SetShowDialogs(bool enabled)
Definition Error.hpp:39