Simple log writer

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
ArakisTheKitsune
Posts: 73
Joined: Sat Jun 27, 2009 6:52 am

Simple log writer

Post by ArakisTheKitsune »

Hi, first of all I want to thank Irrlich developers and community for excellent, easy engine and for friendly forum :)
I have about 1.5 year of experience with Irrlich ( I think that I started with 1.4 version ^^ ) but I never tried something complicated with Irrlich ( in fact I was but... other people have quit because they saw how much there is work to be done.. so I was forced to do little experiments due lack of man power ).
This piece of code is should have been used in my first serious project http://sourceforge.net/projects/jaelc/ but unfortunately after about 8-9 month project leader had to quit due lack of free time. And soon project got freeze status. And few months ago I started to work on game ( to gain more experience in both c++ and Irrlich ) using Irrlich and it was time to implement some kind of log system. And I remembered that I had one... ( And since this it's part of open source project, I don't take credits for all source files. Read info's in them for more details. ) so I did few cleanups here and there and... behold.
This is simple log writer and viewer (and displays some system info). You can sort logs by namespaces, search function, displays function, file, line and time since app started for given log. To view logs just run _JavascriptViewer.html from logs directory. All files are under GNU General Public License version 3.
You must define in file namespace ( category ) that class belongs to.
There are few namespaces as an example:

default
Main
AI
Scripting
Engine
Loading
UI
Vars

See ComplexXSLT.xsl for detailed explanation.
You will also notice "any" namespace in _JavascriptViewer.html, it's not regular namespace it just will show all logs from all namespaces.
You can modify all namespaces ( not recomended to change "any" namespace from obvious reason ) in _JavascriptViewer.html file.
From archive unpack logs directory to your working directory and source files to where ever you like.
As you noticed by now, I am terrible at explaining :) so let's just jump to short example of usage:

Code: Select all

#include "LogFile.h"

#define __NAMESPACE__ "Engine"

int _tmain(int argc, _TCHAR* argv[])
{
    XLOG_0(__FUNCTION__, LOG_COMMENT, L"I am happy comment :)");
    XLOG_1(__FUNCTION__, LOG_MESSAGE, L"I am happy message :)");
    XLOG_2(__FUNCTION__, LOG_EVENT, L"I am happy event :)");
    XLOG_3(__FUNCTION__, LOG_ERROR, L"I am not so happy error :)");
    XLOG_4(__FUNCTION__, LOG_WARNING, L"I am happy warning :)");
    XLOG_4(__FUNCTION__, LOG_DEBUG, L"I am happy debug message :)");
    return 0;
}
Every XLOG_X call will generate ( or append if exist ) new log message in logs\XMLOutputWithComplexXSLT.xml file. When application is run next time then XMLOutputWithComplexXSLT.xml will be deleted and created new.
This is how this example looks when you run _JavascriptViewer.html Example ( click on picture to enlarge )
Enums LOG_ERROR, LOG_WARNING, LOG_COMMENT, LOG_EVENT, LOG_DEBUG, LOG_MESSAGE are categories for log messages.
In MSVC macro __FUNCTION__ returns function name as a string, I am not sure how other compilers handle this, in case that
__FUNCTION__ don't work on your compiler just swap it with function name.

Explanation for XLOG levels
XLOG_0 (level 0) = always shown: use only for special purpose
XLOG_1 (level 1) = always shown: critical errors
XLOG_2 (level 2) = non-critical errors
XLOG_3 (level 3) = warnings
XLOG_4 (level 4) = comments, debugs, messages

By default if application is compiled in debug mode all log levels will shown and in release build only 1 and 0 level log will be shown. You can change that by modifying ub_DebugLevel in CLogFile::CLogFile() in LogFile.cpp
It's intend for windows OS so I doubt that it will work under Linux without tweaking the code.

If I missed something I am really sorry but it's 8:43 AM and I didn't sleep all night :D so if you have any questions feel free to
ask.

Download 18 kb

P.S. Sorry for for grammar errors, English is my second language.
Knowledge is power, understanding is wisdom.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

I think this one would better fit into the Code snippet forum, as it will probably be used as part of an app or game.
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

Unless you have no control over it, I strongly recommend using another license than gnu gpl because that's one license that stops a lot of devs dead in their tracks. Your little story makes me think we should have a develloper journal section and each post our own stories for both others to learn and us not to forget ^^
ArakisTheKitsune
Posts: 73
Joined: Sat Jun 27, 2009 6:52 am

Post by ArakisTheKitsune »

Dorth wrote:Unless you have no control over it, I strongly recommend using another license than gnu gpl because that's one license that stops a lot of devs dead in their tracks.
I have send e-mail to seek permission to change license to zlib.
Still not received answer.
Dorth wrote:Your little story makes me think we should have a develloper journal section and each post our own stories for both others to learn and us not to forget ^^
Imho I think that would be good idea. There are probably tons of interesting stories to hear and to learn from. ^^
Knowledge is power, understanding is wisdom.
VeTaL
Posts: 14
Joined: Mon Feb 21, 2011 8:39 am

Post by VeTaL »

Read a little about GNU General Public License version 3: looking like i can modify it and nobody would arrest me for that.

Just wanted to share my little improvement to this logger. Little snippet below allows to print messages in console and see them in runtime, not only in log.

Find
void CLogFile::FinishEntry(std::string strFunction, wchar_t* strMessage )
and add at the begining this code

Code: Select all

	//! edited by VeTaL
	printf("FUNCTION: %s, SAYS: %s \n", strFunction.c_str(), irr::core::stringc (strMessage) );
So, now calling
void StateLogo::activate()
{
XLOG_2(__FUNCTION__, LOG_EVENT, L"StateLogo is activated");
}
would do this in command line
Image

Edited: found function in core :)
Post Reply