1 /* -*- Mode: C ; c-basic-offset: 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.
31 #include <sys/types.h>
38 #include "../common/debug.h"
41 #define DEFAULT_XDG_LOG "/.log"
42 #define LASH_XDG_SUBDIR "/lash"
43 #define LASH_XDG_LOG "/lash.log"
53 if (stat(dirname
, &st
) != 0)
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
));
66 lash_error("Failed to stat \"%s\": %d (%s)", dirname
, errno
, strerror(errno
));
72 if (!S_ISDIR(st
.st_mode
))
74 lash_error("\"%s\" exists but is not directory.", dirname
);
82 void lash_log_init() __attribute__ ((constructor
));
88 size_t lash_log_dir_len
; /* without terminating '\0' char */
89 const char * home_dir
;
92 home_dir
= getenv("HOME");
95 lash_error("Environment variable HOME not set");
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
);
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
);
113 if (!ensure_dir_exist(xdg_log_home
, 0700))
118 if (!ensure_dir_exist(lash_log_dir
, 0700))
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");
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
));
156 void lash_log_uninit() __attribute__ ((destructor
));
157 void lash_log_uninit()
159 if (g_logfile
!= NULL
)
174 char timestamp_str
[26];
176 if (g_logfile
!= NULL
)
184 case LASH_LOG_LEVEL_DEBUG
:
185 case LASH_LOG_LEVEL_INFO
:
188 case LASH_LOG_LEVEL_WARN
:
189 case LASH_LOG_LEVEL_ERROR
:
190 case LASH_LOG_LEVEL_ERROR_PLAIN
:
197 ctime_r(×tamp
, timestamp_str
);
198 timestamp_str
[24] = 0;
200 fprintf(stream
, "%s: ", timestamp_str
);
202 va_start(ap
, format
);
203 vfprintf(stream
, format
, ap
);