Example #1
0
tb_void_t tb_trace_tail(tb_char_t const* format, ...)
{
    // check
    tb_check_return(format);

    // enter
    tb_spinlock_enter_without_profiler(&g_lock);

    // done
    do
    {
        // check
        tb_check_break(g_mode);

        // init
        tb_va_list_t    l;
        tb_char_t*      p = g_line;
        tb_char_t*      e = g_line + sizeof(g_line);
        tb_va_start(l, format);

        // append format
        if (p < e) p += tb_vsnprintf(p, e - p, format, l);

        // append end
        if (p < e) *p = '\0';
        e[-1] = '\0';

        // print it
        if (g_mode & TB_TRACE_MODE_PRINT) tb_print(g_line);

        // print it to file
#ifndef TB_CONFIG_MICRO_ENABLE
        if ((g_mode & TB_TRACE_MODE_FILE) && g_file) 
        {
            // done
            tb_size_t size = p - g_line;
            tb_size_t writ = 0;
            while (writ < size)
            {
                // writ it
                tb_long_t real = tb_file_writ(g_file, (tb_byte_t const*)g_line + writ, size - writ);
                tb_check_break(real > 0);

                // save size
                writ += real;
            }
        }
#endif

        // exit
        tb_va_end(l);

    } while (0);

    // leave
    tb_spinlock_leave(&g_lock);
}
Example #2
0
File: file.c Project: ljx0305/tbox
tb_hong_t tb_file_writf(tb_file_ref_t file, tb_file_ref_t ifile, tb_hize_t offset, tb_hize_t size)
{
    // check
    tb_assert_and_check_return_val(file && ifile && size, -1);

#ifdef TB_CONFIG_POSIX_HAVE_SENDFILE

    // writ it
    off_t       seek = offset;
    tb_hong_t   real = sendfile(tb_file2fd(file), tb_file2fd(ifile), &seek, (size_t)size);

    // ok?
    if (real >= 0) return real;

    // continue?
    if (errno == EINTR || errno == EAGAIN) return 0;

    // error
    return -1;

#else

    // read data
    tb_byte_t data[8192];
    tb_long_t read = tb_file_pread(ifile, data, sizeof(data), offset);
    tb_check_return_val(read > 0, read);

    // writ data
    tb_size_t writ = 0;
    while (writ < read)
    {
        tb_long_t real = tb_file_writ(file, data + writ, read - writ);
        if (real > 0) writ += real;
        else break;
    }

    // ok?
    return writ == read? writ : -1;
#endif
}
Example #3
0
tb_void_t tb_trace_done_with_args(tb_char_t const* prefix, tb_char_t const* module, tb_char_t const* format, tb_va_list_t args)
{
    // check
    tb_check_return(format);

    // enter
    tb_spinlock_enter_without_profiler(&g_lock);

    // done
    do
    {
        // check
        tb_check_break(g_mode);

        // init
        tb_char_t*      p = g_line;
        tb_char_t*      e = g_line + sizeof(g_line);

        // print prefix to file
        if ((g_mode & TB_TRACE_MODE_FILE) && g_file) 
        {
            // print time to file
            tb_tm_t lt = {0};
            if (p < e && tb_localtime(tb_time(), &lt))
                p += tb_snprintf(p, e - p, "[%04ld-%02ld-%02ld %02ld:%02ld:%02ld]: ", lt.year, lt.month, lt.mday, lt.hour, lt.minute, lt.second);

            // print self to file
            if (p < e) p += tb_snprintf(p, e - p, "[%lx]: ", tb_thread_self());
        }

        // append prefix
        tb_char_t*      b = p;
        if (prefix && p < e) p += tb_snprintf(p, e - p, "[%s]: ", prefix);

        // append module
        if (module && p < e) p += tb_snprintf(p, e - p, "[%s]: ", module);

        // append format
        if (p < e) p += tb_vsnprintf(p, e - p, format, args);

        // append end
        if (p < e) *p = '\0'; e[-1] = '\0';

        // print it
        if (g_mode & TB_TRACE_MODE_PRINT) tb_print(b);

        // print it to file
        if ((g_mode & TB_TRACE_MODE_FILE) && g_file) 
        {
            // done
            tb_size_t size = p - g_line;
            tb_size_t writ = 0;
            while (writ < size)
            {
                // writ it
                tb_long_t real = tb_file_writ(g_file, (tb_byte_t const*)g_line + writ, size - writ);
                tb_check_break(real > 0);

                // save size
                writ += real;
            }
        }

    } while (0);

    // leave
    tb_spinlock_leave(&g_lock);
}