#include "HGInfoImpl.hpp" #include "HGInfo.h" #include "HGInc.h" #include "HGUtility.h" #include #include HGInfoImpl::HGInfoImpl() { HGBase_CreateLock(&m_lock); m_enabled = HGFALSE; m_log = NULL; m_console = NULL; m_type = 0; m_showTime = HGFALSE; m_showId = HGFALSE; m_showType = HGFALSE; Enable(); } HGInfoImpl::~HGInfoImpl() { Disable(); HGBase_DestroyLock(m_lock); m_lock = NULL; } HGResult HGInfoImpl::Enable() { if (m_enabled) { return HGBASE_ERR_FAIL; } HGChar cfgPath[256] = { 0 }; HGBase_GetConfigPath(cfgPath, 256); strcat(cfgPath, "HGGlobal.ini"); HGBool writeLog; HGBase_GetProfileInt(cfgPath, "Info", "writeLog", 1, &writeLog); HGBool writeConsole; HGBase_GetProfileInt(cfgPath, "Info", "writeConsole", 1, &writeConsole); HGUInt defType = HGBASE_INFOTYPE_FATAL | HGBASE_INFOTYPE_ERROR | HGBASE_INFOTYPE_WARNING | HGBASE_INFOTYPE_DESC | HGBASE_INFOTYPE_DEBUG; HGBase_GetProfileInt(cfgPath, "Info", "type", (HGInt)defType, (HGInt*)&m_type); HGBase_GetProfileInt(cfgPath, "Info", "showTime", 1, &m_showTime); HGBase_GetProfileInt(cfgPath, "Info", "showId", 0, &m_showId); HGBase_GetProfileInt(cfgPath, "Info", "showType", 1, &m_showType); if (writeLog) { HGChar logPath[256]; HGBase_GetLogFilePath(logPath, 256); HGBase_CreateDir(logPath); timeb tb; ftime(&tb); struct tm* p = localtime(&tb.time); char fileName[256]; #if defined(HG_CMP_MSC) sprintf(fileName, "\\%04d%02d%02d.log", (1900 + p->tm_year), (1 + p->tm_mon), p->tm_mday); #else sprintf(fileName, "/%04d%02d%02d.log", (1900 + p->tm_year), (1 + p->tm_mon), p->tm_mday); #endif strcat(logPath, fileName); HGBase_OpenLog(logPath, &m_log); } if (writeConsole) { HGBase_OpenConsole(&m_console); } m_enabled = HGTRUE; return HGBASE_ERR_OK; } HGResult HGInfoImpl::Disable() { if (!m_enabled) { return HGBASE_ERR_FAIL; } HGBase_CloseConsole(m_console); m_console = NULL; HGBase_CloseLog(m_log); m_log = NULL; m_enabled = HGFALSE; return HGBASE_ERR_OK; } HGResult HGInfoImpl::Write(HGUInt type, const HGChar* info) { if (!m_enabled) { return HGBASE_ERR_FAIL; } if (HGBASE_INFOTYPE_FATAL != type && HGBASE_INFOTYPE_ERROR != type && HGBASE_INFOTYPE_WARNING != type && HGBASE_INFOTYPE_DESC != type && HGBASE_INFOTYPE_DEBUG != type) { return HGBASE_ERR_INVALIDARG; } if (NULL == info || '\0' == *info) { return HGBASE_ERR_INVALIDARG; } if (0 == (type & m_type)) { return HGBASE_ERR_FAIL; } char writeInfo[2048] = { 0 }; if (m_showTime) { timeb tb; ftime(&tb); struct tm* p = localtime(&tb.time); char timeStr[64] = { 0 }; sprintf(timeStr, "[%04d/%02d/%02d-%02d:%02d:%02d.%03d]", (1900 + p->tm_year), (1 + p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tb.millitm); strcat(writeInfo, timeStr); strcat(writeInfo, " "); } if (m_showId) { char idStr[32] = { 0 }; #if defined(HG_CMP_MSC) sprintf(idStr, "[0x%08X/0x%08X]", GetCurrentProcessId(), GetCurrentThreadId()); #else sprintf(idStr, "[0x%08X/0x%08X]", (HGUInt)getpid(), (HGUInt)syscall(SYS_gettid)); #endif strcat(writeInfo, idStr); strcat(writeInfo, " "); } if (m_showType) { char typeStr[24] = { 0 }; if (HGBASE_INFOTYPE_FATAL == type) sprintf(typeStr, "[%s]", "FAT"); else if (HGBASE_INFOTYPE_ERROR == type) sprintf(typeStr, "[%s]", "ERR"); else if (HGBASE_INFOTYPE_WARNING == type) sprintf(typeStr, "[%s]", "WAR"); else if (HGBASE_INFOTYPE_DESC == type) sprintf(typeStr, "[%s]", "DES"); else sprintf(typeStr, "[%s]", "DEB"); strcat(writeInfo, typeStr); strcat(writeInfo, " "); } strcat(writeInfo, info); HGBase_EnterLock(m_lock); HGBase_WriteLog(m_log, writeInfo); HGBase_WriteConsole(m_console, writeInfo); HGBase_LeaveLock(m_lock); return HGBASE_ERR_OK; }