Jump to content
  • Advertisement
Sign in to follow this  

Error Logs?

This topic is 4338 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have decided that the next part of my engine should be the error log, however I am not sure how I would go about creating one. 1) I am not sure what should go in it 2) How do you check for an error when it has already crashed. How do you know where the error occured? I hope that someone could help answer this or give me a link to a relevant resource. I have not been very sucessful finding anything useful on this subject.

Share this post


Link to post
Share on other sites
Advertisement
You can make it really how ever you want. I use my log class to log certain events or errors to a file so I can see what is going on in the program. You can make a simple log class like this:

class Log {
private:
std::ofstream output;
public:
Log(const std::string &file) {
output.open(file); // Open the log
}
~Log() {
output.close(); // Close the log
}
void Write(const std::string &text) {
output << test << std::endl; // Log the message
}
};


This could be a basic log system with no error checking where you have an instance of the Log and you can log messages to that file. You can also add things such as message importance such as important, informative, etc and get logged depending on what the level in the log is set to so you can set how detailed your log file is. You check for an error and then log a message. For example if you fail to load a texture you could go log.Write("Unable to load test.tga") or something like that. I belive there are macros such as __LINE__ or __FILE__ that you can find the line and file the error occured from also. Hope this helps.

Share this post


Link to post
Share on other sites
You could do something like this:



void Log::Write(std::string message, ...) {

char buffer[1024];

va_list arguments;

va_start(arguments, message);
_vsnprintf(buffer, sizeof(buffer), message.c_str(), arguments);
va_end(arguments);

m_out << buffer << std::endl;

}





Then you can call it like sprintf().

log->Write("Failed to open file: %s", fileName.c_str());

Share this post


Link to post
Share on other sites
1. There is a multitude of different things you can include in Log class. Those include, as Thergothon mentioned, formatted messages via variable arguments, but also the level of an error/warning, multiple log files and cross-platform functionality. One of the great logging resources out there is Boost.Log. It hasn't been approved by Boost community due to various things, however I use it throughout all my projects with ease and peace of mind. It features a great deal of options to choose from, the most notable I may comment on are:
Log hierarchies
Log levels (Program, Debug, Release, Warning, Error)
Log appenders
Being cross-platform and thread-safe as most of Boost libraries

2. You can check for an error by using either Log files or by catching exceptions and/or studying crash dump files. You can also step through the program by using debugger and figuring out what and where such an error has occured.

Share this post


Link to post
Share on other sites
Here's a very simplified version of what I'm using:

enum OutputType { DOT_TEXT = 1, DOT_WARNING, DOT_ERROR };

class Log
{
static std::string m_FileName;
public:
Log();
~Log();

void Create(const std::string& FileName);
void LogDebugOutput(const std::string& Text, int nType, ...);
};





std::string Log::m_FileName = "";

Log::Log()
{
}

Log::~Log()
{
}

void Log::Create(const std::string& FileName)
{
m_FileName = FileName;
std::ofstream fout(m_FileName.c_str());
fout<<"<html>\n<head>\n<title>Your Apps Title Here</title>\n";
fout<<"<link rel=stylesheet type=text/css href=style.css>\n";
fout<<"</head>\n<body>"<<std::endl;
fout.close();
}

void LogDebugOutput::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);

char szTime[9];
_strtime(szTime);

std::ofstream fout(m_FileName.c_str(), std::ios::app);
fout<<"Log [Time: "<<szTime<<"; Level: "<<nType<<"] - "<<szBuffer<<"<br>"<<std::endl;
fout.close();
}





Log g_Log;

g_Log.Create("log.html");
g_Log.LogDebugOutput("This is an %s.", DOT_TEXT, "example");
g_Log.LogDebugOutput("Failed to load \"%s\".", DOT_ERROR, "GuiSkin.png");




HTH!

Share this post


Link to post
Share on other sites
I prefer HTML logs because you can:
- use colors to identify "succes" and errors(eg. green and red)
- you can write lists and trees of list items
- you can write tables with data

Just think about it :)

If you want a sample of how that could be done, visit http://www.lifeisdigital.net/content/category/10/28/55/ then download "COW" and look for /Source/COW/Log.cpp and .h

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!