LogMgr.cpp
资源名称:p2p_vod.rar [点击查看]
上传用户:liguizhu
上传日期:2015-11-01
资源大小:2422k
文件大小:4k
源码类别:
P2P编程
开发平台:
Visual C++
- /*
- * Openmysee
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- */
- #include "stdafx.h"
- #include "LogMgr.h"
- //直接传filename进来,一个绝对路径
- LogMgr::LogMgr()
- {
- m_bPrintTime = TRUE;
- m_tBuf[0] = 0;
- }
- LogMgr::LogMgr(string fileName) : m_fileName(fileName) {
- m_bPrintTime = TRUE;
- m_tBuf[0] = 0;
- }
- LogMgr::LogMgr(LogMgr& logmgr)
- {
- m_fileName = logmgr.m_fileName;
- m_bPrintTime = logmgr.m_bPrintTime;
- strcpy(m_tBuf, logmgr.m_tBuf);
- }
- LogMgr::~LogMgr() {
- Uninit();
- }
- P2P_RETURN_TYPE LogMgr::Init(string fileName) {
- m_fileName = fileName;
- struct tm *newtime;
- time_t long_time;
- time( &long_time ); /* Get time as long integer. */
- newtime = localtime( &long_time ); /* Convert to local time. */
- if(!newtime)
- return PRT_SYS;
- TCHAR buf[MAX_PATH_EX] = _T("");
- sprintf(buf, "%d-%d-%d-%d-%d-%d-%d.tmp",
- newtime->tm_year+1900, newtime->tm_mon+1, newtime->tm_mday,
- newtime->tm_hour, newtime->tm_min, newtime->tm_sec, GetTickCount());
- m_fileName.append(buf);
- // 4. 打开文件
- m_ofs.open(m_fileName.data(), ios_base::out | ios_base::trunc);
- if(!m_ofs.is_open()) {
- // 如果此日志文件已经被占用,则返回错误
- printf("无法打开日志文件: %s.n", m_fileName.data());
- return PRT_SYS;
- }
- return PRT_OK;
- }
- void LogMgr::Uninit() {
- m_ofs.close();
- // 退出的时候不删除日志
- //remove(m_fileName.data());
- }
- void LogMgr::StatusOut(const char* fmt, ...) {
- // 听了某妄人的言论之后,我使用 operator!()代替了is_open(),结果在m_ofs.close()之后,
- // operator!()仍然返回成功!然后在试图写入数据的地方抛出了exception, 恨!
- // 可能is_open()只能判断打开是否成功,不能确定打开是否正确,但是!operator!()也太不符合习惯了
- if(!m_ofs.is_open() || !fmt)
- return;
- m_tBuf[0] = 0;
- if(m_bPrintTime) {
- struct tm *newtime;
- time_t long_time;
- time( &long_time ); /* Get time as long integer. */
- newtime = localtime( &long_time ); /* Convert to local time. */
- if(newtime)
- sprintf(m_tBuf, "%.8s.%d: ", asctime(newtime)+11, GetTickCount()%1000);
- }
- // parse that string format
- try {
- va_list argptr;
- va_start(argptr, fmt);
- _vsnprintf(m_tBuf+strlen(m_tBuf), TBUF_SIZE, fmt, argptr);
- va_end(argptr);
- printf("%sn", m_tBuf);
- m_ofs<<m_tBuf<<endl;
- m_ofs.flush();
- }
- catch(...) {
- assert(0);
- m_tBuf[0] = 0;
- }
- }
- void LogMgr::StatusErr(const char* title, int errcode) {
- if(!m_ofs.is_open() || !title)
- return;
- LPVOID lpMsgBuf = NULL;
- FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- errcode,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
- (LPTSTR) &lpMsgBuf,
- 0,
- NULL
- );
- if(lpMsgBuf)
- StatusOut("%s. Error(%d): %s", title, errcode, (LPCTSTR)lpMsgBuf);
- LocalFree(lpMsgBuf);
- }
- void LogMgr::RemoveOldTmpFile(const char* prefix, const char* tmpPath) {
- if(!tmpPath || !prefix)
- return;
- WIN32_FIND_DATA fileData;
- string match = tmpPath;
- match.append(prefix);
- match.append("*.tmp");
- HANDLE hFind = FindFirstFile(match.data(), &fileData);
- if(hFind == INVALID_HANDLE_VALUE)
- return;
- for(;;) {
- string path = tmpPath;
- path.append(fileData.cFileName);
- DeleteFile(path.data());
- if(!FindNextFile(hFind, &fileData))
- break;
- }
- FindClose(hFind);
- }