Debug_Info跨平台动态库实现(3)

    上一节讲了一下内部函数的实现,这些实现代码有些可能太粗糙了,大家可以互相指正批评。接下来的这一节我将具体的给出对外函数的实现。

    为了连贯之前的内容,本节将接着上一节的内容继续。

    八、对外函数实现

    对外函数只有三个,下面将具体给出

    1、debug_info_init()实现

    该方法的实现:首先判断是否传入的值是否合法;其次调用create_dir内部函数创建目录;然后调用create_or_bak_file内部方法,向文件中写入初始化信息。不过不要忘记fflush和fclose文件。该文件实在create_or_bak_file函数是打开的。

DllAPI int STDCALL
debug_info_init(
const int _category, const int _modual, const char *_pathdir){
char currentdatetime[64] = {0};
if(_category >=0 && _category <=(CATEGORY_SHUTDOWN|
CATEGORY_FATAL_ERROR
|
CATEGORY_ERROR
|
CATEGORY_WARN
|
CATEGORY_AUDIT
|
CATEGORY_TRACE
|
CATEGORY_INFO))
global_category
= _category;
if(_modual >=0 && _modual <=(MODULE_NONE|
MODULE_SERVER
|
MODULE_GSSYS
|
MODULE_GSDB
|
MODULE_SQL_PARSE
|
MODULE_ENCRYPT
|
MODULE_SECURITY))
global_module
= _modual;
if(_pathdir!=NULL ){
snprintf(global_path,
sizeof(global_path) , _pathdir);
}
if(create_dir(global_path)){
return -1;
}
snprintf(global_path_file_name,
sizeof(global_path_file_name),global_path);
snprintf(global_path_file_name
+strlen(global_path),sizeof(global_path_file_name)-strlen(global_path),debug_info_file_name);
if(!create_or_bak_file()){
get_current_time(currentdatetime);
fprintf(debug_info_file,
"[%s][Debug Info Start][Category_Level: %d][Modules_Level: %d]\n",currentdatetime,global_category,global_module);
}
fflush(debug_info_file);
fclose(debug_info_file);
return 0;

}

    2、debug_info_fini()函数实现:

    该函数用于析构,实际上没有太多的需要析构。主要有记录退出信息,同样不要忘记fflush和fclose方法来关闭create_or_bak_file打开的文件。

DllAPI int STDCALL
debug_info_fini(){
char currentdatetime[64] = {0};
if(!create_or_bak_file()){
get_current_time(currentdatetime);
fprintf(debug_info_file,
"[%s][Debug Info Shutdown]\n",currentdatetime);
}
fflush(debug_info_file);
fclose(debug_info_file);

return 0;
}

    3、debug_info_rec()函数实现

    该函数是可变参数的,因此该函数的主要用途是封装参数变量,对可变参数进行解析。可变参数的实现中用到一个va_list数据类型,然后利用va_start,vs_printf,va_end三个函数就可以把可变参数解析成字符串数组了。然后调用的时候,调用内部方法_debug_info_rec了。

DllAPI int STDCALL
debug_info_rec(
const int _cat,
const int _code,
const int _module,
const char* _file,
const char* _func,
const int _line,
const char* _format,
...)
{
debug_info debug_info;
char currentdatetime[64] = {0};
va_list argptr;
char buffer[MAX_ARGS] ={0};
int cnt;
get_current_time(currentdatetime);
debug_info._time
=currentdatetime;
debug_info._cat
=_cat;
debug_info._code
=_code;
debug_info._module
=_module;
debug_info._file
=_file;
debug_info._func
=_func;
debug_info._line
=_line;

if(_format!=NULL){
va_start(argptr,_format);
cnt
= vsprintf(buffer, _format, argptr);
va_end(argptr);
if(cnt==EOF){
return -1;
}
}
return _debug_info_rec(debug_info,buffer);
}

    这样,所有的代码就全部给出了,非常简单的功能实现。当然还需要大家的批评指正了,帮助我不断修改他。希望大家不吝赐教,帮助我进步。

    当然,动态库代码实现了,还有许多东西要做,比如生成项目,将在接下来使用CMake来实现工程的建立。测试动态库的功能性,将写一个具体的测试程序来实现。内容可能就更简单了,将在接下来的内容中给出。

posted @ 2011-04-04 16:33  uber_niello  阅读(230)  评论(0)    收藏  举报