33 #include "lirc/lirc_log.h"
35 #define HOSTNAME_LEN 128
36 char hostname[HOSTNAME_LEN + 1];
41 static int use_syslog = 1;
43 const char* syslogident =
"lircd-" VERSION;
44 const char* logfile =
"syslog";
46 char progname[128] = {
'?',
'\0' };
47 static int nodaemon = 0;
49 static const int PRIO_LEN = 16;
52 static const char* prio2text(
int prio)
55 case LIRC_DEBUG:
return "Debug";
56 case LIRC_NOTICE:
return "Notice";
57 case LIRC_INFO:
return "Info";
58 case LIRC_WARNING:
return "Warning";
59 case LIRC_ERROR:
return "Error";
60 case LIRC_TRACE:
return "Trace";
61 case LIRC_TRACE1:
return "Trace1";
62 case LIRC_TRACE2:
return "Trace2";
63 default:
return "(Bad prio)";
76 if (strcmp(s,
"syslog") == 0) {
87 strncpy(progname, _progname,
sizeof(progname));
95 openlog(syslogident, LOG_CONS | LOG_PID | LOG_PERROR, LOG_LOCAL0);
97 openlog(syslogident, LOG_CONS | LOG_PID, LOG_LOCAL0);
99 lf = fopen(logfile,
"a");
101 fprintf(stderr,
"%s: could not open logfile \"%s\"\n",
106 if (getenv(
"SUDO_USER") != NULL && geteuid() == 0) {
107 user = getenv(
"SUDO_USER");
108 user = user == NULL ?
"root" : user;
110 if (chown(logfile, pw->pw_uid, pw->pw_gid) == -1)
111 perror(
"Cannot reset log file owner.");
113 gethostname(hostname, HOSTNAME_LEN);
132 int lirc_log_reopen(
void)
141 if (-1 == fstat(fileno(lf), &s)) {
142 perror(
"Invalid logfile!");
146 lf = fopen(logfile,
"a");
149 perror(
"Can't open logfile");
152 logprintf(LIRC_INFO,
"reopened logfile");
153 if (-1 == fchmod(fileno(lf), s.st_mode)) {
154 logprintf(LIRC_WARNING,
"could not set file permissions");
172 static loglevel_t symbol2loglevel(
const char* levelstring)
174 static const struct {
const char* label;
int value; } options[] = {
175 {
"TRACE2", LIRC_TRACE2 },
176 {
"TRACE1", LIRC_TRACE1 },
177 {
"TRACE", LIRC_TRACE },
178 {
"DEBUG", LIRC_DEBUG },
179 {
"INFO", LIRC_INFO },
180 {
"NOTICE", LIRC_NOTICE },
181 {
"WARNING", LIRC_WARNING },
182 {
"ERROR", LIRC_ERROR },
189 if (levelstring == NULL || !*levelstring)
190 return LIRC_BADLEVEL;
191 for (i = 0; i <
sizeof(label) && levelstring[i]; i += 1)
192 label[i] = toupper(levelstring[i]);
195 while (options[i].label && strcmp(options[i].label, label) != 0)
197 return options[i].label ? options[i].value : -1;
205 const char*
const level = getenv(
"LIRC_LOGLEVEL");
218 long level = LONG_MAX;
220 if (s == NULL || *s ==
'\0')
221 return LIRC_BADLEVEL;
222 while (isspace(*s) && *s)
225 level = strtol(s, NULL, 10);
227 return LIRC_BADLEVEL;
231 return symbol2loglevel(s);
244 int save_errno = errno;
246 char buff[PRIO_LEN + strlen(format_str)];
248 #ifdef SYSTEMD_LOGPERROR_FIX
250 fprintf(stderr,
"%s: %s ", progname, prio2text(prio));
251 va_start(ap, format_str);
252 vfprintf(stderr, format_str, ap);
259 snprintf(buff,
sizeof(buff),
260 "%s: %s", prio2text(prio), format_str);
261 va_start(ap, format_str);
262 vsyslog(prio, buff, ap);
264 }
else if (lf && prio <=
loglevel) {
268 current = time(¤t);
269 currents = ctime(¤t);
271 fprintf(lf,
"%15.15s %s %s: ",
272 currents + 4, hostname, progname);
273 fprintf(lf,
"%s: ", prio2text(prio));
274 va_start(ap, format_str);
275 vfprintf(lf, format_str, ap);
294 vsnprintf(s,
sizeof(s), fmt, ap);
298 syslog(prio,
"%s: %m\n", s);
300 syslog(prio,
"%m\n");
303 logprintf(prio,
"%s: %s", s, strerror(errno));
316 if (getenv(
"XDG_CACHE_HOME") != NULL) {
317 strncpy(buffer, getenv(
"XDG_CACHE_HOME"), size);
318 buffer[size - 1] =
'\0';
319 strncat(buffer,
"/", size - strlen(buffer) - 1);
320 }
else if (getenv(
"SUDO_USER") != NULL && geteuid() == 0) {
321 user = getenv(
"SUDO_USER");
325 snprintf(buffer, size,
"%s/.cache/", pw->pw_dir);
327 home = getenv(
"HOME");
328 home = home != NULL ? home :
"/";
329 strncpy(buffer, home, size);
330 buffer[size - 1] =
'\0';
331 strncat(buffer,
"/.cache/", size - strlen(buffer) - 1);
333 strncat(buffer, basename, size - strlen(buffer) - 1);
334 strncat(buffer,
".log", size - strlen(buffer) - 1);
339 void hexdump(
char* prefix,
unsigned char* buf,
int len)
346 if (prefix != NULL) {
347 strncpy(str, prefix,
sizeof(str));
348 pos = strnlen(str,
sizeof(str));
351 for (i = 0; i < len; i++) {
352 if (pos + 3 >=
sizeof(str))
358 sprintf(str + pos,
"%02x ", buf[i]);
363 strncpy(str + pos,
"NO DATA",
sizeof(str));
#define LIRC_MAX_LOGLEVEL
int lirc_log_open(const char *_progname, int _nodaemon, loglevel_t level)
void lirc_log_set_file(const char *s)
int lirc_log_use_syslog(void)
#define LIRC_MIN_LOGLEVEL
int lirc_log_setlevel(loglevel_t level)
#define LOGPRINTF(level, fmt, args...)
void hexdump(char *prefix, unsigned char *buf, int len)
loglevel_t lirc_log_defaultlevel(void)
void logperror(loglevel_t prio, const char *fmt,...)
int lirc_log_get_clientlog(const char *basename, char *buffer, ssize_t size)
loglevel_t string2loglevel(const char *s)
void logprintf(loglevel_t prio, const char *format_str,...)