Skip to content

Commit

Permalink
improve logging into files explorer integration
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu Gallien <[email protected]>
  • Loading branch information
mgallien committed Mar 19, 2024
1 parent 331e89a commit f51f73a
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 24 deletions.
8 changes: 4 additions & 4 deletions shell_integration/windows/NCContextMenu/NCClientInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ NCClientInterface::ContextMenuInfo NCClientInterface::FetchInfo(const std::wstri
logger << "error with Connect" << std::endl;
return {};
}
socket.SendMsg(L"GET_STRINGS:CONTEXT_MENU_TITLE\n");
socket.SendMsg((L"GET_MENU_ITEMS:" + files + L"\n").data());
socket.SendMsg(L"GET_STRINGS:CONTEXT_MENU_TITLE\n", logger);
socket.SendMsg((L"GET_MENU_ITEMS:" + files + L"\n").data(), logger);

ContextMenuInfo info;
std::wstring response;
Expand Down Expand Up @@ -97,7 +97,7 @@ NCClientInterface::ContextMenuInfo NCClientInterface::FetchInfo(const std::wstri
return info;
}

void NCClientInterface::SendRequest(const wchar_t *verb, const std::wstring &path)
void NCClientInterface::SendRequest(const wchar_t *verb, const std::wstring &path, std::ofstream &logger)
{
auto pipename = CommunicationSocket::DefaultPipePath();

Expand All @@ -109,5 +109,5 @@ void NCClientInterface::SendRequest(const wchar_t *verb, const std::wstring &pat
return;
}

socket.SendMsg((verb + (L":" + path + L"\n")).data());
socket.SendMsg((verb + (L":" + path + L"\n")).data(), logger);
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class NCClientInterface
std::vector<MenuItem> menuItems;
};
static ContextMenuInfo FetchInfo(const std::wstring &files, std::ofstream &logger);
static void SendRequest(const wchar_t *verb, const std::wstring &path);
static void SendRequest(const wchar_t *verb, const std::wstring &path, std::ofstream &logger);
};

#endif //ABSTRACTSOCKETHANDLER_H
2 changes: 1 addition & 1 deletion shell_integration/windows/NCContextMenu/NCContextMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ IFACEMETHODIMP NCContextMenu::InvokeCommand(LPCMINVOKECOMMANDINFO pici)
return E_FAIL;
}

NCClientInterface::SendRequest(command.data(), m_selectedFiles);
NCClientInterface::SendRequest(command.data(), m_selectedFiles, m_logger);
return S_OK;
}

Expand Down
10 changes: 6 additions & 4 deletions shell_integration/windows/NCOverlays/NCOverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,24 @@ namespace {

unique_ptr<RemotePathChecker> s_instance;

RemotePathChecker *getGlobalChecker()
RemotePathChecker *getGlobalChecker(ofstream &logger)

Check warning on line 38 in shell_integration/windows/NCOverlays/NCOverlay.cpp

View workflow job for this annotation

GitHub Actions / build

shell_integration/windows/NCOverlays/NCOverlay.cpp:38:20 [modernize-use-trailing-return-type]

use a trailing return type for this function
{
// On Vista we'll run into issue #2680 if we try to create the thread+pipe connection
// on any DllGetClassObject of our registered classes.
// Work around the issue by creating the static RemotePathChecker only once actually needed.
static once_flag s_onceFlag;
call_once(s_onceFlag, [] { s_instance.reset(new RemotePathChecker); });
call_once(s_onceFlag, [&logger] { s_instance.reset(new RemotePathChecker{logger}); });

return s_instance.get();
}

}
NCOverlay::NCOverlay(int state)
NCOverlay::NCOverlay(int state)

Check warning on line 50 in shell_integration/windows/NCOverlays/NCOverlay.cpp

View workflow job for this annotation

GitHub Actions / build

shell_integration/windows/NCOverlays/NCOverlay.cpp:50:1 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: ULONG, m_logger
: _referenceCount(1)
, _state(state)
{
m_logger.open("c:\\testOverlay.log");
m_logger << "hello world" << std::endl;
}

NCOverlay::~NCOverlay(void)
Expand Down Expand Up @@ -120,7 +122,7 @@ IFACEMETHODIMP NCOverlay::GetPriority(int *pPriority)

IFACEMETHODIMP NCOverlay::IsMemberOf(PCWSTR pwszPath, DWORD dwAttrib)
{
RemotePathChecker* checker = getGlobalChecker();
RemotePathChecker* checker = getGlobalChecker(m_logger);
std::shared_ptr<const std::vector<std::wstring>> watchedDirectories = checker->WatchedDirectories();

if (watchedDirectories->empty()) {
Expand Down
6 changes: 4 additions & 2 deletions shell_integration/windows/NCOverlays/NCOverlay.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
#pragma once

#include <shlobj.h>

Check failure on line 20 in shell_integration/windows/NCOverlays/NCOverlay.h

View workflow job for this annotation

GitHub Actions / build

shell_integration/windows/NCOverlays/NCOverlay.h:20:10 [clang-diagnostic-error]

'shlobj.h' file not found
#include <fstream>

class NCOverlay : public IShellIconOverlayIdentifier

{
public:
NCOverlay(int state);
explicit NCOverlay(int state);

IFACEMETHODIMP_(ULONG) AddRef();
IFACEMETHODIMP GetOverlayInfo(PWSTR pwszIconFile, int cchMax, int *pIndex, DWORD *pdwFlags);
Expand All @@ -38,6 +39,7 @@ class NCOverlay : public IShellIconOverlayIdentifier
private:
long _referenceCount;
int _state;
std::ofstream m_logger;
};

#endif
#endif
4 changes: 3 additions & 1 deletion shell_integration/windows/NCUtil/CommunicationSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ bool CommunicationSocket::Connect(const std::wstring &pipename)
return true;
}

bool CommunicationSocket::SendMsg(const wchar_t* message) const
bool CommunicationSocket::SendMsg(const wchar_t* message, std::ofstream &logger) const
{
logger << "CommunicationSocket::SendMsg: " << (*message) << std::endl;

auto utf8_msg = StringUtil::toUtf8(message);

DWORD numBytesWritten = 0;
Expand Down
3 changes: 2 additions & 1 deletion shell_integration/windows/NCUtil/CommunicationSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <string>

Check failure on line 22 in shell_integration/windows/NCUtil/CommunicationSocket.h

View workflow job for this annotation

GitHub Actions / build

shell_integration/windows/NCUtil/CommunicationSocket.h:22:10 [clang-diagnostic-error]

'string' file not found
#include <vector>
#include <fstream>
#include <WinSock2.h>

class __declspec(dllexport) CommunicationSocket
Expand All @@ -34,7 +35,7 @@ class __declspec(dllexport) CommunicationSocket
bool Connect(const std::wstring& pipename);
bool Close();

bool SendMsg(const wchar_t*) const;
bool SendMsg(const wchar_t*, std::ofstream &logger) const;
bool ReadLine(std::wstring*);

HANDLE Event() { return _pipe; }
Expand Down
8 changes: 4 additions & 4 deletions shell_integration/windows/NCUtil/RemotePathChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
using namespace std;

// This code is run in a thread
void RemotePathChecker::workerThreadLoop()
void RemotePathChecker::workerThreadLoop(ofstream &logger)

Check warning on line 33 in shell_integration/windows/NCUtil/RemotePathChecker.cpp

View workflow job for this annotation

GitHub Actions / build

shell_integration/windows/NCUtil/RemotePathChecker.cpp:33:25 [readability-function-cognitive-complexity]

function 'workerThreadLoop' has cognitive complexity of 42 (threshold 25)
{
auto pipename = CommunicationSocket::DefaultPipePath();
bool connected = false;
Expand Down Expand Up @@ -62,7 +62,7 @@ void RemotePathChecker::workerThreadLoop()
lock.unlock();
if (!asked.count(filePath)) {
asked.insert(filePath);
socket.SendMsg(wstring(L"RETRIEVE_FILE_STATUS:" + filePath + L'\n').data());
socket.SendMsg(wstring(L"RETRIEVE_FILE_STATUS:" + filePath + L'\n').data(), logger);
}
lock.lock();
}
Expand Down Expand Up @@ -162,12 +162,12 @@ void RemotePathChecker::workerThreadLoop()



RemotePathChecker::RemotePathChecker()
RemotePathChecker::RemotePathChecker(ofstream &logger)

Check warning on line 165 in shell_integration/windows/NCUtil/RemotePathChecker.cpp

View workflow job for this annotation

GitHub Actions / build

shell_integration/windows/NCUtil/RemotePathChecker.cpp:165:1 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: _pending, _cache, _watchedDirectories, _newQueries
: _stop(false)
, _watchedDirectories(make_shared<const vector<wstring>>())
, _connected(false)
, _newQueries(CreateEvent(nullptr, FALSE, FALSE, nullptr))
, _thread([this]{ this->workerThreadLoop(); })
, _thread([this, &logger]{ this->workerThreadLoop(logger); })
{
}

Expand Down
7 changes: 4 additions & 3 deletions shell_integration/windows/NCUtil/RemotePathChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <memory>
#include <mutex>
#include <atomic>
#include <fstream>
#include <condition_variable>

#pragma once
Expand All @@ -36,7 +37,7 @@ class RemotePathChecker {
StateWarning,
StateNone
};
RemotePathChecker();
explicit RemotePathChecker(std::ofstream &logger);
~RemotePathChecker();
std::shared_ptr<const std::vector<std::wstring>> WatchedDirectories() const;
bool IsMonitoredPath(const wchar_t* filePath, int* state);
Expand Down Expand Up @@ -64,7 +65,7 @@ class RemotePathChecker {
HANDLE _newQueries;

std::thread _thread;
void workerThreadLoop();
void workerThreadLoop(std::ofstream &logger);
};

#endif
#endif
5 changes: 3 additions & 2 deletions src/gui/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,8 +659,9 @@ void Application::setupLogging()
logger->setLogDir(_logDir.isEmpty() ? ConfigFile().logDir() : _logDir);
}
logger->setLogExpire(_logExpire > 0 ? _logExpire : ConfigFile().logExpire());
logger->setLogFlush(_logFlush || ConfigFile().logFlush());
logger->setLogDebug(_logDebug || ConfigFile().logDebug());
logger->setLogFlush(true || _logFlush || ConfigFile().logFlush());
logger->setLogDebug(true || _logDebug || ConfigFile().logDebug());

if (!logger->isLoggingToFile() && ConfigFile().automaticLogDir()) {
logger->setupTemporaryFolderLogDir();
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/socketapi/socketapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ SocketApi::FileData SocketApi::FileData::parentFolder() const

void SocketApi::command_GET_MENU_ITEMS(const QString &argument, OCC::SocketListener *listener)
{
listener->sendMessage(QString("GET_MENU_ITEMS:BEGIN"));
listener->sendMessage(QString("GET_MENU_ITEMS:BEGIN"), true);
const QStringList files = split(argument);

// Find the common sync folder.
Expand Down

0 comments on commit f51f73a

Please sign in to comment.