Commit 1852e560 authored by cRTrn13's avatar cRTrn13

Debug logging code + misc fixes.

parent f0d386c9
......@@ -2,12 +2,13 @@
#include "configfile.h"
#include "file.h"
#include "../util/macro.h"
#include "../util/logging.h"
uint8_t *configfile_record(CONFIGFILE *kf, enum configfile_types type, uint16_t *entries, size_t *entry_len)
{
size_t pos = 0, len = 0;
while (pos + 4 <= len) {
while (pos + 4 <= kf->size) {
len = MKINT_BE24(kf->buf + pos + 1);
if (entries) {
......@@ -18,8 +19,11 @@ uint8_t *configfile_record(CONFIGFILE *kf, enum configfile_types type, uint16_t
*entry_len = MKINT_BE32(kf->buf + pos + 6);
}
if (kf->buf[pos] == type)
if (kf->buf[pos] == type) {
DEBUG(DBG_CONFIGFILE, "CONFIGFILE record 0x%02x retrieved (0x%08x)\n", type, kf->buf + pos + 10);
return kf->buf + pos + 10; // only return ptr to first byte of entry
}
pos += len;
}
......@@ -32,6 +36,8 @@ CONFIGFILE *configfile_open(const char *path)
FILE_H *fp = NULL;
CONFIGFILE *kf = malloc(sizeof(CONFIGFILE));
DEBUG(DBG_CONFIGFILE, "Opening configfile %s... (0x%08x)\n", path, kf);
if ((fp = file_open(path, "rb"))) {
file_seek(fp, 0, SEEK_END);
kf->size = file_tell(fp);
......@@ -43,8 +49,6 @@ CONFIGFILE *configfile_open(const char *path)
file_close(fp);
X_FREE(fp);
return kf;
}
......@@ -53,6 +57,8 @@ CONFIGFILE *configfile_open(const char *path)
void configfile_close(CONFIGFILE *kf)
{
DEBUG(DBG_CONFIGFILE, "configfile closed (0x%08x)\n", kf);
X_FREE(kf->buf);
X_FREE(kf);
}
......@@ -4,6 +4,7 @@
#include "file.h"
#include "../util/macro.h"
#include "../util/logging.h"
FILE_H *file_open_linux(const char* filename, const char *mode);
void file_close_linux(FILE_H *file);
......@@ -16,6 +17,9 @@ int file_write_linux(FILE_H *file, uint8_t *buf, int64_t size);
void file_close_linux(FILE_H *file)
{
fclose((FILE *)file->internal);
DEBUG(DBG_FILE, "Closed LINUX file (0x%08x)\n", file);
X_FREE(file);
}
......@@ -44,6 +48,7 @@ FILE_H *file_open_linux(const char* filename, const char *mode)
FILE *fp = NULL;
FILE_H *file = malloc(sizeof(FILE_H));
DEBUG(DBG_CONFIGFILE, "Opening LINUX file %s... (0x%08x)\n", filename, file);
file->close = file_close_linux;
file->seek = file_seek_linux;
file->read = file_read_linux;
......@@ -56,5 +61,9 @@ FILE_H *file_open_linux(const char* filename, const char *mode)
return file;
}
DEBUG(DBG_FILE, "Error opening file! (0x%08x)\n", file);
X_FREE(file);
return NULL;
}
#include <string.h>
#include "logging.h"
const uint32_t master_mask = 0xffff; // this is only temporary
char out[512];
#define HEX_PRINT(X,Y) { int zz; for(zz = 0; zz < Y; zz++) fprintf(stderr, "%02X", X[zz]); fprintf(stderr, "\n"); }
char *print_hex(uint8_t *buf, int count)
{
memset(out, 0, count);
int zz;
for(zz = 0; zz < count; zz++) {
sprintf(out + (zz * 2), "%02X", buf[zz]);
}
return out;
}
void debug(char *file, int line, uint32_t mask, const char *format, ...)
{
uint32_t type = (mask & master_mask) & 0xfffe,
verbose = !((!(master_mask & 1)) & (mask & 1));
if (type && verbose) {
char buffer[512];
va_list args;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
fprintf(stderr, "%s:%d: %s", file, line, buffer);
}
}
#ifndef LOGGING_H_
#define LOGGING_H_
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#define DEBUG(X,Y,...) debug(__FILE__,__LINE__,X,Y,##__VA_ARGS__)
enum {
DBG_CONFIGFILE = 2,
DBG_FILE = 4,
DBG_AACS = 8,
DBG_MKB = 16,
} debug_mask;
char *print_hex(uint8_t *str, int count);
void debug(char *file, int line, uint32_t mask, const char *format, ...);
#endif /* LOGGING_H_ */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment