~ubuntu-branches/ubuntu/trusty/vanessa-logger/trusty-proposed

« back to all changes in this revision

Viewing changes to sample/vanessa_logger_sample.c

  • Committer: Bazaar Package Importer
  • Author(s): Simon Horman
  • Date: 2001-06-28 18:14:14 UTC
  • Revision ID: james.westby@ubuntu.com-20010628181414-wm88moh520kfsqfc
Tags: 0.0.2-1
See ChangeLog

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**********************************************************************
 
2
 * vanessa_logger_sample.c                               September 2000
 
3
 * Horms                                             horms@vergenet.net
 
4
 *
 
5
 * vanessa_logger
 
6
 * Generic logging layer
 
7
 * Copyright (C) 2000  Horms
 
8
 * 
 
9
 * This library is free software; you can redistribute it and/or
 
10
 * modify it under the terms of the GNU Lesser General Public License
 
11
 * as published by the Free Software Foundation; either version 2 of
 
12
 * the License, or (at your option) any later version.
 
13
 * 
 
14
 * This library is distributed in the hope that it will be useful, but
 
15
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
 * Lesser General Public License for more details.
 
18
 * 
 
19
 * You should have received a copy of the GNU Lesser General Public
 
20
 * License along with this library; if not, write to the Free Software
 
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 
22
 * 02111-1307 USA
 
23
 *
 
24
 **********************************************************************/
 
25
 
 
26
#include <vanessa_logger.h>
 
27
#include <unistd.h>
 
28
#include <sys/types.h>
 
29
 
 
30
#include "vanessa_logger_sample_config.h"
 
31
 
 
32
#define MIN_UID 100
 
33
 
 
34
/**********************************************************************
 
35
 * Muriel the main function
 
36
 **********************************************************************/
 
37
 
 
38
int main (int argc, char **argv){
 
39
  vanessa_logger_t *log_fh=NULL;
 
40
  vanessa_logger_t *log_fn=NULL;
 
41
  vanessa_logger_t *log_sl=NULL;
 
42
  vanessa_logger_t *log_sl_bn=NULL;
 
43
 
 
44
  printf("vanessa_logger_sample version %s Copyright Horms\n", VERSION);
 
45
 
 
46
 
 
47
  /* 
 
48
   * Make sure this is _not_ being run by a privileged user
 
49
   *
 
50
   * This programme is not suitable to be run by privileged users
 
51
   * as the filename logger that is opened opens a file
 
52
   * in the PWD. If vanessa logger is used in a programme
 
53
   * designed to be used by a privileged user then a full
 
54
   * pathname should be given to vanessa_logger_openlog_filename()
 
55
   */
 
56
  if(getuid()<MIN_UID || geteuid()<MIN_UID){
 
57
    fflush(stdout);
 
58
    fprintf(
 
59
      stderr,
 
60
      "Error: Run by privileged user with UID<%d or EUID<%d. Exiting\n",
 
61
      MIN_UID,
 
62
      MIN_UID
 
63
    );
 
64
    exit(-1);
 
65
  }
 
66
 
 
67
  /* 
 
68
   * Open logger to filehandle stderr
 
69
   */
 
70
  printf("\nOpening loggers\n");
 
71
 
 
72
  log_fh=vanessa_logger_openlog_filehandle(
 
73
    stderr, 
 
74
    "vanessa_logger_sample", 
 
75
    LOG_DEBUG,
 
76
    0
 
77
  );
 
78
  if(log_fh==NULL){
 
79
    fprintf(stderr, "Error: vanessa_logger_openlog_filehandle\n");
 
80
    exit(-1);
 
81
  }
 
82
 
 
83
  /* 
 
84
   * Open logger to filename ./vanessa_logger_sample.log
 
85
   */
 
86
  log_fn=vanessa_logger_openlog_filename(
 
87
    "./vanessa_logger_sample.log", 
 
88
    "vanessa_logger_sample", 
 
89
    LOG_DEBUG,
 
90
    0
 
91
  );
 
92
  if(log_fn==NULL){
 
93
    fprintf(stderr, "Error: vanessa_logger_openlog_filename\n");
 
94
    exit(-1);
 
95
  }
 
96
 
 
97
  /* 
 
98
   * Open logger to syslog facility LOG_USER
 
99
   */
 
100
  log_sl=vanessa_logger_openlog_syslog(
 
101
    LOG_USER, 
 
102
    "vanessa_logger_sample", 
 
103
    LOG_DEBUG, 
 
104
    0
 
105
  );
 
106
  if(log_sl==NULL){
 
107
    fprintf(stderr, "Error: vanessa_logger_openlog_syslog\n");
 
108
    exit(-1);
 
109
  }
 
110
 
 
111
  /* 
 
112
   * Open logger to syslog facility "mail" by name
 
113
   */
 
114
  log_sl_bn=vanessa_logger_openlog_syslog_byname(
 
115
    "mailstix",
 
116
    "vanessa_logger_sample", 
 
117
    LOG_DEBUG, 
 
118
    0
 
119
  );
 
120
  if(log_sl==NULL){
 
121
    fprintf(stderr, "Error: vanessa_logger_openlog_syslog\n");
 
122
    exit(-1);
 
123
  }
 
124
 
 
125
  /*
 
126
   * Send a message to each logger
 
127
   */
 
128
  printf("Logging message to stderr\n");
 
129
  fflush(stdout);
 
130
  vanessa_logger_log(log_fh, LOG_DEBUG, "This should log to stderr: %d\n", 7);
 
131
 
 
132
  printf("Logging message to ./vanessa_logger_sample.log\n");
 
133
  vanessa_logger_log(
 
134
    log_fn,
 
135
    LOG_DEBUG,
 
136
    "This should log to ./vanessa_logger_sample.log: %d",
 
137
    7
 
138
  );
 
139
 
 
140
  printf(
 
141
    "Logging message to syslog facility LOG_USER, priority LOG_DEBUG\n"
 
142
    "If the message is not logged to syslog then you may need to add\n"
 
143
    "the following to /etc/syslog.conf and restart syslogd:\n"
 
144
    "user.debug                                    /var/log/messages\n"
 
145
  );
 
146
  vanessa_logger_log(
 
147
    log_sl, 
 
148
    LOG_DEBUG,
 
149
    "This should log to syslog facility LOG_USER, priority LOG_DEBUG: %d",
 
150
    7
 
151
  );
 
152
 
 
153
  printf(
 
154
    "Logging message to syslog facility LOG_MAIL (\"mail\"), priority\n"
 
155
    "LOG_DEBUG, If the message is not logged to syslog then you may need\n"
 
156
    "to add the following to /etc/syslog.conf and restart syslogd:\n"
 
157
    "user.mail                                     /var/log/mail\n"
 
158
  );
 
159
  vanessa_logger_log(
 
160
    log_sl_bn, 
 
161
    LOG_DEBUG,
 
162
    "This should log to syslog facility LOG_MAIL, priority LOG_DEBUG: %d",
 
163
    7
 
164
  );
 
165
 
 
166
  fflush(stderr);
 
167
 
 
168
 
 
169
  /*
 
170
   * Reopen each log
 
171
   */
 
172
  printf("\nReopening loggers\n");
 
173
  vanessa_logger_reopen(log_fh);
 
174
  vanessa_logger_reopen(log_fn);
 
175
  vanessa_logger_reopen(log_sl);
 
176
 
 
177
  /*
 
178
   * Send another message to each logger
 
179
   */
 
180
  printf("Logging another message to stderr\n");
 
181
  fflush(stdout);
 
182
  vanessa_logger_log(log_fh, LOG_INFO, "This should also log to stderr\n");
 
183
 
 
184
  printf("Logging another message to ./vanessa_logger_sample.log\n");
 
185
  vanessa_logger_log(
 
186
    log_fn, 
 
187
    LOG_INFO,
 
188
    "This should also log to ./vanessa_logger_sample.log"
 
189
  );
 
190
 
 
191
  printf(
 
192
    "Logging another message to syslog facility LOG_USER, priority LOG_INFO\n"
 
193
  );
 
194
  vanessa_logger_log(
 
195
    log_sl, 
 
196
    LOG_INFO,
 
197
    "This should also log to syslog facility LOG_USER, priority LOG_INFO"
 
198
  );
 
199
 
 
200
  printf(
 
201
    "Logging another message to syslog facility LOG_MAIL, priority LOG_INFO\n"
 
202
  );
 
203
  vanessa_logger_log(
 
204
    log_sl_bn, 
 
205
    LOG_INFO,
 
206
    "This should also log to syslog facility LOG_MAIL, priority LOG_INFO"
 
207
  );
 
208
 
 
209
  fflush(stderr);
 
210
 
 
211
 
 
212
  /*
 
213
   * Change the maximum priority for each logger to LOG_INFO.
 
214
   * The maximum priority is counter-intuitive and sets the
 
215
   * minimum priority that will be accepted for logging.
 
216
   */
 
217
  vanessa_logger_change_max_priority(log_fh, LOG_INFO);
 
218
  vanessa_logger_change_max_priority(log_fn, LOG_INFO);
 
219
  vanessa_logger_change_max_priority(log_sl, LOG_INFO);
 
220
  vanessa_logger_change_max_priority(log_sl_bn, LOG_INFO);
 
221
 
 
222
  /*
 
223
   * These messages should not get logged as their priority,
 
224
   * LOG_DEBUG, is lower than the minimum priority LOG_INFO 
 
225
   * set when each logger was opened
 
226
   */
 
227
  printf("\nTesting that logs are filtered out by priority\n");
 
228
  printf("No logs should appear after this line\n");
 
229
  fflush(stdout);
 
230
  vanessa_logger_log(log_fh, LOG_DEBUG, "This should not log to stderr\n");
 
231
  vanessa_logger_log(
 
232
    log_fn, 
 
233
    LOG_DEBUG,
 
234
    "This should not log to ./vanessa_logger_sample.log"
 
235
  );
 
236
  vanessa_logger_log(
 
237
    log_sl, 
 
238
    LOG_DEBUG,
 
239
    "This should not log to syslog facility LOG_USER, priority LOG_INFO"
 
240
  );
 
241
  vanessa_logger_log(
 
242
    log_sl_bn, 
 
243
    LOG_DEBUG,
 
244
    "This should not log to syslog facility LOG_MAIL, priority LOG_INFO"
 
245
  );
 
246
 
 
247
  /*
 
248
   * Close each logger
 
249
   */
 
250
  vanessa_logger_closelog(log_fh);
 
251
  vanessa_logger_closelog(log_fn);
 
252
  vanessa_logger_closelog(log_sl);
 
253
  vanessa_logger_closelog(log_sl_bn);
 
254
 
 
255
  return(0);
 
256
}