Click here to Skip to main content
15,891,842 members
Articles / Programming Languages / C++

Adding Logging to C Programs with the Pantheios C API

Rate me:
Please Sign up or sign in to vote.
4.89/5 (14 votes)
23 Jun 2008BSD5 min read 44K   292   25  
A tutorial on using the Pantheios logging API library from C compilation units, and a comparison of the features offered by the C and C++ APIs
#include <pantheios/pantheios.h>
#include <pantheios/internal/safestr.h>

#include <stdio.h>
#include <stdlib.h>

#include <assert.h>
#include <winsock2.h>

extern const char PANTHEIOS_FE_PROCESS_IDENTITY[] = "pantheios-C";

int connect_to_peer_1(struct in_addr const* addr)
{
    pantheios_logprintf(PANTHEIOS_SEV_DEBUG
                      , "connect_to_peer(%u.%u.%u.%u)"
                      , (NULL == addr) ? 0 : ((addr->s_addr & 0x000000ff) >> 0)
                      , (NULL == addr) ? 0 : ((addr->s_addr & 0x0000ff00) >> 8)
                      , (NULL == addr) ? 0 : ((addr->s_addr & 0x00ff0000) >> 16)
                      , (NULL == addr) ? 0 : ((addr->s_addr & 0xff000000) >> 24));

	return 0;
}

char const* convert_addr(char* buff, size_t cchBuff, struct in_addr const* addr)
{
	assert(cchBuff >= 16);

#ifdef PANTHEIOS_USING_SAFE_STR_FUNCTIONS
	sprintf_s(buff, cchBuff
#else /* ? PANTHEIOS_USING_SAFE_STR_FUNCTIONS */
	sprintf(buff
#endif /* PANTHEIOS_USING_SAFE_STR_FUNCTIONS */
		  , "%u.%u.%u.%u"
		  , (NULL == addr) ? 0 : ((addr->s_addr & 0x000000ff) >> 0)
		  , (NULL == addr) ? 0 : ((addr->s_addr & 0x0000ff00) >> 8)
		  , (NULL == addr) ? 0 : ((addr->s_addr & 0x00ff0000) >> 16)
		  , (NULL == addr) ? 0 : ((addr->s_addr & 0xff000000) >> 24));

	return buff;
}

int connect_to_peer_2(struct in_addr const* addr)
{
	char buff[16];
    pantheios_logprintf(PANTHEIOS_SEV_DEBUG
                      , "connect_to_peer(%s)"
                      , convert_addr(&buff[0], STLSOFT_NUM_ELEMENTS(buff), addr));

	return 0;
}

int main()
{
    int panres = pantheios_init();
    if(panres < 0)
    {
        fprintf(stderr, "Failed to initialise the Pantheios libraries: %s\n", pantheios_getInitErrorString(panres));
        return EXIT_FAILURE;
    }
    else
    {
		pantheios_logprintf(PANTHEIOS_SEV_DEBUG, "main()");

        /* . . .  rest of program */

		{
			int    i = 10;
			double d = 9.9;

			pantheios_logprintf(PANTHEIOS_SEV_NOTICE, "i=%d, d=%G", i, d);
		}

		{
			struct in_addr addr;

			memset(&addr, 1, sizeof(addr));

			connect_to_peer_1(&addr);
		}

		{
			struct in_addr addr;

			memset(&addr, 2, sizeof(addr));

			connect_to_peer_2(&addr);
		}


        pantheios_uninit();
        return EXIT_SUCCESS;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Instructor / Trainer
Australia Australia
Software Development consultant, specialising in project remediation.

Creator of the FastFormat, Pantheios, STLSoft and VOLE open-source libraries.

Author of the books Extended STL, volume 1 (Addison-Wesley, 2007) and Imperfect C++ (Addison-Wesley, 2004).

Comments and Discussions