Skip to content

Commit 722ce2e

Browse files
committed
Add logger implementation and show logs on the status bar
1 parent e82ae52 commit 722ce2e

32 files changed

Lines changed: 712 additions & 237 deletions

Common/UString.cpp

Lines changed: 95 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,50 @@ void WINAPI FreeStringBuffer(LPWSTR lpbuf)
986986

987987
/////////////////////////////////////////////////////////////////////////////
988988

989+
void __stdcall _FormatStringA(LPWSTR& lpszData, LPCSTR lpszFormat, va_list va, UINT uCodePage = CP_ACP)
990+
{
991+
int n, nRet;
992+
LPSTR lp;
993+
994+
n = 255;
995+
lp = (LPSTR)malloc(sizeof(CHAR) * 255);
996+
nRet = _vsnprintf(lp, n, lpszFormat, va);
997+
while (nRet < 0)
998+
{
999+
n += 255;
1000+
lp = (LPSTR)realloc(lp, sizeof(CHAR) * n);
1001+
nRet = _vsnprintf(lp, n, lpszFormat, va);
1002+
}
1003+
if (lpszData)
1004+
lpszData = ReAllocStringLenA(lpszData, lp, nRet, uCodePage);
1005+
else
1006+
lpszData = AllocStringLenA(lp, nRet, uCodePage);
1007+
free(lp);
1008+
}
1009+
1010+
void __stdcall _FormatStringW(LPWSTR& lpszData, LPCWSTR lpszFormat, va_list va)
1011+
{
1012+
int n, nRet;
1013+
LPWSTR lp;
1014+
1015+
n = 255;
1016+
lp = (LPWSTR)malloc(sizeof(WCHAR) * 255);
1017+
nRet = _vsnwprintf(lp, n, lpszFormat, va);
1018+
while (nRet < 0)
1019+
{
1020+
n += 255;
1021+
lp = (LPWSTR)realloc(lp, sizeof(WCHAR) * n);
1022+
nRet = _vsnwprintf(lp, n, lpszFormat, va);
1023+
}
1024+
if (lpszData)
1025+
lpszData = ReAllocStringLen(lpszData, lp, nRet);
1026+
else
1027+
lpszData = AllocStringLen(lp, nRet);
1028+
free(lp);
1029+
}
1030+
1031+
/////////////////////////////////////////////////////////////////////////////
1032+
9891033
CMyStringW::CMyStringW()
9901034
: m_lpszData(NULL), m_lpszLast(NULL), m_uCodePage(CP_ACP)
9911035
{
@@ -1023,8 +1067,16 @@ CMyStringW::CMyStringW(LPCUWSTR lpszString)
10231067
}
10241068
#endif
10251069

1070+
#ifndef NO_LOADSTRING
1071+
CMyStringW::CMyStringW(UINT uID)
1072+
: m_lpszData(NULL), m_lpszLast(NULL), m_uCodePage(CP_ACP)
1073+
{
1074+
LoadString(uID);
1075+
}
1076+
#endif
1077+
10261078
CMyStringW::CMyStringW(LPCSTR lpszString, UINT uCodePage)
1027-
: m_lpszData(NULL), m_lpszLast(NULL)
1079+
: m_lpszData(NULL), m_lpszLast(NULL), m_uCodePage(uCodePage)
10281080
{
10291081
#ifndef NO_LOADSTRING
10301082
if (HIWORD(lpszString))
@@ -1079,6 +1131,48 @@ CMyStringW::CMyStringW(char ch, size_t nCount, UINT uCodePage)
10791131
m_lpszData = NULL;
10801132
}
10811133

1134+
WINAPIV CMyStringW::CMyStringW(_ConstructorWithVarArg, LPCWSTR lpszString, ...)
1135+
: m_lpszData(NULL), m_lpszLast(NULL), m_uCodePage(CP_ACP)
1136+
{
1137+
va_list va;
1138+
va_start(va, lpszString);
1139+
_FormatStringW(m_lpszData, lpszString, va);
1140+
va_end(va);
1141+
}
1142+
1143+
WINAPIV CMyStringW::CMyStringW(_ConstructorWithVarArg, LPCSTR lpszString, ...)
1144+
: m_lpszData(NULL), m_lpszLast(NULL), m_uCodePage(CP_ACP)
1145+
{
1146+
va_list va;
1147+
va_start(va, lpszString);
1148+
_FormatStringA(m_lpszData, lpszString, va);
1149+
va_end(va);
1150+
}
1151+
1152+
#ifdef ALIGNMENT_MACHINE
1153+
WINAPIV CMyStringW::CMyStringW(_ConstructorWithVarArg, LPCUWSTR lpszString, ...)
1154+
: m_lpszData(NULL), m_lpszLast(NULL), m_uCodePage(CP_ACP)
1155+
{
1156+
CMyStringW str(lpszString);
1157+
va_list va;
1158+
va_start(va, lpszString);
1159+
_FormatStringW(m_lpszData, str, va);
1160+
va_end(va);
1161+
}
1162+
#endif
1163+
1164+
#ifndef NO_LOADSTRING
1165+
WINAPIV CMyStringW::CMyStringW(_ConstructorWithVarArg, UINT uID, ...)
1166+
: m_lpszData(NULL), m_lpszLast(NULL), m_uCodePage(CP_ACP)
1167+
{
1168+
CMyStringW str(MAKEINTRESOURCE(uID));
1169+
va_list va;
1170+
va_start(va, uID);
1171+
_FormatStringW(m_lpszData, str, va);
1172+
va_end(va);
1173+
}
1174+
#endif
1175+
10821176
const CMyStringW& CMyStringW::operator = (LPCWSTR lpszString)
10831177
{
10841178
Empty();
@@ -1858,48 +1952,6 @@ bool CMyStringW::DeleteString(size_t uStart, size_t uLength)
18581952

18591953
/////////////////////////////////////////////////////////////////////////////
18601954

1861-
void __stdcall _FormatStringA(LPWSTR& lpszData, LPCSTR lpszFormat, va_list va, UINT uCodePage = CP_ACP)
1862-
{
1863-
int n, nRet;
1864-
LPSTR lp;
1865-
1866-
n = 255;
1867-
lp = (LPSTR) malloc(sizeof(CHAR) * 255);
1868-
nRet = _vsnprintf(lp, n, lpszFormat, va);
1869-
while (nRet < 0)
1870-
{
1871-
n += 255;
1872-
lp = (LPSTR) realloc(lp, sizeof(CHAR) * n);
1873-
nRet = _vsnprintf(lp, n, lpszFormat, va);
1874-
}
1875-
if (lpszData)
1876-
lpszData = ReAllocStringLenA(lpszData, lp, nRet, uCodePage);
1877-
else
1878-
lpszData = AllocStringLenA(lp, nRet, uCodePage);
1879-
free(lp);
1880-
}
1881-
1882-
void __stdcall _FormatStringW(LPWSTR& lpszData, LPCWSTR lpszFormat, va_list va)
1883-
{
1884-
int n, nRet;
1885-
LPWSTR lp;
1886-
1887-
n = 255;
1888-
lp = (LPWSTR) malloc(sizeof(WCHAR) * 255);
1889-
nRet = _vsnwprintf(lp, n, lpszFormat, va);
1890-
while (nRet < 0)
1891-
{
1892-
n += 255;
1893-
lp = (LPWSTR) realloc(lp, sizeof(WCHAR) * n);
1894-
nRet = _vsnwprintf(lp, n, lpszFormat, va);
1895-
}
1896-
if (lpszData)
1897-
lpszData = ReAllocStringLen(lpszData, lp, nRet);
1898-
else
1899-
lpszData = AllocStringLen(lp, nRet);
1900-
free(lp);
1901-
}
1902-
19031955
BOOL WINAPIV CMyStringW::Format(LPCSTR lpszString, ...)
19041956
{
19051957
va_list va;

Common/UString.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,55 @@ class CMyStringW
108108
public:
109109
CMyStringW(wchar_t wch, size_t nCount);
110110
CMyStringW(char ch, size_t nCount, UINT uCodePage = CP_ACP);
111+
inline CMyStringW(wchar_t wch, int nCount) : CMyStringW(wch, static_cast<size_t>(nCount)) {}
112+
inline CMyStringW(char ch, int nCount, UINT uCodePage = CP_ACP) : CMyStringW(ch, static_cast<size_t>(nCount), uCodePage) {}
111113
CMyStringW(const CMyStringW& _string);
112114
CMyStringW(LPCSTR lpszString, UINT uCodePage = CP_ACP);
113115
CMyStringW(LPCWSTR lpszString);
114116
#ifdef ALIGNMENT_MACHINE
115117
CMyStringW(LPCUWSTR lpszString);
118+
#endif
119+
#ifndef NO_LOADSTRING
120+
explicit CMyStringW(UINT uID);
121+
#endif
122+
private:
123+
struct _ConstructorWithVarArg {
124+
inline explicit _ConstructorWithVarArg()
125+
{
126+
}
127+
};
128+
WINAPIV CMyStringW(_ConstructorWithVarArg, LPCWSTR lpszString, ...);
129+
WINAPIV CMyStringW(_ConstructorWithVarArg, LPCSTR lpszString, ...);
130+
#ifdef ALIGNMENT_MACHINE
131+
WINAPIV CMyStringW(_ConstructorWithVarArg, LPCUWSTR lpszString, ...);
132+
#endif
133+
#ifndef NO_LOADSTRING
134+
WINAPIV CMyStringW(_ConstructorWithVarArg, UINT uID, ...);
135+
#endif
136+
public:
137+
template <typename ...T>
138+
explicit inline WINAPIV CMyStringW(LPCWSTR lpszString, T... args)
139+
: CMyStringW(_ConstructorWithVarArg{}, lpszString, args...)
140+
{
141+
}
142+
template <typename ...T>
143+
explicit inline WINAPIV CMyStringW(LPCSTR lpszString, T... args)
144+
: CMyStringW(_ConstructorWithVarArg{}, lpszString, args...)
145+
{
146+
}
147+
#ifdef ALIGNMENT_MACHINE
148+
template <typename ...T>
149+
explicit inline WINAPIV CMyStringW(LPCUWSTR lpszString, T... args)
150+
: CMyStringW(_ConstructorWithVarArg{}, lpszString, args...)
151+
{
152+
}
153+
#endif
154+
#ifndef NO_LOADSTRING
155+
template <typename ...T>
156+
explicit inline WINAPIV CMyStringW(UINT uID, T... args)
157+
: CMyStringW(_ConstructorWithVarArg{}, uID, args...)
158+
{
159+
}
116160
#endif
117161
CMyStringW();
118162
~CMyStringW();

EasySFTP/EasySFTP.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "IDList.h"
1616
#include "FileStrm.h"
17+
#include "Logger.h"
1718

1819
CMainApplication theApp;
1920

@@ -325,7 +326,20 @@ int CMainApplication::ExitInstance()
325326
if (m_pUnkThreadRef)
326327
m_pUnkThreadRef->Release();
327328
if (m_pEasySFTPRoot)
329+
{
330+
if (m_pMyLogger)
331+
{
332+
IEasySFTPRoot2* pRoot2;
333+
if (SUCCEEDED(m_pEasySFTPRoot->QueryInterface(IID_IEasySFTPRoot2, reinterpret_cast<void**>(&pRoot2))))
334+
{
335+
pRoot2->RemoveLogger(m_pMyLogger);
336+
pRoot2->Release();
337+
}
338+
m_pMyLogger->Release();
339+
m_pMyLogger = nullptr;
340+
}
328341
m_pEasySFTPRoot->Release();
342+
}
329343
if (m_pidlEasySFTP)
330344
::CoTaskMemFree(m_pidlEasySFTP);
331345
if (m_hImageListAddrButtonsL)
@@ -665,12 +679,24 @@ HRESULT CMainApplication::InitEasySFTP()
665679
if (FAILED(hr))
666680
return hr;
667681

668-
IEasySFTPInternal* pInternal;
669-
hr = m_pEasySFTPRoot->QueryInterface(IID_IEasySFTPInternal, (void**) &pInternal);
670-
if (SUCCEEDED(hr))
671682
{
672-
pInternal->SetEmulateRegMode(m_bEmulatingRegistry);
673-
pInternal->Release();
683+
IEasySFTPInternal* pInternal;
684+
hr = m_pEasySFTPRoot->QueryInterface(IID_IEasySFTPInternal, (void**)&pInternal);
685+
if (SUCCEEDED(hr))
686+
{
687+
pInternal->SetEmulateRegMode(m_bEmulatingRegistry);
688+
pInternal->Release();
689+
}
690+
}
691+
{
692+
IEasySFTPRoot2* pRoot2 = nullptr;
693+
hr = m_pEasySFTPRoot->QueryInterface(IID_IEasySFTPRoot2, reinterpret_cast<void**>(&pRoot2));
694+
if (SUCCEEDED(hr))
695+
{
696+
m_pMyLogger = new CEasySFTPLogger();
697+
pRoot2->AddLogger(m_pMyLogger);
698+
pRoot2->Release();
699+
}
674700
}
675701

676702
// check where to register

EasySFTP/EasySFTP.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ class CMainApplication : public CMyApplication
271271
//CMySimpleArray<void*> m_aObjectTransferring;
272272
//CMyStringKeyListW<UINT> m_mapIcon;
273273
IEasySFTPOldRoot* m_pEasySFTPRoot;
274+
IEasySFTPLogger* m_pMyLogger;
274275
PIDLIST_ABSOLUTE m_pidlEasySFTP;
275276
bool m_bEmulatingRegistry;
276277
bool m_bIsRegisteredAsUserClass;

EasySFTP/EasySFTP.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@
232232
<ClCompile Include="ExplrDDE.cpp" />
233233
<ClCompile Include="Func.cpp" />
234234
<ClCompile Include="LFileVw.cpp" />
235+
<ClCompile Include="Logger.cpp" />
235236
<ClCompile Include="MainWnd.cpp" />
236237
<ClCompile Include="MainWnd2.cpp" />
237238
<ClCompile Include="MWndConn.cpp" />
@@ -255,6 +256,7 @@
255256
<ClInclude Include="ExplrDDE.h" />
256257
<ClInclude Include="Func.h" />
257258
<ClInclude Include="LFileVw.h" />
259+
<ClInclude Include="Logger.h" />
258260
<ClInclude Include="MainWnd.h" />
259261
<ClInclude Include="Option.h" />
260262
<ClInclude Include="Resource.h" />

EasySFTP/EasySFTP.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
<ClCompile Include="SyncDlg.cpp">
5858
<Filter>ソース ファイル</Filter>
5959
</ClCompile>
60+
<ClCompile Include="Logger.cpp">
61+
<Filter>ソース ファイル</Filter>
62+
</ClCompile>
6063
</ItemGroup>
6164
<ItemGroup>
6265
<ClInclude Include="AddrCBox.h">
@@ -98,6 +101,9 @@
98101
<ClInclude Include="SyncDlg.h">
99102
<Filter>ヘッダー ファイル</Filter>
100103
</ClInclude>
104+
<ClInclude Include="Logger.h">
105+
<Filter>ヘッダー ファイル</Filter>
106+
</ClInclude>
101107
</ItemGroup>
102108
<ItemGroup>
103109
<None Include="AddrBtns.bmp">

EasySFTP/Func.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,6 @@ extern "C" int strcmplen(const char* string1, size_t str1len, const char* string
3030

3131
///////////////////////////////////////////////////////////////////////////////
3232

33-
#ifdef _DEBUG
34-
extern "C" void __stdcall OutputDebugFTPMessage(int code, LPCWSTR msg)
35-
{
36-
CMyStringW str;
37-
str.Format(L"FTP: %d %s\n", code, msg);
38-
::OutputDebugString(str);
39-
}
40-
#endif
41-
4233
void __stdcall _StartHook(CMyWindow* pWnd);
4334
bool __stdcall _EndHook();
4435

EasySFTP/Func.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,3 @@ extern "C" inline int strcmplen2(const char* string1, const char* string2, size_
1717
extern "C" int __stdcall MyMessageBoxW(HWND hWnd, LPCWSTR lpszText, LPCWSTR lpszCaption, UINT uType);
1818

1919
extern "C" bool __stdcall MyShellOpenW(HWND hWnd, LPCWSTR lpszFileName);
20-
21-
#ifdef _DEBUG
22-
extern "C" void __stdcall OutputDebugFTPMessage(int code, LPCWSTR msg);
23-
#else
24-
extern "C" inline void __stdcall OutputDebugFTPMessage(int code, LPCWSTR msg) { }
25-
#endif

EasySFTP/Logger.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
EasySFTP - Copyright (C) 2026 Kuri-Applications
3+
4+
Logger.cpp - implementations of IEasySFTPLogger
5+
*/
6+
7+
#include "stdafx.h"
8+
#include "EasySFTP.h"
9+
#include "Logger.h"
10+
11+
#include "MainWnd.h"
12+
13+
STDMETHODIMP CEasySFTPLogger::QueryInterface(REFIID riid, void** ppv)
14+
{
15+
if (!ppv)
16+
return E_POINTER;
17+
if (!IsEqualIID(riid, IID_IUnknown) && !IsEqualIID(riid, IID_IEasySFTPLogger))
18+
return E_NOINTERFACE;
19+
*ppv = static_cast<IEasySFTPLogger*>(this);
20+
AddRef();
21+
return S_OK;
22+
}
23+
24+
STDMETHODIMP CEasySFTPLogger::Log(EasySFTPLogLevel Level, BSTR Message, long HResult)
25+
{
26+
#ifdef _DEBUG
27+
{
28+
CMyStringW str;
29+
::MyBSTRToString(Message, str);
30+
str.InsertString(L"[EasySFTP] ", 0);
31+
str += L'\n';
32+
OutputDebugString(str);
33+
}
34+
#endif
35+
if (Level == EasySFTPLogLevel::Debug)
36+
return S_OK;
37+
auto* pWnd = static_cast<CMainWindow*>(theApp.m_pMainWnd);
38+
if (!pWnd)
39+
return S_OK;
40+
pWnd->SetStatusText(Message);
41+
return S_OK;
42+
}

0 commit comments

Comments
 (0)