0

Assume I have a main window. The main window has some child windows, including a log window to log what happens and a number of operating windows to operate a user's data. After each operation in the operating window, I want to log it in the log window. There are several ways to implement it.

  1. Put a pointer pointing to the log window in the operating windows. To do this, I think the operating window is not so reusable since it is bound to a pointer. Someday if I don't want logging, I need to delete it. Also, this way may take a little more memory. In addition, if the operating window is the main window's child's childe's child..., I need to transfer the pointer inside one by one.

  2. Make the main window to be a singleton and add a public log function to it. When I need logging, I just call like MainWindow::Inheritance()->Log(theInformation). But what happens if someday I have 2 main windows.

Any good suggestion for the problem. Thanks a lot!

3 Answers 3

1

I think the right solution is to put the logging data into an entirely separate class, whose purpose is only to store and retrieve (and truncate, and save, and...) log information. All of your other windows, classes, etc that need to log the data would then need access to the single log class pointer.

logSystem->log("my friend is blue"); 

Now, whether logSystem is a global or an object passed to each class during initialization is up to you. Any there are many camps of "globals are evil" and "globals are helpful" people to help you with this separate question.

Then, in your logging window, you simply only need retrieve the logged data and display it.

 // Qt, C++-11 pseudo-api foreach(String log, logMessages) { myListBox->append(log); } 

Another advantage of this approach is that you can open and close (create and destroy) the log window and still not loose the data itself. Yes, you could also hide the log window or other trickery, but being able to completely destroy and recreate it seems much cleaner. Not to mention, by separating out the log data from the log window, you can later create two windows showing two different sets of logging data. [Assuming you didn't use that global for the logging data, mentioned above.]

Sign up to request clarification or add additional context in comments.

Comments

1

I would generally write a logger as a singleton.

If you want to remove your logging functionality, it's easy to either set a flag (for run-time logging control), or replace it with a dummy class (for compile-time logging control).

So at the very basic, you may have:

Log::Get().Error("Name truncated by %u bytes", nBytes ); 

Yes, I like having variadic printf-style logging functions, but you may prefer to have just a single string or to use C++ stream operators.

If I wanted to have multiple logs, I would make it a re-usable class and put instances of those into another singleton (eg an 'application' class). In that case I might have:

Log::EventsLog().Info("Something exciting happened" ); Log::SystemLog().Info("Shutting down" ); 

You generally need to have some form of mutex control on your log messages when they could (will) be called concurrently. You might not need this if you deliver messages to your log window via a single-threaded queue (as in Windows message-passing).

Comments

0

I would have thought that the logging functions should be implemented as a standalone "singleton" class...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.