1 /* -*- Mode: C ; c-basic-offset: 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"
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)
52 if (g_logfile
!= NULL
)
54 ret
= stat(g_log_filename
, &st
);
55 if (ret
!= 0 || g_log_file_ino
!= st
.st_ino
)
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
));
74 ret
= stat(g_log_filename
, &st
);
77 g_log_file_ino
= st
.st_ino
;
85 fprintf(stderr
, "Cannot stat just opened ladishd log file \"%s\": %d (%s). %d retries\n", g_log_filename
, errno
, strerror(errno
), retry
);
89 void ladish_log_init() __attribute__ ((constructor
));
90 void ladish_log_init()
92 char * ladish_log_dir
;
93 const char * home_dir
;
96 home_dir
= getenv("HOME");
99 log_error("Environment variable HOME not set");
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
);
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
);
117 if (!ensure_dir_exist(xdg_log_home
, 0700))
122 if (!ensure_dir_exist(ladish_log_dir
, 0700))
127 g_log_filename
= catdup(ladish_log_dir
, LADISH_XDG_LOG
);
128 if (g_log_filename
== NULL
)
130 log_error("Out of memory");
137 free(ladish_log_dir
);
146 void ladish_log_uninit() __attribute__ ((destructor
));
147 void ladish_log_uninit()
149 if (g_logfile
!= NULL
)
154 free(g_log_filename
);
166 char timestamp_str
[26];
168 if (g_logfile
!= NULL
&& ladish_log_open())
176 case LADISH_LOG_LEVEL_DEBUG
:
177 case LADISH_LOG_LEVEL_INFO
:
180 case LADISH_LOG_LEVEL_WARN
:
181 case LADISH_LOG_LEVEL_ERROR
:
182 case LADISH_LOG_LEVEL_ERROR_PLAIN
:
189 ctime_r(×tamp
, timestamp_str
);
190 timestamp_str
[24] = 0;
192 fprintf(stream
, "%s: ", timestamp_str
);
194 va_start(ap
, format
);
195 vfprintf(stream
, format
, ap
);