Debug.h
Code: Select all
#pragma once
#define BUFFER_SIZE 1028
#include <string>
#include <fstream>
#include <assert.h>
#include <stdarg.h> // va_arg
namespace Debugger
{
// member variables
const unsigned int m_sizebuffer = BUFFER_SIZE;
std::ofstream m_file;
bool m_fileopened;
char m_buffer[m_sizebuffer];
va_list m_vl;
void open_file();
void close_file();
void print(const char* fmt, ...);
}
Code: Select all
void Debugger::open_file()
{
// file cannot already be opened.
assert(!m_fileopened);
m_file.clear(); // clear contents of debug file.
//if directory already exists nothing *SHOULD* happen. platform dependent.
system("mkdir Data");
m_file.open("./Data/ErrorLog.txt"); // hard-coding filename is intentional
if(m_file.is_open() )
{
m_fileopened = true;
print("Debug: successfully loaded file './Data/ErrorLog.txt' \n" );
}
}
void Debugger::close_file()
{
if(m_fileopened)
{
m_file.close();
m_fileopened = false;
}
}
void Debugger::print(const char* fmt, ...)
{
if(!m_fileopened)
{
open_file();
print("Debug file opened. \n");
}
int retval = 0;
va_start(m_vl, fmt);
retval = vsnprintf_s(m_buffer, m_sizebuffer, m_sizebuffer, fmt, m_vl);
va_end(m_vl);
m_file << m_buffer;
m_file.flush();
assert(retval > 0);
}
Code: Select all
#include "stdlib.h"
#include "Debug.h"
int main(int argc, char* argv[])
{
Debugger::print("this should work~! yay");
return EXIT_SUCCESS;
}
When I run the above code, I receive a linker error telling me it does not know what the symbol Debugger::print is.
However, if I remove Debug.cpp and place the implementation of Debug.cpp into Debug.h below the declaration it works just fine. I've been trying to find a way to simplify a debug class without the need for passing references around for a debug object.
Does anyone know why this happens? Thanks, I'll be glad to add any details I may have forgotten to post.