Add dl.ladish.org hyperlink for tarballs
[ladish.git] / common / log.c
blob593aadfa7a711aa005764f4e97a25adbbff728dd
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009, 2010, 2012 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 "config.h"
29 #include "../common.h"
31 #include <errno.h>
32 #include <time.h>
33 #include <stdarg.h>
34 #include <sys/stat.h>
36 #include "../common/catdup.h"
37 #include "../common/dirhelpers.h"
39 #define DEFAULT_XDG_LOG "/.log"
40 #define LADISH_XDG_SUBDIR "/" BASE_NAME
41 #define LADISH_XDG_LOG "/" BASE_NAME ".log"
43 #if !defined(LOG_OUTPUT_STDOUT)
44 static ino_t g_log_file_ino;
45 static FILE * g_logfile;
46 static char * g_log_filename;
48 static bool ladish_log_open(void)
50 struct stat st;
51 int ret;
52 int retry;
54 if (g_logfile != NULL)
56 ret = stat(g_log_filename, &st);
57 if (ret != 0 || g_log_file_ino != st.st_ino)
59 fclose(g_logfile);
61 else
63 return true;
67 for (retry = 0; retry < 10; retry++)
69 g_logfile = fopen(g_log_filename, "a");
70 if (g_logfile == NULL)
72 fprintf(stderr, "Cannot open ladishd log file \"%s\": %d (%s)\n", g_log_filename, errno, strerror(errno));
73 return false;
76 ret = stat(g_log_filename, &st);
77 if (ret == 0)
79 g_log_file_ino = st.st_ino;
80 return true;
83 fclose(g_logfile);
84 g_logfile = NULL;
87 fprintf(stderr, "Cannot stat just opened ladishd log file \"%s\": %d (%s). %d retries\n", g_log_filename, errno, strerror(errno), retry);
88 return false;
91 void ladish_log_init() __attribute__ ((constructor));
92 void ladish_log_init()
94 char * ladish_log_dir;
95 const char * home_dir;
96 char * xdg_log_home;
98 home_dir = getenv("HOME");
99 if (home_dir == NULL)
101 log_error("Environment variable HOME not set");
102 goto exit;
105 xdg_log_home = catdup(home_dir, DEFAULT_XDG_LOG);
106 if (xdg_log_home == NULL)
108 log_error("catdup failed for '%s' and '%s'", home_dir, DEFAULT_XDG_LOG);
109 goto exit;
112 ladish_log_dir = catdup(xdg_log_home, LADISH_XDG_SUBDIR);
113 if (ladish_log_dir == NULL)
115 log_error("catdup failed for '%s' and '%s'", home_dir, LADISH_XDG_SUBDIR);
116 goto free_log_home;
119 if (!ensure_dir_exist(xdg_log_home, 0700))
121 goto free_log_dir;
124 if (!ensure_dir_exist(ladish_log_dir, 0700))
126 goto free_log_dir;
129 g_log_filename = catdup(ladish_log_dir, LADISH_XDG_LOG);
130 if (g_log_filename == NULL)
132 log_error("Out of memory");
133 goto free_log_dir;
136 ladish_log_open();
137 cdbus_log_setup(ladish_log);
139 free_log_dir:
140 free(ladish_log_dir);
142 free_log_home:
143 free(xdg_log_home);
145 exit:
146 return;
149 void ladish_log_uninit() __attribute__ ((destructor));
150 void ladish_log_uninit()
152 if (g_logfile != NULL)
154 fclose(g_logfile);
157 free(g_log_filename);
159 #else
160 void ladish_log_init() __attribute__ ((constructor));
161 void ladish_log_init()
163 cdbus_log_setup(ladish_log);
165 #endif /* #if !defined(LOG_OUTPUT_STDOUT) */
167 #if 0
168 # define log_debug(fmt, args...) ladish_log(CDBUS_LOG_LEVEL_DEBUG, "%s:%d:%s: " fmt "\n", __FILE__, __LINE__, __func__, ## args)
169 # define log_info(fmt, args...) ladish_log(CDBUS_LOG_LEVEL_INFO, fmt "\n", ## args)
170 # define log_warn(fmt, args...) ladish_log(CDBUS_LOG_LEVEL_WARN, ANSI_COLOR_YELLOW "WARNING: " ANSI_RESET "%s: " fmt "\n", __func__, ## args)
171 # define log_error(fmt, args...) ladish_log(CDBUS_LOG_LEVEL_ERROR, ANSI_COLOR_RED "ERROR: " ANSI_RESET "%s: " fmt "\n", __func__, ## args)
172 # define log_error_plain(fmt, args...) ladish_log(CDBUS_LOG_LEVEL_ERROR_PLAIN, ANSI_COLOR_RED "ERROR: " ANSI_RESET fmt "\n", ## args)
173 #endif
175 static
176 bool
177 ladish_log_enabled(
178 unsigned int level,
179 const char * UNUSED(file),
180 unsigned int UNUSED(line),
181 const char * UNUSED(func))
183 return level != CDBUS_LOG_LEVEL_DEBUG;
186 void
187 ladish_log(
188 unsigned int level,
189 const char * file,
190 unsigned int line,
191 const char * func,
192 const char * format,
193 ...)
195 va_list ap;
196 FILE * stream;
197 #if !defined(LOG_OUTPUT_STDOUT)
198 time_t timestamp;
199 char timestamp_str[26];
200 #endif
201 const char * color;
203 if (!ladish_log_enabled(level, file, line, func))
205 return;
208 #if !defined(LOG_OUTPUT_STDOUT)
209 if (g_logfile != NULL && ladish_log_open())
211 stream = g_logfile;
213 else
214 #endif
216 switch (level)
218 case CDBUS_LOG_LEVEL_DEBUG:
219 case CDBUS_LOG_LEVEL_INFO:
220 stream = stdout;
221 break;
222 case CDBUS_LOG_LEVEL_WARN:
223 case CDBUS_LOG_LEVEL_ERROR:
224 case CDBUS_LOG_LEVEL_ERROR_PLAIN:
225 default:
226 stream = stderr;
230 #if !defined(LOG_OUTPUT_STDOUT)
231 time(&timestamp);
232 ctime_r(&timestamp, timestamp_str);
233 timestamp_str[24] = 0;
235 fprintf(stream, "%s: ", timestamp_str);
236 #endif
238 color = NULL;
239 switch (level)
241 case CDBUS_LOG_LEVEL_DEBUG:
242 fprintf(stream, "%s:%d:%s ", file, line, func);
243 break;
244 case CDBUS_LOG_LEVEL_WARN:
245 color = ANSI_COLOR_YELLOW;
246 break;
247 case CDBUS_LOG_LEVEL_ERROR:
248 case CDBUS_LOG_LEVEL_ERROR_PLAIN:
249 color = ANSI_COLOR_RED;
250 break;
253 if (color != NULL)
255 fputs(color, stream);
258 va_start(ap, format);
259 vfprintf(stream, format, ap);
260 va_end(ap);
262 if (color != NULL)
264 fputs(ANSI_RESET, stream);
267 fputs("\n", stream);
269 fflush(stream);