diff --git a/.gitignore b/.gitignore index 486a215..9db3cb7 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ /.vs/ZE-Standard-Libraries/v15 /ZE-Standard-Libraries /ZE-Standard-Libraries.sln +/.vs diff --git a/error/error.c b/error/error.c index 424eb23..d3b8e66 100644 --- a/error/error.c +++ b/error/error.c @@ -1,6 +1,102 @@ #include "error.h" -int main(int argc, char **argv){ - - return 0; +int initErrorSystem(void) { + error_list = initList(); + notice_list = initList(); + if_error = 1; + return 1; } + +int setLogDirectory(const char *path) { + logfile.id = getId(); + int memory_space = strlen(path) + 256; + char *file_path = (char *)malloc(sizeof(char)*memory_space); + + //建立完整的文件路径 + strcat(file_path, path); + strcat(file_path, "log"); + sprintf(file_path, "%d", logfile.id); + + //打开文件,若失败,返回0 + if ((logfile.fp = fopen(file_path, "w")) == NULL) { + printf("Cannot set logfile!"); + return 0; + } + logfile.if_enable = 1; + + free(file_path); + return 1; +} + +int closeLogDirectory(void) { + Node *p = error_list->head; + while (p != NULL) { + saveError(p->value); + p = p->next; + } + + p = notice_list->head; + while (p != NULL) { + saveNotice(p->value); + p = p->next; + } + + releaseList(error_list); + releaseList(notice_list); + if_error = 0; + fclose(logfile.fp); + logfile.if_enable = 0; +} + +int loadFromFile(FILE *fp,char* number) { + + return 1; +} + +int pushInfo(Info *p_info, const char *head, const char *body) { + p_info->head = (char *)malloc(sizeof(char) * strlen(head)); + p_info->body = (char *)malloc(sizeof(char) * strlen(body)); + strcpy(p_info->head, head); + strcpy(p_info->body, body); +} + +int pushError(unsigned int type, int pri, Info *p_info) { + Error error; + error.type = type; + error.priority = pri; + error.p_info = p_info; + error.time = time(NULL); +} + +int pushNotice(unsigned int type, Info *p_info) { + Notice notice; + notice.type = type; + notice.p_info = p_info; + notice.time = time(NULL); +} + +static int saveError(Error *p_error) { + fprintf(logfile.fp, + "--------------------\n\ + ERROR\n\ + Type : %ud\n\ + Priority : %d\n\ + Time : %s\n\ + Info : \n\ + %s\n\ + %s\n\ + ---------------------\n", + p_error->type, p_error->priority, ctime( &(p_error->time) ), p_error->p_info->head, p_error->p_info->body); +} +static int saveNotice(Notice *p_notice) { + fprintf(logfile.fp, + "--------------------\n\ + NOTICE\n\ + Type : %ud\n\ + Time : %s\n\ + Info : \n\ + %s\n\ + %s\n\ + ----------------------\n", + p_notice->type, ctime( &(p_notice->time) ), p_notice->p_info->head, p_notice->p_info->body); +} \ No newline at end of file diff --git a/error/error.h b/error/error.h index e9499a1..fb342e7 100644 --- a/error/error.h +++ b/error/error.h @@ -1,4 +1,5 @@ -#include "../list/list_easy.h" +#include "../list/list_expand.h" +#include #include #ifndef ERROR_H @@ -6,24 +7,23 @@ #define HIGH 0x3 #define STANDARD 0x2 -#define LOW 0x1 +#define LOW 0x1 typedef struct Info{ char *head; char *body; - char *tail; }Info; typedef struct Error{ unsigned int type; - int pri; - Info info; + int priority; + Info *p_info; time_t time; }Error; typedef struct Notice{ unsigned int type; - Info *info; + Info *p_info; time_t time; }Notice; @@ -31,29 +31,25 @@ typedef struct Log{ FILE *fp; int if_enable; unsigned long int id; - unsigned int type; }Log; +Log logfile; List *error_list = NULL; -List *log_list = NULL; +List *notice_list = NULL; int if_error = 0; -int error_init(int if_enable); -int set_logDirectory(char *); -int push_error(unsigned int type, int pri, Info *p_info); -int push_notice(unsigned int type, Info *p_info); -int save_error(Error *p_error); -int save_notice(Notice *p_notice); -Info *init_Info(char *m_info); +int initErrorSystem(void); -int error_init(int if_enable){ - if(if_enable == 1){ - error_list = init_list(); - log_list = init_list(); - if_error = 1; - return 1; - } - return 0; -} +int setLogDirectory(const char *path); +int closeLogDirectory(void); +int loadFromFile(FILE *fp,char* number); -#endif +int pushInfo(Info *p_info, const char *head,const char *body); +int pushError(unsigned int type, int pri, Info *p_info); +int pushNotice(unsigned int type, Info *p_info); + +//为保证处理效果,不允许外调下列函数 +static int saveError(Error *p_error); +static int saveNotice(Notice *p_notice); + +#endif \ No newline at end of file diff --git a/list/list_expand.h b/list/list_expand.h index 4abcd23..cc60f4a 100644 --- a/list/list_expand.h +++ b/list/list_expand.h @@ -1,6 +1,6 @@ #ifndef LIST_EXPAND_H #define LIST_EXPAND_H -#include "list.h" +#include "list.c" Node *nodeWithInt(int);//蹇熷垵濮嬪寲涓涓崟涓鍊艰妭鐐瑰苟璧嬪 Node *nodeWithDouble(double);//蹇熷垵濮嬪寲涓涓妭鍗曚竴鍊肩偣骞惰祴鍊