void consolelogger_log_with_GetErrorNo()

in src/consolelogger.c [133:239]


void consolelogger_log_with_GetErrorNo(const char* file, const char* func, int line, const char* format, ...)
{
    int error_no;

    char message[LOG_SIZE_REGULAR];

    int size = 0; /*size tracks number of character from "message" that are used so far, not counting the last null character. Uses int as data type because snprintf functions return int*/

    int snprintf_result;

    /*this function builds a string in message variable from 3 sources
    1) the system state (time, file, function, line)
    2) the user (format,...)
    3) whatever GetLastError can provide
    */
    va_list args;
    va_start(args, format);

    error_no = errno;

    message[0] = '\0';

    {/*scope for 1) the system state (time, file, function, line)*/
        time_t t;
        t = time(NULL);
        snprintf_result = snprintf(message + size, sizeof(message) - size, "Error: Time:%.24s File:%s Func:%s Line:%d ", ctime(&t), file, func, line);
        if (snprintf_result < 0)
        {
            (void)puts("error in snprintf trying to output the system message");
        }
        else
        {
            if (snprintf_result >= (int)sizeof(message) - size)
            {
                (void)puts("not enough caracters in message to hold the system message");
            }
            else
            {
                size += snprintf_result;
            }
        }
    }

    {/*scope for 2) the user (format,...)*/
        snprintf_result = vsnprintf(message + size, sizeof(message) - size, format, args);
        if (snprintf_result < 0)
        {
            (void)puts("error in vsnprintf trying to output the user message");
        }
        else
        {
            if (snprintf_result >= (int)sizeof(message) - size)
            {
                (void)puts("not enough characters in message to hold the user message");
            }
            else
            {
                size += snprintf_result;
            }
        }
    }

    {/*scope for 3) whatever GetLastError can provide*/

        /*add the getlastError for good measure anyway*/
        snprintf_result = snprintf(message + size, sizeof(message) - size, " ErrorNo()=%d ", error_no);
        if (snprintf_result < 0)
        {
            (void)puts("error in snprintf trying to output GetLastError's value");
        }
        else
        {
            if (snprintf_result >= (int)sizeof(message) - size)
            {
                (void)puts("not enough characters in message to hold  GetLastError's value");
            }
            else
            {
                size += snprintf_result;
                if (strerror_r(errno, message + size, sizeof(message) - size) != 0)
                {
                    (void)puts("error in snprintf trying to output strerror_r's value as string");
                }
                else
                {
                    /*remove extraneous newlines from FormatMessageA's output*/
                    char* whereAreThey;
                    if ((whereAreThey = strchr(message + size, '\r')) != NULL)
                    {
                        *whereAreThey = '\0';
                    }
                    if ((whereAreThey = strchr(message + size, '\n')) != NULL)
                    {
                        *whereAreThey = '\0';
                    }

                    /*everything has been compiled in message...*/
                }
            }
        }
    }

    /*in any case, print the string as is*/
    (void)puts(message);

    va_end(args);
}