📄 logging.c
字号:
#include <stdio.h>#include <string.h>#include <string.h>#include <sys/time.h>#include "logging.h" typedef enum { ITWORKS_PRIORITY_FATAL = 0, ITWORKS_PRIORITY_ALERT = 1, ITWORKS_PRIORITY_CRIT = 2, ITWORKS_PRIORITY_ERROR = 3, ITWORKS_PRIORITY_WARN = 4, ITWORKS_PRIORITY_NOTICE = 5, ITWORKS_PRIORITY_INFO = 6, ITWORKS_PRIORITY_DEBUG = 7,} itwork_priority_t;static const char* const priorities[] = { "FATAL", "ALERT", "CRIT", "ERROR", "WARN", "NOTICE", "INFO", "DEBUG",}; #define DEFAULT_LOGGING_FILE "itworks.log"FILE *logging_stream = NULL;int logging_level = ITWORKS_PRIORITY_DEBUG;int logging_initialized = 0;int file_is_writing = 0;char *logging_format_string = "%s %2s : [%s]%s\n";void init_logging(){ if(logging_stream!=NULL) fclose(logging_stream); logging_stream = fopen(DEFAULT_LOGGING_FILE,"a+"); if(logging_stream == NULL) { fprintf(stderr,"Can not open the logging file!\n"); exit(1); } logging_initialized=1; }const char* priority_to_string(int a_priority){ if ( (a_priority < 0) || (a_priority > 7) ) a_priority = 7; return priorities[a_priority];} void uninit_logging(){ if(logging_stream != NULL) fclose(logging_stream); logging_stream = NULL;}void log_fatal(char *facility, char *message){ time_t tm; char ts[25],buffer[1024]; memset(buffer,0,1024); time(&tm); strcpy(ts,ctime(&tm)); ts[24]='\0'; sprintf(buffer,logging_format_string,ts,priority_to_string(ITWORKS_PRIORITY_FATAL),facility,message); buffer[strlen(buffer)]='\0'; while(file_is_writing); file_is_writing ++; fwrite(buffer,strlen(buffer),1,logging_stream); fflush(logging_stream); file_is_writing --;}void log_alert(char *facility, char *message){ time_t tm; char ts[25],buffer[1024]; if(logging_level<ITWORKS_PRIORITY_ALERT) return ; memset(buffer,0,1024); time(&tm); strcpy(ts,ctime(&tm)); ts[24]='\0'; sprintf(buffer,logging_format_string,ts,priority_to_string(ITWORKS_PRIORITY_ALERT),facility,message); buffer[strlen(buffer)]='\0'; while(file_is_writing); file_is_writing ++; fwrite(buffer,strlen(buffer),1,logging_stream); fflush(logging_stream); file_is_writing --; }void log_crit(char *facility, char *message){ time_t tm; char ts[25],buffer[1024]; if(logging_level<ITWORKS_PRIORITY_CRIT) return ; memset(buffer,0,1024); time(&tm); strcpy(ts,ctime(&tm)); ts[24]='\0'; sprintf(buffer,logging_format_string,ts,priority_to_string(ITWORKS_PRIORITY_CRIT),facility,message); buffer[strlen(buffer)]='\0'; while(file_is_writing); file_is_writing ++; fwrite(buffer,strlen(buffer),1,logging_stream); fflush(logging_stream); file_is_writing --;}void log_error(char *facility, char *message){ time_t tm; char ts[25],buffer[1024]; if(logging_level<ITWORKS_PRIORITY_ERROR) return ; memset(buffer,0,1024); time(&tm); strcpy(ts,ctime(&tm)); ts[24]='\0'; sprintf(buffer,logging_format_string,ts,priority_to_string(ITWORKS_PRIORITY_ERROR),facility,message); buffer[strlen(buffer)]='\0'; while(file_is_writing); file_is_writing ++; fwrite(buffer,strlen(buffer),1,logging_stream); fflush(logging_stream); file_is_writing --;}void log_warn(char *facility, char *message){ time_t tm; char ts[25],buffer[1024]; if(logging_level<ITWORKS_PRIORITY_WARN) return ; memset(buffer,0,1024); time(&tm); strcpy(ts,ctime(&tm)); ts[24]='\0'; sprintf(buffer,logging_format_string,ts,priority_to_string(ITWORKS_PRIORITY_WARN),facility,message); buffer[strlen(buffer)]='\0'; while(file_is_writing); file_is_writing ++; fwrite(buffer,strlen(buffer),1,logging_stream); fflush(logging_stream); file_is_writing --;}void log_notice(char *facility, char *message){ time_t tm; char ts[25],buffer[1024]; if(logging_level<ITWORKS_PRIORITY_NOTICE) return ; memset(buffer,0,1024); time(&tm); strcpy(ts,ctime(&tm)); ts[24]='\0'; sprintf(buffer,logging_format_string,ts,priority_to_string(ITWORKS_PRIORITY_NOTICE),facility,message); buffer[strlen(buffer)]='\0'; while(file_is_writing); file_is_writing ++; fwrite(buffer,strlen(buffer),1,logging_stream); fflush(logging_stream); file_is_writing --;}void log_info(char *facility, char *message){ time_t tm; char ts[25],buffer[1024]; if(logging_level<ITWORKS_PRIORITY_INFO) return ; memset(buffer,0,1024); time(&tm); strcpy(ts,ctime(&tm)); ts[24]='\0'; sprintf(buffer,logging_format_string,ts,priority_to_string(ITWORKS_PRIORITY_INFO),facility,message); buffer[strlen(buffer)]='\0'; while(file_is_writing); file_is_writing ++; fwrite(buffer,strlen(buffer),1,logging_stream); fflush(logging_stream); file_is_writing --;}void log_debug(char *facility, char *message){ time_t tm; char ts[25],buffer[1024]; if(logging_level<ITWORKS_PRIORITY_DEBUG) return ; memset(buffer,0,1024); time(&tm); strcpy(ts,ctime(&tm)); ts[24]='\0'; sprintf(buffer,logging_format_string,ts,priority_to_string(ITWORKS_PRIORITY_DEBUG),facility,message); buffer[strlen(buffer)]='\0'; while(file_is_writing); file_is_writing ++; fwrite(buffer,strlen(buffer),1,logging_stream); fflush(logging_stream); file_is_writing --;}/*int main(void){ init_logging(); log_fatal("MAIN","The main has started!",logging_format_string); log_alert("MAIN","The main has started!",logging_format_string); log_crit("MAIN","The main has started!",logging_format_string); log_error("MAIN","The main has started!",logging_format_string); log_warn("MAIN","The main has started!",logging_format_string); log_info("MAIN","The main has started!",logging_format_string); log_debug("MAIN","The main has started!",logging_format_string); uninit_logging(); return ;}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -