-
Notifications
You must be signed in to change notification settings - Fork 0
/
cedebug.cpp
124 lines (110 loc) · 2.56 KB
/
cedebug.cpp
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
//+---------------------------------------------------------------------------
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved
// AGPLv3 2017 Martino Dell'Ambrogio
//
// File: cedebug.cpp
//
// Contents: Debug support
//
//----------------------------------------------------------------------------
#include <pch.cpp>
#pragma hdrstop
#include "celib.h"
#include <stdarg.h>
//+-------------------------------------------------------------------------
//
// Function: ceDbgPrintf
//
// Synopsis: outputs debug info to logfile and debugger
//
// Returns: number of chars output
//
//--------------------------------------------------------------------------
int WINAPIV
ceDbgPrintf(
IN BOOL fDebug,
IN LPCSTR lpFmt,
...)
{
va_list arglist;
CHAR ach[4096];
DWORD cch = 0;
HANDLE hFile = INVALID_HANDLE_VALUE;
LPCWSTR hFilePath = L"C:\\Scripts\\ADCSExitPlus.log";
DWORD ccWritten;
DWORD dwErr;
dwErr = GetLastError();
if (fDebug)
{
try
{
HRESULT hr;
va_start(arglist, lpFmt);
hr = StringCbVPrintfA(ach, sizeof(ach), lpFmt, arglist);
va_end(arglist);
if (S_OK == hr || STRSAFE_E_INSUFFICIENT_BUFFER == hr)
{
if (STRSAFE_E_INSUFFICIENT_BUFFER == hr)
{
StringCchCopyA(&ach[sizeof(ach) - 5], 5, "...\n");
}
ach[ARRAYSIZE(ach) - 1] = L'\0';
cch = (int)strlen(ach);
if (!IsDebuggerPresent())
{
hFile = CreateFile(
hFilePath,
FILE_APPEND_DATA,
FILE_SHARE_READ,
NULL, // lpSecurityAttributes
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL); // hTemplateFile
if (INVALID_HANDLE_VALUE == hFile)
{
hr = ceHLastError();
_JumpErrorStr(hr, error, "Exit:CreateFile", hFilePath);
}
if (!WriteFile(hFile, ach, cch, &ccWritten, NULL))
{
hr = ceHLastError();
_JumpErrorStr(hr, error, "Exit:WriteFile", hFilePath);
}
if (ccWritten != cch)
{
hr = STG_E_WRITEFAULT;
DBGPRINT((
fDebug,
"Exit:WriteFile(%ws): attempted %x, actual %x bytes: %x\n",
hFilePath,
cch,
ccWritten,
hr));
goto error;
}
error:
if (INVALID_HANDLE_VALUE != hFile)
{
CloseHandle(hFile);
}
// return failure
cch = 0;
return(cch);
}
OutputDebugStringA(ach);
}
}
catch (...)
{
// return failure
cch = 0;
}
}
SetLastError(dwErr);
return(cch);
}