File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / istgt / src / istgt_log.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Sun Jul 21 23:49:22 2013 UTC (11 years, 8 months ago) by misho
Branches: istgt, MAIN
CVS tags: v20121028, HEAD
20121028

    1: /*
    2:  * Copyright (C) 2008-2010 Daisuke Aoyama <aoyama@peach.ne.jp>.
    3:  * All rights reserved.
    4:  *
    5:  * Redistribution and use in source and binary forms, with or without
    6:  * modification, are permitted provided that the following conditions
    7:  * are met:
    8:  * 1. Redistributions of source code must retain the above copyright
    9:  *    notice, this list of conditions and the following disclaimer.
   10:  * 2. Redistributions in binary form must reproduce the above copyright
   11:  *    notice, this list of conditions and the following disclaimer in the
   12:  *    documentation and/or other materials provided with the distribution.
   13:  *
   14:  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17:  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
   18:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24:  * SUCH DAMAGE.
   25:  *
   26:  */
   27: 
   28: #ifdef HAVE_CONFIG_H
   29: #include "config.h"
   30: #endif
   31: 
   32: #include <stdarg.h>
   33: #include <stdio.h>
   34: #include <string.h>
   35: #include <syslog.h>
   36: 
   37: #include "istgt.h"
   38: #include "istgt_log.h"
   39: #include "istgt_misc.h"
   40: 
   41: //static int g_trace_flag = 0;
   42: int g_trace_flag = 0;
   43: int g_warn_flag = 1;
   44: static int g_log_facility = ISTGT_LOG_FACILITY;
   45: static int g_log_priority = ISTGT_LOG_PRIORITY;
   46: 
   47: int
   48: istgt_set_log_facility(const char *facility)
   49: {
   50: 	if (strcasecmp(facility, "daemon") == 0) {
   51: 		g_log_facility = LOG_DAEMON;
   52: 	} else if (strcasecmp(facility, "auth") == 0) {
   53: 		g_log_facility = LOG_AUTH;
   54: 	} else if (strcasecmp(facility, "authpriv") == 0) {
   55: 		g_log_facility = LOG_AUTHPRIV;
   56: 	} else if (strcasecmp(facility, "local1") == 0) {
   57: 		g_log_facility = LOG_LOCAL1;
   58: 	} else if (strcasecmp(facility, "local2") == 0) {
   59: 		g_log_facility = LOG_LOCAL2;
   60: 	} else if (strcasecmp(facility, "local3") == 0) {
   61: 		g_log_facility = LOG_LOCAL3;
   62: 	} else if (strcasecmp(facility, "local4") == 0) {
   63: 		g_log_facility = LOG_LOCAL4;
   64: 	} else if (strcasecmp(facility, "local5") == 0) {
   65: 		g_log_facility = LOG_LOCAL5;
   66: 	} else if (strcasecmp(facility, "local6") == 0) {
   67: 		g_log_facility = LOG_LOCAL6;
   68: 	} else if (strcasecmp(facility, "local7") == 0) {
   69: 		g_log_facility = LOG_LOCAL7;
   70: 	} else {
   71: 		g_log_facility = ISTGT_LOG_FACILITY;
   72: 		return -1;
   73: 	}
   74: 	return 0;
   75: }
   76: 
   77: int
   78: istgt_set_log_priority(const char *priority)
   79: {
   80: 	if (strcasecmp(priority, "emerg") == 0) {
   81: 		g_log_priority = LOG_EMERG;
   82: 	} else if (strcasecmp(priority, "alert") == 0) {
   83: 		g_log_priority = LOG_ALERT;
   84: 	} else if (strcasecmp(priority, "crit") == 0) {
   85: 		g_log_priority = LOG_CRIT;
   86: 	} else if (strcasecmp(priority, "err") == 0) {
   87: 		g_log_priority = LOG_ERR;
   88: 	} else if (strcasecmp(priority, "warning") == 0) {
   89: 		g_log_priority = LOG_WARNING;
   90: 	} else if (strcasecmp(priority, "notice") == 0) {
   91: 		g_log_priority = LOG_NOTICE;
   92: 	} else if (strcasecmp(priority, "info") == 0) {
   93: 		g_log_priority = LOG_INFO;
   94: 	} else if (strcasecmp(priority, "debug") == 0) {
   95: 		g_log_priority = LOG_DEBUG;
   96: 	} else {
   97: 		g_log_priority = ISTGT_LOG_PRIORITY;
   98: 		return -1;
   99: 	}
  100: 	return 0;
  101: }
  102: 
  103: void
  104: istgt_log(const char *file, const int line, const char *func, const char *format, ...)
  105: {
  106: 	char buf[MAX_TMPBUF];
  107: 	va_list ap;
  108: 
  109: 	va_start(ap, format);
  110: 	vsnprintf(buf, sizeof buf, format, ap);
  111: 	if (file != NULL) {
  112: 		if (func != NULL) {
  113: 			fprintf(stderr, "%s:%4d:%s: %s", file, line, func, buf);
  114: 			syslog(g_log_priority, "%s:%4d:%s: %s", file, line, func, buf);
  115: 		} else {
  116: 			fprintf(stderr, "%s:%4d: %s", file, line, buf);
  117: 			syslog(g_log_priority, "%s:%4d: %s", file, line, buf);
  118: 		}
  119: 	} else {
  120: 		fprintf(stderr, "%s", buf);
  121: 		syslog(g_log_priority, "%s", buf);
  122: 	}
  123: 	va_end(ap);
  124: }
  125: 
  126: void
  127: istgt_noticelog(const char *file, const int line, const char *func, const char *format, ...)
  128: {
  129: 	char buf[MAX_TMPBUF];
  130: 	va_list ap;
  131: 
  132: 	va_start(ap, format);
  133: 	vsnprintf(buf, sizeof buf, format, ap);
  134: 	if (file != NULL) {
  135: 		if (func != NULL) {
  136: 			fprintf(stderr, "%s:%4d:%s: %s", file, line, func, buf);
  137: 			syslog(LOG_NOTICE, "%s:%4d:%s: %s", file, line, func, buf);
  138: 		} else {
  139: 			fprintf(stderr, "%s:%4d: %s", file, line, buf);
  140: 			syslog(LOG_NOTICE, "%s:%4d: %s", file, line, buf);
  141: 		}
  142: 	} else {
  143: 		fprintf(stderr, "%s", buf);
  144: 		syslog(LOG_NOTICE, "%s", buf);
  145: 	}
  146: 	va_end(ap);
  147: }
  148: 
  149: void
  150: istgt_tracelog(const int flag, const char *file, const int line, const char *func, const char *format, ...)
  151: {
  152: 	char buf[MAX_TMPBUF];
  153: 	va_list ap;
  154: 
  155: 	va_start(ap, format);
  156: 	if (g_trace_flag & flag) {
  157: 		vsnprintf(buf, sizeof buf, format, ap);
  158: 		if (func != NULL) {
  159: 			fprintf(stderr, "%s:%4d:%s: %s", file, line, func, buf);
  160: 			//syslog(LOG_INFO, "%s:%4d:%s: %s", file, line, func, buf);
  161: 		} else {
  162: 			fprintf(stderr, "%s:%4d: %s", file, line, buf);
  163: 			//syslog(LOG_INFO, "%s:%4d: %s", file, line, buf);
  164: 		}
  165: 	}
  166: 	va_end(ap);
  167: }
  168: 
  169: void
  170: istgt_errlog(const char *file, const int line, const char *func, const char *format, ...)
  171: {
  172: 	char buf[MAX_TMPBUF];
  173: 	va_list ap;
  174: 
  175: 	va_start(ap, format);
  176: 	vsnprintf(buf, sizeof buf, format, ap);
  177: 	if (func != NULL) {
  178: 		fprintf(stderr, "%s:%4d:%s: ***ERROR*** %s", file, line, func, buf);
  179: 		syslog(LOG_ERR, "%s:%4d:%s: ***ERROR*** %s", file, line, func, buf);
  180: 	} else {
  181: 		fprintf(stderr, "%s:%4d: ***ERROR*** %s", file, line, buf);
  182: 		syslog(LOG_ERR, "%s:%4d: ***ERROR*** %s", file, line, buf);
  183: 	}
  184: 	va_end(ap);
  185: }
  186: 
  187: void
  188: istgt_warnlog(const char *file, const int line, const char *func, const char *format, ...)
  189: {
  190: 	char buf[MAX_TMPBUF];
  191: 	va_list ap;
  192: 
  193: 	va_start(ap, format);
  194: 	vsnprintf(buf, sizeof buf, format, ap);
  195: 	if (func != NULL) {
  196: 		fprintf(stderr, "%s:%4d:%s: ***WARNING*** %s", file, line, func, buf);
  197: 		syslog(LOG_WARNING, "%s:%4d:%s: ***WARNING*** %s",
  198: 			   file, line, func, buf);
  199: 	} else {
  200: 		fprintf(stderr, "%s:%4d: ***WARNING*** %s", file, line, buf);
  201: 		syslog(LOG_WARNING, "%s:%4d: ***WARNING*** %s", file, line, buf);
  202: 	}
  203: 	va_end(ap);
  204: }
  205: 
  206: void
  207: istgt_open_log(void)
  208: {
  209: 	if (g_log_facility != 0) {
  210: 		openlog("istgt", LOG_PID, g_log_facility);
  211: 	} else {
  212: 		openlog("istgt", LOG_PID, ISTGT_LOG_FACILITY);
  213: 	}
  214: }
  215: 
  216: void
  217: istgt_close_log(void)
  218: {
  219: 	closelog();
  220: }
  221: 
  222: void
  223: istgtcontrol_open_log(void)
  224: {
  225: 	if (g_log_facility != 0) {
  226: 		openlog("istgtcontrol", LOG_PID, g_log_facility);
  227: 	} else {
  228: 		openlog("istgtcontrol", LOG_PID, ISTGT_LOG_FACILITY);
  229: 	}
  230: }
  231: 
  232: void
  233: istgtcontrol_close_log(void)
  234: {
  235: 	closelog();
  236: }
  237: 
  238: void
  239: istgt_set_trace_flag(int flag)
  240: {
  241: 	if (flag == ISTGT_TRACE_NONE) {
  242: 		g_trace_flag = 0;
  243: 	} else {
  244: 		g_trace_flag |= flag;
  245: 	}
  246: }
  247: 
  248: void
  249: istgt_trace_dump(int flag, const char *label, const uint8_t *buf, size_t len)
  250: {
  251: 	if (g_trace_flag & flag) {
  252: 		istgt_fdump(stderr, label, buf, len);
  253: 	}
  254: }

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