Solved

logging data

Posted on 2006-07-13
10
178 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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
 
LVL 12

Expert Comment

by:rajeev_devin
Comment Utility
You need to modify the code to match your requirement.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:ramnars
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
Check out the following link:
http://axter.com/ezlogger

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now