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.
28 #include "../common.h"
34 #include "../catdup.h"
35 #include "dirhelpers.h"
37 #define DEFAULT_XDG_LOG "/.log"
38 #define LASH_XDG_SUBDIR "/" BASE_NAME
39 #define LASH_XDG_LOG "/" BASE_NAME ".log"
43 void lash_log_init() __attribute__ ((constructor
));
49 size_t lash_log_dir_len
; /* without terminating '\0' char */
50 const char * home_dir
;
53 home_dir
= getenv("HOME");
56 lash_error("Environment variable HOME not set");
60 xdg_log_home
= catdup(home_dir
, DEFAULT_XDG_LOG
);
61 if (xdg_log_home
== NULL
)
63 lash_error("catdup failed for '%s' and '%s'", home_dir
, DEFAULT_XDG_LOG
);
67 lash_log_dir
= catdup(xdg_log_home
, LASH_XDG_SUBDIR
);
68 if (lash_log_dir
== NULL
)
70 lash_error("catdup failed for '%s' and '%s'", home_dir
, LASH_XDG_SUBDIR
);
74 if (!ensure_dir_exist(xdg_log_home
, 0700))
79 if (!ensure_dir_exist(lash_log_dir
, 0700))
84 lash_log_dir_len
= strlen(lash_log_dir
);
86 log_len
= strlen(LASH_XDG_LOG
);
88 log_filename
= malloc(lash_log_dir_len
+ log_len
+ 1);
89 if (log_filename
== NULL
)
91 lash_error("Out of memory");
95 memcpy(log_filename
, lash_log_dir
, lash_log_dir_len
);
96 memcpy(log_filename
+ lash_log_dir_len
, LASH_XDG_LOG
, log_len
);
97 log_filename
[lash_log_dir_len
+ log_len
] = 0;
99 g_logfile
= fopen(log_filename
, "a");
100 if (g_logfile
== NULL
)
102 lash_error("Cannot open jackdbus log file \"%s\": %d (%s)\n", log_filename
, errno
, strerror(errno
));
117 void lash_log_uninit() __attribute__ ((destructor
));
118 void lash_log_uninit()
120 if (g_logfile
!= NULL
)
135 char timestamp_str
[26];
137 if (g_logfile
!= NULL
)
145 case LASH_LOG_LEVEL_DEBUG
:
146 case LASH_LOG_LEVEL_INFO
:
149 case LASH_LOG_LEVEL_WARN
:
150 case LASH_LOG_LEVEL_ERROR
:
151 case LASH_LOG_LEVEL_ERROR_PLAIN
:
158 ctime_r(×tamp
, timestamp_str
);
159 timestamp_str
[24] = 0;
161 fprintf(stream
, "%s: ", timestamp_str
);
163 va_start(ap
, format
);
164 vfprintf(stream
, format
, ap
);