PDA

View Full Version : Writing Log Records To A Log File



vermarajeev
17th March 2007, 08:40
Hi guys,
How do I write a log record to a log file. I'm using Qt3.3.5 on my machine. It is a portable project.

Thanks

Kumosan
17th March 2007, 08:54
Tell us more about your log file. Under linux you'd probably use the syslog daemon via the functions in syslog.h.

I doubt that this is portable.

If you want portable logging, you'd probably have to do it yourself using QTextStream and QFile.

vermarajeev
17th March 2007, 09:05
Tell us more about your log file. Under linux you'd probably use the syslog daemon via the functions in syslog.h.

I doubt that this is portable.

If you want portable logging, you'd probably have to do it yourself using QTextStream and QFile.

My application reads some text file (encrypted one) which I decrypt it to load the data. Now what I want is to creat a log file which will help me to find out what error has occured and at what line in which file etc etc (If there is any errors) so that I can just have a look at the log file to see what is wrong and fix it immediately.

Is there a way in Qt to do that. If not can you show me a sample about how to do it using QTextStream and QFile????

Thanks

Kumosan
17th March 2007, 09:24
Is there a way in Qt to do that. If not can you show me a sample about how to do it using QTextStream and QFile????
Thanks

Qt has no portable logging facility (I think). Just write your logfile yourself.


QFile file("out.txt");
if (!file.open(QIODevice::Append | QIODevice::WriteOnly | QIODevice::Text))
return;

QTextStream out(&file);
out << "The magic number is: " << 49 << "\n";

vermarajeev
20th March 2007, 03:56
Qt has no portable logging facility (I think). Just write your logfile yourself.


QFile file("out.txt");
if (!file.open(QIODevice::Append | QIODevice::WriteOnly | QIODevice::Text))
return;

QTextStream out(&file);
out << "The magic number is: " << 49 << "\n";

But that is not I want. If there is any error in my application (suppose some crash or some error) I want to know in which file, at what line, what made to cause that problem etc etc and all these information will be stored in log file.

There is something called __FILE__, __LINE__ which can help me. This gives me information about what file and which line. I want something organized.

If any error occurs, I should know where the error has occured by just seeing the log file. I know in Java but how can implement the same using Qt or plain C++. Any help????

Thanks

vermarajeev
20th March 2007, 04:26
Something like this



#include <fstream>
using namespace std;
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define AT __FILE__ ":" TOSTRING(__LINE__)

inline std::ofstream& logfile()
{
static std::ofstream log("Logfile.log");
return log;
}
void error(const char *location, const char *msg)
{
logfile()<<"Error at "<<location<<":"<<msg;
}
int main(int , char**)
{
error(AT, "fake error");
return 0;
}

Is there anything I can add here???? Can I make the above code snippet more optimized or organized.

Kumosan
20th March 2007, 07:53
But that is not I want. If there is any error in my application (suppose some crash or some error) I want to know in which file, at what line, what made to cause that problem etc etc and all these information will be stored in log file.
Initially you wanted to write a record to a logfile. That's what my example allows you to do.:)


But that is not I want. If there is any
There is something called __FILE__, __LINE__ which can help me. This gives me information about what file and which line. I want something organized.
I don't understand what you mean with 'organised'. Those macros allow you to created log records, which tell you in which file and line an error occured. But you have to write your error handling stuff yourself, e.g. using exceptions.


If any error occurs, I should know where the error has occured by just seeing the log file. I know in Java but how can implement the same using Qt or plain C++. Any help????

Thanks

Perhaps you tell us how you do int in Java, maybe we see than whether there is something analogous in C++.

Btw. you could use assertions and redirect stderr into a file. But then your program has to be released in debug mode.

Kumosan
20th March 2007, 07:57
Something like this



#include <fstream>
using namespace std;
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define AT __FILE__ ":" TOSTRING(__LINE__)

}



Yuck! Normal macros are bad enough, but parameterised macros.... This really should be punishable, even such simple uses. ;)

vermarajeev
20th March 2007, 09:30
Yuck! Normal macros are bad enough, but parameterised macros.... This really should be punishable, even such simple uses. ;)

You told it is bad so what is the other alternative????

I have also defined exceptions where I'm calling error() function defined above in appropriate places whereever the error occurs.

For example


void myFunc()
{
ifstream ifs("test.txt");
if( !ifs.is_open() )
{
error( AT, "File open Error" );
return;
}
}

So a log file with name logFIle.log will be created with the error information. this will help me to know in which file at which line the error occured.

Thanks

Kumosan
20th March 2007, 14:08
You told it is bad so what is the other alternative????


An inline function? A template?
See: Effective C++/Scott Meyers/Addison Wesley

For quite some time I try to avoid macros in my code at all. I am not sure, whether it really is in 100% of all cases possible, but up to now for my needs it was always ok.