Skip to content

Commit f2bcbc9

Browse files
committed
Enable logging for netcon modules
Implementing chained appender initialization scenario for plog.
1 parent 156cba8 commit f2bcbc9

15 files changed

Lines changed: 175 additions & 225 deletions

File tree

Descent3/multi_dll_mgr.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,16 +333,20 @@ typedef void DLLFUNCCALL (*DLLMultiCall_fp)(int eventnum);
333333
typedef void DLLFUNCCALL (*DLLMultiScoreCall_fp)(int eventnum, void *data);
334334
typedef void DLLFUNCCALL (*DLLMultiInit_fp)(int *api_fp);
335335
typedef void DLLFUNCCALL (*DLLMultiClose_fp)();
336+
typedef void DLLFUNCCALL (*DLLMultiInitLogger_fp)(plog::Severity severity, plog::IAppender* appender);
336337
#else
337338
typedef void(DLLFUNCCALL *DLLMultiCall_fp)(int eventnum);
338339
typedef void(DLLFUNCCALL *DLLMultiScoreCall_fp)(int eventnum, void *data);
339340
typedef void(DLLFUNCCALL *DLLMultiInit_fp)(int *api_fp);
340341
typedef void(DLLFUNCCALL *DLLMultiClose_fp)();
342+
typedef void(DLLFUNCCALL *DLLMultiInitLogger_fp)(plog::Severity severity, plog::IAppender* appender);
341343
#endif
342-
DLLMultiScoreCall_fp DLLMultiScoreCall = NULL;
343-
DLLMultiCall_fp DLLMultiCall = NULL;
344-
DLLMultiInit_fp DLLMultiInit = NULL;
345-
DLLMultiClose_fp DLLMultiClose = NULL;
344+
DLLMultiScoreCall_fp DLLMultiScoreCall = nullptr;
345+
DLLMultiCall_fp DLLMultiCall = nullptr;
346+
DLLMultiInit_fp DLLMultiInit = nullptr;
347+
DLLMultiClose_fp DLLMultiClose = nullptr;
348+
DLLMultiInitLogger_fp DLLMultiInitLogger = nullptr;
349+
346350
// dllmultiiInfo DLLMultiInfo;
347351
// The DLL needs these too.
348352
#define MAXTEXTITEMS 100
@@ -584,9 +588,10 @@ void FreeMultiDLL() {
584588
if (!std::filesystem::remove(Multi_conn_dll_name)) {
585589
LOG_WARNING << "Couldn't delete the tmp dll";
586590
}
587-
DLLMultiCall = NULL;
588-
DLLMultiInit = NULL;
589-
DLLMultiClose = NULL;
591+
DLLMultiCall = nullptr;
592+
DLLMultiInit = nullptr;
593+
DLLMultiClose = nullptr;
594+
DLLMultiInitLogger = nullptr;
590595
}
591596

592597
// Loads the Multi dll. Returns 1 on success, else 0 on failure
@@ -667,6 +672,14 @@ int LoadMultiDLL(const char *name) {
667672
FreeMultiDLL();
668673
return 0;
669674
}
675+
// Initialize logger. For backward compatibility, lack of DLLMultiInitLogger symbols is non-fatal error.
676+
DLLMultiInitLogger = (DLLMultiInitLogger_fp)mod_GetSymbol(&MultiDLLHandle, "DLLMultiInitLogger", 12);
677+
if (!DLLMultiInitLogger) {
678+
mod_GetLastError();
679+
LOG_WARNING << "Couldn't get a handle to the dll function DLLMultiInitLogger!";
680+
} else {
681+
DLLMultiInitLogger(plog::get()->getMaxSeverity(), plog::get());
682+
}
670683

671684
if (first) {
672685
// Jeff: Linux dies if you try to free a DLL/so during atexit

logger/log.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
#pragma once
2020

21-
// This is interface header for chosen logger library. Currently, this is plog.
22-
#include <plog/Log.h>
21+
#include <plog/Log.h> // This is interface header for chosen logger library. Currently, this is plog.
22+
#include <plog/Init.h> // Required for modules.
2323

2424
/*
2525
In case of swapping to another solution here should be redefined following macros:

netcon/descent3onlineclient/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ target_link_libraries(Descent3_Online_TCP_IP PRIVATE
1818
ddio
1919
inetfile
2020
httplib
21+
logger
2122
misc
2223
module
2324
ui

netcon/descent3onlineclient/chat_api.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "chat_api.h"
3232
#include "crossplat.h"
3333
#include "grdefs.h"
34+
#include "log.h"
3435
#include "odtstrings.h"
3536
#include "networking.h"
3637

@@ -48,12 +49,6 @@ extern nw_Asyncgethostbyname_fp DLLnw_Asyncgethostbyname;
4849
typedef int (*PollUI_fp)();
4950
extern PollUI_fp DLLPollUI;
5051

51-
#ifndef RELEASE
52-
#define DLLmprintf(...) DLLDebug_ConsolePrintf(__VA_ARGS__)
53-
#else
54-
#define DLLmprintf(...)
55-
#endif
56-
5752
typedef void (*Debug_ConsolePrintf_fp)(int n, const char *format, ...);
5853
extern Debug_ConsolePrintf_fp DLLDebug_ConsolePrintf;
5954
#define MAXCHATBUFFER 500
@@ -189,8 +184,7 @@ int ConnectToChatServer(const char *serveraddr, int16_t chat_port, char *nicknam
189184
} while (rcode == 0);
190185

191186
if (rcode != 1) {
192-
DLLmprintf(0, "Unable to gethostbyname(\"%s\").\n", serveraddr);
193-
DLLmprintf(0, "WSAGetLastError() returned %d.\n", WSAGetLastError());
187+
LOG_ERROR.printf("Unable to gethostbyname(\"%s\"): error %d", serveraddr, WSAGetLastError());
194188
DLLnw_Asyncgethostbyname(nullptr, NW_AGHBN_CANCEL, nullptr);
195189
return 0;
196190
}
@@ -207,16 +201,16 @@ int ConnectToChatServer(const char *serveraddr, int16_t chat_port, char *nicknam
207201
if (EINPROGRESS == ret || 0 == ret)
208202
#endif
209203
{
210-
DLLmprintf(0, "Beginning socket connect\n");
204+
LOG_INFO << "Beginning socket connect";
211205
Socket_connecting = 1;
212206
return 0;
213207
}
214208
} else {
215209
// This should never happen, connect should always return WSAEWOULDBLOCK
216-
DLLmprintf(0, "connect returned too soon!\n");
210+
LOG_WARNING << "connect returned too soon!";
217211
Socket_connecting = 1;
218212
Socket_connected = 1;
219-
DLLmprintf(0, "Socket connected, sending user and nickname request\n");
213+
LOG_INFO << "Socket connected, sending user and nickname request";
220214
snprintf(signon_str, sizeof(signon_str), "/USER %s %s %s :%s", "user", "user", "user", Chat_tracker_id);
221215
SendChatString(signon_str, 1);
222216
snprintf(signon_str, sizeof(signon_str), "/NICK %s", Nick_name);
@@ -246,7 +240,7 @@ int ConnectToChatServer(const char *serveraddr, int16_t chat_port, char *nicknam
246240
// Writable -- that means it's connected
247241
if (select(Chatsock + 1, nullptr, &write_fds, nullptr, &timeout)) {
248242
Socket_connected = 1;
249-
DLLmprintf(0, "Socket connected, sending user and nickname request\n");
243+
LOG_INFO << "Socket connected, sending user and nickname request";
250244
snprintf(signon_str, sizeof(signon_str), "/USER %s %s %s :%s", "user", "user", "user", Chat_tracker_id);
251245
SendChatString(signon_str, 1);
252246
snprintf(signon_str, sizeof(signon_str), "/NICK %s", Nick_name);
@@ -258,7 +252,7 @@ int ConnectToChatServer(const char *serveraddr, int16_t chat_port, char *nicknam
258252
FD_SET(Chatsock, &error_fds);
259253
// error -- that means it's not going to connect
260254
if (select(Chatsock + 1, nullptr, nullptr, &error_fds, &timeout)) {
261-
DLLmprintf(0, "Select returned an error!\n");
255+
LOG_ERROR << "select() returned an error!";
262256
return -1;
263257
}
264258
return 0;
@@ -462,29 +456,27 @@ const char *ChatGetString() {
462456
if (WSAEWOULDBLOCK != lerror && 0 != lerror)
463457
#endif
464458
{
465-
DLLmprintf(0, "recv caused an error: %d\n", lerror);
459+
LOG_ERROR.printf("recv() caused an error: %d", lerror);
466460
}
467461
return nullptr;
468462
}
469463
if (bytesread) {
470464
ch[1] = '\0';
471-
// DLLmprintf(0,ch);
472465
if ((ch[0] == 0x0a) || (ch[0] == 0x0d)) {
473466
if (Input_chat_buffer[0] == '\0') {
474467
// Blank line, ignore it
475468
return nullptr;
476469
}
477470
strcpy(return_string, Input_chat_buffer);
478471
Input_chat_buffer[0] = '\0';
479-
// DLLmprintf(0,"->|%s\n",return_string);
480472
p = ParseIRCMessage(return_string, MSG_REMOTE);
481473

482474
return p;
483475
}
484476
strcat(Input_chat_buffer, ch);
485477
} else {
486478
// Select said we had read data, but 0 bytes read means disconnected
487-
DLLmprintf(0, "Disconnected! Doh!");
479+
LOG_ERROR << "Disconnected! Doh!";
488480
AddChatCommandToQueue(CC_DISCONNECTED, nullptr, 0);
489481
return nullptr;
490482
}
@@ -931,7 +923,7 @@ char *ParseIRCMessage(char *Line, int iMode) {
931923
if (stricmp(szCmd, "376") == 0) // end of motd, trigger autojoin...
932924
{
933925
if (!Chat_server_connected) {
934-
DLLmprintf(0, "Connected to chat server!\n");
926+
LOG_INFO << "Connected to chat server!";
935927
Chat_server_connected = 1;
936928
// We want to make sure we know our nick. This is somewhat of a hack
937929
strcpy(Nick_name, GetWordNum(0, szRemLine + 1));

netcon/descent3onlineclient/dip_gametrack.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@
2626

2727
#include "dip_gametrack.h"
2828
#include "httpclient.h"
29-
30-
#if (defined(LOGGER) && (!defined(RELEASE)))
31-
#define DLLmprintf(...) DLLDebug_ConsolePrintf(__VA_ARGS__)
32-
#else
33-
#define DLLmprintf(...)
34-
#endif
29+
#include "log.h"
3530

3631
typedef void (*Debug_ConsolePrintf_fp)(int n, const char *format, ...);
3732
extern Debug_ConsolePrintf_fp DLLDebug_ConsolePrintf;
@@ -88,7 +83,7 @@ void DecodeApiAnswer(std::stringstream &data) {
8883
}
8984

9085
void FetchApi() {
91-
DLLmprintf(0, "fetch api.\n");
86+
LOG_INFO << "fetch api.";
9287
D3::HttpClient http_client(TSETSEFLYAPI_HOST);
9388
std::stringstream input;
9489
auto result = http_client.Get(TSETSEFLYAPI_URI);

0 commit comments

Comments
 (0)