/*
* Debug.cpp
* XCODE SFML_ENGINE
*
* Created by Benjamin Adamson on 5/31/10.
* Copyright 2010 Oregon State University. All rights reserved.
*
*/
#ifndef _DEBUG_H_
#include "Debug.h"
#endif
Debug::Debug()
{
}
Debug::~Debug()
{
if(m_fileopened)
{
m_file.close();
}
}
void Debug::open_file(const std::string& filename)
{
// file cannot already be opened.
assert(!m_fileopened);
m_file.clear(); // clear contents of debug file.
m_file.open(filename.c_str());
if(m_file.is_open() )
{
m_fileopened = true;
dbg.print("Debug: successfully loaded file '%s'.\n", filename.c_str() );
}
else
{
// file failed to open
dbg.print("Debug: failed to open file '%s'.\n", filename.c_str() );
}
}
/*
WARNING: Should only accept c-style strings only. If output is ever cryptic double check
that we are not passing c++ Strings instead of char*, do not know how to differentiate if
fmt is a c-style string (char*) or a C++ String.
*/
void Debug::print(const std::string& fmt, ...)
{
// cannot print unless a file is opened.
assert(m_fileopened);
va_start(m_vl, fmt);
vsnprintf(m_buffer, m_sizebuffer, fmt.c_str(), m_vl);
va_end(m_vl);
m_file << m_buffer;
m_file.flush();
}
Hey guys, I decided that my old debug class wasn't good enough so I looked into how printf worked and discovered variadic functions. This is my first time using them, but I was hoping I could get some feedback.
Last edited by short on Tue Jun 01, 2010 2:32 am, edited 4 times in total.
I believe that when using variadic functions there must be at least one other parameter to the function, however you could change your dbg.print to act like printf by making the first argument a string which holds placeholders such as %s, then you can use vsnprintf to automagically replace the placeholders with the arguments.
Unfortunately since vsnprintf is a C style function, you'd need to use a char * as a buffer instead of a std::string, which kinda goes against the C++ style you're using.
Since you asked for any ideas I'll just post my logger class, I have no idea what a variadic function is. Mine writes to the console but obviously it can be switched to write to a text file. I'm not sure but I think you might want to make sure you declare a private copy constructor and private assignment operator for the class you have right now.
//=operator needs to be private
Debug& operator=(const Debug &rhs)
{
}
Ok I added these to my header, I understand declaring =operator as private being necessary. What I don't know is why Debug() being private is not enough. If I do not declare a constructor with a constant reference of type itself as a parameter will the compiler automatically generate one? I was under the assumption the only constructor the compiler automatically created for the user was the default Debug() (no parameters). Am I wrong?
To be more clear, when I first posted this code I had already declared Debug() as private.
Also, thanks a bunch for helping me out. I edited the first post with some slight changes to your post,
Hey you don't have to put the empty body of the assignment operator. And yes, copy constructors are automatically generated so you gotta make it private in this case. Just for future reference, you have to be careful with these default copy constructors because sometimes you MUST write your own. Look up deep vs. shallow copy for a better description than I could give.
X Abstract X wrote:Hey you don't have to put the empty body of the assignment operator. And yes, copy constructors are automatically generated so you gotta make it private in this case. Just for future reference, you have to be careful with these default copy constructors because sometimes you MUST write your own. Look up deep vs. shallow copy for a better description than I could give.
Nah I understand shallow vs deep, I just wasn't aware the compiler generated "2" default constructors.