1 /* -*- Mode: C ; c-basic-offset: 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.
29 #include "../common.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)
54 if (g_logfile
!= NULL
)
56 ret
= stat(g_log_filename
, &st
);
57 if (ret
!= 0 || g_log_file_ino
!= st
.st_ino
)
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
));
76 ret
= stat(g_log_filename
, &st
);
79 g_log_file_ino
= st
.st_ino
;
87 fprintf(stderr
, "Cannot stat just opened ladishd log file \"%s\": %d (%s). %d retries\n", g_log_filename
, errno
, strerror(errno
), retry
);
91 void ladish_log_init() __attribute__ ((constructor
));
92 void ladish_log_init()
94 char * ladish_log_dir
;
95 const char * home_dir
;
98 home_dir
= getenv("HOME");
101 log_error("Environment variable HOME not set");
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
);
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
);
119 if (!ensure_dir_exist(xdg_log_home
, 0700))
124 if (!ensure_dir_exist(ladish_log_dir
, 0700))
129 g_log_filename
= catdup(ladish_log_dir
, LADISH_XDG_LOG
);
130 if (g_log_filename
== NULL
)
132 log_error("Out of memory");
137 cdbus_log_setup(ladish_log
);
140 free(ladish_log_dir
);
149 void ladish_log_uninit() __attribute__ ((destructor
));
150 void ladish_log_uninit()
152 if (g_logfile
!= NULL
)
157 free(g_log_filename
);
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) */
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)
179 const char * UNUSED(file
),
180 unsigned int UNUSED(line
),
181 const char * UNUSED(func
))
183 return level
!= CDBUS_LOG_LEVEL_DEBUG
;
197 #if !defined(LOG_OUTPUT_STDOUT)
199 char timestamp_str
[26];
203 if (!ladish_log_enabled(level
, file
, line
, func
))
208 #if !defined(LOG_OUTPUT_STDOUT)
209 if (g_logfile
!= NULL
&& ladish_log_open())
218 case CDBUS_LOG_LEVEL_DEBUG
:
219 case CDBUS_LOG_LEVEL_INFO
:
222 case CDBUS_LOG_LEVEL_WARN
:
223 case CDBUS_LOG_LEVEL_ERROR
:
224 case CDBUS_LOG_LEVEL_ERROR_PLAIN
:
230 #if !defined(LOG_OUTPUT_STDOUT)
232 ctime_r(×tamp
, timestamp_str
);
233 timestamp_str
[24] = 0;
235 fprintf(stream
, "%s: ", timestamp_str
);
241 case CDBUS_LOG_LEVEL_DEBUG
:
242 fprintf(stream
, "%s:%d:%s ", file
, line
, func
);
244 case CDBUS_LOG_LEVEL_WARN
:
245 color
= ANSI_COLOR_YELLOW
;
247 case CDBUS_LOG_LEVEL_ERROR
:
248 case CDBUS_LOG_LEVEL_ERROR_PLAIN
:
249 color
= ANSI_COLOR_RED
;
255 fputs(color
, stream
);
258 va_start(ap
, format
);
259 vfprintf(stream
, format
, ap
);
264 fputs(ANSI_RESET
, stream
);