00001 #include <stdio.h> 00002 #include <pthread.h> 00003 00004 #include "hlog.h" 00005 00006 static FILE *hlogfile = NULL; 00007 00009 static pthread_mutex_t write_mutex = PTHREAD_MUTEX_INITIALIZER; 00010 00011 int hlog_init(char* logpath) { 00012 hlogfile = fopen(logpath,"w"); 00013 if (hlogfile == NULL) 00014 return -1; 00015 else 00016 return 0; 00017 } 00018 00019 void hlog_write(enum hlog_flag flag, char* text) 00020 { 00021 if (hlogfile == NULL) 00022 return; 00023 00024 00025 pthread_mutex_lock(&write_mutex); 00026 00027 switch (flag) { 00028 case HLOG_ERROR: 00029 fprintf(hlogfile, "ERROR: "); 00030 break; 00031 case HLOG_WARNING: 00032 fprintf(hlogfile, "WARNING: "); 00033 break; 00034 case HLOG_NORMAL: 00035 fprintf(hlogfile, "NORMAL: "); 00036 break; 00037 case HLOG_DEBUG: 00038 fprintf(hlogfile, "DEBUG: "); 00039 break; 00040 default: 00041 fprintf(hlogfile, "UNKNOWN: "); 00042 break; 00043 } 00044 00045 fprintf(hlogfile,"%s", text); 00046 fflush(hlogfile); 00047 00048 pthread_mutex_unlock(&write_mutex); 00049 } 00050 00051 void hlog_write_text(char* text) 00052 { 00053 if (hlogfile == NULL) 00054 return; 00055 00056 pthread_mutex_lock(&write_mutex); 00057 00058 fprintf(hlogfile,"%s", text); 00059 fflush(hlogfile); 00060 00061 pthread_mutex_unlock(&write_mutex); 00062 } 00063 00064 void hlog_write_integer(int number) 00065 { 00066 if (hlogfile == NULL) 00067 return; 00068 00069 pthread_mutex_lock(&write_mutex); 00070 00071 fprintf(hlogfile,"%d", number); 00072 fflush(hlogfile); 00073 00074 pthread_mutex_unlock(&write_mutex); 00075 } 00076 00077 void hlog_close() 00078 { 00079 if (hlogfile == NULL) 00080 return; 00081 00082 fclose(hlogfile); 00083 } 00084