//******************************************************
// FileName : UnitTestLog.h
// :
// Descripe :
// Author : Auding ( SpiritAuding@msn.com)
// Date : 2005-2-4 13:03:04
//
// CopyRight Auding ( SpiritAuding@msn.com)
//
// Permission to use, copy, modify, and distribute this software for any
// purpose is hereby granted without fee, provided that this copyright and
// permissions notice appear in all copies and derivatives.
//
// This software is provided "as is" without express or implied warranty.
//
//******************************************************
#ifndef __UNITTESTLOG_HEADER__
#define __UNITTESTLOG_HEADER__
#pragma message("UnitTestLog class CopyRight Auding( SpiritAuding@msn.com ) 2005-02-14 !\n")
#include <iostream>
#include <fstream>
#include <strstream>
#include <string>
#include <algorithm>
#include <windows.h>
#include <winbase.h>
class UnitTestLog
{
private:
UnitTestLog(UnitTestLog& rth);
UnitTestLog& operator = (UnitTestLog& rth);
public:
UnitTestLog() :
m_strTitle("<?xml version='1.0'?>"),
m_strUnitTestName(""),
m_strWorkDirectory(""),
m_file_ext(".log"),
m_bStarted(false),
m_bXmlFormat(false)
{
};
UnitTestLog(const char* unittestname) :
m_strTitle("<?xml version='1.0'?>"),
m_strUnitTestName(unittestname),
m_strWorkDirectory(""),
m_file_ext(".log"),
m_bStarted(false),
m_bXmlFormat(false)
{
};
~UnitTestLog()
{
stop_log();
};
void set_name(const std::string& strname)
{
m_strUnitTestName = strname;
}
std::string get_name()const
{
return m_strUnitTestName;
}
void start_log()
{
char file_name[MAX_PATH];
CreateFileName(file_name, MAX_PATH);
std::string str_log_file_name(file_name);
clear_file_name(str_log_file_name, m_file_ext);
m_fstream.open(str_log_file_name.c_str(), std::ios_base::out);
if(m_fstream.is_open())
{
m_bXmlFormat = (m_file_ext == ".xml");
if(m_bXmlFormat)
m_fstream << m_strTitle << "\n<UnitTestRoot>\n";
m_bStarted = true;
}
}
void stop_log()
{
if(m_fstream.is_open())
{
if(m_bXmlFormat)
m_fstream << "</UnitTestRoot>";
m_fstream.close();
}
m_bStarted = false;
}
void set_work_dir(const std::string& strWork)
{
m_strWorkDirectory = strWork;
std::string::iterator it = m_strWorkDirectory.end();
if("\\" != it)
m_strWorkDirectory += "\\";
}
std::string get_work_dir()const
{
return m_strWorkDirectory;
}
void set_log_ext(const std::string& strext)
{
m_file_ext = strext;
}
std::string get_log_ext()const
{
return m_file_ext;
}
template<class T>
std::ostream& operator<<(const T& logcontent)
{
m_fstream << logcontent;
return m_fstream;
}
private:
std::string CreateFileName(char* buf, const int& n)
{
std::ostrstream os(buf, n);
SYSTEMTIME systm;
GetLocalTime(&systm);
if(m_strWorkDirectory.length() == 0)
usesysdir();
// filename is looks like : "e:\\UnitTestName_Year_Month_Day_Hour_Minute_Second.FileExt"
os << m_strWorkDirectory << m_strUnitTestName << "_"
<< systm.wYear << "_" << systm.wMonth << "_" << systm.wDay << "_"
<< systm.wHour << "_" << systm.wMinute << "_" << systm.wSecond
<< m_file_ext;
return os.str();
};
void clear_file_name(std::string& strname, const std::string& strsub)
{
int npos = strname.find(strsub, 0);
if(npos > -1 && npos + strsub.length() <= strname.length())
{
strname = strname.substr(0, npos + strsub.length());
}
}
void usesysdir()
{
char tmpbuf[MAX_PATH];
GetTempPath(MAX_PATH, tmpbuf);
m_strWorkDirectory = tmpbuf;
}
private:
std::fstream m_fstream;
std::string m_strUnitTestName;
std::string m_strTitle;
std::string m_strWorkDirectory;
std::string m_file_ext;
bool m_bStarted;
bool m_bXmlFormat;
};
#endif //__UNITTESTLOG_HEADER__