简单日志
#include <fstream>
ofstream fLog;
int len;
fLog.open("d:\\log.txt",ios::app);
if(fLog.good())
{
fLog<<"XXXXXXX"<<endl;
fLog<<len<<endl;
char* str1=new char[len *2+2];
fLog<<str1<<endl;
if (str1 !=NULL)
{
delete [] str1;
str1 =NULL;
}
}
fLog.close();
日志类
#pragma once
#include <fstream>//头文件要加个h
#include <stdio.h>//头文件要加个h
using namespace std;
class CLog
{
protected:
CLog()
: _write(false) {};
public:
~CLog();
static CLog* ins()
{
return &_instance;
};
void Load(const char* log_name);
void LogOn(bool write);
void Write(const char* log, bool force=false);
void Write(int log, int width, bool force=false);
void Write(const BYTE* log, int len, bool force=false);
void Close();
protected:
static CLog _instance;
ofstream _log_file;//vc6的ofstream非标准,不要std的,坑爹啊
bool _write;
FILE *stream_;
};
源文件
#include "StdAfx.h"
#include "log.h"
#include <iomanip>
#include <locale.h>
CLog CLog::_instance;
CLog::~CLog() {
if (_log_file.is_open())
_log_file.close();
}
void CLog::LogOn(bool write) {
_write = write;
}
void CLog::Load(const char* log_name)
{
stream_ = fopen(log_name, "wt");
return;
}
void CLog::Write(const char* log, bool force) {
if (!_write && !force)
return;
fwrite(log, sizeof(char), strlen(log), stream_);
fwrite("\n", sizeof(char), 1, stream_);
return;
}
void CLog::Write(int log, int width, bool force) {
if (!_write && !force)
return;
char buff[20];
sprintf(buff, "%0*X", width, log);
fwrite(buff, sizeof(char), strlen(buff), stream_);
fwrite("\n", sizeof(char), 1, stream_);
return;
}
void CLog::Write(const BYTE* log, int len, bool force) {
if (!_write && !force)
return;
char buff[3];
for (int i = 0; i< len; ++i) {
sprintf(buff, "%02X", log[i]);
fwrite(buff, sizeof(char), 2, stream_);
}
fwrite("\n", sizeof(char), 1, stream_);
return;
}
void CLog::Close() {
if (!_write)
return;
fclose(stream_);
return;
}
VC6 和其他版本包含头文件不一致,需区分