?
Solved

logging data

Posted on 2006-07-13
10
Medium Priority
?
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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 200 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
Independent Software Vendors: 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 200 total points
ID: 17109480
Check out the following link:
http://axter.com/ezlogger

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

752 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