📄 logfile.c
字号:
#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <string.h>#include <errno.h>#include <glib.h>#include <sys/time.h>#include "common.h"#define LOGFILE_START_HEADER "====================================="#define LOGFILE_END_HEADER "-------------------------------------"#define LOGFILE_NEW_LINE "\n"#define LOGFILE_MAX_LINE_LEN 0x1000#define LOGFILE_FMT " %s %s %s (%s/%s%s)"#define LOGFILE_TO_STR "To:"#define LOGFILE_FROM_STR "From:"#define LOGFILE_SND_HLIST_STR "SEND HOST LIST:"static int handle=-1;GStaticMutex logfile_mutex = G_STATIC_MUTEX_INIT;static intopen_log_file(const char *filepath,int *new_fd) { int fd; if ( (!filepath) || (!new_fd) ) return -EINVAL; g_assert(filepath); fd=open(filepath,O_APPEND|O_CREAT|O_WRONLY,S_IRUSR|S_IWUSR); if (fd < 0) { err_out("open fail:[%s] %s(%d)\n",filepath,strerror(errno),errno); return -errno; } *new_fd=fd; return 0;}int logfile_init_logfile(void){ const gchar *filepath; int new_fd; filepath=hostinfo_refer_ipmsg_logfile(); if (!filepath) return -EINVAL; if (handle>0) return -EEXIST; open_log_file(filepath,&new_fd); dbg_out("Init log into %s(fd=%d)\n",filepath,new_fd); handle=new_fd; return 0;}intlogfile_reopen_logfile(const char *filepath){ int new_fd; int rc; if (!filepath) return -EINVAL; rc=open_log_file(filepath,&new_fd); if (rc<0) return rc; g_static_mutex_lock(&logfile_mutex); if (handle>0) { rc=0; if (close(handle)<0) rc=-errno; if (rc<0) { close(new_fd); g_static_mutex_unlock(&logfile_mutex); return rc; } } dbg_out("Change log into %s(fd=%d)\n",filepath,new_fd); handle=new_fd; g_static_mutex_unlock(&logfile_mutex); return 0;}static int write_one_line(const char *string) { int rc; size_t len; if (!string) return -EINVAL; if (handle<0) return -ENOENT; g_static_mutex_lock(&logfile_mutex); len=strlen(string); rc=write(handle,string,len); if (rc<0) { dbg_out("write fail:%s(%d)\n",strerror(errno),errno); goto unlock_out; } if (len != rc) { dbg_out("Partial write: len=%d rc=%d\n",len,rc); goto unlock_out; } len=strlen(LOGFILE_NEW_LINE); rc=write(handle,LOGFILE_NEW_LINE,len); if (rc<0) { dbg_out("write fail:%s(%d)\n",strerror(errno),errno); goto unlock_out; } if (len != rc) { dbg_out("Partial write: len=%d rc=%d\n",len,rc); goto unlock_out; } unlock_out: g_static_mutex_unlock(&logfile_mutex); return 0;}int logfile_write_log(const char *direction,const char *ipaddr,const char *message){ char buffer[LOGFILE_MAX_LINE_LEN]; char logname[64]; char loged_ipaddr[64]; userdb_t *user_info=NULL; int rc; struct timeval tv; g_assert(direction); if ( (!ipaddr) || (!message) ) return -EINVAL; dbg_out("logging: direction: %s addr : %s message:%s\n", direction, ipaddr, message); rc=userdb_search_user_by_addr(ipaddr,(const userdb_t **)&user_info); if (rc) return rc; if (handle<0) return -ENOENT; logname[0]=loged_ipaddr[0]='\0'; if (hostinfo_refer_ipmsg_logname_logging()) snprintf(logname,63,"[%s]",user_info->user); if (hostinfo_refer_ipmsg_ipaddr_logging()) snprintf(loged_ipaddr,63,"/%s",ipaddr); logname[64-1]=loged_ipaddr[64-1]='\0'; write_one_line(LOGFILE_START_HEADER); snprintf(buffer,LOGFILE_MAX_LINE_LEN-1,LOGFILE_FMT, direction, user_info->nickname, logname, user_info->group, user_info->host, loged_ipaddr); write_one_line(buffer); gettimeofday(&tv,NULL); ctime_r(&(tv.tv_sec),buffer); write_one_line(buffer); write_one_line(LOGFILE_END_HEADER); write_one_line(message); write_one_line(""); destroy_user_info(user_info); return 0;}int logfile_send_log(const char *ipaddr,const char *message){ dbg_out("send log: addr : %s message:%s\n", ipaddr, message); if (!hostinfo_refer_ipmsg_enable_log()) return -ENOENT; return logfile_write_log(LOGFILE_TO_STR,ipaddr,message);}int logfile_recv_log(const char *ipaddr,const char *message){ dbg_out("recv log: addr : %s message:%s\n", ipaddr, message); if (!hostinfo_refer_ipmsg_enable_log()) return -ENOENT; return logfile_write_log(LOGFILE_FROM_STR,ipaddr,message);}int logfile_shutdown_logfile(void){ if (handle>0) close(handle); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -