Example #1
0
const char* format_log_message(const char* format,...)
{
    va_list va;
    char* buf=get_format_log_buffer();
    if(!buf)
        return "format_log_message: Unable to allocate memory buffer";
    
    va_start(va,format);
    vsnprintf(buf, FORMAT_LOG_BUF_SIZE-1,format,va);
    va_end(va); 
    return buf;
}
Example #2
0
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());
    }
}