Over a million developers have joined DZone.

Logging Class( Using Singleton )

Download Forrester’s “Vendor Landscape, Application Performance Management” report that examines the evolving role of APM as a key driver of customer satisfaction and business success, brought to you in partnership with BMC.

// 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

See Forrester’s Report, “Vendor Landscape, Application Performance Management” to identify the right vendor to help IT deliver better service at a lower cost, brought to you in partnership with BMC.

Topics:
logging

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

{{ parent.tldr }}

{{ parent.urlSource.name }}