Over a million developers have joined DZone.

Logging Class( Using Singleton )

See Gartner’s latest research on the application performance monitoring landscape and how APM suites are becoming more and more critical to the business, brought to you in partnership with AppDynamics.

// A logging class, using a singleton.
// Will create a backup file
// Coded in C++
// Doesn't use a pointer for the singleton
// Coded by Ian T. Jacobsen 2011

// Header

#ifndef LOGGING_H
#define LOGGING_H

#include 
#include 

#define LOG CLogging::Inst("log.log").Log

class CLogging
{
public:
    static CLogging& Inst(char* filename);
    void Log(std::string message);
    ~CLogging();
protected:
    CLogging(char* filename);
private:
    char* m_filename;
    std::fstream m_fOut;
};

#endif // LOGGING_H


// Source file

#include "include/logging.h"
#include 

        CLogging& CLogging::Inst(char* filename)
        {
            static CLogging m_pInstance(filename);
            return m_pInstance;
        }

        CLogging::CLogging(char* filename)
            : m_filename(filename)
        {
            std::fstream temp(filename, std::ios_base::in);

            std::fstream backup(std::string(std::string(m_filename).append(".backup")).c_str(), std::ios_base::out | std::ios_base::trunc);

            temp.seekg(0,std::ios::end);
            int length = temp.tellg();
            temp.seekg(0,std::ios::beg);

            char* buffer = new char[length];

            temp.read(buffer,length);

            backup.write(buffer, length-4);

            delete[] buffer;
            temp.close();
            backup.close();

            m_fOut.open(filename, std::ios_base::out | std::ios_base::trunc);

            char date[9];
            _strdate(date);

            m_fOut

The Performance Zone is brought to you in partnership with AppDynamics.  See Gartner’s latest research on the application performance monitoring landscape and how APM suites are becoming more and more critical to the business.

Topics:
logging

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}