tags:

views:

59

answers:

2

What design pattern might apply to logging? What is normally used in this type of situation? Any good tutorials?

I am writing a client-server application using C89 and gcc 4.4.4. I now need to implement some logging feature that will display log messages on the screen as well as log to a file.

However, I don't want to display all log messages (warning, error, critical, unrecoverable, debug, etc). Maybe I can set so that it will display just errors and nothing else. For example, the user might not be interested in the debug messages on the screen output.

+3  A: 

Some hints/concepts:

  1. A mechanism to generate the log entry with either a parameter for log level or implied by the function name.
  2. Usually a printf-style format string followed by parameters.
  3. A data structure to collect the messages from one or more threads generating them.
  4. Some form of timestamping.
  5. A back end thread that processes the collected messages and performs the output generation. This can be where you configure what levels get displayed and/or written to a file.
  6. vsnprintf() is a function which takes a variable number of parameters and is often used in the back end processing section.

Often the idea is to defer the string processing to the background thread so the actual work threads generating the log aren't wasting time with string manipulation. However, it makes printing variable strings difficult since they tend to go out of scope by the time the background processes them. So in these cases numeric values are preferred. If real time is not as much an issue, you can copy strings through the log interface instead of just passing the pointers and numeric parameter values.

Good luck.

Amardeep
+1  A: 

If you need the name of a design pattern: try the Observer Pattern.

Perhaps this page might help too: http://blogs.msdn.com/b/dustin_andrews/archive/2007/11/01/how-to-easily-use-the-observer-pattern-to-loosely-couple-tests-to-the-logging-engine.aspx

Moron