// 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)
//}