Skip to content

Commit 2c8e61a

Browse files
committed
- Added logging capabilities for sa_plugin_services.
- Now archiving test execution logs for inspection of failed unit tests. - Added verbose messages for TestPlugins.cpp.
1 parent 3f08d19 commit 2c8e61a

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,5 @@ test_script:
9696

9797
artifacts:
9898
- path: .\build\bin\Release\sa.tests.*.release.xml
99+
- path: .\build\bin\Release\test_logs\*.log
99100
- path: .\build\shellanything-*-win*.*

src/plugins/sa_plugin_services/sa_plugin_services.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,26 @@ extern "C" {
4242

4343
static const char* PLUGIN_NAME_IDENTIFIER = "sa_plugin_services";
4444

45+
void get_service_last_error(DWORD * error_code, char ** buffer, size_t * buffer_size)
46+
{
47+
//Get the error message ID, if any.
48+
(*error_code) = ::GetLastError();
49+
50+
LPSTR messageBuffer = nullptr;
51+
52+
//Ask Win32 to give us the string version of that message ID.
53+
//The parameters we pass in, tell Win32 to create the buffer that holds the message for us (because we don't yet know how long the message string will be).
54+
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
55+
NULL, (*error_code), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
56+
57+
//Copy the error message into the output buffer.
58+
char* message_copy = _strdup(messageBuffer);
59+
*buffer_size = size;
60+
61+
//Free the Win32's string's buffer.
62+
LocalFree(messageBuffer);
63+
}
64+
4565
const char* get_service_status(const char* name)
4666
{
4767
SC_HANDLE hService, hSCManager;
@@ -60,6 +80,17 @@ const char* get_service_status(const char* name)
6080
hService = OpenService(hSCManager, name, SERVICE_QUERY_STATUS);
6181
if (!hService)
6282
{
83+
// Get a description of the error.
84+
char* error_desc = NULL;
85+
size_t error_desc_size = 0;
86+
DWORD win32_error_code = 0;
87+
get_service_last_error(&win32_error_code, &error_desc, &error_desc_size);
88+
89+
// Print a nice logging message
90+
sa_logging_print_format(SA_LOG_LEVEL_WARNING, PLUGIN_NAME_IDENTIFIER, "Failed openning service '%s'. Error: %u, %s.", name, win32_error_code, error_desc);
91+
92+
// Cleanup
93+
free(error_desc);
6394
CloseServiceHandle(hSCManager);
6495
return EMPTY_STATUS;
6596
}
@@ -69,6 +100,17 @@ const char* get_service_status(const char* name)
69100
&dwBytesNeeded);
70101
if (result == 0)
71102
{
103+
// Get a description of the error.
104+
char* error_desc = NULL;
105+
size_t error_desc_size = 0;
106+
DWORD win32_error_code = 0;
107+
get_service_last_error(&win32_error_code, &error_desc, &error_desc_size);
108+
109+
// Print a nice logging message
110+
sa_logging_print_format(SA_LOG_LEVEL_WARNING, PLUGIN_NAME_IDENTIFIER, "QueryServiceStatusEx has failed for service '%s'. Error: %u, %s.", name, win32_error_code, error_desc);
111+
112+
// Cleanup
113+
free(error_desc);
72114
CloseServiceHandle(hService);
73115
CloseServiceHandle(hSCManager);
74116
return EMPTY_STATUS;

src/tests/TestPlugins.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,10 @@ namespace shellanything
319319
{
320320
const std::string& property_name = expected_properties[i];
321321
ASSERT_TRUE(pmgr.HasProperty(property_name)) << "The expected property '" << property_name << "' is not found.";
322-
//const std::string& value = pmgr.GetProperty(property_name);
323-
//printf("Found property '%s' with value '%s'.\n", property_name.c_str(), value.c_str());
322+
323+
// Debug
324+
const std::string& value = pmgr.GetProperty(property_name);
325+
printf("Found property '%s' with value '%s'.\n", property_name.c_str(), value.c_str());
324326
}
325327

326328
//ASSERT expected process status

0 commit comments

Comments
 (0)