File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / bird2 / sysdep / unix / log.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Mon Oct 21 16:03:56 2019 UTC (5 years, 5 months ago) by misho
Branches: bird2, MAIN
CVS tags: v2_0_7p0, HEAD
bird2 ver 2.0.7

    1: /*
    2:  *	BIRD Library -- Logging Functions
    3:  *
    4:  *	(c) 1998--2000 Martin Mares <mj@ucw.cz>
    5:  *
    6:  *	Can be freely distributed and used under the terms of the GNU GPL.
    7:  */
    8: 
    9: /**
   10:  * DOC: Logging
   11:  *
   12:  * The Logging module offers a simple set of functions for writing
   13:  * messages to system logs and to the debug output. Message classes
   14:  * used by this module are described in |birdlib.h| and also in the
   15:  * user's manual.
   16:  */
   17: 
   18: #include <stdio.h>
   19: #include <stdlib.h>
   20: #include <stdarg.h>
   21: #include <time.h>
   22: #include <sys/types.h>
   23: #include <sys/stat.h>
   24: #include <unistd.h>
   25: #include <errno.h>
   26: 
   27: #include "nest/bird.h"
   28: #include "nest/cli.h"
   29: #include "conf/conf.h"
   30: #include "lib/string.h"
   31: #include "lib/lists.h"
   32: #include "sysdep/unix/unix.h"
   33: 
   34: static FILE *dbgf;
   35: static list *current_log_list;
   36: static char *current_syslog_name; /* NULL -> syslog closed */
   37: 
   38: 
   39: #ifdef USE_PTHREADS
   40: 
   41: #include <pthread.h>
   42: 
   43: static pthread_mutex_t log_mutex;
   44: static inline void log_lock(void) { pthread_mutex_lock(&log_mutex); }
   45: static inline void log_unlock(void) { pthread_mutex_unlock(&log_mutex); }
   46: 
   47: static pthread_t main_thread;
   48: void main_thread_init(void) { main_thread = pthread_self(); }
   49: static int main_thread_self(void) { return pthread_equal(pthread_self(), main_thread); }
   50: 
   51: #else
   52: 
   53: static inline void log_lock(void) {  }
   54: static inline void log_unlock(void) {  }
   55: void main_thread_init(void) { }
   56: static int main_thread_self(void) { return 1; }
   57: 
   58: #endif
   59: 
   60: 
   61: #ifdef HAVE_SYSLOG_H
   62: #include <sys/syslog.h>
   63: 
   64: static int syslog_priorities[] = {
   65:   LOG_DEBUG,
   66:   LOG_DEBUG,
   67:   LOG_DEBUG,
   68:   LOG_INFO,
   69:   LOG_ERR,
   70:   LOG_WARNING,
   71:   LOG_ERR,
   72:   LOG_ERR,
   73:   LOG_CRIT,
   74:   LOG_CRIT
   75: };
   76: #endif
   77: 
   78: static char *class_names[] = {
   79:   "???",
   80:   "DBG",
   81:   "TRACE",
   82:   "INFO",
   83:   "RMT",
   84:   "WARN",
   85:   "ERR",
   86:   "AUTH",
   87:   "FATAL",
   88:   "BUG"
   89: };
   90: 
   91: static inline off_t
   92: log_size(struct log_config *l)
   93: {
   94:   struct stat st;
   95:   return (!fstat(rf_fileno(l->rf), &st) && S_ISREG(st.st_mode)) ? st.st_size : 0;
   96: }
   97: 
   98: static void
   99: log_close(struct log_config *l)
  100: {
  101:   rfree(l->rf);
  102:   l->rf = NULL;
  103:   l->fh = NULL;
  104: }
  105: 
  106: static int
  107: log_open(struct log_config *l)
  108: {
  109:   l->rf = rf_open(config->pool, l->filename, "a");
  110:   if (!l->rf)
  111:   {
  112:     /* Well, we cannot do much in case of error as log is closed */
  113:     l->mask = 0;
  114:     return -1;
  115:   }
  116: 
  117:   l->fh = rf_file(l->rf);
  118:   l->pos = log_size(l);
  119: 
  120:   return 0;
  121: }
  122: 
  123: static int
  124: log_rotate(struct log_config *l)
  125: {
  126:   log_close(l);
  127: 
  128:   /* If we cannot rename the logfile, we at least try to delete it
  129:      in order to continue logging and not exceeding logfile size */
  130:   if ((rename(l->filename, l->backup) < 0) &&
  131:       (unlink(l->filename) < 0))
  132:   {
  133:     l->mask = 0;
  134:     return -1;
  135:   }
  136: 
  137:   return log_open(l);
  138: }
  139: 
  140: /**
  141:  * log_commit - commit a log message
  142:  * @class: message class information (%L_DEBUG to %L_BUG, see |lib/birdlib.h|)
  143:  * @buf: message to write
  144:  *
  145:  * This function writes a message prepared in the log buffer to the
  146:  * log file (as specified in the configuration). The log buffer is
  147:  * reset after that. The log message is a full line, log_commit()
  148:  * terminates it.
  149:  *
  150:  * The message class is an integer, not a first char of a string like
  151:  * in log(), so it should be written like *L_INFO.
  152:  */
  153: void
  154: log_commit(int class, buffer *buf)
  155: {
  156:   struct log_config *l;
  157: 
  158:   if (buf->pos == buf->end)
  159:     strcpy(buf->end - 100, " ... <too long>");
  160: 
  161:   log_lock();
  162:   WALK_LIST(l, *current_log_list)
  163:     {
  164:       if (!(l->mask & (1 << class)))
  165: 	continue;
  166:       if (l->fh)
  167: 	{
  168: 	  if (l->terminal_flag)
  169: 	    fputs("bird: ", l->fh);
  170: 	  else
  171: 	    {
  172: 	      byte tbuf[TM_DATETIME_BUFFER_SIZE];
  173: 	      const char *fmt = config ? config->tf_log.fmt1 : "%F %T.%3f";
  174: 	      if (!tm_format_real_time(tbuf, sizeof(tbuf), fmt, current_real_time()))
  175: 		strcpy(tbuf, "<error>");
  176: 
  177: 	      if (l->limit)
  178: 	      {
  179: 		off_t msg_len = strlen(tbuf) + strlen(class_names[class]) +
  180: 		  (buf->pos - buf->start) + 5;
  181: 
  182: 		if (l->pos < 0)
  183: 		  l->pos = log_size(l);
  184: 
  185: 		if (l->pos + msg_len > l->limit)
  186: 		  if (log_rotate(l) < 0)
  187: 		    continue;
  188: 
  189: 		l->pos += msg_len;
  190: 	      }
  191: 
  192: 	      fprintf(l->fh, "%s <%s> ", tbuf, class_names[class]);
  193: 	    }
  194: 	  fputs(buf->start, l->fh);
  195: 	  fputc('\n', l->fh);
  196: 	  fflush(l->fh);
  197: 	}
  198: #ifdef HAVE_SYSLOG_H
  199:       else
  200: 	syslog(syslog_priorities[class], "%s", buf->start);
  201: #endif
  202:     }
  203:   log_unlock();
  204: 
  205:   /* cli_echo is not thread-safe, so call it just from the main thread */
  206:   if (main_thread_self())
  207:     cli_echo(class, buf->start);
  208: 
  209:   buf->pos = buf->start;
  210: }
  211: 
  212: int buffer_vprint(buffer *buf, const char *fmt, va_list args);
  213: 
  214: static void
  215: vlog(int class, const char *msg, va_list args)
  216: {
  217:   buffer buf;
  218:   LOG_BUFFER_INIT(buf);
  219:   buffer_vprint(&buf, msg, args);
  220:   log_commit(class, &buf);
  221: }
  222: 
  223: 
  224: /**
  225:  * log - log a message
  226:  * @msg: printf-like formatting string with message class information
  227:  * prepended (%L_DEBUG to %L_BUG, see |lib/birdlib.h|)
  228:  *
  229:  * This function formats a message according to the format string @msg
  230:  * and writes it to the corresponding log file (as specified in the
  231:  * configuration). Please note that the message is automatically
  232:  * formatted as a full line, no need to include |\n| inside.
  233:  * It is essentially a sequence of log_reset(), logn() and log_commit().
  234:  */
  235: void
  236: log_msg(const char *msg, ...)
  237: {
  238:   int class = 1;
  239:   va_list args;
  240: 
  241:   va_start(args, msg);
  242:   if (*msg >= 1 && *msg <= 8)
  243:     class = *msg++;
  244:   vlog(class, msg, args);
  245:   va_end(args);
  246: }
  247: 
  248: void
  249: log_rl(struct tbf *f, const char *msg, ...)
  250: {
  251:   int class = 1;
  252:   va_list args;
  253: 
  254:   /* Rate limiting is a bit tricky here as it also logs '...' during the first hit */
  255:   if (tbf_limit(f) && (f->drop > 1))
  256:     return;
  257: 
  258:   if (*msg >= 1 && *msg <= 8)
  259:     class = *msg++;
  260: 
  261:   va_start(args, msg);
  262:   vlog(class, (f->drop ? "..." : msg), args);
  263:   va_end(args);
  264: }
  265: 
  266: /**
  267:  * bug - report an internal error
  268:  * @msg: a printf-like error message
  269:  *
  270:  * This function logs an internal error and aborts execution
  271:  * of the program.
  272:  */
  273: void
  274: bug(const char *msg, ...)
  275: {
  276:   va_list args;
  277: 
  278:   va_start(args, msg);
  279:   vlog(L_BUG[0], msg, args);
  280:   va_end(args);
  281:   abort();
  282: }
  283: 
  284: /**
  285:  * bug - report a fatal error
  286:  * @msg: a printf-like error message
  287:  *
  288:  * This function logs a fatal error and aborts execution
  289:  * of the program.
  290:  */
  291: void
  292: die(const char *msg, ...)
  293: {
  294:   va_list args;
  295: 
  296:   va_start(args, msg);
  297:   vlog(L_FATAL[0], msg, args);
  298:   va_end(args);
  299:   exit(1);
  300: }
  301: 
  302: /**
  303:  * debug - write to debug output
  304:  * @msg: a printf-like message
  305:  *
  306:  * This function formats the message @msg and prints it out
  307:  * to the debugging output. No newline character is appended.
  308:  */
  309: void
  310: debug(const char *msg, ...)
  311: {
  312: #define MAX_DEBUG_BUFSIZE       65536
  313:   va_list args;
  314:   static uint bufsize = 4096;
  315:   static char *buf = NULL;
  316: 
  317:   if (!buf)
  318:     buf = mb_alloc(&root_pool, bufsize);
  319: 
  320:   va_start(args, msg);
  321:   if (dbgf)
  322:     {
  323:       while (bvsnprintf(buf, bufsize, msg, args) < 0)
  324:         if (bufsize >= MAX_DEBUG_BUFSIZE)
  325:           bug("Extremely long debug output, split it.");
  326:         else
  327:           buf = mb_realloc(buf, (bufsize *= 2));
  328: 
  329:       fputs(buf, dbgf);
  330:     }
  331:   va_end(args);
  332: }
  333: 
  334: static list *
  335: default_log_list(int initial, char **syslog_name)
  336: {
  337:   static list log_list;
  338:   init_list(&log_list);
  339:   *syslog_name = NULL;
  340: 
  341: #ifdef HAVE_SYSLOG_H
  342:   if (!dbgf)
  343:     {
  344:       static struct log_config lc_syslog = { .mask = ~0 };
  345:       add_tail(&log_list, &lc_syslog.n);
  346:       *syslog_name = bird_name;
  347:     }
  348: #endif
  349: 
  350:   if (dbgf && (dbgf != stderr))
  351:     {
  352:       static struct log_config lc_debug = { .mask = ~0 };
  353:       lc_debug.fh = dbgf;
  354:       add_tail(&log_list, &lc_debug.n);
  355:     }
  356: 
  357:   if (initial || (dbgf == stderr))
  358:     {
  359:       static struct log_config lc_stderr = { .mask = ~0, .terminal_flag = 1};
  360:       lc_stderr.fh = stderr;
  361:       add_tail(&log_list, &lc_stderr.n);
  362:     }
  363: 
  364:   return &log_list;
  365: }
  366: 
  367: void
  368: log_switch(int initial, list *logs, char *new_syslog_name)
  369: {
  370:   struct log_config *l;
  371: 
  372:   if (!logs || EMPTY_LIST(*logs))
  373:     logs = default_log_list(initial, &new_syslog_name);
  374: 
  375:   /* We shouldn't close the logs when other threads may use them */
  376:   log_lock();
  377: 
  378:   /* Close the logs to avoid pinning them on disk when deleted */
  379:   if (current_log_list)
  380:     WALK_LIST(l, *current_log_list)
  381:       if (l->rf)
  382: 	log_close(l);
  383: 
  384:   /* Reopen the logs, needed for 'configure undo' */
  385:   if (logs)
  386:     WALK_LIST(l, *logs)
  387:       if (l->filename && !l->rf)
  388: 	log_open(l);
  389: 
  390:   current_log_list = logs;
  391: 
  392: #ifdef HAVE_SYSLOG_H
  393:   if (!bstrcmp(current_syslog_name, new_syslog_name))
  394:     goto done;
  395: 
  396:   if (current_syslog_name)
  397:   {
  398:     closelog();
  399:     xfree(current_syslog_name);
  400:     current_syslog_name = NULL;
  401:   }
  402: 
  403:   if (new_syslog_name)
  404:   {
  405:     current_syslog_name = xstrdup(new_syslog_name);
  406:     openlog(current_syslog_name, LOG_CONS | LOG_NDELAY, LOG_DAEMON);
  407:   }
  408: 
  409: #endif
  410: 
  411: done:
  412:   /* Logs exchange done, let the threads log as before */
  413:   log_unlock();
  414: }
  415: 
  416: void
  417: log_init_debug(char *f)
  418: {
  419:   if (dbgf && dbgf != stderr)
  420:     fclose(dbgf);
  421:   if (!f)
  422:     dbgf = NULL;
  423:   else if (!*f)
  424:     dbgf = stderr;
  425:   else if (!(dbgf = fopen(f, "a")))
  426:   {
  427:     /* Cannot use die() nor log() here, logging is not yet initialized */
  428:     fprintf(stderr, "bird: Unable to open debug file %s: %s\n", f, strerror(errno));
  429:     exit(1);
  430:   }
  431:   if (dbgf)
  432:     setvbuf(dbgf, NULL, _IONBF, 0);
  433: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>