Skip to content
This repository has been archived by the owner on Dec 25, 2023. It is now read-only.

Commit

Permalink
DLL Projects:
Browse files Browse the repository at this point in the history
- LOGGER.DLL: Finished:
    - Finally discarded the visual console in-game feature due to several problems with memory leaks managing the char* buffer on DIV memory from DLL.
    - Remain the log to file feature.
    - Updated LOGGER.PRG file.
  • Loading branch information
José Miguel Sánchez Fernández committed Jun 8, 2020
1 parent bd5b0df commit 54ee928
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 218 deletions.
Binary file modified DLL/LOGGER.DLL
Binary file not shown.
198 changes: 41 additions & 157 deletions DLL/LOGGER/LOGGER.CPP
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* ----------------------------------------------------------------------------
* LOGGER.DLL - File logger and console for DIV Games Studio 2.
* LOGGER.DLL - File logger for DIV Games Studio 2.
* (C) VisualStudioEX3, José Miguel Sánchez Fernández - 2020
* DIV Games Studio 2 (C) Hammer Technologies - 1998, 1999
* ---------------------------------------------------------------------------- */
Expand All @@ -8,103 +8,49 @@

void init()
{
consoleMaxLines = _max(getparm(), default_console_max_lines);
consoleStrLen = _max(getparm(), default_console_str_length);
consoleEnable = getparm();
fileEnable = getparm();
struct tm *now = getDateTime();

separator = strAlloc(consoleStrLen);
memset(separator, '=', consoleStrLen);
// Log filename format: "LOGS\yyyyMMdd\HHmmss.LOG"
char filename[12];

struct tm *now = getDateTime();
// Move or create base log's folder:
mkdir(log_folder);
chdir(log_folder);

if (fileEnable)
{
// Log filename format: "LOGS\yyyyMMdd\HHmmss.LOG"
char filename[12];

// Move or create base log's folder:
mkdir(log_folder);
chdir(log_folder);

// Move or create today's folder:
sprintf(filename,
"%i%02i%02i", // DOS foldername 8 max chars length.
now->tm_year + 1900,
now->tm_mon + 1,
now->tm_mday);

mkdir(filename);
chdir(filename);

// Create log file:
sprintf(filename,
"%02i%02i%02i.LOG", // DOS filename 8:3 format.
now->tm_hour,
now->tm_min,
now->tm_sec);
// Move or create today's folder:
sprintf(filename,
"%i%02i%02i", // DOS foldername 8 max chars length.
now->tm_year + 1900,
now->tm_mon + 1,
now->tm_mday);

file = div_fopen(filename, "a+");
mkdir(filename);
chdir(filename);

if (file == NULL)
{
fileEnable = 0;
}
// Create log file:
sprintf(filename,
"%02i%02i%02i.LOG", // DOS filename 8:3 format.
now->tm_hour,
now->tm_min,
now->tm_sec);

// Restore initial path:
for (int i = 0; i < 2; i++)
{
chdir("..");
}
}
file = div_fopen(filename, "a+");

if (consoleEnable)
// Restore initial path:
for (int i = 0; i < 2; i++)
{
consoleBuffer = strAlloc(console_buffer_size);
cclear();
chdir("..");
}
consoleVisible = 0;

char* timeStamp = strAlloc(consoleStrLen);
strftime(timeStamp, consoleStrLen, "Log started on %c", now);
_log(timeStamp);
div_free(timeStamp);
_log(separator);

retval(RESULT_OK);
}

void isFileEnabled()
{
retval(fileEnable);
}

void isConsoleEnabled()
{
retval(consoleEnable);
}

void cwrite(int index, char* text)
{
int len = _min(strlen(text), consoleStrLen);
memmove(&consoleBuffer[index * console_full_line_len], text, len);
consoleBuffer[(index * console_full_line_len) + len] = '\0';
}

char* cread(int index)
{
return &consoleBuffer[index * console_full_line_len];
}

void cclear()
{
memset(consoleBuffer, '\0', console_buffer_size);
consoleWriteIndex = 0;
char timeStamp[80];
strftime(timeStamp, 80, "Log started on %c", now);
log(timeStamp);
log(separator);
}

void _log(char* message)
int log(char* message)
{
if (fileEnable)
if (log_ok)
{
// "[dd/MM/yyyy HH:mm:ss] %message%"
struct tm *now = getDateTime();
Expand All @@ -114,95 +60,33 @@ void _log(char* message)
now->tm_min,
now->tm_sec,
message);

return RESULT_OK;
}

if (consoleEnable)
{
if (consoleWriteIndex == consoleMaxLines - 1)
{
for (int x = 0, y = 1; y < consoleMaxLines; x++, y++)
{
cwrite(x, cread(y));
}
}

cwrite(consoleWriteIndex, message);
consoleWriteIndex = _min(++consoleWriteIndex, consoleMaxLines - 1);
}
}

void log()
{
if (enable)
{
_log(getStrParm());
}
retval(RESULT_OK);
}

void show()
{
if (consoleEnable)
{
consoleVisible = 1;
}
retval(RESULT_OK);
return RESULT_ERROR;
}

void hide()
void div_log()
{
if (consoleEnable)
{
consoleVisible = 0;
}
retval(RESULT_OK);
}

void clear()
{
if (consoleEnable)
{
cclear();
}
retval(RESULT_OK);
}

void post_process_buffer(void)
{
if (consoleEnable && consoleVisible)
{
// TODO: Calculate rect space occuped by visible text lines.
// TODO: Draw rect ocupped by visible text lines.

for (int i = 0; i < consoleMaxLines; i++)
{
div_text_out(cread(i), 0, i * 10);
}
}
retval(log(getStrParm()));
}

void __export divlibrary(LIBRARY_PARAMS)
{
COM_export("setup_logger", init, 4);
COM_export("is_logfile_enabled", isFileEnabled, 0);
COM_export("is_log_console_enabled", isConsoleEnabled, 0);
COM_export("log", log, 1);
COM_export("show_log_console", show, 0);
COM_export("hide_log_console", hide, 0);
COM_export("clear_log_console", clear, 0);
COM_export("log", div_log, 1);
}

void __export divmain(COMMON_PARAMS)
{
GLOBAL_IMPORT();

DIV_export("post_process_buffer", post_process_buffer);
GLOBAL_IMPORT();
init();
}

void __export divend(COMMON_PARAMS)
{
_log(separator);
_log("Program terminated.");
log(separator);
log("Program terminated.");

div_fclose(file);
}
33 changes: 5 additions & 28 deletions DLL/LOGGER/LOGGER.H
Original file line number Diff line number Diff line change
@@ -1,41 +1,18 @@
/* ----------------------------------------------------------------------------
* LOGGER.DLL - File logger and console for DIV Games Studio 2.
* LOGGER.DLL - File logger for DIV Games Studio 2.
* (C) VisualStudioEX3, José Miguel Sánchez Fernández - 2020
* DIV Games Studio 2 (C) Hammer Technologies - 1998, 1999
* ---------------------------------------------------------------------------- */

#include <direct.h>
#include "..\common.h"

#define default_console_str_length 80
#define default_console_max_lines 15
#define log_folder "LOGS"
#define log_ok file != NULL
#define separator "-------------------------------------------------------------------------------"

#define enable fileEnable || consoleEnable
#define console_full_line_len consoleStrLen + 1 // strlen + \0
#define console_buffer_size console_full_line_len * consoleMaxLines

int fileEnable;
FILE* file;

char* separator;

int consoleEnable;
int consoleVisible;
int consoleStrLen;
int consoleMaxLines; // Max lines to show.
char* consoleBuffer; // (strLength + 1) * maxLines.
int consoleWriteIndex; // Line to write.

void _log(char* message);
void cwrite(int index, char* text);
char* cread(int index);
void cclear();

void init();
void isFileEnabled();
void isConsoleEnabled();
void log();
void show();
void hide();
void clear();
int log(char* message);
void div_log();
36 changes: 3 additions & 33 deletions PRG/TESTS/DLLTESTS/LOGGER.PRG
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,8 @@ program LOGGER_DLL_TEST;

import "logger.dll";

global
int fpg;
string message;

begin
fpg = load_fpg("help\help.fpg");
put_screen(fpg, 1);

setup_logger(true, true, 80, 10);

//log("Test log...");
//log("Another test log...");
//log("One more time testing log...");

//for (x = 0; x < 15; x++)
// message = "Test iteration: " + itoa(x);
// log(message);
//end

loop
if (key(_s))
show_log_console();
end

if (key(_h))
hide_log_console();
end

if (key(_space))
log(itoa(timer[0]));
end

frame;
end
log("Test log...");
log("Another test log...");
log("One more time testing log...");
end

0 comments on commit 54ee928

Please sign in to comment.