1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
/*
* This is one of log4c example programs.
*
* Notice how no relationships between the category and a certain
* priority, appender, or formatter are coded here. These are all left
* to the log4crc config file so they can be chaned without recompiling
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifndef _WIN32
#include <sys/time.h>
#else
#include <time.h>
#include <windows.h>
#include <winsock.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "log4c.h"
#ifdef _WIN32
#if !defined(__MINGW32__) && !defined(__MINGW64__)
int gettimeofday(struct timeval* tp, void* tzp) {
DWORD t;
t = timeGetTime();
tp->tv_sec = t / 1000;
tp->tv_usec = t % 1000;
/* 0 indicates that the call succeeded. */
return 0;
}
#endif
#if !defined(HAVE_SLEEP) || !HAVE_DECL_SLEEP
#define sleep(x) Sleep(x*1000)
#endif
#endif /* _WIN32 */
int main(int argc, char** argv)
{
struct timeval start_time;
struct timeval now_time;
int looptime = 0;
log4c_category_t* mycat = NULL;
int i = 0;
if (argc < 2)
{
printf("usage: %s loop_time_in_seconds\n",argv[0]);
exit (1);
}
if (sscanf(argv[1],"%d",&looptime) != 1)
{
printf("could not convert %s to number of seconds to loop\n",argv[1]);
exit(1);
}
/* You could put your category class into a file with a wrapper macro
* to make calling it easier and more consistent
*/
log4c_init();
mycat = log4c_category_get("six13log.log.app.application1");
gettimeofday(&start_time, NULL);
gettimeofday(&now_time, NULL);
i = 0;
while ( (now_time.tv_sec - start_time.tv_sec) < looptime)
{
log4c_category_log(mycat, LOG4C_PRIORITY_DEBUG, "Debugging app 1 - loop %d", i);
log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
"some error from app1 at line %d in file %s - loop %d",
__LINE__, __FILE__, i);
sleep(3);
gettimeofday(&now_time, NULL);
i++;
}
/* Explicitly call the log4c cleanup routine */
if ( log4c_fini()){
printf("log4c_fini() failed");
}
return 0;
}
|