| 1 |
/* simplelog.c - Simple logging subsystem for Poldi |
| 2 |
Copyright (C) 2008 g10 Code GmbH |
| 3 |
|
| 4 |
This file is part of Poldi. |
| 5 |
|
| 6 |
Poldi is free software; you can redistribute it and/or modify it |
| 7 |
under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 3 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
Poldi is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program; if not, see |
| 18 |
<http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
#include <util-local.h> |
| 21 |
|
| 22 |
#include <stdio.h> |
| 23 |
#include <assert.h> |
| 24 |
#include <unistd.h> |
| 25 |
#include <sys/types.h> |
| 26 |
#include <time.h> |
| 27 |
#include <assert.h> |
| 28 |
#include <errno.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <syslog.h> |
| 31 |
|
| 32 |
#include <gpg-error.h> |
| 33 |
|
| 34 |
#include "simplelog.h" |
| 35 |
|
| 36 |
struct log_handle |
| 37 |
{ |
| 38 |
log_backend_t backend; |
| 39 |
log_level_t min_level; |
| 40 |
unsigned int flags; |
| 41 |
char prefix[LOG_PREFIX_LENGTH]; |
| 42 |
FILE *stream; |
| 43 |
}; |
| 44 |
|
| 45 |
static gpg_error_t |
| 46 |
internal_release_backend (log_handle_t handle) |
| 47 |
{ |
| 48 |
assert (handle->backend != LOG_BACKEND_NONE); |
| 49 |
|
| 50 |
switch (handle->backend) |
| 51 |
{ |
| 52 |
case LOG_BACKEND_NONE: |
| 53 |
case LOG_BACKEND_STREAM: |
| 54 |
case LOG_BACKEND_SYSLOG: |
| 55 |
/* Nothing to do here. */ |
| 56 |
break; |
| 57 |
|
| 58 |
case LOG_BACKEND_FILE: |
| 59 |
/* FIXME: error checking. */ |
| 60 |
assert (handle->stream); |
| 61 |
fclose (handle->stream); |
| 62 |
break; |
| 63 |
} |
| 64 |
|
| 65 |
handle->backend = LOG_BACKEND_NONE; |
| 66 |
|
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
static gpg_error_t |
| 71 |
internal_set_backend_file (log_handle_t handle, const char *filename) |
| 72 |
{ |
| 73 |
gpg_error_t err = 0; |
| 74 |
FILE *stream = NULL; |
| 75 |
|
| 76 |
assert (handle->backend == LOG_BACKEND_NONE); |
| 77 |
|
| 78 |
stream = fopen (filename, "a"); |
| 79 |
if (!stream) |
| 80 |
{ |
| 81 |
err = gpg_error_from_errno (errno); |
| 82 |
goto out; |
| 83 |
} |
| 84 |
|
| 85 |
handle->backend = LOG_BACKEND_FILE; |
| 86 |
handle->stream = stream; |
| 87 |
|
| 88 |
out: |
| 89 |
|
| 90 |
return err; |
| 91 |
} |
| 92 |
|
| 93 |
static gpg_error_t |
| 94 |
internal_set_backend_stream (log_handle_t handle, FILE *stream) |
| 95 |
{ |
| 96 |
assert (handle->backend == LOG_BACKEND_NONE); |
| 97 |
assert (stream); |
| 98 |
|
| 99 |
handle->backend = LOG_BACKEND_STREAM; |
| 100 |
handle->stream = stream; |
| 101 |
|
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
static gpg_error_t |
| 106 |
internal_set_backend_syslog (log_handle_t handle) |
| 107 |
{ |
| 108 |
assert (handle->backend == LOG_BACKEND_NONE); |
| 109 |
|
| 110 |
handle->backend = LOG_BACKEND_SYSLOG; |
| 111 |
|
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
gpg_error_t |
| 116 |
log_create (log_handle_t *handle) |
| 117 |
{ |
| 118 |
gpg_error_t err = 0; |
| 119 |
|
| 120 |
*handle = xtrymalloc (sizeof (**handle)); |
| 121 |
if (!*handle) |
| 122 |
{ |
| 123 |
err = gpg_error_from_errno (errno); |
| 124 |
goto out; |
| 125 |
} |
| 126 |
|
| 127 |
(*handle)->backend = LOG_BACKEND_NONE; |
| 128 |
(*handle)->min_level = LOG_LEVEL_INFO; |
| 129 |
(*handle)->flags = 0; |
| 130 |
(*handle)->prefix[0] = 0; |
| 131 |
|
| 132 |
out: |
| 133 |
|
| 134 |
return err; |
| 135 |
} |
| 136 |
|
| 137 |
void |
| 138 |
log_destroy (log_handle_t handle) |
| 139 |
{ |
| 140 |
if (handle) |
| 141 |
{ |
| 142 |
if (handle->backend != LOG_BACKEND_NONE) |
| 143 |
internal_release_backend (handle); |
| 144 |
xfree (handle); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
void |
| 149 |
log_set_flags (log_handle_t handle, unsigned int flags) |
| 150 |
{ |
| 151 |
handle->flags |= flags; |
| 152 |
} |
| 153 |
|
| 154 |
void |
| 155 |
log_unset_flags (log_handle_t handle, unsigned int flags) |
| 156 |
{ |
| 157 |
handle->flags &= ~flags; |
| 158 |
} |
| 159 |
|
| 160 |
gpg_error_t |
| 161 |
log_set_backend_file (log_handle_t handle, const char *filename) |
| 162 |
{ |
| 163 |
gpg_error_t err; |
| 164 |
|
| 165 |
assert (handle); |
| 166 |
|
| 167 |
if (handle->backend != LOG_BACKEND_NONE) |
| 168 |
internal_release_backend (handle); |
| 169 |
|
| 170 |
err = internal_set_backend_file (handle, filename); |
| 171 |
|
| 172 |
return err; |
| 173 |
} |
| 174 |
|
| 175 |
gpg_error_t |
| 176 |
log_set_backend_stream (log_handle_t handle, FILE *stream) |
| 177 |
{ |
| 178 |
gpg_error_t err; |
| 179 |
|
| 180 |
assert (handle); |
| 181 |
|
| 182 |
if (handle->backend != LOG_BACKEND_NONE) |
| 183 |
internal_release_backend (handle); |
| 184 |
|
| 185 |
err = internal_set_backend_stream (handle, stream); |
| 186 |
|
| 187 |
return err; |
| 188 |
} |
| 189 |
|
| 190 |
gpg_error_t |
| 191 |
log_set_backend_syslog (log_handle_t handle) |
| 192 |
{ |
| 193 |
gpg_error_t err; |
| 194 |
|
| 195 |
assert (handle); |
| 196 |
|
| 197 |
if (handle->backend != LOG_BACKEND_NONE) |
| 198 |
internal_release_backend (handle); |
| 199 |
|
| 200 |
err = internal_set_backend_syslog (handle); |
| 201 |
|
| 202 |
return err; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
void |
| 207 |
log_set_prefix (log_handle_t handle, const char *prefix) |
| 208 |
{ |
| 209 |
assert (handle); |
| 210 |
|
| 211 |
strncpy (handle->prefix, prefix, sizeof (handle->prefix) - 1); |
| 212 |
handle->prefix[sizeof (handle->prefix) - 1] = 0; |
| 213 |
} |
| 214 |
|
| 215 |
void |
| 216 |
log_set_min_level (log_handle_t handle, log_level_t min_level) |
| 217 |
{ |
| 218 |
assert (handle); |
| 219 |
|
| 220 |
if (min_level == LOG_LEVEL_DEBUG |
| 221 |
|| min_level == LOG_LEVEL_INFO |
| 222 |
|| min_level == LOG_LEVEL_ERROR |
| 223 |
|| min_level == LOG_LEVEL_FATAL) |
| 224 |
handle->min_level = min_level; |
| 225 |
} |
| 226 |
|
| 227 |
static gpg_error_t |
| 228 |
internal_log_write (log_handle_t handle, log_level_t level, |
| 229 |
const char *fmt, va_list ap) |
| 230 |
{ |
| •231 |
gpg_error_t err = 0; |
| 232 |
|
| 233 |
assert (handle->backend != LOG_BACKEND_NONE); |
| 234 |
|
| 235 |
/* FIXME: shall we do error checking here? And what if an error |
| 236 |
occurs? */ |
| 237 |
|
| 238 |
if (level < handle->min_level) |
| 239 |
/* User does not want to receive messages for level smaller than |
| 240 |
min_level. */ |
| 241 |
return 0; |
| 242 |
|
| 243 |
if (handle->backend == LOG_BACKEND_SYSLOG) |
| 244 |
{ |
| 245 |
int syslog_priority; |
| 246 |
|
| 247 |
switch (level) |
| 248 |
{ |
| 249 |
case LOG_LEVEL_DEBUG: |
| 250 |
syslog_priority = LOG_DEBUG; |
| 251 |
break; |
| 252 |
|
| 253 |
case LOG_LEVEL_INFO: |
| 254 |
syslog_priority = LOG_INFO; |
| 255 |
break; |
| 256 |
|
| 257 |
case LOG_LEVEL_ERROR: |
| 258 |
syslog_priority = LOG_ERR; |
| 259 |
break; |
| 260 |
|
| 261 |
case LOG_LEVEL_FATAL: |
| 262 |
syslog_priority = LOG_ALERT; |
| 263 |
break; |
| 264 |
|
| 265 |
default: |
| 266 |
/* FIXME: what to do when the user passes an invalid log |
| 267 |
level? -mo */ |
| 268 |
syslog_priority = LOG_ERR; |
| 269 |
break; |
| 270 |
} |
| 271 |
|
| 272 |
vsyslog (LOG_MAKEPRI (LOG_AUTH, syslog_priority), fmt, ap); |
| 273 |
} |
| 274 |
else if (handle->backend == LOG_BACKEND_STREAM |
| 275 |
|| handle->backend == LOG_BACKEND_FILE) |
| 276 |
{ |
| 277 |
FILE *stream = handle->stream; |
| 278 |
|
| 279 |
assert (stream); |
| 280 |
|
| 281 |
if ((handle->flags & LOG_FLAG_WITH_PREFIX) && (*handle->prefix != 0)) |
| 282 |
fprintf (stream, "%s ", handle->prefix); |
| 283 |
|
| 284 |
if (handle->flags & LOG_FLAG_WITH_TIME) |
| 285 |
{ |
| 286 |
struct tm *tp; |
| 287 |
time_t atime = time (NULL); |
| 288 |
|
| 289 |
tp = localtime (&atime); |
| 290 |
fprintf (stream, "%04d-%02d-%02d %02d:%02d:%02d ", |
| 291 |
1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday, |
| 292 |
tp->tm_hour, tp->tm_min, tp->tm_sec); |
| 293 |
} |
| 294 |
|
| 295 |
if (handle->flags & LOG_FLAG_WITH_PID) |
| 296 |
fprintf (stream, "[%u] ", (unsigned int) getpid ()); |
| 297 |
|
| 298 |
switch (level) |
| 299 |
{ |
| 300 |
case LOG_LEVEL_ERROR: |
| 301 |
case LOG_LEVEL_FATAL: |
| 302 |
fprintf (stream, "error: "); |
| 303 |
break; |
| 304 |
|
| 305 |
case LOG_LEVEL_DEBUG: |
| 306 |
fprintf (stream, "debug: "); |
| 307 |
break; |
| 308 |
|
| 309 |
case LOG_LEVEL_INFO: |
| 310 |
break; |
| 311 |
} |
| 312 |
|
| 313 |
vfprintf (stream, fmt, ap); |
| 314 |
putc ('\n', stream); |
| 315 |
|
| 316 |
} |
| 317 |
|
| 318 |
return err; |
| 319 |
} |
| 320 |
|
| 321 |
gpg_error_t |
| 322 |
log_write (log_handle_t handle, log_level_t level, |
| 323 |
const char *fmt, ...) |
| 324 |
{ |
| 325 |
gpg_error_t err = 0; |
| 326 |
|
| 327 |
assert (handle); |
| 328 |
|
| 329 |
if (handle->backend != LOG_BACKEND_NONE) |
| 330 |
{ |
| 331 |
va_list ap; |
| 332 |
|
| 333 |
va_start (ap, fmt); |
| 334 |
err = internal_log_write (handle, level, fmt, ap); |
| 335 |
va_end (ap); |
| 336 |
} |
| 337 |
|
| 338 |
return err; |
| 339 |
} |
| 340 |
|
| 341 |
gpg_error_t |
| 342 |
log_write_va (log_handle_t handle, log_level_t level, |
| 343 |
const char *fmt, va_list ap) |
| 344 |
{ |
| 345 |
gpg_error_t err = 0; |
| 346 |
|
| 347 |
assert (handle); |
| 348 |
|
| 349 |
if (handle->backend != LOG_BACKEND_NONE) |
| 350 |
err = internal_log_write (handle, level, fmt, ap); |
| 351 |
|
| 352 |
return err; |
| 353 |
} |
| 354 |
|
| 355 |
gpg_error_t |
| 356 |
log_msg_debug (log_handle_t handle, const char *fmt, ...) |
| 357 |
{ |
| 358 |
gpg_error_t err; |
| 359 |
va_list ap; |
| 360 |
|
| 361 |
if (!handle) |
| 362 |
return 0; |
| 363 |
|
| 364 |
va_start (ap, fmt); |
| 365 |
err = log_write_va (handle, LOG_LEVEL_DEBUG, fmt, ap); |
| 366 |
va_end (ap); |
| 367 |
|
| 368 |
return err; |
| 369 |
} |
| 370 |
|
| 371 |
gpg_error_t |
| 372 |
log_msg_info (log_handle_t handle, const char *fmt, ...) |
| 373 |
{ |
| 374 |
gpg_error_t err; |
| 375 |
va_list ap; |
| 376 |
|
| 377 |
if (!handle) |
| 378 |
return 0; |
| 379 |
|
| 380 |
va_start (ap, fmt); |
| 381 |
err = log_write_va (handle, LOG_LEVEL_INFO, fmt, ap); |
| 382 |
va_end (ap); |
| 383 |
|
| 384 |
return err; |
| 385 |
} |
| 386 |
|
| 387 |
gpg_error_t |
| 388 |
log_msg_error (log_handle_t handle, const char *fmt, ...) |
| 389 |
{ |
| 390 |
gpg_error_t err; |
| 391 |
va_list ap; |
| 392 |
|
| 393 |
if (!handle) |
| 394 |
return 0; |
| 395 |
|
| 396 |
va_start (ap, fmt); |
| 397 |
err = log_write_va (handle, LOG_LEVEL_ERROR, fmt, ap); |
| 398 |
va_end (ap); |
| 399 |
|
| 400 |
return err; |
| 401 |
} |
| 402 |
|
| 403 |
gpg_error_t |
| 404 |
log_msg_fatal (log_handle_t handle, const char *fmt, ...) |
| 405 |
{ |
| 406 |
gpg_error_t err; |
| 407 |
va_list ap; |
| 408 |
|
| 409 |
if (!handle) |
| 410 |
return 0; |
| 411 |
|
| 412 |
va_start (ap, fmt); |
| 413 |
err = log_write_va (handle, LOG_LEVEL_FATAL, fmt, ap); |
| 414 |
va_end (ap); |
| 415 |
|
| 416 |
return err; |
| 417 |
} |
| 418 |
|
| 419 |
/* END */ |