implement view switching from world tree
[ladish.git] / daemon / log.c
blob23f8aef1bd40967bd14984b026a8905041da6186
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 <stdlib.h>
29 #include <stdarg.h>
30 #include <stdbool.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <time.h>
38 #include "../common/debug.h"
39 #include "catdup.h"
41 #define DEFAULT_XDG_LOG "/.log"
42 #define LASH_XDG_SUBDIR "/lash"
43 #define LASH_XDG_LOG "/lash.log"
45 FILE * g_logfile;
47 bool
48 ensure_dir_exist(
49 const char * dirname,
50 int mode)
52 struct stat st;
53 if (stat(dirname, &st) != 0)
55 if (errno == ENOENT)
57 lash_info("Directory \"%s\" does not exist. Creating...", dirname);
58 if (mkdir(dirname, mode) != 0)
60 lash_error("Failed to create \"%s\" directory: %d (%s)", dirname, errno, strerror(errno));
61 return false;
64 else
66 lash_error("Failed to stat \"%s\": %d (%s)", dirname, errno, strerror(errno));
67 return false;
70 else
72 if (!S_ISDIR(st.st_mode))
74 lash_error("\"%s\" exists but is not directory.", dirname);
75 return false;
79 return true;
82 void lash_log_init() __attribute__ ((constructor));
83 void lash_log_init()
85 char * log_filename;
86 size_t log_len;
87 char * lash_log_dir;
88 size_t lash_log_dir_len; /* without terminating '\0' char */
89 const char * home_dir;
90 char * xdg_log_home;
92 home_dir = getenv("HOME");
93 if (home_dir == NULL)
95 lash_error("Environment variable HOME not set");
96 goto exit;
99 xdg_log_home = catdup(home_dir, DEFAULT_XDG_LOG);
100 if (xdg_log_home == NULL)
102 lash_error("catdup failed for '%s' and '%s'", home_dir, DEFAULT_XDG_LOG);
103 goto exit;
106 lash_log_dir = catdup(xdg_log_home, LASH_XDG_SUBDIR);
107 if (lash_log_dir == NULL)
109 lash_error("catdup failed for '%s' and '%s'", home_dir, LASH_XDG_SUBDIR);
110 goto free_log_home;
113 if (!ensure_dir_exist(xdg_log_home, 0700))
115 goto free_log_dir;
118 if (!ensure_dir_exist(lash_log_dir, 0700))
120 goto free_log_dir;
123 lash_log_dir_len = strlen(lash_log_dir);
125 log_len = strlen(LASH_XDG_LOG);
127 log_filename = malloc(lash_log_dir_len + log_len + 1);
128 if (log_filename == NULL)
130 lash_error("Out of memory");
131 goto free_log_dir;
134 memcpy(log_filename, lash_log_dir, lash_log_dir_len);
135 memcpy(log_filename + lash_log_dir_len, LASH_XDG_LOG, log_len);
136 log_filename[lash_log_dir_len + log_len] = 0;
138 g_logfile = fopen(log_filename, "a");
139 if (g_logfile == NULL)
141 lash_error("Cannot open jackdbus log file \"%s\": %d (%s)\n", log_filename, errno, strerror(errno));
144 free(log_filename);
146 free_log_dir:
147 free(lash_log_dir);
149 free_log_home:
150 free(xdg_log_home);
152 exit:
153 return;
156 void lash_log_uninit() __attribute__ ((destructor));
157 void lash_log_uninit()
159 if (g_logfile != NULL)
161 fclose(g_logfile);
165 void
166 lash_log(
167 unsigned int level,
168 const char * format,
169 ...)
171 va_list ap;
172 FILE * stream;
173 time_t timestamp;
174 char timestamp_str[26];
176 if (g_logfile != NULL)
178 stream = g_logfile;
180 else
182 switch (level)
184 case LASH_LOG_LEVEL_DEBUG:
185 case LASH_LOG_LEVEL_INFO:
186 stream = stdout;
187 break;
188 case LASH_LOG_LEVEL_WARN:
189 case LASH_LOG_LEVEL_ERROR:
190 case LASH_LOG_LEVEL_ERROR_PLAIN:
191 default:
192 stream = stderr;
196 time(&timestamp);
197 ctime_r(&timestamp, timestamp_str);
198 timestamp_str[24] = 0;
200 fprintf(stream, "%s: ", timestamp_str);
202 va_start(ap, format);
203 vfprintf(stream, format, ap);
204 fflush(stream);
205 va_end(ap);