su_log.c 6.56 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
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
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, 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 this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@ingroup su_log
 * @CFILE su_log.c
 *
 * Implementation of generic logging interface.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * 
 * @date Created: Fri Feb 23 17:30:13 2001 ppessi
 */

#include "config.h"

37 38
#include <sofia-sip/su_log.h>
#include <sofia-sip/su_errno.h>
39

40
#include <stdio.h>
Pekka Pessi's avatar
Pekka Pessi committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include <stdarg.h>
#include <stdlib.h>
#include <assert.h>

#if SU_HAVE_PTHREADS
#include <pthread.h>
#define SU_LOG_IS_INIT(log) pthread_mutex_trylock((log)->log_init)
#define SU_LOG_DO_INIT(log)
#define SU_LOG_LOCK(log)    pthread_mutex_lock((log)->log_lock)
#define SU_LOG_UNLOCK(log)  pthread_mutex_unlock((log)->log_lock)
#else
#define SU_LOG_IS_INIT(log) ((log)->log_init)
#define SU_LOG_DO_INIT(log) ((log)->log_init = 1)
#define SU_LOG_LOCK(log) 
#define SU_LOG_UNLOCK(log)
#endif

58 59 60 61 62 63 64 65 66
/**@defgroup su_log Logging Interface
 *
 * Generic logging interface.
 *
 * The @b su_log submodule contains a generic logging interface. The
 * interface provides means for redirecting the log and filtering log
 * messages based on message priority.
 *
 * @sa @ref debug_logs, <sofia-sip/su_log.h>, 
67
 * su_llog(), su_vllog(), #su_log_t, #SU_DEBUG,
68 69 70 71 72
 * SU_DEBUG_0(), SU_DEBUG_1(), SU_DEBUG_2(), SU_DEBUG_3(), SU_DEBUG_5(), 
 * SU_DEBUG_7(), SU_DEBUG_9()
 */

/** Log message of socket error @errcode at level 0. */
73 74 75 76 77
void su_perror2(const char *s, int errcode)
{
  su_log("%s: %s\n", s, su_strerror(errcode));
}

78
/** Log socket error message at level 0. */
79 80 81 82 83
void su_perror(const char *s)
{
  su_perror2(s, su_errno());
}

Pekka Pessi's avatar
Pekka Pessi committed
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
/** Log a message to default log. 
 *
 * This function is a replacement for printf().
 *
 * Messages are always logged to the default log.
 */
void su_log(char const *fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  su_vllog(su_log_default, 0, fmt, ap);
  va_end(ap);
}

/** Log a message with level. 
 *
 * @note This function is used mainly by SU_DEBUG_n() macros.
 */
void su_llog(su_log_t *log, unsigned level, char const *fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  su_vllog(log, level, fmt, ap);
  va_end(ap);
}

/** Log a message with level (stdarg version). */
void su_vllog(su_log_t *log, unsigned level, char const *fmt, va_list ap)
{
  su_logger_f *logger;
  void *stream;

  assert(log);

  if (!log->log_init)
    su_log_init(log);

  if (log->log_init > 1 ? 
      level > log->log_level :
      level > su_log_default->log_level)
    return;

  logger = log->log_logger;
  stream = log->log_stream;

  if (!logger) {
    logger = su_log_default->log_logger;
    stream = su_log_default->log_stream;
  }

  if (logger)
    logger(stream, fmt, ap);
}

static char const not_initialized[1];
static char const *explicitly_initialized = not_initialized;

/** Initialize a log */
void su_log_init(su_log_t *log)
{
  char *env;

  if (log->log_init)
    return;

  if (explicitly_initialized == not_initialized)
    explicitly_initialized = getenv("SHOW_DEBUG_LEVELS");

  if (log != su_log_default && !su_log_default->log_init)
    su_log_init(su_log_default);

  if (log->log_env && (env = getenv(log->log_env))) {
    int level = atoi(env);
    
    /* Why? */
    /* if (level == 0) level = 1; */
    log->log_level = level;
    log->log_init = 2;

    if (explicitly_initialized) 
      su_llog(log, 0, "%s: initialized log to level %u (%s=%s)\n", 
	      log->log_name, log->log_level, log->log_env, env);
  }
  else {
    log->log_level = log->log_default;
    log->log_init = 1;
    if (explicitly_initialized) {
      if (log != su_log_default)
	su_llog(log, 0, "%s: logging at default level %u\n", 
		log->log_name, su_log_default->log_level);
      else
	su_llog(log, 0, "%s: initialized log to level %u (default)\n", 
		log->log_name, log->log_level);
    }
  }
}

/**Redirect a log.
 *
 * The function su_log_redirect() redirects the su_log() output to
 * @a logger function. The @a logger function has following prototype:
 *
 * @code
 * void logger(void *logarg, char const *format, va_list ap);
 * @endcode
 * 
 * If @a logger is NULL, the default logger will be used. If @a log is NULL,
 * the default logger is changed.
 */
void su_log_redirect(su_log_t *log, su_logger_f *logger, void *logarg)
{
  if (log == NULL)
    log = su_log_default;
  /* XXX - locking ? */
  log->log_logger = logger;
  log->log_stream = logarg;
}

/** Set log level.
 *
 * The function su_log_set_level() sets the logging level.  The log events
 * have certain level (0..9); if logging level is lower than the level of
 * the event, the log message is ignored.
 * 
 * If @a log is NULL, the default log level is changed.
 */
void su_log_set_level(su_log_t *log, unsigned level)
{
  if (log == NULL)
    log = su_log_default;

  log->log_level = level;
  log->log_init = 2;

  if (explicitly_initialized == not_initialized)
    explicitly_initialized = getenv("SHOW_DEBUG_LEVELS");

  if (explicitly_initialized)
    su_llog(log, 0, "%s: set log to level %u\n", 
	    log->log_name, log->log_level);
}

/** Set log level.
 *
 * The function su_log_soft_set_level() sets the logging level if it is not
 * already set, or the environment variable controlling the log level is not
 * set. 
 *
 * The log events have certain level (0..9); if logging level is lower than
 * the level of the event, the log message is ignored.
 * 
 * If @a log is NULL, the default log level is changed.
 */
void su_log_soft_set_level(su_log_t *log, unsigned level)
{
  if (log == NULL)
    log = su_log_default;
243
  if (log->log_init == 1)
Pekka Pessi's avatar
Pekka Pessi committed
244
    return;
245

Pekka Pessi's avatar
Pekka Pessi committed
246 247
  if (log->log_env && getenv(log->log_env)) {
    su_log_init(log);
248 249
  }
  else {
Pekka Pessi's avatar
Pekka Pessi committed
250 251
    log->log_level = level;
    log->log_init = 2;
252 253 254 255

    if (explicitly_initialized == not_initialized)
      explicitly_initialized = getenv("SHOW_DEBUG_LEVELS");

Pekka Pessi's avatar
Pekka Pessi committed
256 257 258 259 260
    if (explicitly_initialized)
      su_llog(log, 0, "%s: soft set log to level %u\n", 
	      log->log_name, log->log_level);
  }
}