blob: 3928d0f1ca27a937e7278d390e91b9bc1966000b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
// *****************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: https://www.gnu.org/licenses/gpl-3.0 *
// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
// *****************************************************************************
#ifndef EXTRA_LOG_H_601673246392441846218957402563
#define EXTRA_LOG_H_601673246392441846218957402563
#include "error_log.h"
#include "thread.h"
/* log errors in "exceptional situations" when no other means are available, e.g.
- while an exception is in flight
- cleanup errors
- nothrow GUI functions */
namespace zen
{
namespace impl
{
class ExtraLog
{
public:
~ExtraLog()
{
assert(reportOutstandingLog_);
if (!log_.empty() && reportOutstandingLog_)
reportOutstandingLog_(log_);
}
void init(const std::function<void(const ErrorLog& log)>& reportOutstandingLog)
{
assert(!reportOutstandingLog_);
reportOutstandingLog_ = reportOutstandingLog;
}
ErrorLog fetchLog() { return std::exchange(log_, ErrorLog()); }
void logError(const std::wstring& msg) { logMsg(log_, msg, MessageType::MSG_TYPE_ERROR); } //nothrow!
private:
ErrorLog log_;
std::function<void(const ErrorLog& log)> reportOutstandingLog_;
};
inline constinit Global<Protected<ExtraLog>> globalExtraLog;
template <class Function>
auto accessExtraLog(Function fun)
{
globalExtraLog.setOnce([] { return std::make_unique<Protected<ExtraLog>>(); });
if (auto protExtraLog = impl::globalExtraLog.get())
protExtraLog->access([&](ExtraLog& log) { fun(log); });
else
assert(false); //access after global shutdown!? => SOL!
}
}
inline
void initExtraLog(const std::function<void(const ErrorLog& log)>& reportOutstandingLog /*nothrow! runs during global shutdown!*/)
{
impl::accessExtraLog([&](impl::ExtraLog& el) { el.init(reportOutstandingLog); });
}
inline
ErrorLog fetchExtraLog()
{
ErrorLog output;
impl::accessExtraLog([&](impl::ExtraLog& el) { output = el.fetchLog(); });
return output;
}
inline
void logExtraError(const std::wstring& msg) //nothrow!
{
impl::accessExtraLog([&](impl::ExtraLog& el) { el.logError(msg); });
}
}
#endif //EXTRA_LOG_H_601673246392441846218957402563
|