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
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
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
Qt has no portable logging facility (I think). Just write your logfile yourself.
Qt Code:
return; out << "The magic number is: " << 49 << "\n";To copy to clipboard, switch view to plain text mode
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
Something like this
Qt Code:
#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; }To copy to clipboard, switch view to plain text mode
Is there anything I can add here???? Can I make the above code snippet more optimized or organized.
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
Qt Code:
void myFunc() { ifstream ifs("test.txt"); if( !ifs.is_open() ) { error( AT, "File open Error" ); return; } }To copy to clipboard, switch view to plain text mode
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
Initially you wanted to write a record to a logfile. That's what my example allows you to do.
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.
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.
Bookmarks