?
Solved

Implementing a error debug info reporting

Posted on 2006-11-20
3
Medium Priority
?
261 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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.
Suggested Courses

839 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