Solved

logging data

Posted on 2006-07-13
10
183 Views
Last Modified: 2010-04-01
In my processing function, depending upon the activity code , we do some processing  for different activities .  We need to implement logging now. Depending upon the activity code, the data that needs to log would vary.
for ex activity code 01 , log field 1 and field 2
for activity code 02, log field 2 and field 5.

I need some ideas to implement this logging mechanism.

Is it best to have a base logging class and then derive classes out of it for each and every activity code . the derived classes would have data members specific to what it needs to log and a overridenn method persistlog in all the classes would write all of its data members to the disk.

is there any design pattern examples / code that accomplishes some thing like this ?


Thanks for ur help
0
Comment
Question by:ramnars
10 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17105530
>> is there any design pattern examples / code that accomplishes some thing like this ?
Most of the logger code I have gone so far is singleton class. That is, they will have only one object.
0
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 50 total points
ID: 17106712
you may look into 'Design' of event logger classes which are similar

http://www.codeproject.com/debug/alx_log.asp <== Advanced Logging for all kind of applications
http://www.codeproject.com/system/xeventlog.asp
http://www.codeproject.com/cpp/logdriver.asp

-MAHESH
0
 
LVL 12

Assisted Solution

by:rajeev_devin
rajeev_devin earned 50 total points
ID: 17106929
Here is a sample logging mechanism

#include <stdio.h>

#define LOGGER Logger::getInstance()

enum LOGLEVELS{
      LEVEL_01,
      LEVEL_02,
};

class Logger {

public:
      static Logger& getInstance() {

            if (m_pLogger == NULL)
            {
                  m_pLogger = new Logger;
            }
            return *m_pLogger;
      }

      void operator<<(const char* string) const {
            fprintf(m_pLogFile, "%s\n", string);
      }

      const Logger& log(LOGLEVELS level) {
            fprintf(m_pLogFile, "Level : %2d ", level);
            return *this;
      }

      void initialize(FILE* file) {
            m_pLogFile = file;
      }

      void finalize() {
            if (m_pLogFile) {
                  fclose(m_pLogFile);
                  m_pLogFile = NULL;
            }
      }

private:
      Logger() : m_pLogFile(NULL) {
      }

      ~Logger() {

            if (m_pLogFile) {
                  fclose(m_pLogFile);
                  m_pLogFile = NULL;
            }
      }

      static Logger* m_pLogger;
      FILE* m_pLogFile;
};

Logger* Logger::m_pLogger = NULL;

void main()
{
      LOGGER.initialize(stdout); // You can pass your own file object here.
      LOGGER.log(LEVEL_01) << "This is the first log message";
      LOGGER.log(LEVEL_02) << "This is the second log message";
      LOGGER.finalize();
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17106935
You need to modify the code to match your requirement.
0
 

Author Comment

by:ramnars
ID: 17108198
ok. the thing is depending upon the activity code ( there could be  more than 5) , the data that i need to log differs. shoudl i handle that scenario by testing for the activity codes ? I want it to be flexible enough to be modified in the future , in terms of logging data for a new activity code and also in terms of introducing new fields to the data that is being logged.

should i handle all the if conditions in the code where i invoke the logger ? i guess not.

so thats why i was leaning for the idea for invidiual classes for the activity codes and let them decide which fields they want to log


0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17109290
>> should i handle all the if conditions in the code where i invoke the logger ? i guess not.
As I told you need to extend the logger as per your requirement.

>> so thats why i was leaning for the idea for invidiual classes for the activity codes and let them decide which fields >> they want to log
If you extend the base class into number of derived classes. Your derived classes won't have much work to do.
So, what's the use of extending them.
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17109306
The code I posted is not the complete solution for you. You need to extend it.
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 50 total points
ID: 17109480
Check out the following link:
http://axter.com/ezlogger

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question