OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [snmp/] [lib/] [v2_0/] [src/] [snmp_logging.c] - Blame information for rev 773

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      ./lib/current/src/snmp_logging.c
4
//
5
//
6
//==========================================================================
7
//####ECOSGPLCOPYRIGHTBEGIN####
8
// -------------------------------------------
9
// This file is part of eCos, the Embedded Configurable Operating System.
10
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
11
//
12
// eCos is free software; you can redistribute it and/or modify it under
13
// the terms of the GNU General Public License as published by the Free
14
// Software Foundation; either version 2 or (at your option) any later version.
15
//
16
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
17
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
18
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19
// for more details.
20
//
21
// You should have received a copy of the GNU General Public License along
22
// with eCos; if not, write to the Free Software Foundation, Inc.,
23
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24
//
25
// As a special exception, if other files instantiate templates or use macros
26
// or inline functions from this file, or you compile this file and link it
27
// with other works to produce a work based on this file, this file does not
28
// by itself cause the resulting work to be covered by the GNU General Public
29
// License. However the source code for this file must still be made available
30
// in accordance with section (3) of the GNU General Public License.
31
//
32
// This exception does not invalidate any other reasons why a work based on
33
// this file might be covered by the GNU General Public License.
34
//
35
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
36
// at http://sources.redhat.com/ecos/ecos-license/
37
// -------------------------------------------
38
//####ECOSGPLCOPYRIGHTEND####
39
//####UCDSNMPCOPYRIGHTBEGIN####
40
//
41
// -------------------------------------------
42
//
43
// Portions of this software may have been derived from the UCD-SNMP
44
// project,  <http://ucd-snmp.ucdavis.edu/>  from the University of
45
// California at Davis, which was originally based on the Carnegie Mellon
46
// University SNMP implementation.  Portions of this software are therefore
47
// covered by the appropriate copyright disclaimers included herein.
48
//
49
// The release used was version 4.1.2 of May 2000.  "ucd-snmp-4.1.2"
50
// -------------------------------------------
51
//
52
//####UCDSNMPCOPYRIGHTEND####
53
//==========================================================================
54
//#####DESCRIPTIONBEGIN####
55
//
56
// Author(s):    hmt
57
// Contributors: hmt
58
// Date:         2000-05-30
59
// Purpose:      Port of UCD-SNMP distribution to eCos.
60
// Description:  
61
//              
62
//
63
//####DESCRIPTIONEND####
64
//
65
//==========================================================================
66
/********************************************************************
67
       Copyright 1989, 1991, 1992 by Carnegie Mellon University
68
 
69
                          Derivative Work -
70
Copyright 1996, 1998, 1999, 2000 The Regents of the University of California
71
 
72
                         All Rights Reserved
73
 
74
Permission to use, copy, modify and distribute this software and its
75
documentation for any purpose and without fee is hereby granted,
76
provided that the above copyright notice appears in all copies and
77
that both that copyright notice and this permission notice appear in
78
supporting documentation, and that the name of CMU and The Regents of
79
the University of California not be used in advertising or publicity
80
pertaining to distribution of the software without specific written
81
permission.
82
 
83
CMU AND THE REGENTS OF THE UNIVERSITY OF CALIFORNIA DISCLAIM ALL
84
WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
85
WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL CMU OR
86
THE REGENTS OF THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY SPECIAL,
87
INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
88
FROM THE LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
89
CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
90
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
91
*********************************************************************/
92
/* logging.c - generic logging for snmp-agent
93
 * Contributed by Ragnar Kjørstad, ucd@ragnark.vestdata.no 1999-06-26 */
94
 
95
#include "config.h"
96
#include <stdio.h>
97
#if HAVE_MALLOC_H
98
#include <malloc.h>
99
#endif
100
#if HAVE_STRING_H
101
#include <string.h>
102
#else
103
#include <strings.h>
104
#endif
105
#if HAVE_STDLIB_H
106
#include <stdlib.h>
107
#endif
108
#include <sys/types.h>
109
#ifndef __ECOS
110
#include <sys/stat.h>
111
#endif
112
#if HAVE_FCNTL_H
113
#include <fcntl.h>
114
#endif
115
#include <errno.h>
116
#if HAVE_SYSLOG_H
117
#include <syslog.h>
118
#endif
119
#if TIME_WITH_SYS_TIME
120
# ifdef WIN32
121
#  include <sys/timeb.h>
122
# else
123
#  include <sys/time.h>
124
# endif
125
# include <time.h>
126
#else
127
# if HAVE_SYS_TIME_H
128
#  include <sys/time.h>
129
# else
130
#  include <time.h>
131
# endif
132
#endif
133
 
134
#if HAVE_STDARG_H
135
#include <stdarg.h>
136
#else
137
#include <varargs.h>
138
#endif
139
 
140
#if HAVE_DMALLOC_H
141
#include <dmalloc.h>
142
#endif
143
 
144
#ifdef WIN32
145
#include <winsock.h>
146
#endif
147
 
148
#include "asn1.h"
149
#include "default_store.h"
150
#include "snmp_logging.h"
151
#include "callback.h"
152
#define LOGLENGTH 1024
153
 
154
static int do_syslogging=0;
155
static int do_filelogging=0;
156
static int do_stderrlogging=1;
157
static int do_log_callback=0;
158
static int newline = 1;
159
static FILE *logfile;
160
 
161
void
162
init_snmp_logging(void) {
163
  ds_register_premib(ASN_BOOLEAN, "snmp", "logTimestamp", DS_LIBRARY_ID,
164
                     DS_LIB_LOG_TIMESTAMP);
165
}
166
 
167
int
168
snmp_get_do_logging(void) {
169
  return (do_syslogging || do_filelogging || do_stderrlogging ||
170
          do_log_callback);
171
}
172
 
173
 
174
static char *
175
sprintf_stamp (time_t *now, char *sbuf)
176
{
177
    time_t Now;
178
    struct tm *tm;
179
 
180
    if (now == NULL) {
181
        now = &Now;
182
        time (now);
183
    }
184
    tm = localtime (now);
185
    sprintf(sbuf, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d ",
186
            tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
187
            tm->tm_hour, tm->tm_min, tm->tm_sec);
188
    return sbuf;
189
}
190
 
191
void
192
snmp_disable_syslog(void) {
193
#if HAVE_SYSLOG_H
194
  if (do_syslogging)
195
    closelog();
196
#endif
197
  do_syslogging=0;
198
}
199
 
200
 
201
void
202
snmp_disable_filelog(void) {
203
  if (do_filelogging)
204
  {
205
    fputs("\n",logfile);
206
    fclose(logfile);
207
  }
208
  do_filelogging=0;
209
}
210
 
211
 
212
void
213
snmp_disable_stderrlog(void) {
214
  do_stderrlogging=0;
215
}
216
 
217
 
218
void
219
snmp_disable_log(void) {
220
  snmp_disable_syslog();
221
  snmp_disable_filelog();
222
  snmp_disable_stderrlog();
223
  snmp_disable_calllog();
224
}
225
 
226
 
227
void
228
snmp_enable_syslog(void)
229
{
230
  snmp_disable_syslog();
231
#if HAVE_SYSLOG_H
232
  openlog("ucd-snmp", LOG_CONS|LOG_PID, LOG_DAEMON);
233
  do_syslogging=1;
234
#endif
235
}
236
 
237
 
238
void
239
snmp_enable_filelog(const char *logfilename, int dont_zero_log)
240
{
241
  snmp_disable_filelog();
242
  logfile=fopen(logfilename, dont_zero_log ? "a" : "w");
243
  if (logfile) {
244
    do_filelogging=1;
245
    setvbuf(logfile, NULL, _IOLBF, BUFSIZ);
246
  }
247
  else
248
    do_filelogging=0;
249
}
250
 
251
 
252
void
253
snmp_enable_stderrlog(void) {
254
  do_stderrlogging=1;
255
}
256
 
257
 
258
void
259
snmp_enable_calllog(void) {
260
  do_log_callback = 1;
261
}
262
 
263
 
264
void
265
snmp_disable_calllog(void) {
266
  do_log_callback = 0;
267
}
268
 
269
 
270
void
271
snmp_log_string (int priority, const char *string)
272
{
273
    char sbuf[40];
274
    struct snmp_log_message slm;
275
 
276
#if HAVE_SYSLOG_H
277
  if (do_syslogging) {
278
    syslog(priority, string);
279
  }
280
#endif
281
 
282
  if (do_log_callback) {
283
      slm.priority = priority;
284
      slm.msg = string;
285
      snmp_call_callbacks(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_LOGGING, &slm);
286
  }
287
 
288
  if (do_filelogging || do_stderrlogging) {
289
 
290
    if (ds_get_boolean(DS_LIBRARY_ID, DS_LIB_LOG_TIMESTAMP) && newline) {
291
      sprintf_stamp(NULL, (char *)&sbuf);
292
    } else {
293
      strcpy(sbuf, "");
294
    }
295
    newline = string[strlen(string)-1] == '\n';
296
 
297
    if (do_filelogging)
298
      fprintf(logfile, "%s%s", sbuf, string);
299
 
300
    if (do_stderrlogging)
301
      fprintf(stderr, "%s%s", sbuf, string);
302
  }
303
}
304
 
305
int
306
snmp_vlog (int priority, const char *format, va_list ap)
307
{
308
  char buffer[LOGLENGTH];
309
  int length;
310
#if HAVE_VSNPRINTF
311
  char *dynamic;
312
 
313
  length=vsnprintf(buffer, LOGLENGTH, format, ap);
314
#else
315
  length=vsprintf(buffer, format, ap);
316
#endif
317
 
318
  if (length == 0)
319
    return(0);           /* Empty string */
320
 
321
  if (length == -1) {
322
    snmp_log_string(LOG_ERR, "Could not format log-string\n");
323
    return(-1);
324
  }
325
 
326
  if (length < LOGLENGTH) {
327
    snmp_log_string(priority, buffer);
328
    return(0);
329
  }
330
 
331
#if HAVE_VSNPRINTF
332
  dynamic=malloc(length+1);
333
  if (dynamic==NULL) {
334
    snmp_log_string(LOG_ERR, "Could not allocate memory for log-message\n");
335
    snmp_log_string(priority, buffer);
336
    return(-2);
337
  }
338
 
339
  vsnprintf(dynamic, length+1, format, ap);
340
  snmp_log_string(priority, dynamic);
341
  free(dynamic);
342
  return(0);
343
 
344
#else
345
  snmp_log_string(priority, buffer);
346
  snmp_log_string(LOG_ERR, "Log-message too long!\n");
347
  return(-3);
348
#endif
349
}
350
 
351
 
352
int
353
#if HAVE_STDARG_H
354
snmp_log (int priority, const char *format, ...)
355
#else
356
snmp_log (va_alist)
357
  va_dcl
358
#endif
359
{
360
  va_list ap;
361
  int ret;
362
#if HAVE_STDARG_H
363
  va_start(ap, format);
364
#else
365
  int priority;
366
  const char *format;
367
  va_start(ap);
368
 
369
  priority = va_arg(ap, int);
370
  format = va_arg(ap, const char *);
371
#endif
372
  ret=snmp_vlog(priority, format, ap);
373
  va_end(ap);
374
  return(ret);
375
}
376
 
377
/*
378
 * log a critical error.
379
 */
380
void
381
snmp_log_perror(const char *s)
382
{
383
  char *error  = strerror(errno);
384
  if (s) {
385
    if (error)
386
      snmp_log(LOG_ERR, "%s: %s\n", s, error);
387
    else
388
      snmp_log(LOG_ERR, "%s: Error %d out-of-range\n", s, errno);
389
  } else {
390
    if (error)
391
      snmp_log(LOG_ERR, "%s\n", error);
392
    else
393
      snmp_log(LOG_ERR, "Error %d out-of-range\n", errno);
394
  }
395
}
396
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.