Add support for using a callback for writing debug messages

This commit is contained in:
Cameron Gutman 2014-10-31 22:27:18 -07:00
parent 7fb3ef3f79
commit 058ad2df23
7 changed files with 24 additions and 22 deletions

View File

@ -4,7 +4,9 @@
static int stage = STAGE_NONE; static int stage = STAGE_NONE;
static CONNECTION_LISTENER_CALLBACKS listenerCallbacks; static CONNECTION_LISTENER_CALLBACKS listenerCallbacks;
static CONNECTION_LISTENER_CALLBACKS originalCallbacks; static CONNECTION_LISTENER_CALLBACKS originalCallbacks;
static PLATFORM_CALLBACKS platformCallbacks;
// This is used for debug prints so it's not declared static
PLATFORM_CALLBACKS platformCallbacks;
static int alreadyTerminated; static int alreadyTerminated;
@ -161,13 +163,13 @@ int LiStartConnection(IP_ADDRESS host, PSTREAM_CONFIGURATION streamConfig, PCONN
Limelog("Initializing platform..."); Limelog("Initializing platform...");
listenerCallbacks.stageStarting(STAGE_PLATFORM_INIT); listenerCallbacks.stageStarting(STAGE_PLATFORM_INIT);
err = initializePlatformSockets(&platformCallbacks); err = initializePlatformSockets();
if (err != 0) { if (err != 0) {
Limelog("failed: %d\n", err); Limelog("failed: %d\n", err);
listenerCallbacks.stageFailed(STAGE_PLATFORM_INIT, err); listenerCallbacks.stageFailed(STAGE_PLATFORM_INIT, err);
goto Cleanup; goto Cleanup;
} }
err = initializePlatformThreads(&platformCallbacks); err = initializePlatformThreads();
if (err != 0) { if (err != 0) {
Limelog("failed: %d\n", err); Limelog("failed: %d\n", err);
listenerCallbacks.stageFailed(STAGE_PLATFORM_INIT, err); listenerCallbacks.stageFailed(STAGE_PLATFORM_INIT, err);

View File

@ -91,9 +91,11 @@ typedef struct _CONNECTION_LISTENER_CALLBACKS {
} CONNECTION_LISTENER_CALLBACKS, *PCONNECTION_LISTENER_CALLBACKS; } CONNECTION_LISTENER_CALLBACKS, *PCONNECTION_LISTENER_CALLBACKS;
typedef void(*PlatformThreadStart)(void); typedef void(*PlatformThreadStart)(void);
typedef void(*PlatformDebugPrint)(char* string);
typedef struct _PLATFORM_CALLBACKS { typedef struct _PLATFORM_CALLBACKS {
PlatformThreadStart threadStart; PlatformThreadStart threadStart;
PlatformDebugPrint debugPrint;
} PLATFORM_CALLBACKS, *PPLATFORM_CALLBACKS; } PLATFORM_CALLBACKS, *PPLATFORM_CALLBACKS;
int LiStartConnection(IP_ADDRESS host, PSTREAM_CONFIGURATION streamConfig, PCONNECTION_LISTENER_CALLBACKS clCallbacks, int LiStartConnection(IP_ADDRESS host, PSTREAM_CONFIGURATION streamConfig, PCONNECTION_LISTENER_CALLBACKS clCallbacks,

View File

@ -27,11 +27,13 @@
#endif #endif
#include <stdio.h> #include <stdio.h>
#include "Limelight.h"
#if defined(LC_WINDOWS_PHONE) || defined(LC_WINDOWS) #if defined(LC_WINDOWS_PHONE) || defined(LC_WINDOWS)
extern WCHAR DbgBuf[512]; extern char DbgBuf[512];
extern PLATFORM_CALLBACKS platformCallbacks;
#define Limelog(s, ...) \ #define Limelog(s, ...) \
swprintf(DbgBuf, sizeof(DbgBuf) / sizeof(WCHAR), L ## s, ##__VA_ARGS__); \ sprintf(DbgBuf, s, ##__VA_ARGS__); \
OutputDebugStringW(DbgBuf) platformCallbacks.debugPrint(DbgBuf)
#else #else
#define Limelog printf #define Limelog printf
#endif #endif

View File

@ -80,7 +80,7 @@ int enableNoDelay(SOCKET s) {
return 0; return 0;
} }
int initializePlatformSockets(PPLATFORM_CALLBACKS plCallbacks) { int initializePlatformSockets(void) {
#if defined(LC_WINDOWS) || defined(LC_WINDOWS_PHONE) #if defined(LC_WINDOWS) || defined(LC_WINDOWS_PHONE)
WSADATA data; WSADATA data;
return WSAStartup(MAKEWORD(2, 0), &data); return WSAStartup(MAKEWORD(2, 0), &data);

View File

@ -33,5 +33,5 @@ typedef ssize_t SOCK_RET;
SOCKET connectTcpSocket(IP_ADDRESS dstaddr, unsigned short port); SOCKET connectTcpSocket(IP_ADDRESS dstaddr, unsigned short port);
SOCKET bindUdpSocket(void); SOCKET bindUdpSocket(void);
int enableNoDelay(SOCKET s); int enableNoDelay(SOCKET s);
int initializePlatformSockets(PPLATFORM_CALLBACKS plCallbacks); int initializePlatformSockets(void);
void cleanupPlatformSockets(void); void cleanupPlatformSockets(void);

View File

@ -2,14 +2,13 @@
#include "Platform.h" #include "Platform.h"
#if defined(LC_WINDOWS_PHONE) || defined(LC_WINDOWS) #if defined(LC_WINDOWS_PHONE) || defined(LC_WINDOWS)
WCHAR DbgBuf[512]; CHAR DbgBuf[512];
#endif #endif
#if defined(LC_WINDOWS) || defined(LC_WINDOWS_PHONE) #if defined(LC_WINDOWS) || defined(LC_WINDOWS_PHONE)
PLT_MUTEX thread_list_lock; static PLT_MUTEX thread_list_lock;
PLT_THREAD *pending_thread_head; static PLT_THREAD *pending_thread_head;
PLT_THREAD *thread_head; static PLT_THREAD *thread_head;
PPLATFORM_CALLBACKS platformCallbacks;
#else #else
void* ThreadProc(void* context) { void* ThreadProc(void* context) {
struct thread_context *ctx = (struct thread_context *)context; struct thread_context *ctx = (struct thread_context *)context;
@ -208,7 +207,7 @@ int PltCreateThread(ThreadEntry entry, void* context, PLT_THREAD *thread) {
PltUnlockMutex(&thread_list_lock); PltUnlockMutex(&thread_list_lock);
// Make a callback to managed code to ask for a thread to grab this // Make a callback to managed code to ask for a thread to grab this
platformCallbacks->threadStart(); platformCallbacks.threadStart();
err = 0; err = 0;
} }
@ -308,14 +307,8 @@ int PltWaitForEvent(PLT_EVENT *event) {
#endif #endif
} }
int initializePlatformThreads(PPLATFORM_CALLBACKS plCallbacks) { int initializePlatformThreads(void) {
#if defined(LC_WINDOWS) || defined(LC_WINDOWS_PHONE) #if defined(LC_WINDOWS) || defined(LC_WINDOWS_PHONE)
pending_thread_head = thread_head = NULL;
// This data is stored in static memory in Connection.c, so we don't
// bother making our own copy
platformCallbacks = plCallbacks;
return PltCreateMutex(&thread_list_lock); return PltCreateMutex(&thread_list_lock);
#else #else
return 0; return 0;
@ -324,6 +317,9 @@ int initializePlatformThreads(PPLATFORM_CALLBACKS plCallbacks) {
void cleanupPlatformThreads(void) { void cleanupPlatformThreads(void) {
#if defined(LC_WINDOWS) || defined(LC_WINDOWS_PHONE) #if defined(LC_WINDOWS) || defined(LC_WINDOWS_PHONE)
LC_ASSERT(pending_thread_head == NULL);
LC_ASSERT(thread_head == NULL);
PltDeleteMutex(&thread_list_lock); PltDeleteMutex(&thread_list_lock);
#else #else
#endif #endif

View File

@ -35,7 +35,7 @@ typedef struct _PLT_EVENT {
#error Unsupported platform #error Unsupported platform
#endif #endif
int initializePlatformThreads(PPLATFORM_CALLBACKS plCallbacks); int initializePlatformThreads(void);
void cleanupPlatformThreads(void); void cleanupPlatformThreads(void);
int PltCreateMutex(PLT_MUTEX *mutex); int PltCreateMutex(PLT_MUTEX *mutex);