File: logging.c

package info (click to toggle)
gmediaserver 0.13.0-8
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 4,044 kB
  • ctags: 3,078
  • sloc: ansic: 19,496; sh: 4,347; xml: 513; makefile: 109; yacc: 98; perl: 18; sed: 16
file content (204 lines) | stat: -rw-r--r-- 4,895 bytes parent folder | download | duplicates (3)
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
/* log.c - Logging and user information routines
 *
 * Copyright (C) 2005, 2007  Oskar Liljeblad
 *
 * 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include <config.h>
#include <sys/types.h>		/* POSIX */
#include <unistd.h>		/* POSIX */
#include <time.h>		/* POSIX */
#include <stdio.h>		/* C89 */
#include <stdlib.h>		/* C89 */
#include <stdarg.h>		/* C89 */
#include <errno.h>		/* C89 */
#include "gettext.h"		/* Gnulib/gettext */
#define _(s) gettext(s)
#define N_(s) gettext_noop(s)
#include "xvasprintf.h"		/* Gnulib */
#include "strftime.h"		/* Gnulib */
#include "progname.h"		/* Gnulib */
#include "quote.h"		/* Gnulib */
#include "gmediaserver.h"

#define MAX_TIMESTAMP_LENGTH 1024

uint32_t verbosity = 0;
static FILE *logfh = NULL;
static const char *timestamp_format = NULL;
static bool started = false;

static char *
prepare_msg_v(const char *format, va_list args)
{
    char *msg1;
    char *msg2;

    msg1 = xvasprintf(format, args);
    msg2 = convert_string_to_log(msg1);
    if (msg2 == NULL) {
        warn(_("cannot convert log message: %s\n"), errstr);
        msg2 = msg1; /* print raw! */
    } else {
        free(msg1);
    }

    return msg2;
}

static char *
prepare_msg(const char *format, ...)
{
    va_list args;
    char *msg;

    va_start(args, format);
    msg = prepare_msg_v(format, args);
    va_end(args);

    return msg;
}



static void
log_timestamp(FILE *out)
{
    if (timestamp_format != NULL) {
        time_t curtime;
        struct tm *loctime;
        size_t len;

        if (time(&curtime) < 0) {
            /* XXX: report error somehow? */
            return;
        }
	loctime = localtime(&curtime);
        len = nstrftime(NULL, -1, timestamp_format, loctime, 0, 0);

        if (len > 0) {
            char buf[len+1];
            char *msg;

            nstrftime(buf, len+1, timestamp_format, loctime, 0, 0);
            msg = prepare_msg("%s ", buf);
            fputs(msg, out);
            free(msg);
        }
    }
}

void
init_logging(const char *logfilename, const char *ts_format)
{
    timestamp_format = ts_format;

    started = true;
    if (logfilename != NULL) {
        logfh = fopen(logfilename, "a");
        if (logfh == NULL)
            die(_("cannot open log file %s for appending\n"), quote(conv_filename(logfilename)));
        setlinebuf(logfh);
    }
    say(1, _("%s process id %d starting\n"), PACKAGE, getpid());
}

void
finish_logging(bool success)
{
    if (success)
        say(1, _("%s process id %d terminated normally\n"), PACKAGE, getpid());
    else
	say(1, _("%s process id %d terminated due to error\n"), PACKAGE, getpid());

    if (logfh != NULL) {
        fclose(logfh);
        logfh = NULL;
    }
    started = false;
}

void
say(int level, const char *format, ...)
{
    if (verbosity >= level) {
    	va_list args;
        char *msg;

    	va_start(args, format);
    	msg = prepare_msg_v(format, args);
        if (logfh != NULL) {
            log_timestamp(logfh); /* XXX: what about conversion!? */
            fputs(msg, logfh);
        } else {
            log_timestamp(stdout); /* XXX: what about conversion!? */
            fputs(msg, stdout);
        }
        free(msg);
	va_end(args);
    }
}

void
die(const char *format, ...)
{
    va_list args;
    char *msg1;
    char *msg2;

    va_start(args, format);
    msg1 = xvasprintf(format, args);
    if (logfh != NULL) {
    	log_timestamp(logfh);
    	msg2 = prepare_msg(_("Error: %s"), msg1);
      	fputs(msg2, logfh);
    } else {
        log_timestamp(stderr);
        msg2 = prepare_msg("%s: %s", program_name, msg1);
        fputs(msg2, stderr);
    }
    free(msg1);
    free(msg2);
    va_end(args);

    finish_logging(false);

    exit(EXIT_FAILURE);
}

void
warn(const char *format, ...)
{
    va_list args;
    char *msg1;
    char *msg2;

    va_start(args, format);
    msg1 = xvasprintf(format, args);
    if (logfh != NULL) {
        log_timestamp(logfh);
        msg2 = prepare_msg(_("Warning: %s"), msg1);
        fputs(msg2, logfh);
    } else {
        log_timestamp(stderr);
        msg2 = prepare_msg("%s: %s", program_name, msg1);
        fputs(msg2, stderr);
    }
    free(msg1);
    free(msg2);
    va_end(args);
}