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