Move cyclical logging to the new logging system per obat feedback.
This commit is contained in:
111
src/86box.c
111
src/86box.c
@@ -255,13 +255,8 @@ static volatile atomic_int pause_ack = 0;
|
|||||||
#ifndef RELEASE_BUILD
|
#ifndef RELEASE_BUILD
|
||||||
|
|
||||||
#define LOG_SIZE_BUFFER 1024 /* Log size buffer */
|
#define LOG_SIZE_BUFFER 1024 /* Log size buffer */
|
||||||
#define LOG_SIZE_BUFFER_CYCLIC_LINES 32 /* Cyclic log size buffer (number of lines that should be cehcked) */
|
|
||||||
#define LOG_MINIMUM_REPEAT_ORDER 4 /* Minimum repeat size */
|
|
||||||
|
|
||||||
static char buff[LOG_SIZE_BUFFER];
|
static char buff[LOG_SIZE_BUFFER];
|
||||||
static char cyclic_buff[LOG_SIZE_BUFFER_CYCLIC_LINES][LOG_SIZE_BUFFER];
|
|
||||||
static int32_t cyclic_last_line = 0;
|
|
||||||
static int32_t log_cycles = 0;
|
|
||||||
|
|
||||||
static int seen = 0;
|
static int seen = 0;
|
||||||
|
|
||||||
@@ -322,112 +317,6 @@ pclog_ex(const char *fmt, va_list ap)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
Starfrost, 7-8 January 2025:
|
|
||||||
|
|
||||||
For RIVA 128 emulation I needed a way to suppress logging if a repeated pattern of the same set of lines were found.
|
|
||||||
|
|
||||||
Implements a version of the Rabin-Karp algorithm https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
pclog_ex_cyclic(const char* fmt, va_list ap)
|
|
||||||
{
|
|
||||||
#ifndef RELEASE_BUILD
|
|
||||||
char temp[LOG_SIZE_BUFFER];
|
|
||||||
|
|
||||||
cyclic_last_line %= LOG_SIZE_BUFFER_CYCLIC_LINES;
|
|
||||||
|
|
||||||
vsprintf(temp, fmt, ap);
|
|
||||||
|
|
||||||
pclog_ensure_stdlog_open();
|
|
||||||
|
|
||||||
strncpy(cyclic_buff[cyclic_last_line], temp, LOG_SIZE_BUFFER);
|
|
||||||
|
|
||||||
uint32_t hashes[LOG_SIZE_BUFFER_CYCLIC_LINES] = {0};
|
|
||||||
|
|
||||||
// Random numbers
|
|
||||||
uint32_t base = 257;
|
|
||||||
uint32_t mod = 1000000007;
|
|
||||||
|
|
||||||
uint32_t repeat_order = 0;
|
|
||||||
bool is_cycle = false;
|
|
||||||
|
|
||||||
// compute the set of hashes for the current log buffer
|
|
||||||
for (int32_t log_line = 0; log_line < LOG_SIZE_BUFFER_CYCLIC_LINES; log_line++)
|
|
||||||
{
|
|
||||||
if (cyclic_buff[log_line][0] == '\0')
|
|
||||||
continue; // skip
|
|
||||||
|
|
||||||
for (int32_t log_line_char = 0; log_line_char < LOG_SIZE_BUFFER; log_line_char++)
|
|
||||||
{
|
|
||||||
hashes[log_line] = hashes[log_line] * base + cyclic_buff[log_line][log_line_char] % mod;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Now see if there are real cycles...
|
|
||||||
// We implement a minimum repeat size.
|
|
||||||
for (int32_t check_size = LOG_MINIMUM_REPEAT_ORDER; check_size < LOG_SIZE_BUFFER_CYCLIC_LINES / 2; check_size++)
|
|
||||||
{
|
|
||||||
//TODO: Log what we need for cycle 1.
|
|
||||||
//TODO: Command line option that lets us turn off this behaviour.
|
|
||||||
for (int32_t log_line_to_check = 0; log_line_to_check < check_size; log_line_to_check++)
|
|
||||||
{
|
|
||||||
if (hashes[log_line_to_check] == hashes[(log_line_to_check + check_size) % LOG_SIZE_BUFFER_CYCLIC_LINES])
|
|
||||||
{
|
|
||||||
repeat_order = check_size;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
is_cycle = (repeat_order != 0);
|
|
||||||
|
|
||||||
// if there still is a cycle..
|
|
||||||
if (is_cycle)
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_cycle)
|
|
||||||
{
|
|
||||||
if (cyclic_last_line % repeat_order == 0)
|
|
||||||
{
|
|
||||||
log_cycles++;
|
|
||||||
|
|
||||||
if (log_cycles == 1)
|
|
||||||
{
|
|
||||||
// 'Replay' the last few log entries so they actually show up
|
|
||||||
// Todo: is this right?
|
|
||||||
|
|
||||||
for (uint32_t index = cyclic_last_line - 1; index > (cyclic_last_line - repeat_order); index--)
|
|
||||||
{
|
|
||||||
// *very important* to prevent out of bounds index
|
|
||||||
uint32_t real_index = index % LOG_SIZE_BUFFER_CYCLIC_LINES;
|
|
||||||
fprintf(stdlog, "%s", cyclic_buff[real_index]);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
fprintf(stdlog, "%s", temp); // allow normal logging
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (log_cycles > 1 && log_cycles < 100)
|
|
||||||
fprintf(stdlog, "***** Cyclical Log Repeat of Order %d #%d *****\n", repeat_order, log_cycles);
|
|
||||||
else if (log_cycles == 100)
|
|
||||||
fprintf(stdlog, "Logged the same cycle 100 times...shutting up until something interesting happens\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
log_cycles = 0;
|
|
||||||
fprintf(stdlog, "%s", temp);
|
|
||||||
}
|
|
||||||
|
|
||||||
cyclic_last_line++;
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
pclog_toggle_suppr(void)
|
pclog_toggle_suppr(void)
|
||||||
|
|||||||
@@ -189,7 +189,6 @@ extern int config_changed; /* config has changed */
|
|||||||
#ifdef HAVE_STDARG_H
|
#ifdef HAVE_STDARG_H
|
||||||
extern void pclog_ex(const char *fmt, va_list ap);
|
extern void pclog_ex(const char *fmt, va_list ap);
|
||||||
extern void fatal_ex(const char *fmt, va_list ap);
|
extern void fatal_ex(const char *fmt, va_list ap);
|
||||||
extern void pclog_ex_cyclic(const char* fmt, va_list ap);
|
|
||||||
#endif
|
#endif
|
||||||
extern void pclog_toggle_suppr(void);
|
extern void pclog_toggle_suppr(void);
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
|
|||||||
@@ -12,9 +12,11 @@
|
|||||||
*
|
*
|
||||||
* Authors: Miran Grca, <mgrca8@gmail.com>
|
* Authors: Miran Grca, <mgrca8@gmail.com>
|
||||||
* Fred N. van Kempen, <decwiz@yahoo.com>
|
* Fred N. van Kempen, <decwiz@yahoo.com>
|
||||||
|
* Connor Hyde <mario64crashed@gmail.com, nomorestarfrost@gmail.com>
|
||||||
*
|
*
|
||||||
* Copyright 2021 Miran Grca.
|
* Copyright 2021 Miran Grca.
|
||||||
* Copyright 2021 Fred N. van Kempen.
|
* Copyright 2021 Fred N. van Kempen.
|
||||||
|
* Copyright 2025 Connor Hyde.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef EMU_LOG_H
|
#ifndef EMU_LOG_H
|
||||||
@@ -26,11 +28,16 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
|
#define LOG_SIZE_BUFFER 1024 /* Log size buffer */
|
||||||
|
#define LOG_SIZE_BUFFER_CYCLIC_LINES 32 /* Cyclic log size buffer (number of lines that should be cehcked) */
|
||||||
|
#define LOG_MINIMUM_REPEAT_ORDER 4 /* Minimum repeat size */
|
||||||
|
|
||||||
/* Function prototypes. */
|
/* Function prototypes. */
|
||||||
extern void log_set_suppr_seen(void *priv, int suppr_seen);
|
extern void log_set_suppr_seen(void *priv, int suppr_seen);
|
||||||
extern void log_set_dev_name(void *priv, char *dev_name);
|
extern void log_set_dev_name(void *priv, char *dev_name);
|
||||||
# ifdef HAVE_STDARG_H
|
# ifdef HAVE_STDARG_H
|
||||||
extern void log_out(void *priv, const char *fmt, va_list);
|
extern void log_out(void *priv, const char *fmt, va_list);
|
||||||
|
extern void log_out_cyclic(void* priv, const char *fmt, va_list);
|
||||||
extern void log_fatal(void *priv, const char *fmt, ...);
|
extern void log_fatal(void *priv, const char *fmt, ...);
|
||||||
# endif
|
# endif
|
||||||
extern void *log_open(char *dev_name);
|
extern void *log_open(char *dev_name);
|
||||||
|
|||||||
157
src/log.c
157
src/log.c
@@ -12,12 +12,15 @@
|
|||||||
*
|
*
|
||||||
* Authors: Miran Grca, <mgrca8@gmail.com>
|
* Authors: Miran Grca, <mgrca8@gmail.com>
|
||||||
* Fred N. van Kempen, <decwiz@yahoo.com>
|
* Fred N. van Kempen, <decwiz@yahoo.com>
|
||||||
|
* Connor Hyde <mario64crashed@gmail.com, nomorestarfrost@gmail.com>
|
||||||
*
|
*
|
||||||
* Copyright 2021 Miran Grca.
|
* Copyright 2021 Miran Grca.
|
||||||
* Copyright 2021 Fred N. van Kempen.
|
* Copyright 2021 Fred N. van Kempen.
|
||||||
|
* Copyright 2025 Connor Hyde.
|
||||||
*/
|
*/
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -40,9 +43,27 @@ typedef struct log_t {
|
|||||||
char *dev_name;
|
char *dev_name;
|
||||||
int seen;
|
int seen;
|
||||||
int suppr_seen;
|
int suppr_seen;
|
||||||
|
char cyclic_buff[LOG_SIZE_BUFFER_CYCLIC_LINES][LOG_SIZE_BUFFER]; // Cyclical log buffer. This is 32kb, might calloc?
|
||||||
|
int32_t cyclic_last_line;
|
||||||
|
int32_t log_cycles;
|
||||||
} log_t;
|
} log_t;
|
||||||
|
|
||||||
extern FILE *stdlog; /* file to log output to */
|
extern FILE *stdlog; /* file to log output to */
|
||||||
|
// Functions only used in this translation unit
|
||||||
|
void log_ensure_stdlog_open(void);
|
||||||
|
|
||||||
|
void
|
||||||
|
log_ensure_stdlog_open(void)
|
||||||
|
{
|
||||||
|
if (stdlog == NULL) {
|
||||||
|
if (log_path[0] != '\0') {
|
||||||
|
stdlog = plat_fopen(log_path, "w");
|
||||||
|
if (stdlog == NULL)
|
||||||
|
stdlog = stdout;
|
||||||
|
} else
|
||||||
|
stdlog = stdout;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
log_set_suppr_seen(void *priv, int suppr_seen)
|
log_set_suppr_seen(void *priv, int suppr_seen)
|
||||||
@@ -91,14 +112,7 @@ log_out(void *priv, const char *fmt, va_list ap)
|
|||||||
if (strcmp(fmt, "") == 0)
|
if (strcmp(fmt, "") == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (stdlog == NULL) {
|
log_ensure_stdlog_open();
|
||||||
if (log_path[0] != '\0') {
|
|
||||||
stdlog = plat_fopen(log_path, "w");
|
|
||||||
if (stdlog == NULL)
|
|
||||||
stdlog = stdout;
|
|
||||||
} else
|
|
||||||
stdlog = stdout;
|
|
||||||
}
|
|
||||||
|
|
||||||
vsprintf(temp, fmt, ap);
|
vsprintf(temp, fmt, ap);
|
||||||
if (log->suppr_seen && !strcmp(log->buff, temp))
|
if (log->suppr_seen && !strcmp(log->buff, temp))
|
||||||
@@ -117,6 +131,131 @@ log_out(void *priv, const char *fmt, va_list ap)
|
|||||||
fflush(stdlog);
|
fflush(stdlog);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
Starfrost, 7-8 January 2025:
|
||||||
|
|
||||||
|
For RIVA 128 emulation I needed a way to suppress logging if a repeated pattern of the same set of lines were found.
|
||||||
|
|
||||||
|
Implements a version of the Rabin-Karp algorithm https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
log_out_cyclic(void* priv, const char* fmt, va_list ap)
|
||||||
|
{
|
||||||
|
#ifndef RELEASE_BUILD
|
||||||
|
// get our new logging system instance.
|
||||||
|
log_t* log = (log_t*)priv;
|
||||||
|
|
||||||
|
// does the log actually exist?
|
||||||
|
if (!log)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// is the string empty?
|
||||||
|
if (fmt[0] == '\0')
|
||||||
|
return;
|
||||||
|
|
||||||
|
// ensure stdlog is open
|
||||||
|
log_ensure_stdlog_open();
|
||||||
|
|
||||||
|
char temp[LOG_SIZE_BUFFER] = {0};
|
||||||
|
|
||||||
|
log->cyclic_last_line %= LOG_SIZE_BUFFER_CYCLIC_LINES;
|
||||||
|
|
||||||
|
vsprintf(temp, fmt, ap);
|
||||||
|
|
||||||
|
log_copy(log, log->cyclic_buff[log->cyclic_last_line], temp, LOG_SIZE_BUFFER);
|
||||||
|
|
||||||
|
uint32_t hashes[LOG_SIZE_BUFFER_CYCLIC_LINES] = {0};
|
||||||
|
|
||||||
|
// Random numbers
|
||||||
|
uint32_t base = 257;
|
||||||
|
uint32_t mod = 1000000007;
|
||||||
|
|
||||||
|
uint32_t repeat_order = 0;
|
||||||
|
bool is_cycle = false;
|
||||||
|
|
||||||
|
// compute the set of hashes for the current log buffer
|
||||||
|
for (int32_t log_line = 0; log_line < LOG_SIZE_BUFFER_CYCLIC_LINES; log_line++)
|
||||||
|
{
|
||||||
|
if (log->cyclic_buff[log_line][0] == '\0')
|
||||||
|
continue; // skip
|
||||||
|
|
||||||
|
for (int32_t log_line_char = 0; log_line_char < LOG_SIZE_BUFFER; log_line_char++)
|
||||||
|
{
|
||||||
|
hashes[log_line] = hashes[log_line] * base + log->cyclic_buff[log_line][log_line_char] % mod;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Now see if there are real cycles...
|
||||||
|
// We implement a minimum repeat size.
|
||||||
|
for (int32_t check_size = LOG_MINIMUM_REPEAT_ORDER; check_size < LOG_SIZE_BUFFER_CYCLIC_LINES / 2; check_size++)
|
||||||
|
{
|
||||||
|
//TODO: Log what we need for cycle 1.
|
||||||
|
//TODO: Command line option that lets us turn off this behaviour.
|
||||||
|
for (int32_t log_line_to_check = 0; log_line_to_check < check_size; log_line_to_check++)
|
||||||
|
{
|
||||||
|
if (hashes[log_line_to_check] == hashes[(log_line_to_check + check_size) % LOG_SIZE_BUFFER_CYCLIC_LINES])
|
||||||
|
{
|
||||||
|
repeat_order = check_size;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
is_cycle = (repeat_order != 0);
|
||||||
|
|
||||||
|
// if there still is a cycle..
|
||||||
|
if (is_cycle)
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_cycle)
|
||||||
|
{
|
||||||
|
if (log->cyclic_last_line % repeat_order == 0)
|
||||||
|
{
|
||||||
|
log->log_cycles++;
|
||||||
|
|
||||||
|
if (log->log_cycles == 1)
|
||||||
|
{
|
||||||
|
// 'Replay' the last few log entries so they actually show up
|
||||||
|
// Todo: is this right?
|
||||||
|
|
||||||
|
for (uint32_t index = log->cyclic_last_line - 1; index > (log->cyclic_last_line - repeat_order); index--)
|
||||||
|
{
|
||||||
|
// *very important* to prevent out of bounds index
|
||||||
|
uint32_t real_index = index % LOG_SIZE_BUFFER_CYCLIC_LINES;
|
||||||
|
log_copy(log, temp, log->cyclic_buff[real_index], LOG_SIZE_BUFFER);
|
||||||
|
|
||||||
|
fprintf(stdlog, "%s", log->cyclic_buff[real_index]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// restore the original line
|
||||||
|
log_copy(log, temp, log->cyclic_buff[log->cyclic_last_line], LOG_SIZE_BUFFER);
|
||||||
|
|
||||||
|
fprintf(stdlog, "%s", temp); // allow normal logging
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (log->log_cycles > 1 && log->log_cycles < 100)
|
||||||
|
fprintf(stdlog, "***** Cyclical Log Repeat of Order %d #%d *****\n", repeat_order, log->log_cycles);
|
||||||
|
else if (log->log_cycles == 100)
|
||||||
|
fprintf(stdlog, "Logged the same cycle 100 times...shutting up until something interesting happens\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log->log_cycles = 0;
|
||||||
|
fprintf(stdlog, "%s", temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
log->cyclic_last_line++;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
log_fatal(void *priv, const char *fmt, ...)
|
log_fatal(void *priv, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
@@ -145,6 +284,8 @@ log_open(char *dev_name)
|
|||||||
|
|
||||||
log->dev_name = dev_name;
|
log->dev_name = dev_name;
|
||||||
log->suppr_seen = 1;
|
log->suppr_seen = 1;
|
||||||
|
log->cyclic_last_line = 0;
|
||||||
|
log->log_cycles = 0;
|
||||||
|
|
||||||
return (void *) log;
|
return (void *) log;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user