xref: /openbmc/ipmitool/lib/log.c (revision c18ec02f)
1 /*
2  * Copyright (c) 2003 Sun Microsystems, Inc.  All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * Redistribution of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * Redistribution in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * Neither the name of Sun Microsystems, Inc. or the names of
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * This software is provided "AS IS," without a warranty of any kind.
20  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
21  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
22  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
23  * SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE
24  * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
25  * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.  IN NO EVENT WILL
26  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
27  * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
28  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
29  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  */
32 
33 #include <unistd.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <syslog.h>
37 #include <errno.h>
38 #include <stdarg.h>
39 #include <string.h>
40 
41 #include <ipmitool/log.h>
42 
43 struct logpriv_s {
44 	char * name;
45 	int daemon;
46 	int level;
47 };
48 struct logpriv_s *logpriv;
49 
log_reinit(void)50 static void log_reinit(void)
51 {
52 	log_init(NULL, 0, 0);
53 }
54 
lprintf(int level,const char * format,...)55 void lprintf(int level, const char * format, ...)
56 {
57 	static char logmsg[LOG_MSG_LENGTH];
58 	va_list vptr;
59 
60 	if (!logpriv)
61 		log_reinit();
62 
63 	if (logpriv->level < level)
64 		return;
65 
66 	va_start(vptr, format);
67 	vsnprintf(logmsg, LOG_MSG_LENGTH, format, vptr);
68 	va_end(vptr);
69 
70 	if (logpriv->daemon)
71 		syslog(level, "%s", logmsg);
72 	else
73 		fprintf(stderr, "%s\n", logmsg);
74 	return;
75 }
76 
lperror(int level,const char * format,...)77 void lperror(int level, const char * format, ...)
78 {
79 	static char logmsg[LOG_MSG_LENGTH];
80 	va_list vptr;
81 
82 	if (!logpriv)
83 		log_reinit();
84 
85 	if (logpriv->level < level)
86 		return;
87 
88 	va_start(vptr, format);
89 	vsnprintf(logmsg, LOG_MSG_LENGTH, format, vptr);
90 	va_end(vptr);
91 
92 	if (logpriv->daemon)
93 		syslog(level, "%s: %s", logmsg, strerror(errno));
94 	else
95 		fprintf(stderr, "%s: %s\n", logmsg, strerror(errno));
96 	return;
97 }
98 
99 /*
100  * open connection to syslog if daemon
101  */
log_init(const char * name,int isdaemon,int verbose)102 void log_init(const char * name, int isdaemon, int verbose)
103 {
104 	if (logpriv)
105 		return;
106 
107 	logpriv = malloc(sizeof(struct logpriv_s));
108 	if (!logpriv)
109 		return;
110 
111 	if (name != NULL)
112 		logpriv->name = strdup(name);
113 	else
114 		logpriv->name = strdup(LOG_NAME_DEFAULT);
115 
116 	if (logpriv->name == NULL)
117 		fprintf(stderr, "ipmitool: malloc failure\n");
118 
119 	logpriv->daemon = isdaemon;
120 	logpriv->level = verbose + LOG_NOTICE;
121 
122 	if (logpriv->daemon)
123 		openlog(logpriv->name, LOG_CONS, LOG_LOCAL4);
124 }
125 
126 /*
127  * stop syslog logging if daemon mode,
128  * free used memory that stored log service
129  */
log_halt(void)130 void log_halt(void)
131 {
132 	if (!logpriv)
133 		return;
134 
135 	if (logpriv->name) {
136 		free(logpriv->name);
137 		logpriv->name = NULL;
138 	}
139 
140 	if (logpriv->daemon)
141 		closelog();
142 
143 	free(logpriv);
144 	logpriv = NULL;
145 }
146 
log_level_get(void)147 int log_level_get(void)
148 {
149 	return logpriv->level;
150 }
151 
log_level_set(int level)152 void log_level_set(int level)
153 {
154 	logpriv->level = level;
155 }
156 
157