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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
/*
* The Spread Toolkit.
*
* The contents of this file are subject to the Spread Open-Source
* License, Version 1.0 (the ``License''); you may not use
* this file except in compliance with the License. You may obtain a
* copy of the License at:
*
* http://www.spread.org/license/
*
* or in the file ``license.txt'' found in this distribution.
*
* Software distributed under the License is distributed on an AS IS basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Creators of Spread are:
* Yair Amir, Michal Miskin-Amir, Jonathan Stanton.
*
* Copyright (C) 1993-2004 Spread Concepts LLC <spread@spreadconcepts.com>
*
* All Rights Reserved.
*
* Major Contributor(s):
* ---------------
* Cristina Nita-Rotaru crisn@cs.purdue.edu - group communication security.
* Theo Schlossnagle jesus@omniti.com - Perl, skiplists, autoconf.
* Dan Schoenblum dansch@cnds.jhu.edu - Java interface.
* John Schultz jschultz@cnds.jhu.edu - contribution to process group membership.
*
*/
#include "arch.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* undef redefined variables under windows */
#ifdef ARCH_PC_WIN95
#undef EINTR
#undef EAGAIN
#undef EWOULDBLOCK
#undef EINPROGRESS
#endif
#include <errno.h>
#ifdef HAVE_GOOD_VARGS
#include <stdarg.h>
#endif
#include "alarm.h"
static int32 Alarm_type_mask = PRINT | EXIT ;
static int16 Alarm_cur_priority = SPLOG_DEBUG ;
static char *Alarm_timestamp_format = NULL;
static const char *DEFAULT_TIMESTAMP_FORMAT="[%a %d %b %Y %H:%M:%S]";
static int AlarmInteractiveProgram = FALSE;
#ifdef HAVE_GOOD_VARGS
/* Probably should work on all platforms, but just in case, I leave it to the
developers...
*/
void Alarmp( int16 priority, int32 mask, char *message, ...)
{
/* log event if in mask and of higher priority, or if FATAL event, always log */
if ( (( Alarm_type_mask & mask ) && (Alarm_cur_priority <= priority))
|| (priority == SPLOG_FATAL) )
{
va_list ap;
if ( Alarm_timestamp_format && (priority != SPLOG_PRINT_NODATE) )
{
char timestamp[42];
struct tm *tm_now;
time_t time_now;
size_t length;
time_now = time(NULL);
tm_now = localtime(&time_now);
length = strftime(timestamp, 40,
Alarm_timestamp_format, tm_now);
timestamp[length] = ' ';
fwrite(timestamp, length+1, sizeof(char), stdout);
}
va_start(ap,message);
vprintf(message, ap);
va_end(ap);
}
if ( ( EXIT & mask ) || (priority == SPLOG_FATAL) )
{
printf("Exit caused by Alarm(EXIT)\n");
exit( 0 );
}
}
/* For backwards compatibility while moving all Alarm calls over, this provides
* the old interface and logs them as WARNING events.
*/
void Alarm( int32 mask, char *message, ...)
{
if ( ( Alarm_type_mask & mask ) && (Alarm_cur_priority <= SPLOG_WARNING) )
{
va_list ap;
if ( Alarm_timestamp_format )
{
char timestamp[42];
struct tm *tm_now;
time_t time_now;
size_t length;
time_now = time(NULL);
tm_now = localtime(&time_now);
length = strftime(timestamp, 40,
Alarm_timestamp_format, tm_now);
timestamp[length] = ' ';
fwrite(timestamp, length+1, sizeof(char), stdout);
}
va_start(ap,message);
vprintf(message, ap);
va_end(ap);
}
if ( EXIT & mask )
{
printf("Exit caused by Alarm(EXIT)\n");
exit( 0 );
}
}
#else
void Alarm( int16 priority, int32 mask, char *message,
void *ptr1, void *ptr2, void *ptr3, void *ptr4,
void *ptr5, void *ptr6, void *ptr7, void *ptr8,
void *ptr9, void *ptr10, void*ptr11, void *ptr12,
void *ptr13, void *ptr14, void *ptr15, void *ptr16,
void *ptr17, void *ptr18, void *ptr19, void *ptr20)
{
if ( ( Alarm_type_mask & mask ) && (Alarm_cur_priority < priority) )
{
if ( Alarm_timestamp_format )
{
char timestamp[42];
struct tm *tm_now;
time_t time_now;
size_t length;
time_now = time(NULL);
tm_now = localtime(&time_now);
length = strftime(timestamp, 40,
Alarm_timestamp_format, tm_now);
timestamp[length] = ' ';
fwrite(timestamp, length+1, sizeof(char), stdout);
}
printf(message, ptr1, ptr2, ptr3, ptr4, ptr5, ptr6, ptr7, ptr8, ptr9, ptr10, ptr11, ptr12, ptr13, ptr14, ptr15, ptr16, ptr17, ptr18, ptr19, ptr20 );
}
if ( EXIT & mask )
{
printf("Exit caused by Alarm(EXIT)\n");
exit( 0 );
}
}
#endif /* HAVE_GOOD_VARGS */
void Alarm_set_interactive(void)
{
AlarmInteractiveProgram = TRUE;
}
int Alarm_get_interactive(void)
{
return(AlarmInteractiveProgram);
}
void Alarm_set_output(char *filename) {
FILE *newfile;
newfile = freopen(filename, "a", stdout);
if ( NULL == newfile ) {
printf("failed to open file (%s) for stdout. Error: %d\n", filename, errno);
}
newfile = freopen(filename, "a", stderr);
if ( NULL == newfile ) {
printf("failed to open file (%s) for stderr. Error: %d\n", filename, errno);
}
setvbuf(stderr, (char *)0, _IONBF, 0);
setvbuf(stdout, (char *)0, _IONBF, 0);
}
void Alarm_enable_timestamp(char *format)
{
static char _local_timestamp[40];
if(format)
strncpy(_local_timestamp, format, 40);
else
strncpy(_local_timestamp, DEFAULT_TIMESTAMP_FORMAT, 40);
Alarm_timestamp_format = _local_timestamp;
}
void Alarm_disable_timestamp(void)
{
Alarm_timestamp_format = NULL;
}
void Alarm_set_types(int32 mask)
{
Alarm_type_mask = Alarm_type_mask | mask;
}
void Alarm_clear_types(int32 mask)
{
Alarm_type_mask = Alarm_type_mask & ~mask;
}
int32 Alarm_get_types(void)
{
return(Alarm_type_mask);
}
void Alarm_set_priority(int16 priority)
{
Alarm_cur_priority = priority;
}
int16 Alarm_get_priority(void)
{
return(Alarm_cur_priority);
}
|