Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
234 changes: 73 additions & 161 deletions common/IDebugLog.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "common/IDebugLog.h"
#include <share.h>
#include "common/IDebugLog.h"
#include "common/IFileStream.h"
#include <shlobj.h>

std::FILE * IDebugLog::logFile = NULL;
std::FILE *IDebugLog::logFile = nullptr;
char IDebugLog::sourceBuf[16] = { 0 };
char IDebugLog::headerText[16] = { 0 };
char IDebugLog::formatBuf[8192] = { 0 };
Expand All @@ -12,54 +12,46 @@ int IDebugLog::rightMargin = 0;
int IDebugLog::cursorPos = 0;
int IDebugLog::inBlock = 0;
bool IDebugLog::autoFlush = true;
IDebugLog::LogLevel IDebugLog::logLevel = IDebugLog::kLevel_DebugMessage;
IDebugLog::LogLevel IDebugLog::printLevel = IDebugLog::kLevel_Message;

IDebugLog::IDebugLog()
{
//
}

IDebugLog::IDebugLog(const char * name)
{
bool IDebugLog::logToFolder = false;
char IDebugLog::cPath[] = {};
IDebugLog::LogLevel IDebugLog::logLevel = kLevel_DebugMessage;
IDebugLog::LogLevel IDebugLog::printLevel = kLevel_Message;

IDebugLog::IDebugLog(const char *name, const bool useLogsFolder) {
logToFolder = useLogsFolder;
GetCurrentDirectoryA(MAX_PATH, cPath); // Find CWD (e.g. C:/Steam/SteamApps/Common/Fallout New Vegas)
Open(name);
}

IDebugLog::~IDebugLog()
{
if(logFile)
fclose(logFile);
}
IDebugLog::~IDebugLog() { if (logFile) { fclose(logFile); } }

void IDebugLog::Open(const char * path)
{
logFile = _fsopen(path, "w", _SH_DENYWR);
void IDebugLog::Open(const char *path) {
if (strlen(path) > MAX_PATH) { return; } // Guard against max path

if(!logFile)
{
UInt32 id = 0;
char name[1024];
char name[1024] = { 0 };
if (logToFolder) {
if (sprintf_s(name, sizeof(name), "%s\\%s\\%s", cPath, "Logs", path) < 0) { return; }
IFileStream::MakeAllDirs(name);
}
else { if (sprintf_s(name, sizeof(name), "%s", path) < 0) { return; } }

do
{
sprintf_s(name, sizeof(name), "%s%d", path, id);
logFile = logFile ? _fsopen(name, "w", _SH_DENYWR) : nullptr;
if (!logFile) {
UInt32 id = 0;
while(!logFile && (id < 5)) {
if (sprintf_s(name, sizeof(name), "%s%d", path, id) < 0) { return; }
logFile = logFile ? _fsopen(name, "w", _SH_DENYWR) : nullptr;
id++;

logFile = NULL;
logFile = _fsopen(name, "w", _SH_DENYWR);
}
while(!logFile && (id < 5));
}
}

void IDebugLog::OpenRelative(int folderID, const char * relPath)
{
void IDebugLog::OpenRelative(const int folderID, const char *relPath) {
char path[MAX_PATH];

ASSERT(SUCCEEDED(SHGetFolderPath(NULL, folderID, NULL, SHGFP_TYPE_CURRENT, path)));

strcat_s(path, sizeof(path), relPath);

IFileStream::MakeAllDirs(path);

Open(path);
Expand All @@ -71,19 +63,12 @@ void IDebugLog::OpenRelative(int folderID, const char * relPath)
* @param message the message
* @param source the source of the message, or NULL to use the previous source
*/
void IDebugLog::Message(const char * message, const char * source)
{
if(source)
SetSource(source);
void IDebugLog::Message(const char * message, const char * source) {
if (source) { SetSource(source); }

if(inBlock)
{
SeekCursor(RoundToTab((indentLevel * 4) + strlen(headerText)));
}
else
{
if (inBlock) { SeekCursor(RoundToTab((indentLevel * 4) + strlen(headerText))); }
else {
SeekCursor(indentLevel * 4);

PrintText(headerText);
}

Expand All @@ -97,8 +82,7 @@ void IDebugLog::Message(const char * message, const char * source)
* @note It is impossible to set the source of a formatted message.
* The previous source will be used.
*/
void IDebugLog::FormattedMessage(const char * fmt, ...)
{
void IDebugLog::FormattedMessage(const char * fmt, ...) {
va_list argList;

va_start(argList, fmt);
Expand All @@ -113,32 +97,22 @@ void IDebugLog::FormattedMessage(const char * fmt, ...)
* @note It is impossible to set the source of a formatted message.
* The previous source will be used.
*/
void IDebugLog::FormattedMessage(const char * fmt, va_list args)
{
void IDebugLog::FormattedMessage(const char * fmt, const va_list args) {
vsprintf_s(formatBuf, sizeof(formatBuf), fmt, args);
Message(formatBuf);
}

void IDebugLog::Log(LogLevel level, const char * fmt, va_list args)
{
bool log = (level <= logLevel);
bool print = (level <= printLevel);

if(log || print)
vsprintf_s(formatBuf, sizeof(formatBuf), fmt, args);
void IDebugLog::Log(const LogLevel level, const char * fmt, const va_list args) {
const bool log = (level <= logLevel);
const bool print = (level <= printLevel);

if(log)
Message(formatBuf);

if(print)
printf("%s\n", formatBuf);
if(log || print) { vsprintf_s(formatBuf, sizeof(formatBuf), fmt, args); }
if(log) { Message(formatBuf); }
if(print) { printf("%s\n", formatBuf); }
}

/**
* Set the current message source
*/
void IDebugLog::SetSource(const char * source)
{
// Set the current message source
void IDebugLog::SetSource(const char * source) {
strcpy_s(sourceBuf, sizeof(sourceBuf), source);
strcpy_s(headerText, sizeof(headerText), "[ ]\t");

Expand All @@ -149,100 +123,57 @@ void IDebugLog::SetSource(const char * source)
*tgt = *src;
}

/**
* Clear the current message source
*/
void IDebugLog::ClearSource(void)
{
sourceBuf[0] = 0;
}
bool IDebugLog::GetLogFolderOption() { return logToFolder; }
bool IDebugLog::SetLogFolderOption(const bool option) { return logToFolder = option; }

/**
* Increase the indentation level
*/
void IDebugLog::Indent(void)
{
indentLevel++;
}
// Clear the current message source
void IDebugLog::ClearSource() { sourceBuf[0] = 0; }

/**
* Decrease the indentation level
*/
void IDebugLog::Outdent(void)
{
if(indentLevel)
indentLevel--;
}
// Increase the indentation level
void IDebugLog::Indent() { indentLevel++; }

/**
* Enter a logical block
*/
void IDebugLog::OpenBlock(void)
{
SeekCursor(indentLevel * 4);
// Decrease the indentation level
void IDebugLog::Outdent() { if(indentLevel) { indentLevel--; } }

// Enter a logical block
void IDebugLog::OpenBlock() {
SeekCursor(indentLevel * 4);
PrintText(headerText);

inBlock = 1;
}

/**
* Close a logical block
*/
void IDebugLog::CloseBlock(void)
{
inBlock = 0;
}
// Close a logical block
void IDebugLog::CloseBlock() { inBlock = 0; }

/**
* Enable/disable autoflush
*
* @param inAutoFlush autoflush state
*/
void IDebugLog::SetAutoFlush(bool inAutoFlush)
{
autoFlush = inAutoFlush;
}
void IDebugLog::SetAutoFlush(const bool inAutoFlush) { autoFlush = inAutoFlush; }

/**
* Print spaces to the log
*
* If possible, tabs are used instead of spaces.
*/
void IDebugLog::PrintSpaces(int numSpaces)
{
int originalNumSpaces = numSpaces;

if(logFile)
{
while(numSpaces > 0)
{
if(numSpaces >= TabSize())
{
// Print spaces to the log. If possible, tabs are used instead of spaces.
void IDebugLog::PrintSpaces(int numSpaces) {
const int originalNumSpaces = numSpaces;
if(logFile) {
while(numSpaces > 0) {
if(numSpaces >= TabSize()) {
numSpaces -= TabSize();
fputc('\t', logFile);
}
else
{
else {
numSpaces--;
fputc(' ', logFile);
}
}
}

cursorPos += originalNumSpaces;
}

/**
* Prints raw text to the log file
*/
void IDebugLog::PrintText(const char * buf)
{
if(logFile)
{
// Prints raw text to the log file
void IDebugLog::PrintText(const char *buf) {
if(logFile) {
fputs(buf, logFile);
if(autoFlush)
fflush(logFile);
if(autoFlush) { fflush(logFile); }
}

const char * traverse = buf;
Expand All @@ -260,16 +191,11 @@ void IDebugLog::PrintText(const char * buf)
/**
* Moves to the next line of the log file
*/
void IDebugLog::NewLine(void)
{
if(logFile)
{
void IDebugLog::NewLine() {
if(logFile) {
fputc('\n', logFile);

if(autoFlush)
fflush(logFile);
if (autoFlush) { fflush(logFile); }
}

cursorPos = 0;
}

Expand All @@ -279,24 +205,10 @@ void IDebugLog::NewLine(void)
* @note The cursor move will not be performed if the request would move the cursor
* backwards.
*/
void IDebugLog::SeekCursor(int position)
{
if(position > cursorPos)
PrintSpaces(position - cursorPos);
}
void IDebugLog::SeekCursor(const int position) { if(position > cursorPos) { PrintSpaces(position - cursorPos); } }

/**
* Returns the number of spaces a tab would occupy at the current cursor position
*/
int IDebugLog::TabSize(void)
{
return ((~cursorPos) & 3) + 1;
}
// Returns the number of spaces a tab would occupy at the current cursor position
int IDebugLog::TabSize() { return (~cursorPos & 3) + 1; }

/**
* Rounds a number of spaces to the nearest tab
*/
int IDebugLog::RoundToTab(int spaces)
{
return (spaces + 3) & ~3;
}
// Rounds a number of spaces to the nearest tab
int IDebugLog::RoundToTab(const int spaces) { return (spaces + 3) & ~3; }
Loading