forked from meh/moc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
134 lines (114 loc) · 3 KB
/
log.c
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
/*
* 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 <sys/time.h>
#include <time.h>
#include "common.h"
#include "lists.h"
#include "log.h"
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;
/* Put something into the log */
void internal_logit (const char *file, const int line, const char *function,
const char *format, ...)
{
int len;
char *msg, time_str[20];
struct timeval utc_time;
va_list va;
struct tm tm_time;
const char fmt[] = "%s.%06u: %s:%d %s(): %s\n";
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;
return;
case LOGGING:
return;
}
}
va_start (va, format);
len = vsnprintf (NULL, 0, format, va) + 1;
va_end (va);
msg = xmalloc (len);
va_start (va, format);
vsnprintf (msg, len, format, va);
va_end (va);
gettimeofday (&utc_time, NULL);
localtime_r (&utc_time.tv_sec, &tm_time);
strftime (time_str, sizeof (time_str), "%b %e %T", &tm_time);
if (logfp) {
fprintf (logfp, fmt, time_str, (unsigned)utc_time.tv_usec,
file, line, function, msg);
fflush (logfp);
}
else {
char *str;
len = snprintf (NULL, 0, fmt, time_str, (unsigned)utc_time.tv_usec,
file, line, function, msg);
str = xmalloc (len + 1);
snprintf (str, len + 1, fmt, time_str, (unsigned)utc_time.tv_usec,
file, line, function, msg);
lists_strs_push (buffered_log, str);
}
free (msg);
}
/* fake logit() function for NDEBUG */
void fake_logit (const char *format ATTR_UNUSED, ...)
{
}
/* Initialize logging stream */
void log_init_stream (FILE *f, const char *fn)
{
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));
fflush (logfp);
}
lists_strs_free (buffered_log);
buffered_log = NULL;
}
logging_state = LOGGING;
logit ("Writing log to: %s", fn);
if (log_records_spilt > 0)
logit ("%d log records spilt", log_records_spilt);
}
void log_close ()
{
if (!(logfp == stdout || logfp == stderr || logfp == NULL))
fclose (logfp);
if (buffered_log) {
lists_strs_free (buffered_log);
buffered_log = NULL;
}
logging_state = UNINITIALISED;
log_records_spilt = 0;
}