1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
/*
* MOC - music on console
* Copyright (C) 2004 Damian Pietras <daper@daper.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <assert.h>
#include <pthread.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include "common.h"
#include "lists.h"
#include "log.h"
#include "options.h"
#ifndef NDEBUG
static FILE *logfp = NULL; /* logging file stream */
static enum {
UNINITIALISED,
BUFFERING,
LOGGING
} logging_state = UNINITIALISED;
static lists_t_strs *buffered_log = NULL;
static int log_records_spilt = 0;
static lists_t_strs *circular_log = NULL;
static int circular_ptr = 0;
static pthread_mutex_t logging_mtx = PTHREAD_MUTEX_INITIALIZER;
static struct {
int sig;
const char *name;
volatile uint64_t raised;
uint64_t logged;
} sig_info[] = {
{SIGINT, "SIGINT", 0, 0},
{SIGHUP, "SIGHUP", 0, 0},
{SIGQUIT, "SIGQUIT", 0, 0},
{SIGTERM, "SIGTERM", 0, 0},
{SIGCHLD, "SIGCHLD", 0, 0},
#ifdef SIGWINCH
{SIGWINCH, "SIGWINCH", 0, 0},
#endif
{0, "SIG other", 0, 0}
};
#endif
#ifndef NDEBUG
void log_signal (int sig)
{
int ix = 0;
while (sig_info[ix].sig && sig_info[ix].sig != sig)
ix += 1;
sig_info[ix].raised += 1;
}
#endif
#ifndef NDEBUG
static inline void flush_log (void)
{
int rc;
if (logfp) {
do {
rc = fflush (logfp);
} while (rc != 0 && errno == EINTR);
}
}
#endif
#ifndef NDEBUG
static void locked_logit (const char *file, const int line,
const char *function, const char *msg)
{
int len;
char *str, time_str[20];
struct timespec utc_time;
time_t tv_sec;
struct tm tm_time;
const char fmt[] = "%s.%06ld: %s:%d %s(): %s\n";
assert (logging_state == BUFFERING || logging_state == LOGGING);
assert (logging_state != BUFFERING || !logfp);
assert (logging_state != BUFFERING || !circular_log);
assert (logging_state != LOGGING || logfp || !circular_log);
if (logging_state == LOGGING && !logfp)
return;
get_realtime (&utc_time);
tv_sec = utc_time.tv_sec;
localtime_r (&tv_sec, &tm_time);
strftime (time_str, sizeof (time_str), "%b %e %T", &tm_time);
if (logfp && !circular_log) {
fprintf (logfp, fmt, time_str, utc_time.tv_nsec / 1000L,
file, line, function, msg);
return;
}
len = snprintf (NULL, 0, fmt, time_str, utc_time.tv_nsec / 1000L,
file, line, function, msg);
str = xmalloc (len + 1);
snprintf (str, len + 1, fmt, time_str, utc_time.tv_nsec / 1000L,
file, line, function, msg);
if (logging_state == BUFFERING) {
lists_strs_push (buffered_log, str);
return;
}
assert (circular_log);
if (circular_ptr == lists_strs_capacity (circular_log))
circular_ptr = 0;
if (circular_ptr < lists_strs_size (circular_log))
free (lists_strs_swap (circular_log, circular_ptr, str));
else
lists_strs_push (circular_log, str);
circular_ptr += 1;
}
#endif
#ifndef NDEBUG
static void log_signals_raised (void)
{
size_t ix;
for (ix = 0; ix < ARRAY_SIZE(sig_info); ix += 1) {
while (sig_info[ix].raised > sig_info[ix].logged) {
locked_logit (__FILE__, __LINE__, __func__, sig_info[ix].name);
sig_info[ix].logged += 1;
}
}
}
#endif
/* Put something into the log. If built with logging disabled,
* this function is provided as a stub so independant plug-ins
* configured with logging enabled can still resolve it. */
void internal_logit (const char *file LOGIT_ONLY,
const int line LOGIT_ONLY,
const char *function LOGIT_ONLY,
const char *format LOGIT_ONLY, ...)
{
#ifndef NDEBUG
int saved_errno = errno;
char *msg;
va_list va;
LOCK(logging_mtx);
if (!logfp) {
switch (logging_state) {
case UNINITIALISED:
buffered_log = lists_strs_new (128);
logging_state = BUFFERING;
break;
case BUFFERING:
/* Don't let storage run away on us. */
if (lists_strs_size (buffered_log) < lists_strs_capacity (buffered_log))
break;
log_records_spilt += 1;
case LOGGING:
goto end;
}
}
log_signals_raised ();
va_start (va, format);
msg = format_msg_va (format, va);
va_end (va);
locked_logit (file, line, function, msg);
free (msg);
flush_log ();
log_signals_raised ();
end:
UNLOCK(logging_mtx);
errno = saved_errno;
#endif
}
/* Initialize logging stream */
void log_init_stream (FILE *f LOGIT_ONLY, const char *fn LOGIT_ONLY)
{
#ifndef NDEBUG
char *msg;
LOCK(logging_mtx);
logfp = f;
if (logging_state == BUFFERING) {
if (logfp) {
int ix;
for (ix = 0; ix < lists_strs_size (buffered_log); ix += 1)
fprintf (logfp, "%s", lists_strs_at (buffered_log, ix));
}
lists_strs_free (buffered_log);
buffered_log = NULL;
}
logging_state = LOGGING;
if (!logfp)
goto end;
msg = format_msg ("Writing log to: %s", fn);
locked_logit (__FILE__, __LINE__, __func__, msg);
free (msg);
if (log_records_spilt > 0) {
msg = format_msg ("%d log records spilt", log_records_spilt);
locked_logit (__FILE__, __LINE__, __func__, msg);
free (msg);
}
flush_log ();
end:
UNLOCK(logging_mtx);
#endif
}
/* Start circular logging (if enabled). */
void log_circular_start ()
{
#ifndef NDEBUG
int circular_size;
assert (logging_state == LOGGING);
assert (!circular_log);
if (!logfp)
return;
circular_size = options_get_int ("CircularLogSize");
if (circular_size > 0) {
LOCK(logging_mtx);
circular_log = lists_strs_new (circular_size);
circular_ptr = 0;
UNLOCK(logging_mtx);
}
#endif
}
/* Internal circular log reset. */
#ifndef NDEBUG
static inline void locked_circular_reset ()
{
lists_strs_clear (circular_log);
circular_ptr = 0;
}
#endif
/* Reset the circular log (if enabled). */
void log_circular_reset ()
{
#ifndef NDEBUG
assert (logging_state == LOGGING);
if (!circular_log)
return;
LOCK(logging_mtx);
locked_circular_reset ();
UNLOCK(logging_mtx);
#endif
}
/* Write circular log (if enabled) to the log file. */
void log_circular_log ()
{
#ifndef NDEBUG
int ix;
assert (logging_state == LOGGING && (logfp || !circular_log));
if (!circular_log)
return;
LOCK(logging_mtx);
fprintf (logfp, "\n* Circular Log Starts *\n\n");
for (ix = circular_ptr; ix < lists_strs_size (circular_log); ix += 1)
fprintf (logfp, "%s", lists_strs_at (circular_log, ix));
fflush (logfp);
for (ix = 0; ix < circular_ptr; ix += 1)
fprintf (logfp, "%s", lists_strs_at (circular_log, ix));
fprintf (logfp, "\n* Circular Log Ends *\n\n");
fflush (logfp);
locked_circular_reset ();
UNLOCK(logging_mtx);
#endif
}
/* Stop circular logging (if enabled). */
void log_circular_stop ()
{
#ifndef NDEBUG
assert (logging_state == LOGGING);
if (!circular_log)
return;
LOCK(logging_mtx);
lists_strs_free (circular_log);
circular_log = NULL;
circular_ptr = 0;
UNLOCK(logging_mtx);
#endif
}
void log_close ()
{
#ifndef NDEBUG
LOCK(logging_mtx);
if (!(logfp == stdout || logfp == stderr || logfp == NULL)) {
fclose (logfp);
logfp = NULL;
}
if (buffered_log) {
lists_strs_free (buffered_log);
buffered_log = NULL;
}
log_records_spilt = 0;
UNLOCK(logging_mtx);
#endif
}
|