File: userloc_formatter.c

package info (click to toggle)
log4c 1.2.4-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,872 kB
  • sloc: sh: 11,246; ansic: 8,645; makefile: 682; lex: 134; cpp: 6
file content (86 lines) | stat: -rw-r--r-- 2,316 bytes parent folder | download | duplicates (3)
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
/******************************************************************************
 *
 * Part of the log4c examples.
 *
 * Along with example_appenders.c this file is used to create a small
 * library of custom appenders and formatters.
 *
 * This library is excercised using application_3 and a sample log4crc
 * config file.
 *
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <string.h>
#include <log4c.h>
/* using internal log4c debug function here,
 * this is not belong to examples :-) */
#include <sd/error.h>
#include "application_3.h"

/**********************************************************************
 *
 * Formatted to look for extended user location info
 *
 **********************************************************************/
static const char* userloc_format(
    const log4c_layout_t*       a_layout,
    const log4c_logging_event_t*a_event)
{
    static char buffer[4096];
    user_locinfo_t* uloc = NULL;

    sd_debug("Formatter s13_userloc checking location info for userdata %X",a_event->evt_loc->loc_data);
    if (a_event->evt_loc->loc_data != NULL)
    {
	sd_debug("Formatter s13_userloc getting a valid user location info pointer");
        uloc = (user_locinfo_t*) a_event->evt_loc->loc_data;
        sprintf(buffer, "[%s][HOST:%s][PID:%i][FILE:%s][LINE:%i][MSG:%s]",
		a_event->evt_category,  
		uloc->hostname, uloc->pid, a_event->evt_loc->loc_file,
		a_event->evt_loc->loc_line,a_event->evt_msg);

    }
    else
    {
        sprintf(buffer, "[%s]::[FILE:%s][LINE:%i][MSG::%s]", 
		a_event->evt_category,  
		a_event->evt_loc->loc_file,
		a_event->evt_loc->loc_line,a_event->evt_msg);
    }
    return buffer;
}

const log4c_layout_type_t log4c_layout_type_userloc  = {
   "s13_userloc",
   userloc_format,
};

/*****************************/
/*
 * Here provide an init routine for this lib 
 *
******************************/
static const log4c_layout_type_t * const layout_types[] = {
    &log4c_layout_type_userloc,
};
static int nlayout_types =
	(int)(sizeof(layout_types) / sizeof(layout_types[0]));


int init_userloc_formatters(){

  int rc = 0; int i = 0;
	
  for (i = 0; i < nlayout_types; i++) 
     log4c_layout_type_set(layout_types[i]);

  return(rc);

}