Solved

logging data

Posted on 2006-07-13
10
182 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

786 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