/*= -*- c-file-style: "bsd"; indent-tabs-mode: nil; -*-
 *
 * librsync -- generate and apply network deltas
 * $Id: log.h,v 1.12 2001/06/04 12:04:19 mbp Exp $
 * 
 * Copyright (C) 2000, 2001 by Martin Pool <mbp@samba.org>
 * 
 * This program 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 2.1 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


/*
 * TODO: A function like perror that includes strerror output.  Apache
 * does this by adding flags as well as the severity level which say
 * whether such information should be included.
 */


/*
 * trace may be turned off.
 *
 * error is always on, but you can return and continue in some way
 *
 * fatal terminates the whole process
 */

typedef void (n4log_fn_t)(int, char const *, char const *);

void n4_set_log_fn(n4log_fn_t *);

void n4log_to_stderr(int, char const *, char const *);
void n4log_to_syslog(int, char const *, char const *);

void n4fatal0(char const *s, ...);
void n4log_error0(char const *s, ...);
void n4trace0(char const *s, ...);

void n4log0_nofn(int level, char const *fmt, ...);

#ifndef LOG_DOMAIN
#  define LOG_DOMAIN NULL
#endif

#if defined(__GNUC__)
#  define SCOPE __FUNCTION__
#else
#  define SCOPE __FILE__
#endif
/*
 * TODO: Also look for the C9X predefined identifier `_function', or
 * whatever it's called.
 */

#if !defined(__GNUC__)
#  define __attribute__(x)
#endif

void n4log0(int level, char const *domain,
             char const *fn, char const *fmt, ...)
     __attribute__((format(printf, 4, 5)));

#define n4trace(fmt, arg...)                                    \
    do { n4log0(N4LOG_DEBUG, LOG_DOMAIN, SCOPE, fmt , ##arg);   \
    } while (0)

#define n4log_binary(flags, _bin, _len, fmt, arg...)            \
    do {                                                        \
    char        *_hex;                                          \
    n4log0((flags), LOG_DOMAIN, SCOPE, (fmt "%s%s") , ##arg ,   \
           ((_len) ? "\n" : ""),                                \
           _hex = hextype_tostr((_bin), (_len)));               \
    free(_hex);                                                 \
    } while (0)

#define n4log(l, s, str...) do {              \
     n4log0((l), LOG_DOMAIN, SCOPE, (s) , ##str);  \
     } while (0)

#define n4log_error(s, str...) do {                       \
     n4log0(N4LOG_ERR, LOG_DOMAIN, SCOPE, (s) , ##str);  \
     } while (0)

#define n4log_crit(s, str...) do {                              \
     n4log0(N4LOG_CRIT, LOG_DOMAIN, SCOPE, (s) , ##str);        \
     } while (0)

#define n4log_warning(s, str...) do {                       \
     n4log0(N4LOG_WARNING, LOG_DOMAIN, SCOPE, (s) , ##str);  \
     } while (0)

#define n4log_info(s, str...) do {                       \
     n4log0(N4LOG_INFO, LOG_DOMAIN, SCOPE, (s) , ##str);  \
     } while (0)

#define n4log_notice(s, str...) do {                       \
     n4log0(N4LOG_NOTICE, LOG_DOMAIN, SCOPE, (s) , ##str); \
     } while (0)

#define n4fatal(s, str...) do {                \
     n4log0(N4LOG_CRIT, LOG_DOMAIN, SCOPE,     \
	      (s) , ##str);                     \
     abort();                                   \
     } while (0)

#define n4_unhandled() do { n4fatal("sorry, unhandled case at %s:%d",   \
                                     __FILE__, __LINE__); } while(0)

#define n4_outofmem() do { n4log_crit("out of memory at %s:%d",         \
                                      __FILE__, __LINE__); } while(0)

#define n4_assert(cond) if (!(cond)) {n4fatal("assertion failed: %s", #cond);}

#define n4log_elapsed(what, before, after) { \
    n4log_elapsed0(LOG_DOMAIN, SCOPE, what, before, after); }


void n4log0(int level, char const *domain,
             char const *fn, char const *fmt, ...);

void n4log_elapsed0(char const *, char const *, char const *,
                    struct timeval *, struct timeval *);

typedef enum {
    N4LOG_EMERG         = 0,
    N4LOG_ALERT         = 1,
    N4LOG_CRIT          = 2,
    N4LOG_ERR           = 3,
    N4LOG_WARNING       = 4,
    N4LOG_NOTICE        = 5,
    N4LOG_INFO          = 6,
    N4LOG_DEBUG         = 7
} n4loglevel;

enum {
    N4LOG_PRIMASK       = 7,   /**< Mask to extract priority
                                   part. \internal */

    N4LOG_NOFN          = 8    /**< \b Don't show function name in message. */
};



/**
 * \macro n4trace_enabled()
 *
 * Call this before putting too much effort into generating trace
 * messages.
 */

extern int n4trace_level;

#define n4trace_enabled() ((n4trace_level & N4LOG_PRIMASK) >= N4LOG_DEBUG)

void n4log_open_syslog(int facility);
