| 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 |
#ifdef HAVE_CONFIG_H |
| 29 |
#include "config.h" |
| 30 |
#endif |
| 31 |
|
| 32 |
#include <stdarg.h> |
| 33 |
#include <stdio.h> |
| 34 |
#include <string.h> |
| 35 |
#include <syslog.h> |
| 36 |
|
| 37 |
#include "istgt.h" |
| 38 |
#include "istgt_log.h" |
| 39 |
#include "istgt_misc.h" |
| 40 |
|
| 41 |
|
| 42 |
int g_trace_flag = 0; |
| 43 |
int g_warn_flag = 1; |
| 44 |
static int g_log_facility = ISTGT_LOG_FACILITY; |
| 45 |
static int g_log_priority = ISTGT_LOG_PRIORITY; |
| 46 |
|
| 47 |
int |
| 48 |
istgt_set_log_facility(const char *facility) |
| 49 |
{ |
| 50 |
if (strcasecmp(facility, "daemon") == 0) { |
| 51 |
g_log_facility = LOG_DAEMON; |
| 52 |
} else if (strcasecmp(facility, "auth") == 0) { |
| 53 |
g_log_facility = LOG_AUTH; |
| 54 |
} else if (strcasecmp(facility, "authpriv") == 0) { |
| 55 |
g_log_facility = LOG_AUTHPRIV; |
| 56 |
} else if (strcasecmp(facility, "local1") == 0) { |
| 57 |
g_log_facility = LOG_LOCAL1; |
| 58 |
} else if (strcasecmp(facility, "local2") == 0) { |
| 59 |
g_log_facility = LOG_LOCAL2; |
| 60 |
} else if (strcasecmp(facility, "local3") == 0) { |
| 61 |
g_log_facility = LOG_LOCAL3; |
| 62 |
} else if (strcasecmp(facility, "local4") == 0) { |
| 63 |
g_log_facility = LOG_LOCAL4; |
| 64 |
} else if (strcasecmp(facility, "local5") == 0) { |
| 65 |
g_log_facility = LOG_LOCAL5; |
| 66 |
} else if (strcasecmp(facility, "local6") == 0) { |
| 67 |
g_log_facility = LOG_LOCAL6; |
| 68 |
} else if (strcasecmp(facility, "local7") == 0) { |
| 69 |
g_log_facility = LOG_LOCAL7; |
| 70 |
} else { |
| 71 |
g_log_facility = ISTGT_LOG_FACILITY; |
| 72 |
return -1; |
| 73 |
} |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
int |
| 78 |
istgt_set_log_priority(const char *priority) |
| 79 |
{ |
| 80 |
if (strcasecmp(priority, "emerg") == 0) { |
| 81 |
g_log_priority = LOG_EMERG; |
| 82 |
} else if (strcasecmp(priority, "alert") == 0) { |
| 83 |
g_log_priority = LOG_ALERT; |
| 84 |
} else if (strcasecmp(priority, "crit") == 0) { |
| 85 |
g_log_priority = LOG_CRIT; |
| 86 |
} else if (strcasecmp(priority, "err") == 0) { |
| 87 |
g_log_priority = LOG_ERR; |
| 88 |
} else if (strcasecmp(priority, "warning") == 0) { |
| 89 |
g_log_priority = LOG_WARNING; |
| 90 |
} else if (strcasecmp(priority, "notice") == 0) { |
| 91 |
g_log_priority = LOG_NOTICE; |
| 92 |
} else if (strcasecmp(priority, "info") == 0) { |
| 93 |
g_log_priority = LOG_INFO; |
| 94 |
} else if (strcasecmp(priority, "debug") == 0) { |
| 95 |
g_log_priority = LOG_DEBUG; |
| 96 |
} else { |
| 97 |
g_log_priority = ISTGT_LOG_PRIORITY; |
| 98 |
return -1; |
| 99 |
} |
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
void |
| 104 |
istgt_log(const char *file, const int line, const char *func, const char *format, ...) |
| 105 |
{ |
| 106 |
char buf[MAX_TMPBUF]; |
| 107 |
va_list ap; |
| 108 |
|
| 109 |
va_start(ap, format); |
| 110 |
vsnprintf(buf, sizeof buf, format, ap); |
| 111 |
if (file != NULL) { |
| 112 |
if (func != NULL) { |
| 113 |
fprintf(stderr, "%s:%4d:%s: %s", file, line, func, buf); |
| 114 |
syslog(g_log_priority, "%s:%4d:%s: %s", file, line, func, buf); |
| 115 |
} else { |
| 116 |
fprintf(stderr, "%s:%4d: %s", file, line, buf); |
| 117 |
syslog(g_log_priority, "%s:%4d: %s", file, line, buf); |
| 118 |
} |
| 119 |
} else { |
| 120 |
fprintf(stderr, "%s", buf); |
| 121 |
syslog(g_log_priority, "%s", buf); |
| 122 |
} |
| 123 |
va_end(ap); |
| 124 |
} |
| 125 |
|
| 126 |
void |
| 127 |
istgt_noticelog(const char *file, const int line, const char *func, const char *format, ...) |
| 128 |
{ |
| 129 |
char buf[MAX_TMPBUF]; |
| 130 |
va_list ap; |
| 131 |
|
| 132 |
va_start(ap, format); |
| 133 |
vsnprintf(buf, sizeof buf, format, ap); |
| 134 |
if (file != NULL) { |
| 135 |
if (func != NULL) { |
| 136 |
fprintf(stderr, "%s:%4d:%s: %s", file, line, func, buf); |
| 137 |
syslog(LOG_NOTICE, "%s:%4d:%s: %s", file, line, func, buf); |
| 138 |
} else { |
| 139 |
fprintf(stderr, "%s:%4d: %s", file, line, buf); |
| 140 |
syslog(LOG_NOTICE, "%s:%4d: %s", file, line, buf); |
| 141 |
} |
| 142 |
} else { |
| 143 |
fprintf(stderr, "%s", buf); |
| 144 |
syslog(LOG_NOTICE, "%s", buf); |
| 145 |
} |
| 146 |
va_end(ap); |
| 147 |
} |
| 148 |
|
| 149 |
void |
| 150 |
istgt_tracelog(const int flag, const char *file, const int line, const char *func, const char *format, ...) |
| 151 |
{ |
| 152 |
char buf[MAX_TMPBUF]; |
| 153 |
va_list ap; |
| 154 |
|
| 155 |
va_start(ap, format); |
| 156 |
if (g_trace_flag & flag) { |
| 157 |
vsnprintf(buf, sizeof buf, format, ap); |
| 158 |
if (func != NULL) { |
| 159 |
fprintf(stderr, "%s:%4d:%s: %s", file, line, func, buf); |
| 160 |
|
| 161 |
} else { |
| 162 |
fprintf(stderr, "%s:%4d: %s", file, line, buf); |
| 163 |
|
| 164 |
} |
| 165 |
} |
| 166 |
va_end(ap); |
| 167 |
} |
| 168 |
|
| 169 |
void |
| 170 |
istgt_errlog(const char *file, const int line, const char *func, const char *format, ...) |
| 171 |
{ |
| 172 |
char buf[MAX_TMPBUF]; |
| 173 |
va_list ap; |
| 174 |
|
| 175 |
va_start(ap, format); |
| 176 |
vsnprintf(buf, sizeof buf, format, ap); |
| 177 |
if (func != NULL) { |
| 178 |
fprintf(stderr, "%s:%4d:%s: ***ERROR*** %s", file, line, func, buf); |
| 179 |
syslog(LOG_ERR, "%s:%4d:%s: ***ERROR*** %s", file, line, func, buf); |
| 180 |
} else { |
| 181 |
fprintf(stderr, "%s:%4d: ***ERROR*** %s", file, line, buf); |
| 182 |
syslog(LOG_ERR, "%s:%4d: ***ERROR*** %s", file, line, buf); |
| 183 |
} |
| 184 |
va_end(ap); |
| 185 |
} |
| 186 |
|
| 187 |
void |
| 188 |
istgt_warnlog(const char *file, const int line, const char *func, const char *format, ...) |
| 189 |
{ |
| 190 |
char buf[MAX_TMPBUF]; |
| 191 |
va_list ap; |
| 192 |
|
| 193 |
va_start(ap, format); |
| 194 |
vsnprintf(buf, sizeof buf, format, ap); |
| 195 |
if (func != NULL) { |
| 196 |
fprintf(stderr, "%s:%4d:%s: ***WARNING*** %s", file, line, func, buf); |
| 197 |
syslog(LOG_WARNING, "%s:%4d:%s: ***WARNING*** %s", |
| 198 |
file, line, func, buf); |
| 199 |
} else { |
| 200 |
fprintf(stderr, "%s:%4d: ***WARNING*** %s", file, line, buf); |
| 201 |
syslog(LOG_WARNING, "%s:%4d: ***WARNING*** %s", file, line, buf); |
| 202 |
} |
| 203 |
va_end(ap); |
| 204 |
} |
| 205 |
|
| 206 |
void |
| 207 |
istgt_open_log(void) |
| 208 |
{ |
| 209 |
if (g_log_facility != 0) { |
| 210 |
openlog("istgt", LOG_PID, g_log_facility); |
| 211 |
} else { |
| 212 |
openlog("istgt", LOG_PID, ISTGT_LOG_FACILITY); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
void |
| 217 |
istgt_close_log(void) |
| 218 |
{ |
| 219 |
closelog(); |
| 220 |
} |
| 221 |
|
| 222 |
void |
| 223 |
istgt_set_trace_flag(int flag) |
| 224 |
{ |
| 225 |
if (flag == ISTGT_TRACE_NONE) { |
| 226 |
g_trace_flag = 0; |
| 227 |
} else { |
| 228 |
g_trace_flag |= flag; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
void |
| 233 |
istgt_trace_dump(int flag, const char *label, const uint8_t *buf, size_t len) |
| 234 |
{ |
| 235 |
if (g_trace_flag & flag) { |
| 236 |
istgt_fdump(stderr, label, buf, len); |
| 237 |
} |
| 238 |
} |