-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbResult.cpp
More file actions
132 lines (120 loc) · 3.68 KB
/
DbResult.cpp
File metadata and controls
132 lines (120 loc) · 3.68 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* DbResult.h
*
* Created: 2016
* \copyright 2016 DCBlaha. Distributed under the Mozilla Public License 2.0.
*/
#include "DbResult.h"
#include <map>
#include <mutex>
#ifndef __linux__
#include <conio.h>
#endif
class DbResultContext
{
public:
DbResultContext():
mResultIndex(DbResult::RES_START)
{}
~DbResultContext()
{
std::lock_guard<std::mutex> lock(mMutex);
if(mErrorStrings.size() != 0)
{
fprintf(stderr, "Unhandled errors\n");
for(auto const &err:mErrorStrings)
{
fprintf(stderr, " %s\n", err.second.c_str());
}
// This should not be done because it will hang processes, where the stderr text
// may not be visible.
// _getch();
}
}
int setErrorOrWarning(std::string const &errStr)
{
int resultId = mResultIndex;
std::lock_guard<std::mutex> lock(mMutex);
mErrorStrings[mResultIndex] = errStr;
// Increment with rollover.
mResultIndex = (mResultIndex & DbResult::RES_CODEMASK) + 1;
return resultId;
}
void insertContext(int resultId, std::string const &errStr)
{
std::lock_guard<std::mutex> lock(mMutex);
std::string str = errStr;
str += "\n";
str += mErrorStrings[resultId & DbResult::RES_CODEMASK];
mErrorStrings[resultId & DbResult::RES_CODEMASK] = str;
}
std::string const getErrorString(int resultId)
{
std::lock_guard<std::mutex> lock(mMutex);
auto iter = mErrorStrings.find(resultId & DbResult::RES_CODEMASK);
std::string errStr;
if(iter != mErrorStrings.end())
{
errStr = iter->second;
mErrorStrings.erase(iter);
}
return(errStr);
};
/*
std::string const getAllErrors()
{
std::string errStr;
for(auto const &err : mErrorStrings)
{
if(errStr.length() == 0)
{
errStr += ", ";
}
errStr += err.second.c_str();
}
return errStr;
}
*/
private:
int mResultIndex;
std::mutex mMutex;
std::map<int, std::string> mErrorStrings;
};
DbResultContext gDbResultContext;
void DbResult::setError(std::string const &errStr)
{
if(!(mResultId & RES_ERROR))
{
// This should probably remove the warning string since the warning
// is being overwritten. See comments for getMrError().
mResultId = gDbResultContext.setErrorOrWarning(errStr);
mResultId |= RES_ERROR;
}
else
{
std::string addedErrStr = "Multiple errors: ";
addedErrStr += errStr;
gDbResultContext.insertContext(mResultId, addedErrStr);
}
}
void DbResult::setWarning(std::string const &errStr)
{
// Don't overwrite error codes.
if(!(mResultId & RES_ERROR))
{
mResultId = gDbResultContext.setErrorOrWarning(errStr);
mResultId |= RES_WARNING;
}
}
void DbResult::insertContext(std::string const &errStr)
{
if(!(mResultId & RES_ERROR))
{
setError("Setting context before error");
}
gDbResultContext.insertContext(mResultId, errStr);
}
std::string const getDbResultString(DbResult const &result)
{
return gDbResultContext.getErrorString(result.getResultId());
}