[go: up one dir, main page]

Menu

[r25]: / Error.cpp  Maximize  Restore  History

Download this file

167 lines (152 with data), 3.6 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Error.cpp: implementation of the Error class.
//
//////////////////////////////////////////////////////////////////////
#include "Error.h"
#include "Helpers.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Error::Error(LPTSTR strIdentifier):
lpMsg(NULL), dwMsgLen(0), lpIdent(NULL)
{
hOut=GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut==INVALID_HANDLE_VALUE) {
//AllocConsole();
//hOut=GetStdHandle(STD_OUTPUT_HANDLE);
}
SetIdentifier(strIdentifier);
}
Error::~Error()
{
//if (lpIdent) delete lpIdent;
if (lpMsg) LocalFree(lpMsg);
}
/*__declspec ( naked )
void __cdecl Error::DebugPrint(LPTSTR format, ...)
{
va_list arg;
LPTSTR p1;
int p2;
va_start(arg, format);
p1 = va_arg(arg, LPTSTR);
p2 = va_arg(arg, int);
p2 = va_arg(arg, int);
va_end(arg);
//TCHAR prnbuf[255];
//CheckBuffer();
//_stprintf(prnbuf va
}
*/
bool Error::CheckBuffer(DWORD needed)
{
if (lpMsg) return true;
if (needed==0) needed=100;
//if (needed>(dwMsgLen*sizeof(TCHAR))) {
// lpMsg = LocalReAlloc(
return false;
}
/*
void Error::DebugWriteString(LPCSTR string)
{
if (hOut != INVALID_HANDLE_VALUE) {
DWORD wrtn;
WriteFile(hOut, string, StrLen(string),&wrtn, NULL);
} else {
OutputDebugStringA(string);
}
}
void Error::DebugWriteString(LPCWSTR string)
{
if (hOut != INVALID_HANDLE_VALUE) {
DWORD wrtn;
WriteFile(hOut, string, StrLen(string),&wrtn, NULL);
} else {
OutputDebugStringW(string);
}
}
*/
void Error::DebugWriteString(LPCTSTR str, bool IncErrMsg)
{
LPTSTR prnbuf;
int ln = StrLen(lpIdent)+StrLen(str)+StrLen(lpMsg);
/*if (hOut != INVALID_HANDLE_VALUE) {
DWORD wrtn;
WriteFile(hOut, string, StrLen(string),&wrtn, NULL);
} else {
OutputDebugString(string);
}*/
prnbuf = new TCHAR[ln+10]; // actually 7
try{
if (IncErrMsg && lpMsg) {
ln = StrLen(lpIdent)+StrLen(str);
wsprintf(prnbuf, TEXT("[%s] %s: %s"), lpIdent, str, lpMsg);
} else {
wsprintf(prnbuf, TEXT("[%s]: %s\n"), lpIdent, str);
}
OutputDebugString(prnbuf);
}
catch (...) {
OutputDebugString(TEXT("Exception in Error::DebugWriteString\n"));
}
delete prnbuf;
//}
}
void Error::Clear()
{
dwCode = 0;
if (lpMsg) {
LocalFree(lpMsg);
lpMsg=NULL;
}
}
void Error::Refresh(DWORD code)
{
if (code==0) code=GetLastError();
Clear();
if (code==ERROR_EXTENDED_ERROR) {
lpMsg =(LPTSTR) LocalAlloc(LMEM_FIXED,sizeof(TCHAR)*512);
if (lpMsg) {
if (NO_ERROR == WNetGetLastError(&dwCode,lpMsg+256, 256, lpMsg, 256)) {
LPTSTR tmp = StrChar(lpMsg, 0);
*(tmp++) = ':';
*(tmp++) = ' ';
StrCopy(tmp, lpMsg+256);
}
}
} else {
dwCode=code;
if (code) {
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsg,
0,
NULL
);
}
}
}
void Error::SetIdentifier(LPTSTR strIdentifier)
{
lpIdent = strIdentifier;
/*if (strIdentifier) {
int ln=StrLen(strIdentifier);
if (lpIdent) delete lpIdent;
lpIdent=new TCHAR[ln+1];
StrCopy(lpIdent,strIdentifier);
}
*/
//DebugWriteString(TEXT("Error object created\n"));
}
LPCTSTR Error::operator *()
{
return lpMsg;
}
//void Error::RefreshNet(DWORD code)
//{
// if (code==0)
//}