2 * Copyright 2011 André Hentschel
3 * Copyright 2013 Mislav Blažević
4 * Copyright 2015 Mark Jansen
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define WIN32_NO_STATUS
28 #include "wine/winternl.h"
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
33 #define DPFLTR_APPCOMPAT_ID 123
36 #define NT_SUCCESS(StatCode) ((NTSTATUS)(StatCode) >= 0)
39 ULONG g_ShimDebugLevel
= 0xffffffff;
41 void ApphelppInitDebugLevel(void)
43 UNICODE_STRING DebugKey
, DebugValue
;
48 RtlInitUnicodeString(&DebugKey
, L
"SHIM_DEBUG_LEVEL");
49 DebugValue
.MaximumLength
= sizeof(Buffer
);
50 DebugValue
.Buffer
= Buffer
;
51 DebugValue
.Length
= 0;
53 /* Hold the lock as short as possible. */
55 Status
= RtlQueryEnvironmentVariable_U(NULL
, &DebugKey
, &DebugValue
);
58 if (NT_SUCCESS(Status
))
60 if (!NT_SUCCESS(RtlUnicodeStringToInteger(&DebugValue
, 10, &NewLevel
)))
63 g_ShimDebugLevel
= NewLevel
;
66 BOOL WINAPI
DllMain( HINSTANCE hinst
, DWORD reason
, LPVOID reserved
)
71 case DLL_WINE_PREATTACH
:
72 return FALSE
; /* prefer native version */
74 case DLL_PROCESS_ATTACH
:
75 DisableThreadLibraryCalls( hinst
);
78 case DLL_PROCESS_DETACH
:
87 * Outputs diagnostic info.
89 * @param [in] Level The level to log this message with, choose any of [SHIM_ERR,
90 * SHIM_WARN, SHIM_INFO].
91 * @param [in] FunctionName The function this log should be attributed to.
92 * @param [in] Format The format string.
93 * @param ... Variable arguments providing additional information.
95 * @return Success: TRUE Failure: FALSE.
97 BOOL WINAPIV
ShimDbgPrint(SHIM_LOG_LEVEL Level
, PCSTR FunctionName
, PCSTR Format
, ...)
101 char* Current
= Buffer
, *LevelStr
;
102 size_t Length
= sizeof(Buffer
);
104 if (g_ShimDebugLevel
== 0xffffffff)
105 ApphelppInitDebugLevel();
107 if (Level
> g_ShimDebugLevel
)
114 Level
= DPFLTR_MASK
| (1 << DPFLTR_ERROR_LEVEL
);
118 Level
= DPFLTR_MASK
| (1 << DPFLTR_WARNING_LEVEL
);
122 Level
= DPFLTR_MASK
| (1 << DPFLTR_INFO_LEVEL
);
126 Level
= DPFLTR_MASK
| (1 << DPFLTR_INFO_LEVEL
);
129 StringCchPrintfExA(Current
, Length
, &Current
, &Length
, STRSAFE_NULL_ON_FAILURE
, "[%s][%-20s] ", LevelStr
, FunctionName
);
130 va_start(ArgList
, Format
);
131 StringCchVPrintfExA(Current
, Length
, &Current
, &Length
, STRSAFE_NULL_ON_FAILURE
, Format
, ArgList
);
133 return NT_SUCCESS(DbgPrintEx(DPFLTR_APPCOMPAT_ID
, Level
, "%s", Buffer
));