logstream.c 14.4 KB
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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2010-2012, 2014-2017 Free Software Foundation, Inc.

   This library is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/cctype.h>
#include <mailutils/cstr.h>
#include <mailutils/log.h>

#include <mailutils/nls.h>
#include <mailutils/stream.h>
#include <mailutils/debug.h>
#include <mailutils/locus.h>
#include <mailutils/sys/logstream.h>

char *_mu_severity_str[] = {
  N_("debug"),
  N_("info"),
  N_("notice"),
  N_("warning"),
  N_("error"),
  N_("crit"),
  N_("alert"),
  N_("emerg"),
};
int _mu_severity_num = MU_ARRAY_SIZE (_mu_severity_str);

int
mu_severity_from_string (const char *str, unsigned *pn)
{
  int i;

  for (i = 0; i < _mu_severity_num; i++)
    {
      if (mu_c_strcasecmp (_mu_severity_str[i], str) == 0)
	{
	  *pn = i;
	  return 0;
	}
    }
  return MU_ERR_NOENT;
}

int
mu_severity_to_string (unsigned n, const char **pstr)
{
  if (n >= _mu_severity_num)
    return MU_ERR_NOENT;
  *pstr = _mu_severity_str[n];
  return 0;
}

static void
lr_set_line (struct mu_locus_range *loc, unsigned val, int end)
{
  if (end)
    loc->end.mu_line = val;
  else
    loc->beg.mu_line = val;
}

static void
lr_set_col (struct mu_locus_range *loc, unsigned val, int end)
{
  if (end)
    loc->end.mu_col = val;
  else
    loc->beg.mu_col = val;
}

static int
lr_set_file (struct mu_locus_range *loc, char const *fname, unsigned len,
	     int end)
{
  char const *refname;
  struct mu_locus_point *pt = end ? &loc->end : &loc->beg;
  int rc;
  
  if (fname == NULL)
    {
      refname = NULL;
      rc = 0;
    }
  else if (len == 0)
    rc = mu_ident_ref (fname, &refname);
  else
    {
      char *name;

      name = malloc (len + 1);
      if (!name)
	return errno;
      memcpy (name, fname, len);
      name[len] = 0;
      rc = mu_ident_ref (name, &refname);
      free (name);
    }
  if (rc)
    return rc;
  mu_ident_deref (pt->mu_file);
  pt->mu_file = refname;
  return 0;
}

/* Field modification map (binary):

     FfLlCc

   The bits f, l, and c (file, line, and column) are toggled cyclically.
   The value 0 means locus beg, 1 meand locus end.
   The bits F, L, and C are set once and indicate that the corresponding
   bit was toggled at least once.
 */
#define FMM_COL  0
#define FMM_LINE 1
#define FMM_FILE 2

#define FMM_SHIFT(n) ((n)<<1)
#define FMM_MASK(n) (0x3 << FMM_SHIFT (n))
#define FMM_VAL(m,n) (((m) >> FMM_SHIFT (n)) & 0x1)
#define FMM_SET(m,n,v) ((m) = ((m) & ~FMM_MASK (n)) | (((v) << FMM_SHIFT (n))|0x2))
#define FMM_CYCLE(m, n) \
  FMM_SET ((m), (n), ((FMM_VAL ((m), (n)) + 1) % 2))

static int
_log_write (struct _mu_stream *str, const char *buf, size_t size,
	    size_t *pnwrite)
{
  struct _mu_log_stream *sp = (struct _mu_log_stream *)str;
  unsigned severity = sp->severity;
  int logmode = sp->logmode;
  struct mu_locus_range loc;
  int fmm = 0;
  unsigned flen = 0;
  int save_locus = 0;
  int rc;
  int escape_error = 0;
  
#define NEXT do { if (size == 0) return EINVAL; buf++; size--; } while (0)
#define READNUM(n) do {				\
    unsigned __x = 0;				\
    if (*buf != '<')				\
      return EINVAL;				\
    NEXT;					\
    while (*buf != '>')				\
      {						\
        if (!mu_isdigit (*buf))			\
	  return EINVAL;			\
	__x = __x * 10 + (*buf - '0');		\
	NEXT;					\
      }						\
    NEXT;					\
    n = __x;					\
} while (0)

  loc = sp->locrange;
  mu_ident_ref (loc.beg.mu_file, &loc.beg.mu_file);
  mu_ident_ref (loc.end.mu_file, &loc.end.mu_file);
  
  /* Tell them we've consumed everything */
  *pnwrite = size;
  
  /* Process escapes */
  while (!escape_error && *buf == '\033')
    {
      int code;
      unsigned n;
      
      NEXT;
      code = *buf;
      NEXT;
      switch (code)
	{
	case 'S':
	  /* Save locus */
	  save_locus = 1;
	  break;

	case 's':
	  /* Severity code in decimal (single digit) */
	  if (*buf == '<')
	    READNUM (severity);
	  else if (mu_isdigit (*buf))
	    {
	      severity = *buf - '0';
	      NEXT;
	    }
	  else
	    return EINVAL;
	  break;
	  
	case 'O':
	  /* Flags on.  Followed by a bitmask of MU_LOGMODE_*, in decimal  */
	  READNUM (n);
	  logmode |= n;
	  break;
	  
	case 'X':
	  /* Flags off.  Followed by a bitmask of MU_LOGMODE_*, in decimal  */
	  READNUM (n);
	  logmode &= ~n;
	  break;
	  
	case 'l':
	  /* Input line (decimal) */
	  READNUM (n);
	  lr_set_line (&loc, n, FMM_VAL (fmm, FMM_LINE));
	  FMM_CYCLE (fmm, FMM_LINE);
	  logmode |= MU_LOGMODE_LOCUS;
	  break;

	case 'c':
	  /* Column in input line (decimal) */
	  READNUM (n);
	  lr_set_col (&loc, n, FMM_VAL (fmm, FMM_COL));
	  FMM_CYCLE (fmm, FMM_COL);
	  logmode |= MU_LOGMODE_LOCUS;
	  break;
	  
	case 'f':
	  /* File name. Format: <N>S */
	  READNUM (flen);
	  lr_set_file (&loc, buf, flen, FMM_VAL (fmm, FMM_FILE));
	  FMM_CYCLE (fmm, FMM_FILE);
	  buf += flen;
	  size -= flen;
	  logmode |= MU_LOGMODE_LOCUS;
	  break;

	default:
	  buf -= 2;
	  size += 2;
	  escape_error = 1;
	}
    }

  if (severity >= _mu_severity_num)
    severity = MU_LOG_EMERG;

  if (save_locus)
    {
      sp->locrange = loc;
      mu_ident_ref (sp->locrange.beg.mu_file, &sp->locrange.beg.mu_file);
      mu_ident_ref (sp->locrange.end.mu_file, &sp->locrange.end.mu_file);
    }
  
  if (severity < sp->threshold)
    rc = 0;
  else
    {
      mu_stream_ioctl (sp->transport, MU_IOCTL_LOGSTREAM,
		       MU_IOCTL_LOGSTREAM_SET_SEVERITY, &severity);
  
      if ((logmode & MU_LOGMODE_LOCUS) && loc.beg.mu_file)
	{
	  mu_stream_print_locus_range (sp->transport, &loc);
  	  mu_stream_write (sp->transport, ": ", 2, NULL);
	}
      
      if ((logmode & MU_LOGMODE_SEVERITY) &&
	  !(sp->sevmask & MU_DEBUG_LEVEL_MASK (severity)))
	{
	  char *s = gettext (_mu_severity_str[severity]);
	  rc = mu_stream_write (sp->transport, s, strlen (s), NULL);
	  if (rc)
	    return rc;
	  mu_stream_write (sp->transport, ": ", 2, NULL);
	}
      rc = mu_stream_write (sp->transport, buf, size, NULL);
    }
  
  mu_ident_deref (loc.beg.mu_file);
  mu_ident_deref (loc.end.mu_file);
  return rc;
}

static int
_log_flush (struct _mu_stream *str)
{
  struct _mu_log_stream *sp = (struct _mu_log_stream *)str;
  return mu_stream_flush (sp->transport);
}

static void
_log_done (struct _mu_stream *str)
{
  struct _mu_log_stream *sp = (struct _mu_log_stream *)str;
  mu_stream_destroy (&sp->transport);
}

static int
_log_close (struct _mu_stream *str)
{
  struct _mu_log_stream *sp = (struct _mu_log_stream *)str;
  return mu_stream_close (sp->transport);
}

static int
_log_setbuf_hook (mu_stream_t str, enum mu_buffer_type type, size_t size)
{
  if (type != mu_buffer_line)
    return EACCES;
  return 0;
}

static int
_log_ctl (struct _mu_stream *str, int code, int opcode, void *arg)
{
  struct _mu_log_stream *sp = (struct _mu_log_stream *)str;
  int status;
  
  switch (code)
    {
    case MU_IOCTL_TRANSPORT:
      if (!arg)
	return EINVAL;
      else
	{
	  mu_transport_t *ptrans = arg;
	  switch (opcode)
	    {
	    case MU_IOCTL_OP_GET:
	      ptrans[0] = (mu_transport_t) sp->transport;
	      ptrans[1] = NULL;
	      break;

	    case MU_IOCTL_OP_SET:
	      ptrans = arg;
	      if (ptrans[0])
		sp->transport = (mu_stream_t) ptrans[0];
	      break;

	    default:
	      return EINVAL;
	    }
	}
      break;

    case MU_IOCTL_SUBSTREAM:
      if (sp->transport &&
          ((status = mu_stream_ioctl (sp->transport, code, opcode, arg)) == 0 ||
           status != ENOSYS))
        return status;
      /* fall through */

    case MU_IOCTL_TOPSTREAM:
      if (!arg)
	return EINVAL;
      else
	{
	  mu_stream_t *pstr = arg;
	  switch (opcode)
	    {
	    case MU_IOCTL_OP_GET:
	      pstr[0] = sp->transport;
	      mu_stream_ref (pstr[0]);
	      pstr[1] = NULL;
	      break;

	    case MU_IOCTL_OP_SET:
	      mu_stream_unref (sp->transport);
	      sp->transport = pstr[0];
	      mu_stream_ref (sp->transport);
	      break;

	    default:
	      return EINVAL;
	    }
	}
      break;
      
    case MU_IOCTL_LOGSTREAM:
      switch (opcode)
	{
	case MU_IOCTL_LOGSTREAM_GET_SEVERITY:
	  if (!arg)
	    return EINVAL;
	  *(unsigned*)arg = sp->severity;
	  break;
      
	case MU_IOCTL_LOGSTREAM_SET_SEVERITY:
	  if (!arg)
	    return EINVAL;
	  if (*(unsigned*)arg >= _mu_severity_num)
	    return EINVAL;
	  sp->severity = *(unsigned*)arg;
	  break;

	case MU_IOCTL_LOGSTREAM_GET_MODE:
	  if (!arg)
	    return EINVAL;
	  *(int*)arg = sp->logmode;
	  break;

	case MU_IOCTL_LOGSTREAM_SET_MODE:
	  if (!arg)
	    return EINVAL;
	  sp->logmode = *(int*)arg;
	  break;
      
	case MU_IOCTL_LOGSTREAM_SET_LOCUS_RANGE:
	  {
	    struct mu_locus_range *lr = arg;
	    if (!arg)
	      {
		mu_ident_deref (sp->locrange.beg.mu_file);
		mu_ident_deref (sp->locrange.end.mu_file);
		memset (&sp->locrange, 0, sizeof sp->locrange);
	      }
	    else
	      {
		char const *begname, *endname;
		
		status = mu_ident_ref (lr->beg.mu_file, &begname);
		if (status)
		  return status;
		status = mu_ident_ref (lr->end.mu_file, &endname);
		if (status)
		  {
		    mu_ident_deref (begname);
		    return status;
		  }
		mu_ident_deref (sp->locrange.beg.mu_file);
		sp->locrange.beg.mu_file = begname;
		sp->locrange.beg.mu_line = lr->beg.mu_line;
		sp->locrange.beg.mu_col = lr->beg.mu_col;

		mu_ident_deref (sp->locrange.end.mu_file);
		sp->locrange.end.mu_file = endname;
		sp->locrange.end.mu_line = lr->end.mu_line;
		sp->locrange.end.mu_col = lr->end.mu_col;
	      }
	  }
	  break;

	case MU_IOCTL_LOGSTREAM_GET_LOCUS_RANGE:
	  if (!arg)
	    return EINVAL;
	  else
	    {
	      struct mu_locus_range *lr = arg;
	      char const *begname, *endname;

	      status = mu_ident_ref (sp->locrange.beg.mu_file, &begname);
	      if (status)
		return status;
	      status = mu_ident_ref (sp->locrange.end.mu_file, &endname);
	      if (status)
		{
		  mu_ident_deref (begname);
		  return status;
		}
	      lr->beg.mu_file = begname;
	      lr->beg.mu_line = sp->locrange.beg.mu_line;
	      lr->beg.mu_col = sp->locrange.beg.mu_col;
	      lr->end.mu_file = endname;
	      lr->end.mu_line = sp->locrange.end.mu_line;
	      lr->end.mu_col = sp->locrange.end.mu_col;
	    }
	  break;
	  
	case MU_IOCTL_LOGSTREAM_GET_LOCUS:
	  if (!arg)
	    return EINVAL;
	  else
	    {
	      struct mu_locus *ploc = arg;
	      if (sp->locrange.beg.mu_file)
		{
		  ploc->mu_file = strdup (sp->locrange.beg.mu_file);
		  if (!ploc->mu_file)
		    return ENOMEM;
		}
	      else
		ploc->mu_file = NULL;
	      ploc->mu_line = sp->locrange.beg.mu_line;
	      ploc->mu_col = sp->locrange.beg.mu_col;
	    }
	  break;
	
	case MU_IOCTL_LOGSTREAM_SET_LOCUS:
	  {
	    struct mu_locus *ploc = arg;

	    mu_ident_deref (sp->locrange.end.mu_file);
	    sp->locrange.end.mu_file = NULL;
	    if (arg)
	      {
		status = lr_set_file (&sp->locrange, ploc->mu_file, 0, 0);
		if (status)
		  return status;
		lr_set_line (&sp->locrange, ploc->mu_line, 0);
		lr_set_col (&sp->locrange, ploc->mu_col, 0);
	      }
	    else
	      {
		mu_ident_deref (sp->locrange.beg.mu_file);
		sp->locrange.beg.mu_file = NULL;
	      }
	    
	    break;
	  }

	case MU_IOCTL_LOGSTREAM_SET_LOCUS_LINE:
	  if (!arg)
	    return EINVAL;
	  sp->locrange.beg.mu_line = *(unsigned*)arg;
	  break;
	  
	case MU_IOCTL_LOGSTREAM_SET_LOCUS_COL:
	  if (!arg)
	    return EINVAL;
	  sp->locrange.beg.mu_col = *(unsigned*)arg;
	  break;
	  
	case MU_IOCTL_LOGSTREAM_ADVANCE_LOCUS_LINE:
	  if (!arg)
	    sp->locrange.beg.mu_line++;
	  else
	    sp->locrange.beg.mu_line += *(int*)arg;
	  break;

	case MU_IOCTL_LOGSTREAM_ADVANCE_LOCUS_COL:
	  if (!arg)
	    sp->locrange.beg.mu_col++;
	  else
	    sp->locrange.beg.mu_col += *(int*)arg;
	  break;

	case MU_IOCTL_LOGSTREAM_SUPPRESS_SEVERITY:
	  if (!arg)
	    sp->threshold = MU_LOG_DEBUG;
	  else if (*(unsigned*)arg >= _mu_severity_num)
	    return MU_ERR_NOENT;
	  sp->threshold = *(unsigned*)arg;
	  break;
	  
	case MU_IOCTL_LOGSTREAM_SUPPRESS_SEVERITY_NAME:
	  if (!arg)
	    sp->threshold = MU_LOG_DEBUG;
	  else
	    return mu_severity_from_string ((const char *) arg, &sp->threshold);

	case MU_IOCTL_LOGSTREAM_GET_SEVERITY_MASK:
	  if (!arg)
	    return EINVAL;
	  *(int*)arg = sp->sevmask;
	  break;

	case MU_IOCTL_LOGSTREAM_SET_SEVERITY_MASK:
	  if (!arg)
	    return EINVAL;
	  sp->sevmask = *(int*)arg;
	  break;

	case MU_IOCTL_LOGSTREAM_CLONE:
	  if (!arg)
	    return EINVAL;
	  else
	    {
	      mu_stream_t str;
	      struct _mu_log_stream *newp;
	      int rc = mu_log_stream_create (&str, sp->transport);
	      if (rc)
		return rc;
	      newp = (struct _mu_log_stream *) str;
	      newp->severity = sp->severity;
	      newp->threshold = sp->threshold;
	      newp->logmode = sp->logmode;
	      newp->sevmask = sp->sevmask;
	      newp->locrange = sp->locrange;
	      mu_ident_ref (sp->locrange.beg.mu_file,
			    &sp->locrange.beg.mu_file);
	      mu_ident_ref (sp->locrange.end.mu_file,
			    &sp->locrange.end.mu_file);
	      
	      *(mu_stream_t*) arg = str;
	    }
	  break;
	  
	default:
	  return EINVAL;
	}
      break;

    case MU_IOCTL_FILTER:
    case MU_IOCTL_SYSLOGSTREAM:
      return mu_stream_ioctl (sp->transport, code, opcode, arg);
            
    default:
      return ENOSYS;
    }
  return 0;
}

void
_mu_log_stream_setup (struct _mu_log_stream *sp, mu_stream_t transport)
{
  mu_stream_t stream;
  
  sp->base.write = _log_write;
  sp->base.flush = _log_flush;
  sp->base.close = _log_close;
  sp->base.done = _log_done;
  sp->base.setbuf_hook = _log_setbuf_hook;
  sp->base.ctl = _log_ctl;
  sp->transport = transport;
  mu_stream_ref (transport);
  sp->severity = MU_LOG_ERROR;
  sp->logmode = 0;
  
  stream = (mu_stream_t) sp;
  mu_stream_set_buffer (stream, mu_buffer_line, 0);
}

int
mu_log_stream_create (mu_stream_t *pstr, mu_stream_t transport)
{
  struct _mu_log_stream *sp;
  
  sp = (struct _mu_log_stream *)
    _mu_stream_create (sizeof (*sp), MU_STREAM_WRITE);
  if (!sp)
    return ENOMEM;
  _mu_log_stream_setup (sp, transport);
  *pstr = (mu_stream_t) sp;
  
  return 0;
}