目前代码只考虑linux环境,win32将随后补充:
Assertx.h
/*
* File: Assertx.h
* Author: Vicky.H
* Email: eclipser@163.com
*
* Created on 2014年1月16日, 下午8:33
*/
// 断言
#ifndef CN_VICKY__ASSERTX_H
#define CN_VICKY__ASSERTX_H
#include <sys/types.h>
#include <stdio.h>
extern int g_Command_Assert; //控制参数,不提示Assert的对话框,直接忽略
extern int g_Command_IgnoreMessageBox; //控制参数,跳过MyMessageBox的中断
void __assert__(const char* file, uint line, const char* func, const char* expr);
void __assertex__(const char* file, uint line, const char* func, const char* expr, const char* msg);
void __assertspecial__(const char* file, uint line, const char* func, const char* expr, const char* msg);
void __messagebox__(const char* msg);
void __protocol_assert__(const char* file, int line, const char* func, const char* expr);
#define Assert(expr) {if(!(expr)){__assert__(__FILE__,__LINE__,__PRETTY_FUNCTION__,#expr);}}
#define ProtocolAssert(expr) ((VOID)((expr)?0:(__protocol_assert__(__FILE__,__LINE__,__PRETTY_FUNCTION__,#expr),0)))
#define AssertEx(expr,msg) {if(!(expr)){__assertex__(__FILE__,__LINE__,__PRETTY_FUNCTION__,#expr,msg);}}
#define AssertSpecial(expr,msg) {if(!(expr)){__assertspecial__(__FILE__,__LINE__,__PRETTY_FUNCTION__,#expr,msg);}}
#define AssertExPass(expr,msg) {if(!(expr)){__assertex__(__FILE__,__LINE__,__PRETTY_FUNCTION__,#expr,msg);}}
#define MyMessageBox(msg) ((VOID)0)
#endif /* CN_VICKY__ASSERTX_H */
Assertx
#include "Assertx.h"
#include <time.h>
#include <execinfo.h>
int g_Command_Assert = 0;
// 控制参数,0:会通过弹出对话框让用户选择(缺省值)
// 1:忽略
// 2:继续抛出异常用于获取运行堆栈
int g_Command_IgnoreMessageBox = false; //控制参数,跳过MyMessageBox的中断
void __show__(const char* szTemp) {
printf("Assert:%s", szTemp);
throw (1);
}
void __messagebox__(const char*msg) {
if (g_Command_IgnoreMessageBox)
return;
}
void __assert__(const char * file, uint line, const char * func, const char * expr) {
char szTemp[1024] = {0};
sprintf(szTemp, "[%s][%d][%s][%s]\n", file, line, func, expr);
__show__(szTemp);
}
void __assertex__(const char * file, uint line, const char * func, const char * expr, const char* msg) {
char szTemp[1024] = {0};
sprintf(szTemp, "[%s][%d][%s][%s]\n[%s]\n", file, line, func, expr, msg);
__show__(szTemp);
}
void __assertspecial__(const char * file, uint line, const char * func, const char * expr, const char* msg) {
char szTemp[1024] = {0};
sprintf(szTemp, "S[%s][%d][%s][%s]\n[%s]\n", file, line, func, expr, msg);
__show__(szTemp);
}
void __protocol_assert__(const char * file, uint line, const char * func, const char * expr) {
printf("[%s][%d][%s][%s]", file, line, func, expr);
}
Common.h
/*
* File: Common.h
* Author: Vicky.H
* Email: eclipser@163.com
*
* Created on 2014年1月16日, 下午10:46
*/
#ifndef CN_VICKY__COMMON_H
#define CN_VICKY__COMMON_H
#include "Assertx.h"
//无效的ID值
#define INVALID_ID -1
#define __ENTER_FUNCTION {try{
#define __LEAVE_FUNCTION }catch(...){AssertSpecial(false,__PRETTY_FUNCTION__);}}
//根据指针值删除内存
#ifndef SAFE_DELETE
#define SAFE_DELETE(x) if( (x)!=NULL ) { delete (x); (x)=NULL; }
#endif
//根据指针值删除数组类型内存
#ifndef SAFE_DELETE_ARRAY
#define SAFE_DELETE_ARRAY(x) if( (x)!=NULL ) { delete[] (x); (x)=NULL; }
#endif
//根据指针调用free接口
#ifndef SAFE_FREE
#define SAFE_FREE(x) if( (x)!=NULL ) { free(x); (x)=NULL; }
#endif
//根据指针调用Release接口
#ifndef SAFE_RELEASE
#define SAFE_RELEASE(x) if( (x)!=NULL ) { (x)->Release(); (x)=NULL; }
#endif
#endif /* CN_VICKY__COMMON_H */
log.h
/*
* File: Log.h
* Author: Vicky.H
* Email: eclipser@163.com
*
* Created on 2014年1月16日, 下午10:45
*/
#ifndef CN_VICKY__LOG_H
#define CN_VICKY__LOG_H
#include <pthread.h>
#define SHMEM_LOG_PATH "./Log/ShareMemory.log"
extern pthread_mutex_t log_mutex;
class Log {
public:
Log();
~Log();
//支持异步写入操作的日志写入
static void SaveLog(const char* filename, const char* msg, ...);
};
#endif /* CN_VICKY__LOG_H */
Log.cpp
#include "Log.h"
#include "Common.h"
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
// 保证日志记录线程安全
pthread_mutex_t log_mutex;
Log::Log() {
}
Log::~Log() {
}
void Log::SaveLog(const char* filename, const char* msg, ...) {
__ENTER_FUNCTION
pthread_mutex_lock(&log_mutex);
char buffer[2048];
memset(buffer, 0, 2048);
va_list argptr;
try {
va_start(argptr, msg);
vsprintf(buffer, msg, argptr);
va_end(argptr);
FILE* f = fopen(filename, "ab");
fwrite(buffer, 1, strlen(buffer), f);
fclose(f);
printf(buffer);
} catch (...) {
printf("a big error here");
}
pthread_mutex_unlock(&log_mutex);
return;
__LEAVE_FUNCTION
pthread_mutex_unlock(&log_mutex);
}
ShareMemApi.h