Jump to content
  • Advertisement
  • entries
    707
  • comments
    1173
  • views
    436451

THE log

Sign in to follow this  
Programmer16

107 views

Well, I decided that it's time to share my HtmlLog code. Take, use it, whatever.

enum DEBUGOUTPUTTYPE
{
DOT_TEXT = 0,
DOT_WARNING,
DOT_ERROR,
DOT_USERSTART,
};

class HtmlLog
{
std::string m_FileName;
bool m_bFirstSection;

typedef bool (*USERLISTENER)(const std::string&, int);
USERLISTENER m_fpUserListener;

static HtmlLog* m_pCurrentLog;
public:
HtmlLog();
~HtmlLog();

void Create(const std::string& FileName, const std::string& Title, const std::string& StyleSheets);
void StartSection(const std::string& Name);
void LogDebugOutput(const std::string& Text, int nType, ...);

void SetUserListener(USERLISTENER fpListner);
USERLISTENER GetUserListener() const;

static void SetCurrentLog(HtmlLog* pLog);
static HtmlLog* GetCurrentLog();
};







//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
HtmlLog* HtmlLog::m_pCurrentLog = 0;

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
HtmlLog::HtmlLog()
{
m_bFirstSection = true;
m_fpUserListener = 0;
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
HtmlLog::~HtmlLog()
{
std::ofstream Stream(m_FileName.c_str(), std::ios::app);
Stream<<"
\n\n"< Stream.close();
m_fpUserListener = 0;
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void HtmlLog::Create(const std::string& FileName, const std::string& Title, const std::string& StyleSheets)
{
m_FileName = FileName;
std::ofstream Stream(m_FileName.c_str());
Stream<<"\n\n"</span><<Title.c_str()<<<span class="cpp-literal">"\n";

std::vector Segments = dft::GetSegments(StyleSheets, '\n');
for(unsigned int nCurSegment = 0; nCurSegment < Segments.size(); ++nCurSegment)
Stream<<"">\n";
Stream<<"\n\n"< Stream.close();
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void HtmlLog::StartSection(const std::string& Name)
{
std::ofstream Stream(m_FileName.c_str(), std::ios::app);
if(!m_bFirstSection)
Stream<<"

\n";
m_bFirstSection = false;

Stream<<"\n
\n"<"\n
"< Stream.close();
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void HtmlLog::LogDebugOutput(const std::string& Text, int nType, ...)
{
char szBuffer[768];
va_list VaList;

va_start(VaList, nType);
vsprintf(szBuffer, Text.c_str(), VaList);
va_end(VaList);

if(m_fpUserListener)
{
if(!m_fpUserListener(szBuffer, nType))
return;
}

char szTime[9];
_strtime(szTime);

std::ofstream Stream(m_FileName.c_str(), std::ios::app);
Stream<<"dftLog ["<"] - <">"<"
"< Stream.close();
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void HtmlLog::SetUserListener(USERLISTENER fpListener)
{
m_fpUserListener = fpListener;
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
HtmlLog::USERLISTENER HtmlLog::GetUserListener() const
{
return m_fpUserListener;
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void HtmlLog::SetCurrentLog(HtmlLog* pLog)
{
m_pCurrentLog = pLog;
assert(m_pCurrentLog != 0);
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
HtmlLog* HtmlLog::GetCurrentLog()
{
return m_pCurrentLog;
}






Go forth and use it. Or, just ignore it.

Edit: For fullness and such, here is my GetSegments() function and an example:

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
std::vector GetSegments(const std::string& String, char cSeparator)
{
std::vector Segments;

int nNodeIndex = 0;
int nEndOfNode = (int)String.find(cSeparator);
while(nEndOfNode != -1)
{
std::string Node = String.substr(nNodeIndex, nEndOfNode - nNodeIndex);
Segments.push_back(Node);
nNodeIndex = nEndOfNode + 1;
nEndOfNode = (int)String.find(cSeparator, nNodeIndex);
}
std::string Node = String.substr(nNodeIndex, nEndOfNode - nNodeIndex);
if(Node != "")
Segments.push_back(Node);
return Segments;
}







HtmlLog g_Log;

int main()
{
g_Log.Create("DEBUGLOG.html", "Project Name or whatnot", "style1.css\nstyle2.css");
g_Log.SetUserListener(UserListenerFunction);
HtmlLog::SetCurrentLog(&g_Log);

g_Log.LogDebugOut("Formatted %s", DOT_TEXT, "String");
return 0;
}

enum CUSTOM_DEBUGOUTPUTTYPES
{
CDOT_CLIENT = DOT_USERSTART,
CDOT_SERVER,
};

void SomeOtherFunction()
{
if(HtmlLog::GetCurrentLog())
{
HtmlLog::GetCurrentLog()->LogDebugOutput("This is some stupid function.", DOT_WARNING);
HtmlLog::GetCurrentLog()->LogDebugOutput("OMG, THIS FUNCTION SUCKS!!!", DOT_ERROR);

HtmlLog::GetCurrentLog()->LogDebugOutput("I sent some data to you, client.", DOT_SERVER);
HtmlLog::GetCurrentLog()->LogDebugOutput("Well too bad, it sucks so I don't want it.", DOT_CLIENT);
}
}






Now, if anybody does use this, I'd suggest that you either remove the current log thing (and pass the log around via pointers/const references/whatever or replace it with an all-out singleton.

In other news, I got my scripting system pretty much patched together. It doesn't offer much (pretty much nothing other than taking care of init/shutdown and the stuff I talked about in my last entry), but I like the coding A LOT better than straight lua.

Edit2: Ok, before it gets asked by anybody, I'm going to offer up a few examples for using the UserListener system:

bool MyUserListener(const std::string& Text, int nType)
{
if(nType == DOT_ERROR)
{
//Minimize game window here
MessageBox(GameWindowHandle, Text.c_str(), "Hope you saved, because...", MB_OK);
PostQuitMessage(0);
}
return true;
}

// OR
bool LogListener(const std::string& Text, int nType)
{
std::string StrippedText = "";

int nStart = 0;
int nLastEnd = 0;
for(unsigned int nIndex = 0; nIndex < Text.size(); ++nIndex)
{
if(Text[nIndex] == '<')
{
nStart = nIndex;
for(unsigned int nSubIndex = nIndex + 1; nSubIndex < Text.size(); ++nSubIndex)
{
if(Text[nSubIndex] == '<')
{
nIndex = nSubIndex - 1;
break;
}

if(Text[nSubIndex] == '>')
{
StrippedText += Text.substr(nLastEnd, nStart - nLastEnd);
nLastEnd = nSubIndex + 1;
nIndex = nSubIndex + 1;
}
}
}
}

if(nLastEnd != Text.size() - 1)
StrippedText += Text.substr(nLastEnd, Text.size() - nLastEnd);

static std::string OutputTypes[] = { "Text", "Warning", "Error" };
std::cout<<"["<"]: "< return true;
}





Edit3: Screw starting a new entry. I've been going over what I want to do with my RPG and I just can't really fake it with tiles. I could go with a big-bitmap system like Baldur's Gate, but that would be WAY too expensive and would be almost impossible to have filler art (map wise) since it also requires a 3D mesh.

So, I've decided that for my big project I'm going to switch to 3D. I know, what about the "I'm going to finish this 2D RPG no matter" that I posted a few entries back. Well, here's your answer:


I got the entire map system and entity system implemented except for scripting, which will be easy to do with my new script system.

So, I'm going to finish it, it's just not going to be ANYTHING like what I've been showing/talking about. It'll still be mouse oriented though, since I really don't feel like implementing a key-based interface system after I spent all that time on my fancy new mouse-based system.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!