bool Log_Writer::loginit(LogLevel l, const char *filelocation, bool append, bool issync) { MACRO_RET(NULL != fp, false); m_system_level = l; m_isappend = append; m_issync = issync; if(strlen(filelocation) >= (sizeof(m_filelocation) -1)) { fprintf(stderr, "the path of log file is too long:%d limit:%d\n", strlen(filelocation), sizeof(m_filelocation) -1); exit(0); } //本地存储filelocation 以防止在栈上的非法调用调用 strncpy(m_filelocation, filelocation, sizeof(m_filelocation)); m_filelocation[sizeof(m_filelocation) -1] = '\0'; if('\0' == m_filelocation[0]) { fp = stdout; fprintf(stderr, "now all the running-information are going to put to stderr\n"); return true; } fp = fopen(m_filelocation, append ? "a":"w"); if(fp == NULL) { fprintf(stderr, "cannot open log file,file location is %s\n", m_filelocation); exit(0); } //setvbuf (fp, io_cached_buf, _IOLBF, sizeof(io_cached_buf)); //buf set _IONBF _IOLBF _IOFBF setvbuf (fp, (char *)NULL, _IOLBF, 0); fprintf(stderr, "now all the running-information are going to the file %s\n", m_filelocation); return true; }
bool LogWriter::log(LogLevel l, const char* logformat,...) { MACRO_RET(!checklevel(l), false); char * start = m_buffer; int size = premakestr(start, l); va_list args; va_start(args, logformat); size += vsnprintf(start+size, M_LOG_BUFFSIZE-size, logformat, args); va_end(args); if(m_verbose) fprintf(stderr, "%s", m_buffer); if(fp) write(m_buffer, size); return true; }
bool Log_Writer::log(LogLevel l, char* logformat,...) { MACRO_RET(!checklevel(l), false); int _size; int prestrlen = 0; char * star = m_buffer; prestrlen = premakestr(star, l); star += prestrlen; va_list args; va_start(args, logformat); _size = vsnprintf(star, _LOG_BUFFSIZE - prestrlen, logformat, args); va_end(args); if(NULL == fp) fprintf(stderr, "%s", m_buffer); else _write(m_buffer, prestrlen + _size); return true; }