xref: /rkdeveloptool/RKLog.cpp (revision 26edf8560157251a87ac2dba355d11ed8a1445de)
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 		printf("%s\n",szBuf);
68 		Write( szBuf);
69 	}
70 }
71 bool CRKLog::Write(string text)
72 {
73 	time_t	now;
74 	struct tm timeNow;
75 	char szDateTime[100];
76 	string  strName;
77 	FILE *file=NULL;
78 	time(&now);
79 	localtime_r(&now, &timeNow);
80 	sprintf(szDateTime, "%04d-%02d-%02d.txt", timeNow.tm_year + 1900, timeNow.tm_mon + 1, timeNow.tm_mday);
81 	strName = m_path + m_name+szDateTime;
82 
83 	try {
84 		file = fopen(strName.c_str(), "ab+");
85 		if (!file) {
86 			return false;
87 		}
88 		sprintf(szDateTime, "%02d:%02d:%02d \t", timeNow.tm_hour, timeNow.tm_min, timeNow.tm_sec);
89 		text = szDateTime + text + "\r\n";
90 		fwrite(text.c_str(), 1, text.size() * sizeof(char), file);
91 		fclose(file);
92 	} catch (...) {
93 		fclose(file);
94 		return false;
95 	}
96 	return true;
97 }
98 bool CRKLog::SaveBuffer(string fileName, PBYTE lpBuffer, DWORD dwSize)
99 {
100 	FILE *file;
101 	file = fopen(fileName.c_str(), "wb+");
102 	if (!file) {
103 		return false;
104 	}
105 	fwrite(lpBuffer, 1, dwSize, file);
106 	fclose(file);
107 	return true;
108 }
109 void CRKLog::PrintBuffer(string &strOutput, PBYTE lpBuffer, DWORD dwSize, UINT uiLineCount)
110 {
111 	UINT i,count;
112 	char strHex[32];
113 	strOutput = "";
114 	for (i = 0, count = 0; i < dwSize; i++, count++) {
115 		sprintf(strHex, "%X", lpBuffer[i]);
116 		strOutput = strOutput + " " + strHex;
117 		if (count >= uiLineCount) {
118 			strOutput += "\r\n";
119 			count = 0;
120 		}
121 	}
122 }
123