📄 mesg.c
字号:
#include <stdio.h>#include <stdarg.h>#include <string.h>#include <sys/time.h>#include <fcntl.h>#include <cleanup.h>#include <firestorm.h>char *mesg_str[]={"undefined","critical","error","warning","info","debug"};#define MESG_INITBUF_LEN 8192char *init_buf;char *init_buf_p;size_t init_buflen;/* print out messages stored in the internal buffer */void mesg_flush(int code, void *u){ if ( !init_buf ) return; printf("%s", init_buf); free(init_buf); init_buf=NULL; init_buflen=0;}/* Initialise the logging code */void mesg_init(void){ init_buflen=MESG_INITBUF_LEN; if ( !(init_buf=init_buf_p=malloc(init_buflen)) ) cperror("calloc"); cleanup_add(mesg_flush, NULL);}/* Print a message to the internal text buffer *//* We know at this point how the user wants us to log * from the config file, so lets do it */void mesg_logfile(char *fn){ int fd; fflush(stdout); if ( !fn ) goto no_open; if ( (fd=open(fn, O_WRONLY|O_CREAT|O_TRUNC, 00640))<0 ) { cleanup(EXIT_ERR, "%s: open(): %s", fn, get_err()); } if ( fd!=1 ) { if ( dup2(fd, 1)<0 ) cleanup(EXIT_ERR, "%s: dup2(): %s", fn, get_err()); close(fd); }no_open: mesg_flush(0,NULL);}/* put a message in to the initial buffer */void mesg_initlog(struct timeval *tv, int code, char *buf){ int ret; if ( !init_buflen ) { printf("warning: internal message buffer overflow\n"); return; } ret=snprintf(init_buf_p, init_buflen, "%.8lu.%.6lu %s: %s\n", tv->tv_sec, tv->tv_usec, mesg_str[code], buf); init_buf_p+=ret; if ( ret > init_buflen ) { init_buflen=0; }else{ init_buflen-=ret; }}/* Print out a firestorm internal system log message */void mesg(unsigned char code, char *fmt, ...){ static char buf[1024]; struct timeval tv; va_list va; if ( code > M_MAX ) code=0; gettimeofday(&tv, NULL); va_start(va, fmt); vsnprintf(buf, sizeof(buf), fmt, va); va_end(va); if ( !init_buflen ) { /* No point checking for errors, what are we gonna do * print them to screen? ;) */ printf("%.8lu.%.6lu %s: %s\n", tv.tv_sec, tv.tv_usec, mesg_str[code], buf); fflush(stdout); return; }else{ mesg_initlog(&tv, code, buf); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -