Solved

Implementing a error debug info reporting

Posted on 2006-11-20
3
255 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: 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

726 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