Make Debug_Log* functions to crossplatform

This commit is contained in:
Azamat H. Hackimov 2024-06-01 01:55:16 +03:00
parent 9416458f9c
commit 6c5403c232
6 changed files with 80 additions and 92 deletions

View File

@ -1,4 +1,5 @@
set(CPPS
debug.cpp
$<$<PLATFORM_ID:Darwin,Linux>:
lnxdebug.cpp
lnxmono.cpp

55
ddebug/debug.cpp Normal file
View File

@ -0,0 +1,55 @@
/*
* Descent 3
* Copyright (C) 2024 Parallax Software
* Copyright (C) 2024 Descent Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "debug.h"
void Debug_LogClose();
FILE *Debug_logfile = nullptr;
bool Debug_Logfile(const char *filename) {
if (Debug_logfile == nullptr) {
Debug_logfile = fopen(filename, "w");
if (Debug_logfile == nullptr) {
Debug_MessageBox(OSMBOX_OK, "Debug", "FYI Logfile couldn't be created.");
return false;
}
atexit(Debug_LogClose);
}
Debug_LogWrite("BEGINNING LOG\n\n");
return true;
}
void Debug_LogWrite(const char *str) {
if (Debug_logfile)
fwrite(str, strlen(str), 1, Debug_logfile);
}
void Debug_LogClose() {
if (Debug_logfile) {
Debug_LogWrite("\nEND LOG");
fclose(Debug_logfile);
Debug_logfile = nullptr;
}
}

View File

@ -104,7 +104,7 @@
*/
#ifndef DEBUG_H
#define DEBUG_H
#include "pstypes.h"
// ---------------------------------------------------------------------------
// Debug system is a member of the 'platform' library.
// ---------------------------------------------------------------------------
@ -150,7 +150,8 @@ constexpr const int OSMBOX_OKCANCEL = 5;
// Functions
// ---------------------------------------------------------------------------
extern bool Debug_break;
// if we are running under a debugger, then pass true
// if we are running under a debugger, then pass true
bool Debug_Init(bool debugger, bool mono_debug);
// Does a messagebox with a stack dump
// Messagebox shows topstring, then stack dump, then bottomstring
@ -159,12 +160,14 @@ int Debug_ErrorBox(int type, const char *topstring, const char *title, const cha
// displays a message box
// Returns the same values as the Win32 MessageBox() function
int Debug_MessageBox(int type, const char *title, const char *str);
// these functions deal with debug spew support
// these functions deal with debug spew support
bool Debug_Logfile(const char *filename);
void Debug_LogWrite(const char *str);
void Debug_ConsolePrintf(int n, const char *format, ...);
void Debug_ConsolePrintfAt(int n, int row, int col, const char *format, ...);
// DEBUGGING MACROS
// DEBUGGING MACROS
// Break into the debugger, if this feature was enabled in Debug_init()
#if !defined(RELEASE)
#include "debugbreak.h"

View File

@ -46,67 +46,28 @@
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include "debug.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <memory.h>
#include <unistd.h>
#include "debug.h"
#include "mono.h"
static int Debug_logfile = 0;
void Debug_LogClose();
bool Debug_Logfile(const char *filename) {
if (Debug_logfile == -1) {
Debug_logfile = open(filename, O_CREAT | O_WRONLY, S_IREAD | S_IWRITE);
if (Debug_logfile == -1) {
Debug_MessageBox(OSMBOX_OK, "Debug", "FYI Logfile couldn't be created.");
return false;
}
atexit(Debug_LogClose);
}
Debug_LogWrite("BEGINNING LOG\n\n");
return true;
}
void Debug_LogWrite(const char *str) {
if (Debug_logfile > -1)
write(Debug_logfile, str, strlen(str));
}
void Debug_LogClose() {
if (Debug_logfile > -1) {
Debug_LogWrite("\nEND LOG");
close(Debug_logfile);
Debug_logfile = -1;
}
}
#ifdef _DEBUG
#define MAX_MONO_BUFFER 2048
#else
#define MAX_MONO_BUFFER 32
#endif
static char Mono_buffer[MAX_MONO_BUFFER];
static char Mono_buffer[MAX_MONO_LENGTH];
void Debug_ConsolePrintf(int n, const char *format, ...) {
va_list marker;
va_start(marker, format);
std::vsnprintf(Mono_buffer, MAX_MONO_BUFFER, format, marker);
std::vsnprintf(Mono_buffer, MAX_MONO_LENGTH, format, marker);
va_end(marker);
Debug_LogWrite(Mono_buffer);
if (n == 0) {
printf("%s", Mono_buffer);
int end = strlen(Mono_buffer) - 1;
if ((end > 1) && (Mono_buffer[end] != 0x0a) && (Mono_buffer[end] != 0x0d)) {
printf("\n");
Debug_LogWrite("\n");
}
}
}

View File

@ -64,6 +64,13 @@
#ifndef _MONO_H
#define _MONO_H
#include "debug.h"
#ifdef _DEBUG
#define MAX_MONO_LENGTH 2048
#else
#define MAX_MONO_LENGTH 256
#endif
#if (!defined(RELEASE)) && defined(LOGGER)
// Prints a formatted string to the debug window
#define mprintf(...) Debug_ConsolePrintf(__VA_ARGS__)

View File

@ -20,57 +20,18 @@
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include "Debug.h"
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "debug.h"
#include "mono.h"
// ---------------------------------------------------------------------------
// console debugging functions
#ifndef RELEASE
#define MAX_MONO_LENGTH 2048
#else
#define MAX_MONO_LENGTH 256
#endif
static char Mono_buffer[MAX_MONO_LENGTH];
static int Debug_logfile = -1;
void Debug_LogClose();
bool Debug_Logfile(const char *filename) {
if (Debug_logfile == -1) {
Debug_logfile = _open(filename, _O_CREAT | _O_WRONLY | _O_TEXT, _S_IREAD | _S_IWRITE);
if (Debug_logfile == -1) {
Debug_MessageBox(OSMBOX_OK, "Debug", "FYI Logfile couldn't be created.");
return false;
}
atexit(Debug_LogClose);
}
Debug_LogWrite("BEGINNING LOG\n\n");
return true;
}
void Debug_LogWrite(const char *str) {
if (Debug_logfile > -1)
_write(Debug_logfile, str, strlen(str));
}
void Debug_LogClose() {
if (Debug_logfile > -1) {
Debug_LogWrite("\nEND LOG");
_close(Debug_logfile);
Debug_logfile = -1;
}
}
void Debug_ConsolePrintf(int n, const char *format, ...) {
char *ptr = Mono_buffer;
std::va_list args;