Results 1 to 10 of 10

Thread: Writing Log Records To A Log File

  1. #1
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Writing Log Records To A Log File

    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

  2. #2
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: Writing Log Records To A Log File

    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.

  3. #3
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Writing Log Records To A Log File

    Quote Originally Posted by Kumosan View Post
    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

  4. #4
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: Writing Log Records To A Log File

    Quote Originally Posted by vermarajeev View Post
    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:
    1. QFile file("out.txt");
    2. if (!file.open(QIODevice::Append | QIODevice::WriteOnly | QIODevice::Text))
    3. return;
    4.  
    5. QTextStream out(&file);
    6. out << "The magic number is: " << 49 << "\n";
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Writing Log Records To A Log File

    Quote Originally Posted by Kumosan View Post
    Qt has no portable logging facility (I think). Just write your logfile yourself.

    Qt Code:
    1. QFile file("out.txt");
    2. if (!file.open(QIODevice::Append | QIODevice::WriteOnly | QIODevice::Text))
    3. return;
    4.  
    5. QTextStream out(&file);
    6. 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

  6. #6
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Writing Log Records To A Log File

    Something like this

    Qt Code:
    1. #include <fstream>
    2. using namespace std;
    3. #define STRINGIFY(x) #x
    4. #define TOSTRING(x) STRINGIFY(x)
    5. #define AT __FILE__ ":" TOSTRING(__LINE__)
    6.  
    7. inline std::ofstream& logfile()
    8. {
    9. static std::ofstream log("Logfile.log");
    10. return log;
    11. }
    12. void error(const char *location, const char *msg)
    13. {
    14. logfile()<<"Error at "<<location<<":"<<msg;
    15. }
    16. int main(int , char**)
    17. {
    18. error(AT, "fake error");
    19. return 0;
    20. }
    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.

  7. #7
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: Writing Log Records To A Log File

    Quote Originally Posted by vermarajeev View Post
    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.
    Quote Originally Posted by vermarajeev View Post
    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.
    Quote Originally Posted by vermarajeev View Post
    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.

  8. #8
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: Writing Log Records To A Log File

    Quote Originally Posted by vermarajeev View Post
    Something like this

    Qt Code:
    1. #include <fstream>
    2. using namespace std;
    3. #define STRINGIFY(x) #x
    4. #define TOSTRING(x) STRINGIFY(x)
    5. #define AT __FILE__ ":" TOSTRING(__LINE__)
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 
    Yuck! Normal macros are bad enough, but parameterised macros.... This really should be punishable, even such simple uses.

  9. #9
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Writing Log Records To A Log File

    Quote Originally Posted by Kumosan View Post
    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

    Qt Code:
    1. void myFunc()
    2. {
    3. ifstream ifs("test.txt");
    4. if( !ifs.is_open() )
    5. {
    6. error( AT, "File open Error" );
    7. return;
    8. }
    9. }
    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

  10. #10
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: Writing Log Records To A Log File

    Quote Originally Posted by vermarajeev View Post
    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.

Similar Threads

  1. Sending Binary File with QFTP
    By nbkhwjm in forum Newbie
    Replies: 2
    Last Post: 7th March 2007, 18:10
  2. Writing a XML file
    By Djony in forum Qt Programming
    Replies: 7
    Last Post: 5th February 2007, 16:23
  3. Writing to file at specific
    By safknw in forum Qt Programming
    Replies: 3
    Last Post: 1st December 2006, 11:12
  4. XML file writing
    By mbjerkne in forum Qt Programming
    Replies: 2
    Last Post: 24th May 2006, 19:04
  5. Replies: 6
    Last Post: 27th February 2006, 12:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.