📄 log.hpp
字号:
/**************************************************************************/
/* Copyright(C) 2001 by Nanjing XINWANG TEC CO.,LTD. */
/**************************************************************************/
/* Name: log.hpp Version: 1.0.0 */
/* Created by: Zhangbo Date: 2001-12-04 */
/* Content: declare of class LogFile */
/* Comment: standard output and log file output */
/* global variable clLog used by files which includes log.hpp*/
/* Modified: */
/**************************************************************************/
#ifndef _LOG_HPP_
#define _LOG_HPP_
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
//output level define
enum OUTPUT_LEVEL{LEVEL_DEBUG=0, LEVEL_INFO, LEVEL_WARN, LEVEL_ERROR,
LEVEL_PANIC, LEVEL_NOOUT, LEVEL_COUNT};
//default log file output level
static const OUTPUT_LEVEL DEFAULT_LOG_LEVEL = LEVEL_INFO;
//default standard output level
static const OUTPUT_LEVEL DEFAULT_TERM_LEVEL = LEVEL_INFO;
class LogFile
{
public:
LogFile(char* sProcName = "Unknown",
OUTPUT_LEVEL eLogLevel = DEFAULT_LOG_LEVEL,
OUTPUT_LEVEL eTermLevel = DEFAULT_TERM_LEVEL)
{
assert((NULL != sProcName) && (0 != strlen(sProcName)));
m_sFileName = NULL;
m_sProcName = new char[strlen(sProcName) + 1];
if (NULL == m_sProcName)
{
Panic(0, "Memory exhausted !");
}
strcpy(m_sProcName, sProcName);
m_eLogLevel = eLogLevel;
m_eTermLevel = eTermLevel;
m_pFile = NULL;
m_bOpen = false;
}
~LogFile()
{
if (m_bOpen)
{
fclose(m_pFile);
m_pFile = NULL;
m_bOpen = false;
}
if (NULL != m_sProcName)
{
delete []m_sProcName;
}
}
void SetProcName(const char* sProcName);
void SetLogLevel(OUTPUT_LEVEL eLevel);
void SetTermLevel(OUTPUT_LEVEL eLevel);
//COMMENT: Open log file to write(append mode) log information.
// Standard output is always open.
//INPUT: sFileName -- name of log file
//OUTPUT: none
//RETURN: 0 for OK, -1 for error
int Open(const char* sFileName);
//COMMENT: Close log file.
// Standard output need not be closed.
//INPUT: none
//OUTPUT: none
//RETURN: 0 for OK, -1 for error
int Close(void);
//COMMENT: These five function used to write standard output and log
// file. Each fuction uses an output level. If the level
// greater than or equal to log level, the information will
// be output. Otherwise, no output.
//INPUT: iErrno -- system errno, 0 for nothing
// pszFormat, ... -- information content,
// syntax is the same as printf
//OUTPUT: none
//RETURN: none
void DeBug(int iErrno, const char* sFormat, ...);
void Info(int iErrno, const char* sFormat, ...);
void Warn(int iErrno, const char* sFormat, ...);
void Error(int iErrno, const char* sFormat, ...);
void Panic(int iErrno, const char* sFormat, ...);
#ifdef _TEST
char* GetOutput(void);
#endif
private:
char* m_sFileName; //name of log file
char* m_sProcName; //name of program which one logs
OUTPUT_LEVEL m_eLogLevel; //log file output level
OUTPUT_LEVEL m_eTermLevel; //standard output level
FILE* m_pFile; //file pointer for writing log file
bool m_bOpen; //if file was openned
#ifdef _TEST
char m_sOutput[2048]; //output content
#endif
//COMMENT: The basic common function used to write standard output
// and log file. If the level greater than or equal to log
// level, the information will be output. otherwise, no output.
//INPUT: eOutputLevel -- level output fuction
// iErrno -- system errno, 0 for nothing
// pszFormat -- information content, syntax is the same as printf
// args -- arguments list
//OUTPUT: none
void Output(OUTPUT_LEVEL eOutputLevel, int iErrno,
const char* sFormat, va_list args);
};
extern LogFile g_clLog; //global variable
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -