(file) Return to Syslog.c CVS log (file) (dir) Up to [OMI] / omi / oi / gen_c / common

File: [OMI] / omi / oi / gen_c / common / Syslog.c (download)
Revision: 1.1, Mon Apr 20 17:19:54 2015 UTC (9 years, 2 months ago) by krisbash
Branch: MAIN
CVS Tags: OMI_1_0_8_2, OMI_1_0_8_1, HEAD
OMI 1.0.8-1

/*
**==============================================================================
**
** Open Management Infrastructure (OMI)
**
** Copyright (c) Microsoft Corporation
** 
** Licensed under the Apache License, Version 2.0 (the "License"); you may not 
** use this file except in compliance with the License. You may obtain a copy 
** of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
** MERCHANTABLITY OR NON-INFRINGEMENT. 
**
** See the Apache 2 License for the specific language governing permissions 
** and limitations under the License.
**
**==============================================================================
*/

#include "oicommon.h"
#include "Syslog.h"
#include <pal/strings.h>
#include <stdio.h>

#define SYSLOG_CGEN_START NL \
    "#define APPNAME \"%s\"" NL \
    "#define LOGOPT %s" NL \
    "#define LOGFACILITY %s" NL \
    "#include <oi/oi_syslog.h>" NL

#define SYSLOGEVENT  "SYSLOG_EVENT%d("
#define SYSLOGEVENTD "SYSLOG_EVENTD%d("

static PAL_Boolean _AddEvents(
    _In_ FILE * out,
    _In_ OIEvent * events)
{
    char buf[BUFFER_SIZE];
    OIEvent * current = events;

    while(current)
    {
        OIEvent * next = current->next;
        OIArgument * arg = current->Argument;
        int ArgCount = CountArguments(arg);
        int wrote;

        buf[0] = 0;

#if defined(CONFIG_OS_WINDOWS)
        wrote = sprintf_s(buf, BUFFER_SIZE, UseDebugMacro(current->Priority) ? SYSLOGEVENTD : SYSLOGEVENT, ArgCount);
#else
        wrote = sprintf(buf, UseDebugMacro(current->Priority) ? SYSLOGEVENTD : SYSLOGEVENT, ArgCount );
#endif
        if (wrote >= BUFFER_SIZE)
            goto error;

        if (Strcat(buf, BUFFER_SIZE, current->EventId) == 0)
            goto error;
        if (Strcat(buf, BUFFER_SIZE, ", ") == 0)
            goto error;
        if (Strcat(buf, BUFFER_SIZE, current->Name) == 0)
            goto error;
        if (Strcat(buf, BUFFER_SIZE, ", ") == 0)
            goto error;
        if (Strcat(buf, BUFFER_SIZE, current->Priority) == 0)
            goto error;
        if (Strcat(buf, BUFFER_SIZE, ", PAL_T(") == 0)
            goto error;
        if (Strcat(buf, BUFFER_SIZE, current->Format) == 0)
            goto error;
        if (Strcat(buf, BUFFER_SIZE, ")") == 0)
            goto error;

        while(arg)
        {
            OIArgument * next = arg->next;

            if (Strcat(buf, BUFFER_SIZE, ", ") == 0)
                goto error;
            if (Strcat(buf, BUFFER_SIZE, arg->Type) == 0)
                goto error;

            arg = next;
        }
        
        if (Strcat(buf, BUFFER_SIZE, ")") == 0)
                goto error;

        /* buf may contain %d which we need to preserve */
        fprintf(out, "%s", buf);
        fprintf(out, NL);

        current = next;
    }

    return PAL_TRUE;

error:
    OIERROR1("Out of buffer space while generating! Buffer so far was [%s]", buf);
    return PAL_FALSE;
}


/************* Public Definitions ******************/

_Use_decl_annotations_
PAL_Boolean GenerateSyslog(
    OIEvent * events,
    const char * ident,
    const char * options,
    const char * facility,
    const char * target)
{
    /* overwrite existing file */
    FILE* out = fopen(target, "w");
    if (!out)
    {
        OIERROR1("Failed to open file [%s] for writing!", target);
        return PAL_FALSE;
    }

    fprintf(out, SYSLOG_CGEN_START, ident, options, facility);
    fprintf(out, NL);

    if (!_AddEvents(out, events))
    {
        fclose(out);
        OIERROR1("Failed to generate syslog implementation for file [%s]!", target);
        return PAL_FALSE;
    }

    fclose(out);

    OITRACE("Success!");
    return PAL_TRUE;
}

ViewCVS 0.9.2