1 /* 2 * (C) Copyright 2017 Fuzhou Rockchip Electronics Co., Ltd 3 * Seth Liu 2017.03.01 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 #include "RKLog.h" 8 int file_stat(string strPath) 9 { 10 struct stat statBuf; 11 int ret; 12 ret = stat(strPath.c_str(), &statBuf); 13 if (ret != 0) { 14 return STAT_NOT_EXIST; 15 } 16 if (S_ISDIR(statBuf.st_mode)) 17 return STAT_DIR; 18 return STAT_FILE; 19 } 20 string CRKLog::GetLogSavePath() 21 { 22 return m_path; 23 } 24 bool CRKLog::GetEnableLog() 25 { 26 return m_enable; 27 } 28 void CRKLog::SetEnableLog(bool bEnable) 29 { 30 m_enable = bEnable; 31 } 32 33 CRKLog::CRKLog(string logFilePath, string logFileName, bool enable) 34 { 35 LogSavePath.setContainer(this); 36 LogSavePath.getter(&CRKLog::GetLogSavePath); 37 38 EnableLog.setContainer(this); 39 EnableLog.getter(&CRKLog::GetEnableLog); 40 EnableLog.setter(&CRKLog::SetEnableLog); 41 42 if (!opendir(logFilePath.c_str())) { 43 m_path = ""; 44 } else { 45 if (logFilePath[logFilePath.size() - 1] != '/') { 46 logFilePath += '/'; 47 } 48 m_path = logFilePath; 49 } 50 if (logFileName.size() <= 0) { 51 m_name = "Log"; 52 } else 53 m_name = logFileName; 54 m_enable = enable; 55 56 } 57 CRKLog::~CRKLog() 58 { 59 } 60 void CRKLog::Record(const char *lpFmt,...) 61 { 62 /************************* �������־ ***********************/ 63 char szBuf[1024] = ""; 64 GET_FMT_STRING(lpFmt, szBuf); 65 if ((m_enable) && (m_path.size() > 0)) 66 { 67 Write( szBuf); 68 } 69 } 70 bool CRKLog::Write(string text) 71 { 72 time_t now; 73 struct tm timeNow; 74 char szDateTime[100]; 75 string strName; 76 FILE *file=NULL; 77 time(&now); 78 localtime_r(&now, &timeNow); 79 sprintf(szDateTime, "%04d-%02d-%02d.txt", timeNow.tm_year + 1900, timeNow.tm_mon + 1, timeNow.tm_mday); 80 strName = m_path + m_name+szDateTime; 81 82 try { 83 file = fopen(strName.c_str(), "ab+"); 84 if (!file) { 85 return false; 86 } 87 sprintf(szDateTime, "%02d:%02d:%02d \t", timeNow.tm_hour, timeNow.tm_min, timeNow.tm_sec); 88 text = szDateTime + text + "\r\n"; 89 fwrite(text.c_str(), 1, text.size() * sizeof(char), file); 90 fclose(file); 91 } catch (...) { 92 fclose(file); 93 return false; 94 } 95 return true; 96 } 97 bool CRKLog::SaveBuffer(string fileName, PBYTE lpBuffer, DWORD dwSize) 98 { 99 FILE *file; 100 file = fopen(fileName.c_str(), "wb+"); 101 if (!file) { 102 return false; 103 } 104 fwrite(lpBuffer, 1, dwSize, file); 105 fclose(file); 106 return true; 107 } 108 void CRKLog::PrintBuffer(string &strOutput, PBYTE lpBuffer, DWORD dwSize, UINT uiLineCount) 109 { 110 UINT i,count; 111 char strHex[32]; 112 strOutput = ""; 113 for (i = 0, count = 0; i < dwSize; i++, count++) { 114 sprintf(strHex, "%X", lpBuffer[i]); 115 strOutput = strOutput + " " + strHex; 116 if (count >= uiLineCount) { 117 strOutput += "\r\n"; 118 count = 0; 119 } 120 } 121 } 122