gladish: rename "Run..." menu entries to "New Application..."
[ladish.git] / daemon / log.c
blob38daa845a6f66ca3d0b02af164bb17994bdc649e
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009, 2010 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>
33 #include <sys/stat.h>
35 #include "../common/catdup.h"
36 #include "../common/dirhelpers.h"
38 #define DEFAULT_XDG_LOG "/.log"
39 #define LADISH_XDG_SUBDIR "/" BASE_NAME
40 #define LADISH_XDG_LOG "/" BASE_NAME ".log"
42 static ino_t g_log_file_ino;
43 static FILE * g_logfile;
44 static char * g_log_filename;
46 static bool ladish_log_open(void)
48 struct stat st;
49 int ret;
50 int retry;
52 if (g_logfile != NULL)
54 ret = stat(g_log_filename, &st);
55 if (ret != 0 || g_log_file_ino != st.st_ino)
57 fclose(g_logfile);
59 else
61 return true;
65 for (retry = 0; retry < 10; retry++)
67 g_logfile = fopen(g_log_filename, "a");
68 if (g_logfile == NULL)
70 fprintf(stderr, "Cannot open ladishd log file \"%s\": %d (%s)\n", g_log_filename, errno, strerror(errno));
71 return false;
74 ret = stat(g_log_filename, &st);
75 if (ret == 0)
77 g_log_file_ino = st.st_ino;
78 return true;
81 fclose(g_logfile);
82 g_logfile = NULL;
85 fprintf(stderr, "Cannot stat just opened ladishd log file \"%s\": %d (%s). %d retries\n", g_log_filename, errno, strerror(errno), retry);
86 return false;
89 void ladish_log_init() __attribute__ ((constructor));
90 void ladish_log_init()
92 char * ladish_log_dir;
93 const char * home_dir;
94 char * xdg_log_home;
96 home_dir = getenv("HOME");
97 if (home_dir == NULL)
99 log_error("Environment variable HOME not set");
100 goto exit;
103 xdg_log_home = catdup(home_dir, DEFAULT_XDG_LOG);
104 if (xdg_log_home == NULL)
106 log_error("catdup failed for '%s' and '%s'", home_dir, DEFAULT_XDG_LOG);
107 goto exit;
110 ladish_log_dir = catdup(xdg_log_home, LADISH_XDG_SUBDIR);
111 if (ladish_log_dir == NULL)
113 log_error("catdup failed for '%s' and '%s'", home_dir, LADISH_XDG_SUBDIR);
114 goto free_log_home;
117 if (!ensure_dir_exist(xdg_log_home, 0700))
119 goto free_log_dir;
122 if (!ensure_dir_exist(ladish_log_dir, 0700))
124 goto free_log_dir;
127 g_log_filename = catdup(ladish_log_dir, LADISH_XDG_LOG);
128 if (g_log_filename == NULL)
130 log_error("Out of memory");
131 goto free_log_dir;
134 ladish_log_open();
136 free_log_dir:
137 free(ladish_log_dir);
139 free_log_home:
140 free(xdg_log_home);
142 exit:
143 return;
146 void ladish_log_uninit() __attribute__ ((destructor));
147 void ladish_log_uninit()
149 if (g_logfile != NULL)
151 fclose(g_logfile);
154 free(g_log_filename);
157 void
158 ladish_log(
159 unsigned int level,
160 const char * format,
161 ...)
163 va_list ap;
164 FILE * stream;
165 time_t timestamp;
166 char timestamp_str[26];
168 if (g_logfile != NULL && ladish_log_open())
170 stream = g_logfile;
172 else
174 switch (level)
176 case LADISH_LOG_LEVEL_DEBUG:
177 case LADISH_LOG_LEVEL_INFO:
178 stream = stdout;
179 break;
180 case LADISH_LOG_LEVEL_WARN:
181 case LADISH_LOG_LEVEL_ERROR:
182 case LADISH_LOG_LEVEL_ERROR_PLAIN:
183 default:
184 stream = stderr;
188 time(&timestamp);
189 ctime_r(&timestamp, timestamp_str);
190 timestamp_str[24] = 0;
192 fprintf(stream, "%s: ", timestamp_str);
194 va_start(ap, format);
195 vfprintf(stream, format, ap);
196 fflush(stream);
197 va_end(ap);