Пример #1
0
int WindowsDebugger_Context::rawLogEntry(int /* severity4 */, int /* severityX */, const pan_slice_t (&ar)[rawLogArrayDimension], size_t cchTotal)
{
    // Allocate the buffer

    buffer_t buff(cchTotal + 1 + 1);

#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
    // When exception support is not enabled, failure to allocate will yield an empty instance
    if(buff.empty())
    {
        return 0;
    }
    else
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
    {
        size_t nWritten = concatenateSlices(&buff[0], buff.size(), rawLogArrayDimension, &ar[0]);

        PANTHEIOS_CONTRACT_ENFORCE_POSTCONDITION_RETURN_INTERNAL(nWritten == cchTotal, "Written length differs from allocated length");

        buff[nWritten++] = '\n';
        buff[nWritten] = '\0';

        PANTHEIOS_CONTRACT_ENFORCE_ASSUMPTION(nWritten + 1 == buff.size());

        // Output

        pan_OutputDebugString_(&buff[0]);

        return static_cast<int>(nWritten);
    }
}
Пример #2
0
int be_speech_context::rawLogEntry(int severity4, int /* severityX */, const pan_slice_t (&ar)[rawLogArrayDimension], size_t cchTotal)
{
    // Define auto_buffer to use Windows process heap

    // Allocate the buffer

    buffer_t buff(cchTotal + 1 + 1);

#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
    // When exception support is not enabled, failure to allocate will yield an empty instance
    if(buff.empty())
    {
        return 0;
    }
    else
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
    {
        size_t nWritten = concatenateSlices(&buff[0], buff.size(), rawLogArrayDimension, &ar[0]);

        PANTHEIOS_CONTRACT_ENFORCE_POSTCONDITION_RETURN_INTERNAL(nWritten == cchTotal, "Written length differs from allocated length");

        buff[nWritten++] = '\n';
        buff[nWritten] = '\0';

        PANTHEIOS_CONTRACT_ENFORCE_ASSUMPTION(nWritten + 1 == buff.size());

        // Output

        return this->speak(severity4, buff.data(), nWritten);
    }
}
Пример #3
0
void pointer::construct_()
{
    PANTHEIOS_CONTRACT_ENFORCE_PRECONDITION_PARAMS_API(0 == m_sz[0], "cannot construct if value is non-empty");
    PANTHEIOS_CONTRACT_ENFORCE_PRECONDITION_PARAMS_API(0 == m_len, "cannot construct if length is non-0");

    if(static_cast<void const*>(0) == m_value)
    {
        static const pan_char_t  s_null[] = PANTHEIOS_LITERAL_STRING("(null)");

        STLSOFT_STATIC_ASSERT(sizeof(m_sz) >= sizeof(s_null));

        ::memcpy(&m_sz[0], &s_null[0], sizeof(s_null));

        m_len = STLSOFT_NUM_ELEMENTS(s_null) - 1;
    }
    else
    {
        pan_char_t          szFmt[101];
        int                 width;
        pan_char_t const*   zeroX;
        pan_char_t const*   leadingMinus;
        pan_char_t const*   zeroPad;

        if(m_minWidth < 0)
        {
            width           =   -m_minWidth;
            leadingMinus    =   PANTHEIOS_LITERAL_STRING("-");
        }
        else
        {
            width           =   m_minWidth;
            leadingMinus    =   PANTHEIOS_LITERAL_STRING("");
        }

        zeroX   =   (m_format & fmt::zeroXPrefix) ? PANTHEIOS_LITERAL_STRING("0x") : PANTHEIOS_LITERAL_STRING("");
        zeroPad =   (m_format & fmt::zeroPad) ? PANTHEIOS_LITERAL_STRING("0") : PANTHEIOS_LITERAL_STRING("");

        if( 0 != width &&
            fmt::zeroXPrefix == (m_format & (fmt::zeroXPrefix | fmt::zeroPad)))
        {
            // Special case

            pan_char_t szTemp[23]; // 23 is always big enough, since the width is 21

            PANTHEIOS_CONTRACT_ENFORCE_ASSUMPTION(0 == (m_format & fmt::zeroPad));

            // Determine ptr-sized integer type
#if defined(STLSOFT_COMPILER_IS_GCC)
            typedef unsigned long                                             intptr_t_;
#else /* ? compiler */
            typedef stlsoft::int_size_traits<sizeof(void*)>::unsigned_type    intptr_t_;
#endif /* compiler */

            int r = pantheios_util_snprintf(
                        &szTemp[0]
                    ,   STLSOFT_NUM_ELEMENTS(szTemp)
                    ,   PANTHEIOS_LITERAL_STRING("0x%lx")
                    ,   static_cast<intptr_t_>(stlsoft::union_cast<intptr_t_>(m_value))
                    );

            if(r < 0)
            {
                return;
            }
            else
            {
                size_t n = static_cast<size_t>(r);

                PANTHEIOS_CONTRACT_ENFORCE_POSTCONDITION_RETURN_INTERNAL((n + 2) <= STLSOFT_NUM_ELEMENTS(szTemp), "snprintf() overwrote the local buffer capacity");

                width += 2;

                if(static_cast<size_t>(width) > n)
                {
                    if(m_minWidth < 0)
                    {
                        PANTHEIOS_char_copy(&m_sz[0], szTemp, n);
                        std::fill_n(&m_sz[0] + n, size_t(width - n), ' ');
                        m_sz[width] = '\0';
                    }
                    else
                    {
                        std::fill_n(&m_sz[0], size_t(width - n), ' ');
                        PANTHEIOS_char_copy(&m_sz[0] + (size_t(width) - n), szTemp, (n + 1));
                    }

                    m_len = static_cast<size_t>(width);
                }
                else
                {
                    PANTHEIOS_char_copy(&m_sz[0], szTemp, (n + 1));

                    m_len = n;
                }
            }
        }
        else
        {
            pantheios_util_snprintf(&szFmt[0]
                                ,   STLSOFT_NUM_ELEMENTS(szFmt)
                                ,   PANTHEIOS_LITERAL_STRING("%s%%%s%s%dx")
                                ,   zeroX
                                ,   leadingMinus
                                ,   zeroPad
                                ,   width);

            m_len = static_cast<size_t>(pantheios_util_snprintf(&m_sz[0], STLSOFT_NUM_ELEMENTS(m_sz), szFmt, m_value));
        }
    }

    PANTHEIOS_CONTRACT_ENFORCE_PRECONDITION_PARAMS_API(0 != m_sz[0], "failed to set value to non-empty");
    PANTHEIOS_CONTRACT_ENFORCE_PRECONDITION_PARAMS_API(m_len > 0, "failed to set length");
    PANTHEIOS_CONTRACT_ENFORCE_PRECONDITION_PARAMS_API(m_len < 23, "pantheios::pointer string too long");
}
Пример #4
0
static size_t pantheios_getHostName_body_(pan_char_t* buffer, size_t cchBuffer)
{
#if defined(PLATFORMSTL_OS_IS_UNIX)

    if(0 == cchBuffer)
    {
        errno = ENAMETOOLONG;

        return 0;
    }
    else
    {
        int res;

        PANTHEIOS_CONTRACT_ENFORCE_ASSUMPTION(NULL != buffer);

        /* First, we mark the last available character in the buffer with
         * the nul terminator, to test later
         */
        buffer[cchBuffer - 1] = '\0';

        errno = 0;

        res = gethostname(&buffer[0], cchBuffer);

        /* Test for ENAMETOOLONG, to avoid any implementation-dependent
         * behaviour wrt whether this it is set on a 0 or a -1 return
         */
        if(ENAMETOOLONG == errno)
        {
            /* To homogenise platform behaviour, we ensure that no fragment is filled out */
            buffer[0] = '\0';

            return cchBuffer;
        }
        else
        {
            if(0 != res)
            {
                /* Was a failure, so return 0 */
                return 0;
            }
            else if('\0' != buffer[cchBuffer - 1])
            {
                /* Was insufficient buffer, so we return the given size (which is the
                 * failure indicator for that condition
                 *
                 * Also, to homogenise platform behaviour, we ensure that no fragment
                 * of the buffer is returned as filled out.
                 */
                buffer[0] = '\0';

                return cchBuffer;
            }
            else
            {
                /* Buffer was sufficient, and the value has been written. The only
                 * way to return the length is to do strlen on it
                 */

                return pan_strlen_(buffer);
            }
        }
    }

#elif defined(PLATFORMSTL_OS_IS_WINDOWS)

    DWORD cchSize = stlsoft_static_cast(DWORD, cchBuffer);

    if(!pan_GetComputerName_(&buffer[0], &cchSize))
    {
        DWORD   err = GetLastError();

        if(ERROR_BUFFER_OVERFLOW == err)
        {
            return cchBuffer;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return cchSize;
    }

#else /* ? PLATFORMSTL_OS_IS_???? */
# error Platform not discriminated
#endif /* PLATFORMSTL_OS_IS_???? */
}
Пример #5
0
static int pantheios_be_WindowsSyslog_logEntry_a_(
    void*       feToken
,   void*       beToken
,   int         severity
,   char const* entry
,   size_t      cchEntry
)
{
    STLSOFT_SUPPRESS_UNUSED(feToken);

    PANTHEIOS_CONTRACT_ENFORCE_PRECONDITION_PARAMS_API(NULL != beToken, "back-end token must be non-null");

    static const char   tm_fmt[]                                =   "%b %d %I:%M:%S";
# ifdef PANTHEIOS_USING_SAFE_STR_FUNCTIONS
    static errno_t      (*fns[2])(struct tm*, const time_t*)    =   { ::localtime_s, ::gmtime_s };
# else /* ? PANTHEIOS_USING_SAFE_STR_FUNCTIONS */
    static struct tm*   (*fns[2])(const time_t*)                =   { ::localtime, ::gmtime };
# endif /* PANTHEIOS_USING_SAFE_STR_FUNCTIONS */

    WindowsSysLog_Context* ctxt       =   static_cast<WindowsSysLog_Context*>(beToken);
    char                szPri_[6];
    char*               szPri;
    char                szTime[31];
    const int           priority    =   (ctxt->facility * 8) + (severity % 8);  /* This stock back-end ignores any custom severity information. */
    time_t              t           =   time(NULL);
# ifdef PANTHEIOS_USING_SAFE_STR_FUNCTIONS
    struct tm           tm_;
    struct tm*          tm          =   &tm_;
    errno_t             err         =   fns[0 != (ctxt->flags & PANTHEIOS_BE_WINDOWSSYSLOG_F_USE_SYSTEM_TIME)](tm, &t);

    if(0 != err)
    {
        char    msg[1001];

        if(0 != ::strerror_s(&msg[0], STLSOFT_NUM_ELEMENTS(msg), err))
        {
            msg[0] = '\0';
        }

        pantheios_onBailOut3(pantheios::critical, "failed to elicit time", msg);

        return 0;
    }
# else /* ? PANTHEIOS_USING_SAFE_STR_FUNCTIONS */
    struct tm*          tm          =   fns[0 != (ctxt->flags & PANTHEIOS_BE_WINDOWSSYSLOG_F_USE_SYSTEM_TIME)](&t);
# endif /* PANTHEIOS_USING_SAFE_STR_FUNCTIONS */
    size_t              cchPriority;
    size_t              cchTime;
    size_t              cchTotal;

    PANTHEIOS_CONTRACT_ENFORCE_ASSUMPTION(priority < 1000);

    szPri       =   pri_print_(&szPri_[0], STLSOFT_NUM_ELEMENTS(szPri_), priority, cchPriority);
    cchTime     =   ::strftime(&szTime[0], STLSOFT_NUM_ELEMENTS(szTime), tm_fmt, tm);

    cchTotal = cchPriority + cchTime + 1 + ctxt->cchHostIdentity + 1 + ctxt->cchProcessIdentity + 1 + cchEntry;

    buffer_a_t buffer(1 + cchTotal);

#ifndef STLSOFT_CF_THROW_BAD_ALLOC
    if(0 == buffer.size())
    {
        return PANTHEIOS_INIT_RC_OUT_OF_MEMORY;
    }
    else
#endif /* !STLSOFT_CF_THROW_BAD_ALLOC */
    {
        // === "%s%s %s %s %s", szPri, szTime, ctxt->hostIdentity, ctxt->processIdentity, entry

        size_t  cchWritten  =   0;
        char*   p           =   &buffer[0];

        ::memcpy(p, szPri, cchPriority * sizeof(char));
        cchWritten += cchPriority;
        p += cchPriority;

        ::memcpy(p, szTime, cchTime * sizeof(char));
        cchWritten += cchTime;
        p += cchTime;

        *p++ = ' ';
        ++cchWritten;

        ::memcpy(p, ctxt->hostIdentity, ctxt->cchHostIdentity * sizeof(char));
        cchWritten += ctxt->cchHostIdentity;
        p += ctxt->cchHostIdentity;

        *p++ = ' ';
        ++cchWritten;

        ::memcpy(p, ctxt->processIdentity, ctxt->cchProcessIdentity * sizeof(char));
        cchWritten += ctxt->cchProcessIdentity;
        p += ctxt->cchProcessIdentity;

        *p++ = ' ';
        ++cchWritten;

        ::memcpy(p, entry, cchEntry * sizeof(char));
        cchWritten += cchEntry;
        p += cchEntry;

        PANTHEIOS_CONTRACT_ENFORCE_ASSUMPTION(cchWritten == size_t(p - buffer.data()));
        PANTHEIOS_CONTRACT_ENFORCE_ASSUMPTION(cchWritten == cchTotal);

        STLSOFT_SUPPRESS_UNUSED(p);

        return ::send(ctxt->sk, buffer.data(), (int)cchWritten, 0);
    }
}