update laditools submodule
[ladish.git] / daemon / log.c
blob98fc9b38a13a5c05e0cc340ccff744040299d058
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009 Nedko Arnaudov <nedko@arnaudov.name>
6 * Copyright (C) 2008 Marc-Olivier Barre
8 **************************************************************************
9 * This file contains code that implements logging functionality
10 **************************************************************************
12 * LADI Session Handler is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * LADI Session Handler is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
24 * or write to the Free Software Foundation, Inc.,
25 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "../common.h"
30 #include <errno.h>
31 #include <time.h>
32 #include <stdarg.h>
34 #include "../catdup.h"
35 #include "dirhelpers.h"
37 #define DEFAULT_XDG_LOG "/.log"
38 #define LADISH_XDG_SUBDIR "/" BASE_NAME
39 #define LADISH_XDG_LOG "/" BASE_NAME ".log"
41 FILE * g_logfile;
43 void ladish_log_init() __attribute__ ((constructor));
44 void ladish_log_init()
46 char * log_filename;
47 size_t log_len;
48 char * ladish_log_dir;
49 size_t ladish_log_dir_len; /* without terminating '\0' char */
50 const char * home_dir;
51 char * xdg_log_home;
53 home_dir = getenv("HOME");
54 if (home_dir == NULL)
56 log_error("Environment variable HOME not set");
57 goto exit;
60 xdg_log_home = catdup(home_dir, DEFAULT_XDG_LOG);
61 if (xdg_log_home == NULL)
63 log_error("catdup failed for '%s' and '%s'", home_dir, DEFAULT_XDG_LOG);
64 goto exit;
67 ladish_log_dir = catdup(xdg_log_home, LADISH_XDG_SUBDIR);
68 if (ladish_log_dir == NULL)
70 log_error("catdup failed for '%s' and '%s'", home_dir, LADISH_XDG_SUBDIR);
71 goto free_log_home;
74 if (!ensure_dir_exist(xdg_log_home, 0700))
76 goto free_log_dir;
79 if (!ensure_dir_exist(ladish_log_dir, 0700))
81 goto free_log_dir;
84 ladish_log_dir_len = strlen(ladish_log_dir);
86 log_len = strlen(LADISH_XDG_LOG);
88 log_filename = malloc(ladish_log_dir_len + log_len + 1);
89 if (log_filename == NULL)
91 log_error("Out of memory");
92 goto free_log_dir;
95 memcpy(log_filename, ladish_log_dir, ladish_log_dir_len);
96 memcpy(log_filename + ladish_log_dir_len, LADISH_XDG_LOG, log_len);
97 log_filename[ladish_log_dir_len + log_len] = 0;
99 g_logfile = fopen(log_filename, "a");
100 if (g_logfile == NULL)
102 log_error("Cannot open jackdbus log file \"%s\": %d (%s)\n", log_filename, errno, strerror(errno));
105 free(log_filename);
107 free_log_dir:
108 free(ladish_log_dir);
110 free_log_home:
111 free(xdg_log_home);
113 exit:
114 return;
117 void ladish_log_uninit() __attribute__ ((destructor));
118 void ladish_log_uninit()
120 if (g_logfile != NULL)
122 fclose(g_logfile);
126 void
127 ladish_log(
128 unsigned int level,
129 const char * format,
130 ...)
132 va_list ap;
133 FILE * stream;
134 time_t timestamp;
135 char timestamp_str[26];
137 if (g_logfile != NULL)
139 stream = g_logfile;
141 else
143 switch (level)
145 case LADISH_LOG_LEVEL_DEBUG:
146 case LADISH_LOG_LEVEL_INFO:
147 stream = stdout;
148 break;
149 case LADISH_LOG_LEVEL_WARN:
150 case LADISH_LOG_LEVEL_ERROR:
151 case LADISH_LOG_LEVEL_ERROR_PLAIN:
152 default:
153 stream = stderr;
157 time(&timestamp);
158 ctime_r(&timestamp, timestamp_str);
159 timestamp_str[24] = 0;
161 fprintf(stream, "%s: ", timestamp_str);
163 va_start(ap, format);
164 vfprintf(stream, format, ap);
165 fflush(stream);
166 va_end(ap);