Recently in the game development, read some of the source code after doing some summary and record, I hope to himself and other friends to help. Software development often will be the key operation, general warning and serious errors, through the form of string output to the log file, the notebook system to achieve Bootlog,debuglog,warning,fatallog output form, support multithreading. There is no place to welcome corrections, exchanges. Email:caohaitao_linux@163.com
Lolog Log System Key Knowledge :
1. File operation
2. Gets the string of the current time
3. The processing of variable parameter function
Lolog component Elements :
1. File pointer
2. File status (can be province)
3. Mutual exclusion Lock
Note: The file state can be obtained by determining whether the file pointer is empty, but this process is used frequently, and the function call form will make the pressure stack and stack operations too much. static int occupies less space, variable see name known to use convenient, it is best not to omit file state variables
UML diagram:
File Open operation
int Lologs::openlog (const char *pcloglead)
{
int nret = 0;
if (M_nfilestatus = = Filestatus_close)
{
char szopenpathname[maxsize_pathname] = {0};
time_t ltime;
TM Tmstruct;
Time (<ime);
Tmstruct = *localtime (<ime);
strcat (Szopenpathname, subdirname_log);
sprintf (Szopenpathname + strlen (szopenpathname), "%s%04d-%02d-%02d.log", Pcloglead, tmstruct.tm_year+1900, Tmstruct.tm_mon+1,tmstruct.tm_mday);
#ifdef WIN32
createdirectory (Subdirname_log, NULL);
#else/* LINUX
/mkdir (Subdirname_log, 0700);
#endif
m_filesvrlog = fopen (Szopenpathname, "A +");
if (M_filesvrlog)
{
m_nfilestatus = Filestatus_open;
}
else
{
nret =-1;
}
}
return nret;
}
Gets the string of the current time
Clogs class member functions
const char *lologs::getcurdatetimewithstring (char *pstringbuf)
{
time_t Tcurdatetime;
TM *ptagcurdatatime;
Char sztimestringbuf[maxsize_timestring] = {0};
char *pwritestringbuf = pstringbuf;
if (NULL = = pwritestringbuf)
{
pwritestringbuf = sztimestringbuf;
}
Time (&tcurdatetime);
Ptagcurdatatime = LocalTime (&tcurdatetime);
sprintf (Pwritestringbuf, "%d-%02d-%02d%02d:%02d:%02d",
ptagcurdatatime->tm_year+1900,
Ptagcurdatatime->tm_mon+1,
ptagcurdatatime->tm_mday,
ptagcurdatatime->tm_hour,
Ptagcurdatatime->tm_min,
ptagcurdatatime->tm_sec);
return pwritestringbuf;
}
The processing of variable parameter function
void Lologs::writelog (const char *pclogmsg, ...)
{
char szmsgbuf[maxsize_msgbuf] = {0};
Va_list va;
Va_start (VA, pclogmsg);
vsprintf (Szmsgbuf, Pclogmsg, VA);
#ifdef WIN32
printf (szmsgbuf);
#endif
va_end (VA);
Writeloginner (Szmsgbuf, logleadname_boot);
}
C + + implementation of the log system source code