aboutsummaryrefslogtreecommitdiff
path: root/src/dutil/logutil.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/dutil/logutil.cpp')
-rw-r--r--src/dutil/logutil.cpp67
1 files changed, 41 insertions, 26 deletions
diff --git a/src/dutil/logutil.cpp b/src/dutil/logutil.cpp
index 438cdbb8..35251274 100644
--- a/src/dutil/logutil.cpp
+++ b/src/dutil/logutil.cpp
@@ -2,6 +2,21 @@
2 2
3#include "precomp.h" 3#include "precomp.h"
4 4
5
6// Exit macros
7#define LoguExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_LOGUTIL, x, s, __VA_ARGS__)
8#define LoguExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_LOGUTIL, x, s, __VA_ARGS__)
9#define LoguExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_LOGUTIL, x, s, __VA_ARGS__)
10#define LoguExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_LOGUTIL, x, s, __VA_ARGS__)
11#define LoguExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_LOGUTIL, x, s, __VA_ARGS__)
12#define LoguExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_LOGUTIL, x, s, __VA_ARGS__)
13#define LoguExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_LOGUTIL, p, x, e, s, __VA_ARGS__)
14#define LoguExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_LOGUTIL, p, x, s, __VA_ARGS__)
15#define LoguExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_LOGUTIL, p, x, e, s, __VA_ARGS__)
16#define LoguExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_LOGUTIL, p, x, s, __VA_ARGS__)
17#define LoguExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_LOGUTIL, e, x, s, __VA_ARGS__)
18#define LoguExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_LOGUTIL, g, x, s, __VA_ARGS__)
19
5// globals 20// globals
6static HMODULE LogUtil_hModule = NULL; 21static HMODULE LogUtil_hModule = NULL;
7static BOOL LogUtil_fDisabled = FALSE; 22static BOOL LogUtil_fDisabled = FALSE;
@@ -110,23 +125,23 @@ extern "C" HRESULT DAPI LogOpen(
110 if (wzExt && *wzExt) 125 if (wzExt && *wzExt)
111 { 126 {
112 hr = PathCreateTimeBasedTempFile(wzDirectory, wzLog, wzPostfix, wzExt, &LogUtil_sczLogPath, &LogUtil_hLog); 127 hr = PathCreateTimeBasedTempFile(wzDirectory, wzLog, wzPostfix, wzExt, &LogUtil_sczLogPath, &LogUtil_hLog);
113 ExitOnFailure(hr, "Failed to create log based on current system time."); 128 LoguExitOnFailure(hr, "Failed to create log based on current system time.");
114 } 129 }
115 else 130 else
116 { 131 {
117 hr = PathConcat(wzDirectory, wzLog, &LogUtil_sczLogPath); 132 hr = PathConcat(wzDirectory, wzLog, &LogUtil_sczLogPath);
118 ExitOnFailure(hr, "Failed to combine the log path."); 133 LoguExitOnFailure(hr, "Failed to combine the log path.");
119 134
120 hr = PathGetDirectory(LogUtil_sczLogPath, &sczLogDirectory); 135 hr = PathGetDirectory(LogUtil_sczLogPath, &sczLogDirectory);
121 ExitOnFailure(hr, "Failed to get log directory."); 136 LoguExitOnFailure(hr, "Failed to get log directory.");
122 137
123 hr = DirEnsureExists(sczLogDirectory, NULL); 138 hr = DirEnsureExists(sczLogDirectory, NULL);
124 ExitOnFailure(hr, "Failed to ensure log file directory exists: %ls", sczLogDirectory); 139 LoguExitOnFailure(hr, "Failed to ensure log file directory exists: %ls", sczLogDirectory);
125 140
126 LogUtil_hLog = ::CreateFileW(LogUtil_sczLogPath, GENERIC_WRITE, FILE_SHARE_READ, NULL, (fAppend) ? OPEN_ALWAYS : CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 141 LogUtil_hLog = ::CreateFileW(LogUtil_sczLogPath, GENERIC_WRITE, FILE_SHARE_READ, NULL, (fAppend) ? OPEN_ALWAYS : CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
127 if (INVALID_HANDLE_VALUE == LogUtil_hLog) 142 if (INVALID_HANDLE_VALUE == LogUtil_hLog)
128 { 143 {
129 ExitOnLastError(hr, "failed to create log file: %ls", LogUtil_sczLogPath); 144 LoguExitOnLastError(hr, "failed to create log file: %ls", LogUtil_sczLogPath);
130 } 145 }
131 146
132 if (fAppend) 147 if (fAppend)
@@ -152,7 +167,7 @@ extern "C" HRESULT DAPI LogOpen(
152 if (psczLogPath) 167 if (psczLogPath)
153 { 168 {
154 hr = StrAllocString(psczLogPath, LogUtil_sczLogPath, 0); 169 hr = StrAllocString(psczLogPath, LogUtil_sczLogPath, 0);
155 ExitOnFailure(hr, "Failed to copy log path."); 170 LoguExitOnFailure(hr, "Failed to copy log path.");
156 } 171 }
157 172
158LExit: 173LExit:
@@ -217,15 +232,15 @@ HRESULT DAPI LogRename(
217 ReleaseFileHandle(LogUtil_hLog); 232 ReleaseFileHandle(LogUtil_hLog);
218 233
219 hr = FileEnsureMove(LogUtil_sczLogPath, wzNewPath, TRUE, TRUE); 234 hr = FileEnsureMove(LogUtil_sczLogPath, wzNewPath, TRUE, TRUE);
220 ExitOnFailure(hr, "Failed to move logfile to new location: %ls", wzNewPath); 235 LoguExitOnFailure(hr, "Failed to move logfile to new location: %ls", wzNewPath);
221 236
222 hr = StrAllocString(&LogUtil_sczLogPath, wzNewPath, 0); 237 hr = StrAllocString(&LogUtil_sczLogPath, wzNewPath, 0);
223 ExitOnFailure(hr, "Failed to store new logfile path: %ls", wzNewPath); 238 LoguExitOnFailure(hr, "Failed to store new logfile path: %ls", wzNewPath);
224 239
225 LogUtil_hLog = ::CreateFileW(LogUtil_sczLogPath, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 240 LogUtil_hLog = ::CreateFileW(LogUtil_sczLogPath, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
226 if (INVALID_HANDLE_VALUE == LogUtil_hLog) 241 if (INVALID_HANDLE_VALUE == LogUtil_hLog)
227 { 242 {
228 ExitOnLastError(hr, "failed to create log file: %ls", LogUtil_sczLogPath); 243 LoguExitOnLastError(hr, "failed to create log file: %ls", LogUtil_sczLogPath);
229 } 244 }
230 245
231 // Enable "append" mode by moving file pointer to the end 246 // Enable "append" mode by moving file pointer to the end
@@ -307,7 +322,7 @@ HRESULT DAPI LogSetSpecialParams(
307 else 322 else
308 { 323 {
309 hr = StrAllocConcat(&LogUtil_sczSpecialBeginLine, wzSpecialBeginLine, 0); 324 hr = StrAllocConcat(&LogUtil_sczSpecialBeginLine, wzSpecialBeginLine, 0);
310 ExitOnFailure(hr, "Failed to allocate copy of special beginline string"); 325 LoguExitOnFailure(hr, "Failed to allocate copy of special beginline string");
311 } 326 }
312 327
313 // Handle special string to be appended to every time stamp 328 // Handle special string to be appended to every time stamp
@@ -318,7 +333,7 @@ HRESULT DAPI LogSetSpecialParams(
318 else 333 else
319 { 334 {
320 hr = StrAllocConcat(&LogUtil_sczSpecialAfterTimeStamp, wzSpecialAfterTimeStamp, 0); 335 hr = StrAllocConcat(&LogUtil_sczSpecialAfterTimeStamp, wzSpecialAfterTimeStamp, 0);
321 ExitOnFailure(hr, "Failed to allocate copy of special post-timestamp string"); 336 LoguExitOnFailure(hr, "Failed to allocate copy of special post-timestamp string");
322 } 337 }
323 338
324 // Handle special string to be appended before every full line 339 // Handle special string to be appended before every full line
@@ -329,7 +344,7 @@ HRESULT DAPI LogSetSpecialParams(
329 else 344 else
330 { 345 {
331 hr = StrAllocConcat(&LogUtil_sczSpecialEndLine, wzSpecialEndLine, 0); 346 hr = StrAllocConcat(&LogUtil_sczSpecialEndLine, wzSpecialEndLine, 0);
332 ExitOnFailure(hr, "Failed to allocate copy of special endline string"); 347 LoguExitOnFailure(hr, "Failed to allocate copy of special endline string");
333 } 348 }
334 349
335LExit: 350LExit:
@@ -597,14 +612,14 @@ extern "C" HRESULT DAPI LogErrorStringArgs(
597 LPWSTR sczMessage = NULL; 612 LPWSTR sczMessage = NULL;
598 613
599 hr = StrAllocStringAnsi(&sczFormat, szFormat, 0, CP_ACP); 614 hr = StrAllocStringAnsi(&sczFormat, szFormat, 0, CP_ACP);
600 ExitOnFailure(hr, "Failed to convert format string to wide character string"); 615 LoguExitOnFailure(hr, "Failed to convert format string to wide character string");
601 616
602 // format the string as a unicode string - this is necessary to be able to include 617 // format the string as a unicode string - this is necessary to be able to include
603 // international characters in our output string. This does have the counterintuitive effect 618 // international characters in our output string. This does have the counterintuitive effect
604 // that the caller's "%s" is interpreted differently 619 // that the caller's "%s" is interpreted differently
605 // (so callers should use %hs for LPSTR and %ls for LPWSTR) 620 // (so callers should use %hs for LPSTR and %ls for LPWSTR)
606 hr = StrAllocFormattedArgs(&sczMessage, sczFormat, args); 621 hr = StrAllocFormattedArgs(&sczMessage, sczFormat, args);
607 ExitOnFailure(hr, "Failed to format error message: \"%ls\"", sczFormat); 622 LoguExitOnFailure(hr, "Failed to format error message: \"%ls\"", sczFormat);
608 623
609 hr = LogStringLine(REPORT_ERROR, "Error 0x%x: %ls", hrError, sczMessage); 624 hr = LogStringLine(REPORT_ERROR, "Error 0x%x: %ls", hrError, sczMessage);
610 625
@@ -636,14 +651,14 @@ extern "C" HRESULT DAPI LogErrorIdModule(
636 WORD cStrings = 1; // guaranteed wzError is in the list 651 WORD cStrings = 1; // guaranteed wzError is in the list
637 652
638 hr = ::StringCchPrintfW(wzError, countof(wzError), L"0x%08x", hrError); 653 hr = ::StringCchPrintfW(wzError, countof(wzError), L"0x%08x", hrError);
639 ExitOnFailure(hr, "failed to format error code: \"0%08x\"", hrError); 654 LoguExitOnFailure(hr, "failed to format error code: \"0%08x\"", hrError);
640 655
641 cStrings += wzString1 ? 1 : 0; 656 cStrings += wzString1 ? 1 : 0;
642 cStrings += wzString2 ? 1 : 0; 657 cStrings += wzString2 ? 1 : 0;
643 cStrings += wzString3 ? 1 : 0; 658 cStrings += wzString3 ? 1 : 0;
644 659
645 hr = LogIdModule(REPORT_ERROR, dwLogId, hModule, wzError, wzString1, wzString2, wzString3); 660 hr = LogIdModule(REPORT_ERROR, dwLogId, hModule, wzError, wzString1, wzString2, wzString3);
646 ExitOnFailure(hr, "Failed to log id module."); 661 LoguExitOnFailure(hr, "Failed to log id module.");
647 662
648LExit: 663LExit:
649 return hr; 664 return hr;
@@ -771,7 +786,7 @@ extern "C" HRESULT LogStringWorkRaw(
771 if (INVALID_HANDLE_VALUE == LogUtil_hLog) 786 if (INVALID_HANDLE_VALUE == LogUtil_hLog)
772 { 787 {
773 hr = StrAnsiAllocConcat(&LogUtil_sczPreInitBuffer, szLogData, 0); 788 hr = StrAnsiAllocConcat(&LogUtil_sczPreInitBuffer, szLogData, 0);
774 ExitOnFailure(hr, "Failed to concatenate string to pre-init buffer"); 789 LoguExitOnFailure(hr, "Failed to concatenate string to pre-init buffer");
775 790
776 ExitFunction1(hr = S_OK); 791 ExitFunction1(hr = S_OK);
777 } 792 }
@@ -781,7 +796,7 @@ extern "C" HRESULT LogStringWorkRaw(
781 { 796 {
782 if (!::WriteFile(LogUtil_hLog, reinterpret_cast<const BYTE*>(szLogData) + cbTotal, cbLogData - cbTotal, &cbWrote, NULL)) 797 if (!::WriteFile(LogUtil_hLog, reinterpret_cast<const BYTE*>(szLogData) + cbTotal, cbLogData - cbTotal, &cbWrote, NULL))
783 { 798 {
784 ExitOnLastError(hr, "Failed to write output to log: %ls - %ls", LogUtil_sczLogPath, szLogData); 799 LoguExitOnLastError(hr, "Failed to write output to log: %ls - %hs", LogUtil_sczLogPath, szLogData);
785 } 800 }
786 801
787 cbTotal += cbWrote; 802 cbTotal += cbWrote;
@@ -816,7 +831,7 @@ static HRESULT LogIdWork(
816 831
817 if (0 == cch) 832 if (0 == cch)
818 { 833 {
819 ExitOnLastError(hr, "failed to log id: %d", dwLogId); 834 LoguExitOnLastError(hr, "failed to log id: %d", dwLogId);
820 } 835 }
821 836
822 if (2 <= cch && L'\r' == pwz[cch-2] && L'\n' == pwz[cch-1]) 837 if (2 <= cch && L'\r' == pwz[cch-2] && L'\n' == pwz[cch-1])
@@ -850,14 +865,14 @@ static HRESULT LogStringWorkArgs(
850 LPWSTR sczMessage = NULL; 865 LPWSTR sczMessage = NULL;
851 866
852 hr = StrAllocStringAnsi(&sczFormat, szFormat, 0, CP_ACP); 867 hr = StrAllocStringAnsi(&sczFormat, szFormat, 0, CP_ACP);
853 ExitOnFailure(hr, "Failed to convert format string to wide character string"); 868 LoguExitOnFailure(hr, "Failed to convert format string to wide character string");
854 869
855 // format the string as a unicode string 870 // format the string as a unicode string
856 hr = StrAllocFormattedArgs(&sczMessage, sczFormat, args); 871 hr = StrAllocFormattedArgs(&sczMessage, sczFormat, args);
857 ExitOnFailure(hr, "Failed to format message: \"%ls\"", sczFormat); 872 LoguExitOnFailure(hr, "Failed to format message: \"%ls\"", sczFormat);
858 873
859 hr = LogStringWork(rl, 0, sczMessage, fLOGUTIL_NEWLINE); 874 hr = LogStringWork(rl, 0, sczMessage, fLOGUTIL_NEWLINE);
860 ExitOnFailure(hr, "Failed to write formatted string to log:%ls", sczMessage); 875 LoguExitOnFailure(hr, "Failed to write formatted string to log:%ls", sczMessage);
861 876
862LExit: 877LExit:
863 ReleaseStr(sczFormat); 878 ReleaseStr(sczFormat);
@@ -909,24 +924,24 @@ static HRESULT LogStringWork(
909 hr = StrAllocFormatted(&scz, L"%ls[%04X:%04X][%04hu-%02hu-%02huT%02hu:%02hu:%02hu]%hs%03d:%ls %ls%ls", LogUtil_sczSpecialBeginLine ? LogUtil_sczSpecialBeginLine : L"", 924 hr = StrAllocFormatted(&scz, L"%ls[%04X:%04X][%04hu-%02hu-%02huT%02hu:%02hu:%02hu]%hs%03d:%ls %ls%ls", LogUtil_sczSpecialBeginLine ? LogUtil_sczSpecialBeginLine : L"",
910 dwProcessId, dwThreadId, st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, szType, dwId, 925 dwProcessId, dwThreadId, st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, szType, dwId,
911 LogUtil_sczSpecialAfterTimeStamp ? LogUtil_sczSpecialAfterTimeStamp : L"", sczString, LogUtil_sczSpecialEndLine ? LogUtil_sczSpecialEndLine : L"\r\n"); 926 LogUtil_sczSpecialAfterTimeStamp ? LogUtil_sczSpecialAfterTimeStamp : L"", sczString, LogUtil_sczSpecialEndLine ? LogUtil_sczSpecialEndLine : L"\r\n");
912 ExitOnFailure(hr, "Failed to format line prefix."); 927 LoguExitOnFailure(hr, "Failed to format line prefix.");
913 } 928 }
914 929
915 wzLogData = scz ? scz : sczString; 930 wzLogData = scz ? scz : sczString;
916 931
917 // Convert to UTF-8 before writing out to the log file 932 // Convert to UTF-8 before writing out to the log file
918 hr = StrAnsiAllocString(&sczMultiByte, wzLogData, 0, CP_UTF8); 933 hr = StrAnsiAllocString(&sczMultiByte, wzLogData, 0, CP_UTF8);
919 ExitOnFailure(hr, "Failed to convert log string to UTF-8"); 934 LoguExitOnFailure(hr, "Failed to convert log string to UTF-8");
920 935
921 if (s_vpfLogStringWorkRaw) 936 if (s_vpfLogStringWorkRaw)
922 { 937 {
923 hr = s_vpfLogStringWorkRaw(sczMultiByte, s_vpvLogStringWorkRawContext); 938 hr = s_vpfLogStringWorkRaw(sczMultiByte, s_vpvLogStringWorkRawContext);
924 ExitOnFailure(hr, "Failed to write string to log using redirected function: %ls", sczString); 939 LoguExitOnFailure(hr, "Failed to write string to log using redirected function: %ls", sczString);
925 } 940 }
926 else 941 else
927 { 942 {
928 hr = LogStringWorkRaw(sczMultiByte); 943 hr = LogStringWorkRaw(sczMultiByte);
929 ExitOnFailure(hr, "Failed to write string to log using default function: %ls", sczString); 944 LoguExitOnFailure(hr, "Failed to write string to log using default function: %ls", sczString);
930 } 945 }
931 946
932LExit: 947LExit: