File: ldaplog.c

package info (click to toggle)
ldapdiff 0.9.2-1.1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 716 kB
  • ctags: 182
  • sloc: ansic: 2,392; sh: 768; makefile: 11
file content (136 lines) | stat: -rw-r--r-- 2,762 bytes parent folder | download | duplicates (2)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
    ldapdiff
    Copyright (C) 2000-2002 Thomas.Reith@rhoen.de

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <lber.h>
#include <ldap.h>

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include <syslog.h>
#include <ctype.h>

#include "ldapdiff.h"

static void ldiflogstdout(char *msg)
{
 time_t       t;

 t = time(NULL);
 printf("%s: %s\n",strtok(ctime(&t),"\n"),msg);
}

static void ldiflogfile(char *msg)
{
 extern char *logfile;
 FILE        *f;
 time_t       t;

 if((f = fopen(logfile,"a")) == NULL){
  fprintf(stderr,"fopen() of %s failed: file: %s, line: %d\n",logfile,__FILE__,__LINE__
);
   exit(-1);
 }

 t = time(NULL);
 fprintf(f,"%s: %s\n",strtok(ctime(&t),"\n"),msg);

 fclose(f);
}

static void ldiflogsyslog(int aloglevel, char *msg)
{
 extern int facility;
 int        level = LOG_ERR;

 openlog(SYSLOGNAME,LOG_ODELAY,facility);
 switch(aloglevel){
  case LOG0:  level = LOG_ERR;
       break;
  case LOG1:  level = LOG_INFO;
       break;
  case LOG2 : level = LOG_DEBUG;
       break;
 }
 syslog(level,msg);
 closelog();
}

void ldiflogval(teloglevel aloglevel,char *s,char* var,char *val,size_t val_len)
{
 char *tval;

 tval = LDALLOC(1,val_len + 1);
 if(val != NULL){
  memcpy(tval,val,val_len);
 }
 ldiflog(aloglevel,s,var,tval);
 free(tval);
}

void ldiflog(teloglevel aloglevel,char *s,...)
{
 extern int   facility;
 extern int   loglevel;
 extern char *logfile;
 int          i = 0;
 int          j = 0;

 char buf[MAXATTRLEN];
 char pbuf[MAXATTRLEN];

 va_list ap;

 if(aloglevel > loglevel){
  return;
 }

 va_start(ap,s);
 vsprintf(buf,s,ap);
 va_end(ap);

 for(i=0;i<strlen(buf);i++){
  if(isalnum(buf[i]) || ispunct(buf[i]) || isspace(buf[i])){
   pbuf[j++] = buf[i];
  }
  else{
   sprintf(pbuf+j,"[%3.3o]",(unsigned char)buf[i]);
   j += 5;
  }

  if(j >= MAXLOGLEN){
   sprintf(pbuf+j,"[...]");
   j += 5;
   break;
  }
 } 
 pbuf[j] = '\0';
 
 if(facility != -1){
  ldiflogsyslog(aloglevel,pbuf);
 }
 else if(logfile != NULL){
  ldiflogfile(pbuf);
 }
 else{
  ldiflogstdout(pbuf);
 }
}