<commit_before>#include "c4/log.hpp"
#include "c4/memory_resource.hpp"
#include "c4/compiler.hpp"
#include "c4/time.hpp"

#include <assert.h>
#include <string.h>

#if defined(_WIN32) || defined(_WIN64)
#   include "c4/windows.hpp"
#endif

C4_BEGIN_NAMESPACE(c4)

// force early construction of c4::log to prevent other translation
// units from logging before this object is initialized.
Log& _fn_log()
{
    static Log log;
    return log;
}
Log& log = _fn_log();
thread_local LogBuffer s_log_buffer;

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
Log::Proxy Log::operator() (Channel* ch, Level_e lev)
{
    assert(channel_exists(ch));
    return Proxy(*ch, lev);
}
Log::Proxy Log::operator() (Level_e lev)
{
    return Proxy(*main_channel(), INFO);
}

LogBuffer& Log::buf()
{
    return s_log_buffer;
}

/** set the level of all channels */
void Log::level(Level_e l)
{
    for(uint32_t h = 0; h < m_num_channels; ++h)
    {
        m_channels[h].level = l;
    }
}

Log::Channel* Log::channel(const char *name) const
{
    for(uint32_t h = 0; h < m_num_channels; ++h)
    {
        if(m_channels[h].cmp(name))
        {
            return const_cast< Channel* >(&(m_channels[0]) + h);
        }
    }
    C4_LOGF_ERR("could not find log channel named %s\n", name);
    return nullptr;
}

bool Log::channel_exists(const char *name) const
{
    for(uint32_t h = 0; h < m_num_channels; ++h)
    {
        if(m_channels[h].cmp(name))
        {
            return true;
        }
    }
    return false;
}

Log::Channel* Log::add_channel(const char *name, Level_e lev)
{
    C4_CHECK(m_num_channels < C4_LOG_MAX_CHANNELS);
    m_channels[m_num_channels].init(name, lev);
    Channel* ret = &(m_channels[0]) + m_num_channels;
    ++m_num_channels;
    return ret;
}

void Log::_print_prefix(Channel const& ch, LogBuffer &buf)
{
    uint8_t md = mode();
    if((md & SHOW_TIMESTAMP) && (ch.name_len > 0))
    {
        buf.printf("%lfms[%s]: ", exetime()/1.e3, ch.name);
    }
    else if((md & SHOW_TIMESTAMP))
    {
        buf.printf("%lfms: ", exetime()/1.e3, ch.name);
    }
    else if((ch.name_len > 0))
    {
        buf.printf("[%s]: ", ch.name);
    }
}

/** print formatted output to the main channel, at INFO level */
void Log::printf(const char *fmt, ...)
{
    Channel &ch = m_channels[0];
    if(ch.skip(INFO)) return;
    va_list args;
    va_start(args, fmt);
    auto& b = buf();
    _print_prefix(ch, b);
    b.vprintf(fmt, args);
    pump(b.rd(), b.pos);
    b.clear();
}
/** print formatted output to the main channel, at the given level */
void Log::printfl(Level_e level, const char *fmt, ...)
{
    Channel &ch = m_channels[0];
    if(ch.skip(level)) return;<commit_msg>log time in milliseconds<commit_after>#include "c4/log.hpp"
#include "c4/memory_resource.hpp"
#include "c4/compiler.hpp"
#include "c4/time.hpp"

#include <assert.h>
#include <string.h>

#if defined(_WIN32) || defined(_WIN64)
#   include "c4/windows.hpp"
#endif

C4_BEGIN_NAMESPACE(c4)

// force early construction of c4::log to prevent other translation
// units from logging before this object is initialized.
Log& _fn_log()
{
    static Log log;
    return log;
}
Log& log = _fn_log();
thread_local LogBuffer s_log_buffer;

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
Log::Proxy Log::operator() (Channel* ch, Level_e lev)
{
    assert(channel_exists(ch));
    return Proxy(*ch, lev);
}
Log::Proxy Log::operator() (Level_e lev)
{
    return Proxy(*main_channel(), INFO);
}

LogBuffer& Log::buf()
{
    return s_log_buffer;
}

/** set the level of all channels */
void Log::level(Level_e l)
{
    for(uint32_t h = 0; h < m_num_channels; ++h)
    {
        m_channels[h].level = l;
    }
}

Log::Channel* Log::channel(const char *name) const
{
    for(uint32_t h = 0; h < m_num_channels; ++h)
    {
        if(m_channels[h].cmp(name))
        {
            return const_cast< Channel* >(&(m_channels[0]) + h);
        }
    }
    C4_LOGF_ERR("could not find log channel named %s\n", name);
    return nullptr;
}

bool Log::channel_exists(const char *name) const
{
    for(uint32_t h = 0; h < m_num_channels; ++h)
    {
        if(m_channels[h].cmp(name))
        {
            return true;
        }
    }
    return false;
}

Log::Channel* Log::add_channel(const char *name, Level_e lev)
{
    C4_CHECK(m_num_channels < C4_LOG_MAX_CHANNELS);
    m_channels[m_num_channels].init(name, lev);
    Channel* ret = &(m_channels[0]) + m_num_channels;
    ++m_num_channels;
    return ret;
}

void Log::_print_prefix(Channel const& ch, LogBuffer &buf)
{
    uint8_t md = mode();
    if((md & SHOW_TIMESTAMP) && (ch.name_len > 0))
    {
        buf.printf("%lfms[%s]: ", Time::exetime().ms(), ch.name);
    }
    else if((md & SHOW_TIMESTAMP))
    {
        buf.printf("%lfms: ", Time::exetime().ms(), ch.name);
    }
    else if((ch.name_len > 0))
    {
        buf.printf("[%s]: ", ch.name);
    }
}

/** print formatted output to the main channel, at INFO level */
void Log::printf(const char *fmt, ...)
{
    Channel &ch = m_channels[0];
    if(ch.skip(INFO)) return;
    va_list args;
    va_start(args, fmt);
    auto& b = buf();
    _print_prefix(ch, b);
    b.vprintf(fmt, args);
    pump(b.rd(), b.pos);
    b.clear();
}
/** print formatted output to the main channel, at the given level */
void Log::printfl(Level_e level, const char *fmt, ...)
{
    Channel &ch = m_channels[0];
    if(ch.skip(level)) return;
{'fraction_non_alphanumeric': 0.22966672422724918, 'fraction_numerical': 0.011396995337592817, 'mean_word_length': 2.2612612612612613, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5166457', 'n_tokens_mistral': 2023, 'n_tokens_neox': 1996, 'n_words': 460}