CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2001
    Location
    Chicago, USA
    Posts
    222

    Creating my own stream for loggin?

    Hi I want to create a logging mechanism which I can use like std iostreams, with << >> operators and pass Log levels probably using some functions?

    How do I go about doing this? Because I want to use this data passed and send it over a socket, so that a logging client can connected to the app and start downloading the log.

    Can someone please give me some hints for this and how do I go about creating my own stream?

    And for something like this do you prefer std streams format or function call?

    Thanks
    Ankur

  2. #2
    Join Date
    Oct 2001
    Location
    Chicago, USA
    Posts
    222

    Re: Creating my own stream for loggin?

    I figured it out, thanks anyways...

    Ankur

  3. #3
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Creating my own stream for loggin?

    I created one like this using a "has-a" relationship for the stream.

    You had to acquire the stream to log and doing so also automatically "locked" a mutex to make it thread-safe.

  4. #4
    Join Date
    May 2005
    Posts
    99

    Re: Creating my own stream for loggin?

    What is this "has-a" relationship.

    In the case of logging we can also create a factory class which can have all the functions as per requirement. After creating it we can just pass the message over the object/handle of that factory class.
    As NM suggested a proper locking mechanism will have to be implemented. I think it can be solved by using a message queue at the entry point of logger.
    In that case all methods using it will post messages in queue and continue with there operation.
    The advantage I see is that the backend methof (like writing to socket and invoking program or just copying the messages to a log file etc) can be changed without changes at frontend users.

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Creating my own stream for loggin?

    Quote Originally Posted by manish_velankani
    What is this "has-a" relationship.
    You have a class which "has-a" reference (or pointer - a reference is better) to a stream and then any streaming is directed through this reference.

    Your best way to create the "lock" is have an object that takes a constructor to your "log" and then stream through that. Let's call our object LogAcquire. So:

    Code:
    if ( myLog.level() > 50 )
    {
       LogAcquire acq( myLog );
       acq << "** This is a log message **\n";
    }
    and when acq goes out of scope it unlocks. LogAcquire in its constructor will get the stream object out of myLog and will lock its mutex. In its destructor it will unlock its mutex.

    You might make LogAcquire a friend class of your log class (thus allowing it to do things to myLog that users cannot do directly). This is an example of tightly-coupled classes and using friend to increase encapsulation.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured