Solved

Implementing a error debug info reporting

Posted on 2006-11-20
3
244 Views
Last Modified: 2010-04-01
Hi Experts,

I am writing a simple logging utility for my application.  Basically, I have four different reporting to be done.
1. Informational logs
2. Debug logs
3. Error logs
4. Warning logs.

I want to be able to do something like the following

LOG( INFORMATIONAL, "fileName", "an information message");
LOG(DEBUG, "fileName", "a debug message"); etc....

I believe I can do this with simple MACRO definitions and cout.....

Can somebody give some suggestions?  It would be better if I can implement that as a class....
I was thinking the following way, but then the problem is that I have to create an object to call the function LOGGER.  That would not be nice.  So, can I put that instantiation of the class into a macro so that to hide it.  I will make it a sigleton object.

#include <iostream>
#include <string>

using namespace std;


/////////#define LOGGER(a, b, c) SimpleLog(a, b, c);

class SimpleLogger
{

public:
      
      typedef enum LogType_t{
            LOG_INFO,
            LOG_DEBUG,
            LOG_WARNING,
            LOG_ERROR
      }LogType;


public:
      SimpleLogger(){};
      void SimpleLog( LogType_t type, string fileName, string msg );
private:


};

void SimpleLogger::SimpleLog(LogType_t type, string fileName, string msg )
{

      if ( type == LOG_INFO ) {
            cout << "INFO" << " " << fileName << " " << msg << endl;
      }
      else if( type == LOG_DEBUG ) {
            cout << "DEBUG" << " " << fileName << " "<< msg << endl;
      }

}



void main()
{

      SimpleLogger *logger = new SimpleLogger();
      logger->LOGGER( SimpleLogger::LOG_INFO, "myfile", "just a string...");

}




Thanks a lot.
0
Comment
Question by:ambuli
  • 3
3 Comments
 
LVL 12

Accepted Solution

by:
rajeev_devin earned 500 total points
Comment Utility
#include <iostream>
#include<string>
using namespace std;

#define LOGGER(a, b, c) SimpleLogger::SimpleLog(a, b, c);

class SimpleLogger
{
public:      
      typedef enum LogType_t{
            LOG_INFO,
            LOG_DEBUG,
            LOG_WARNING,
            LOG_ERROR
      } LogType;

      static SimpleLogger* getInstance() {

            if (m_singleInstance == NULL) {
                  m_singleInstance = new SimpleLogger;
            }

            return (m_singleInstance);
      }

      void deleteInstance() {
            if (m_singleInstance != NULL) {
                  delete m_singleInstance;
            }
      }

      static void SimpleLog( LogType_t type, const string& fileName, const string& msg );

private:
      static SimpleLogger* m_singleInstance;
      SimpleLogger(){
      }
      ~SimpleLogger(){
            deleteInstance();
      }
};

SimpleLogger* SimpleLogger::m_singleInstance = NULL;

void SimpleLogger::SimpleLog(LogType_t type, const string& fileName, const string& msg )
{
      if ( type == LOG_INFO ) {
            cout << "INFO" << " " << fileName << " " << msg << endl;
      }
      else if( type == LOG_DEBUG ) {
            cout << "DEBUG" << " " << fileName << " "<< msg << endl;
      }
}

void main()
{
      LOGGER(SimpleLogger::LOG_INFO, "myfile", "just a string...")            
}
0
 
LVL 12

Expert Comment

by:rajeev_devin
Comment Utility
The above modified code fulfillyour requirement.
0
 
LVL 12

Expert Comment

by:rajeev_devin
Comment Utility
>> The above modified code fulfillyour requirement.
Sorry that would have been
The above modified code fulfill your requirement.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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. …
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

744 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

10 Experts available now in Live!

Get 1:1 Help Now