Solved

Implementing a error debug info reporting

Posted on 2006-11-20
3
253 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
[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
  • 3
3 Comments
 
LVL 12

Accepted Solution

by:
rajeev_devin earned 500 total points
ID: 17984929
#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
ID: 17984934
The above modified code fulfillyour requirement.
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17984936
>> The above modified code fulfillyour requirement.
Sorry that would have been
The above modified code fulfill your requirement.
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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

749 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