feat: improve logging

This commit is contained in:
45Tatami 2023-05-23 21:29:20 +02:00
parent 3447bac6e5
commit bd438dc77c
1 changed files with 22 additions and 6 deletions

View File

@ -7,6 +7,7 @@
#include <deque> #include <deque>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <iostream>
#include <locale> #include <locale>
#include <mutex> #include <mutex>
#include <string> #include <string>
@ -16,6 +17,8 @@
#include <ws2tcpip.h> #include <ws2tcpip.h>
#include <strsafe.h> #include <strsafe.h>
using std::cerr;
using std::endl;
using std::filesystem::path; using std::filesystem::path;
using std::lock_guard; using std::lock_guard;
using std::mutex; using std::mutex;
@ -59,13 +62,24 @@ std::deque<wstring> msg_q;
SOCKET _connect(); SOCKET _connect();
bool _send(SOCKET &, string const &); bool _send(SOCKET &, string const &);
wstring getEditBoxText(HWND win_hndl, int item) { /**
* try creating a message box, on failure write to stderr
*/
void tryMessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
{
if (0 == MessageBox(hWnd, lpText, lpCaption, uType)) {
cerr << "[TCPSender] err: unable to show message box; " << lpCaption << ": " << lpText << endl;
}
}
wstring getEditBoxText(HWND win_hndl, int item)
{
if (win_hndl == NULL) if (win_hndl == NULL)
return L""; return L"";
HWND edit = GetDlgItem(win_hndl, item); HWND edit = GetDlgItem(win_hndl, item);
if (edit == NULL) { if (edit == NULL) {
MessageBox(NULL, L"Could not get editbox handle", L"Error", 0); tryMessageBox(NULL, L"Could not get editbox handle", L"Error", 0);
return L""; return L"";
} }
@ -92,6 +106,8 @@ void log(string const& msg)
char* buf = (char *) GlobalAlloc(GPTR, msg.length() + 1); char* buf = (char *) GlobalAlloc(GPTR, msg.length() + 1);
msg.copy(buf, msg.length()); msg.copy(buf, msg.length());
PostMessage(win_hndl, WM_USR_LOG, (WPARAM) NULL, (LPARAM) buf); PostMessage(win_hndl, WM_USR_LOG, (WPARAM) NULL, (LPARAM) buf);
cerr << "[TCPSender] " << msg << endl;
} }
void log(wstring const& msg) void log(wstring const& msg)
@ -114,7 +130,7 @@ void save_config(path const& filepath, wstring const& remote, bool connect)
f << connect; f << connect;
} }
else { else {
MessageBox(NULL, L"Could not open config file for writing", L"Error", 0); tryMessageBox(NULL, L"Could not open config file for writing", L"Error", 0);
} }
} }
@ -303,7 +319,7 @@ config_done:
} }
} }
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID)
{ {
switch (ul_reason_for_call) switch (ul_reason_for_call)
{ {
@ -319,7 +335,7 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved
NULL, DialogProc, 0); NULL, DialogProc, 0);
if (win_hndl == NULL) { if (win_hndl == NULL) {
MessageBox(NULL, L"Could not open plugin dialog", L"Error", 0); tryMessageBox(NULL, L"Could not open plugin dialog", L"Error", 0);
return false; return false;
} }
ShowWindow(win_hndl, SW_NORMAL); ShowWindow(win_hndl, SW_NORMAL);
@ -437,7 +453,7 @@ bool _send(SOCKET &sock, string const &msg) {
bool ProcessSentence(wstring & sentence, SentenceInfo sentenceInfo) bool ProcessSentence(wstring & sentence, SentenceInfo sentenceInfo)
{ {
if (sentenceInfo["current select"]) { if (sentenceInfo["current select"]) {
log("Received sentence"); log("received sentence");
lock_guard<mutex> lock{conn_mut}; lock_guard<mutex> lock{conn_mut};