Solved

logging data

Posted on 2006-07-13
10
184 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 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
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 50 total points
ID: 17109480
Check out the following link:
http://axter.com/ezlogger

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

739 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