?
Solved

Implementing a error debug info reporting

Posted on 2006-11-20
3
Medium Priority
?
257 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 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.

764 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