-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclog.cpp
More file actions
56 lines (50 loc) · 1.05 KB
/
clog.cpp
File metadata and controls
56 lines (50 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "clog.h"
#include<string>
#include <stdarg.h>
#include<time.h>
//author hzkai
#define DateTimeFormat "%Y-%m-%d %H:%M:%S"
Log::Log()
{
memset(m_fileName,0,STRING_LENGTH);
m_fileStream=NULL;
}
Log::Log(const char* fileName)
{
memset(m_fileName,0,STRING_LENGTH);
m_fileStream=fopen(fileName,"ab+");
assert(NULL != m_fileStream);
}
Log* Log::getInstance()
{
return new Log();
}
int Log::openFile(const char* fileName)
{
FILE* temp=fopen(fileName,"ab+");
m_fileStream= temp?temp:m_fileStream;
if(NULL == temp)
{
return EE_FAILURE;
}
int len=strlen(fileName);
strncpy(m_fileName,fileName,len);
return EE_SUCSESS;
}
Log::~Log()
{
fclose(m_fileStream);
memset(m_fileName,0,STRING_LENGTH);
}
void Log::writeLog(LogLevel logLevel,const char* fmt,...)
{
char temp[STRING_LENGTH]={0};
va_list ap;
va_start(ap,fmt);
vsnprintf(temp,sizeof(temp),fmt,ap);
va_end(ap);
time_t now = time(NULL);
char buf[STRING_LENGTH]={0};
strftime(buf, sizeof(buf), DateTimeFormat, gmtime(&now));
fprintf(m_fileStream,"%s %s | %s\r\n",buf,LevelMsg[logLevel],temp);
}