libisdn
asn1_log.c
Go to the documentation of this file.
00001 /*
00002  *
00003  */
00004 #ifdef HAVE_CONFIG_H
00005 #include "config.h"
00006 #endif
00007 
00008 #include <stdio.h>
00009 #include <stdarg.h>
00010 #include <strings.h>
00011 
00012 #include "asn1.h"
00013 #include "asn1_common.h"
00014 #include "asn1_log.h"
00015 
00016 #ifdef DEBUG
00017 static int asn1_loglevel = ASN1_LOG_DEBUG_TRACE;
00018 #else
00019 static int asn1_loglevel = ASN1_LOG_NOTICE;
00020 #endif
00021 
00022 static struct {
00023         const asn1_loglevel_t id;
00024         const char *name;
00025 } asn1_loglevels[] = {
00026         { ASN1_LOG_NONE,    "none" },
00027         { ASN1_LOG_CRIT,    "critical" },
00028         { ASN1_LOG_ERROR,   "error" },
00029         { ASN1_LOG_WARNING, "warning" },
00030         { ASN1_LOG_NOTICE,  "notice" },
00031         { ASN1_LOG_INFO,    "info" },
00032         { ASN1_LOG_DEBUG,   "debug" },
00033         { ASN1_LOG_TRACE,   "trace" }
00034 };
00035 
00036 const char *asn1_loglevel_name(const asn1_loglevel_t id)
00037 {
00038         for (int i = 0; i < ARRAY_SIZE(asn1_loglevels); i++) {
00039                 if (asn1_loglevels[i].id == id)
00040                         return asn1_loglevels[i].name;
00041         }
00042         return NULL;
00043 }
00044 
00045 int asn1_set_loglevel(const asn1_loglevel_t level)
00046 {
00047         if (level < ASN1_LOG_NONE || level >= ASN1_LOG_MAX)
00048                 return -1;
00049 
00050         asn1_loglevel = level;
00051         return 0;
00052 }
00053 
00054 int asn1_set_loglevel_by_name(const char *name)
00055 {
00056         for (int i = 0; i < ARRAY_SIZE(asn1_loglevels); i++) {
00057                 if (!strcasecmp(name, asn1_loglevels[i].name)) {
00058                         asn1_loglevel = asn1_loglevels[i].id;
00059                         return 0;
00060                 }
00061         }
00062         return -1;
00063 }
00064 
00065 asn1_loglevel_t asn1_get_loglevel(void)
00066 {
00067         return asn1_loglevel;
00068 }
00069 
00070 const char *asn1_get_loglevel_name(void)
00071 {
00072         return asn1_loglevel_name(asn1_loglevel);
00073 }
00074 
00075 static inline void _asn1_log(const int level, const char *fmt, va_list ap)
00076 {
00077         if (level > asn1_loglevel)
00078                 return;
00079         vfprintf(stderr, fmt, ap);
00080 }
00081 
00082 void asn1_log(const int level, const char *fmt, ...)
00083 {
00084         va_list ap;
00085         va_start(ap, fmt);
00086         _asn1_log(level, fmt, ap);
00087         va_end(ap);
00088 }
00089 
00090 void asn1_error(const char *fmt, ...)
00091 {
00092         va_list ap;
00093         va_start(ap, fmt);
00094         _asn1_log(ASN1_LOG_ERROR, fmt, ap);
00095         va_end(ap);
00096 }
00097 
00098 void asn1_info(const char *fmt, ...)
00099 {
00100         va_list ap;
00101         va_start(ap, fmt);
00102         _asn1_log(ASN1_LOG_INFO, fmt, ap);
00103         va_end(ap);
00104 }
00105 
00106 void asn1_debug(const char *fmt, ...)
00107 {
00108         va_list ap;
00109         va_start(ap, fmt);
00110         _asn1_log(ASN1_LOG_DEBUG, fmt, ap);
00111         va_end(ap);
00112 }
00113 
00114 void asn1_trace(const char *fmt, ...)
00115 {
00116         va_list ap;
00117         va_start(ap, fmt);
00118         _asn1_log(ASN1_LOG_TRACE, fmt, ap);
00119         va_end(ap);
00120 }