void log_message(ZooLogLevel curLevel,int line,const char* funcName, const char* message) { static const char* dbgLevelStr[]={"ZOO_INVALID","ZOO_ERROR","ZOO_WARN", "ZOO_INFO","ZOO_DEBUG"}; static pid_t pid=0; #ifdef WIN32 char timebuf [TIME_NOW_BUF_SIZE]; #endif if(pid==0)pid=getpid(); #ifndef THREADED fprintf(LOGSTREAM, "%s:%d:%s@%s@%d: %s\n", time_now(get_time_buffer()),pid, dbgLevelStr[curLevel],funcName,line,message); #else #ifdef WIN32 fprintf(LOGSTREAM, "%s:%d(0x%lx):%s@%s@%d: %s\n", time_now(timebuf),pid, (unsigned long int)(pthread_self().thread_id), dbgLevelStr[curLevel],funcName,line,message); #else fprintf(LOGSTREAM, "%s:%d(0x%lx):%s@%s@%d: %s\n", time_now(get_time_buffer()),pid, (unsigned long int)pthread_self(), dbgLevelStr[curLevel],funcName,line,message); #endif #endif fflush(LOGSTREAM); }
static const char* time_now(){ struct timeval tv; struct tm lt; time_t now = 0; size_t len = 0; char* now_str=get_time_buffer(); if(!now_str) return "time_now(): Failed to allocate memory buffer"; gettimeofday(&tv,0); now = tv.tv_sec; localtime_r(&now, <); // clone the format used by log4j ISO8601DateFormat // specifically: "yyyy-MM-dd HH:mm:ss,SSS" len = strftime(now_str, TIME_NOW_BUF_SIZE, "%F %H:%M:%S", <); len += snprintf(now_str + len, TIME_NOW_BUF_SIZE - len, ",%03d", (int)(tv.tv_usec/1000)); return now_str; }
void log_message(log_callback_fn callback, ZooLogLevel curLevel, int line, const char* funcName, const char* format, ...) { static const char* dbgLevelStr[]={"ZOO_INVALID","ZOO_ERROR","ZOO_WARN", "ZOO_INFO","ZOO_DEBUG"}; static pid_t pid=0; va_list va; int ofs = 0; #ifdef THREADED unsigned long int tid = 0; #endif #ifdef WIN32 char timebuf [TIME_NOW_BUF_SIZE]; const char* time = time_now(timebuf); #else const char* time = time_now(get_time_buffer()); #endif char* buf = get_format_log_buffer(); if(!buf) { fprintf(stderr, "log_message: Unable to allocate memory buffer"); return; } if(pid==0) { pid=getpid(); } #ifndef THREADED ofs = snprintf(buf, FORMAT_LOG_BUF_SIZE, "%s:%d:%s@%s@%d: ", time, pid, dbgLevelStr[curLevel], funcName, line); #else #ifdef WIN32 tid = (unsigned long int)(pthread_self().thread_id); #else tid = (unsigned long int)(pthread_self()); #endif ofs = snprintf(buf, FORMAT_LOG_BUF_SIZE-1, "%s:%d(0x%lx):%s@%s@%d: ", time, pid, tid, dbgLevelStr[curLevel], funcName, line); #endif // Now grab the actual message out of the variadic arg list va_start(va, format); vsnprintf(buf+ofs, FORMAT_LOG_BUF_SIZE-1-ofs, format, va); va_end(va); if (callback) { callback(buf); } else { fprintf(zoo_get_log_stream(), "%s\n", buf); fflush(zoo_get_log_stream()); } }