File: logging.c

package info (click to toggle)
shellinabox 2.21
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 2,112 kB
  • sloc: ansic: 8,729; sh: 437; makefile: 355; ruby: 76; xml: 5
file content (221 lines) | stat: -rw-r--r-- 5,457 bytes parent folder | download | duplicates (2)
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
// logging.c -- Utility functions for managing log messages
// Copyright (C) 2008-2009 Markus Gutschke <markus@shellinabox.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// 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 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.
//
// In addition to these license terms, the author grants the following
// additional rights:
//
// If you modify this program, or any covered work, by linking or
// combining it with the OpenSSL project's OpenSSL library (or a
// modified version of that library), containing parts covered by the
// terms of the OpenSSL or SSLeay licenses, the author
// grants you additional permission to convey the resulting work.
// Corresponding Source for a non-source form of such a combination
// shall include the source code for the parts of OpenSSL used as well
// as that of the covered work.
//
// You may at your option choose to remove this additional permission from
// the work, or from any part of it.
//
// It is possible to build this program in a way that it loads OpenSSL
// libraries at run-time. If doing so, the following notices are required
// by the OpenSSL and SSLeay licenses:
//
// This product includes software developed by the OpenSSL Project
// for use in the OpenSSL Toolkit. (http://www.openssl.org/)
//
// This product includes cryptographic software written by Eric Young
// (eay@cryptsoft.com)
//
//
// The most up-to-date version of this program is always available from
// http://shellinabox.com

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "logging/logging.h"

#ifdef HAVE_SYSLOG_H
# include <syslog.h>
# ifndef HAVE_VSYSLOG
static void vsyslog(int priority, const char *fmt, va_list ap) {
  char *s = vStringPrintf(NULL, fmt, ap);
  if (s) {
    syslog(priority, "%s", s);
    free(s);
  }
}
# endif
#endif

static int verbosity = MSG_DEFAULT;

static void debugMsg(int level, const char *fmt, va_list ap) {
  if (level <= verbosity) {
    vfprintf(stderr, fmt, ap);
    fputs("\n", stderr);
  }
}

void debug(const char *fmt, ...) {
  va_list ap;
  va_start(ap, fmt);
  debugMsg(MSG_DEBUG, fmt, ap);
  va_end(ap);
}

void info(const char *fmt, ...) {
  va_list ap;
  va_start(ap, fmt);
  debugMsg(MSG_INFO, fmt, ap);
  va_end(ap);
}

void warn(const char *fmt, ...) {
  va_list ap;
  va_start(ap, fmt);
  debugMsg(MSG_WARN, fmt, ap);
  va_end(ap);
}

void error(const char *fmt, ...) {
  va_list ap;
  va_start(ap, fmt);
  debugMsg(MSG_ERROR, fmt, ap);
#ifdef HAVE_SYSLOG_H
  va_list apSyslog;
  va_copy(apSyslog, ap);
  va_start(apSyslog, fmt);
  vsyslog(LOG_ERR, fmt, apSyslog);
  va_end(apSyslog);
#endif
  va_end(ap);
}

void message(const char *fmt, ...) {
  va_list ap;
  va_start(ap, fmt);
  debugMsg(MSG_MESSAGE, fmt, ap);
  va_end(ap);
}

void fatal(const char *fmt, ...) {
  va_list ap;
  va_start(ap, fmt);
  debugMsg(MSG_QUIET, fmt, ap);
#ifdef HAVE_SYSLOG_H
  va_list apSyslog;
  va_copy(apSyslog, ap);
  va_start(apSyslog, fmt);
  vsyslog(LOG_CRIT, fmt, apSyslog);
  va_end(apSyslog);
  syslog(LOG_CRIT, "[server] Aborting...");
#endif
  va_end(ap);
  _exit(1);
}

int logIsDebug(void) {
  return verbosity >= MSG_DEBUG;
}

int logIsInfo(void) {
  return verbosity >= MSG_INFO;
}

int logIsWarn(void) {
  return verbosity >= MSG_WARN;
}

int logIsError(void) {
  return verbosity >= MSG_ERROR;
}

int logIsMessage(void) {
  return verbosity >= MSG_MESSAGE;
}

int logIsQuiet(void) {
  return verbosity <= MSG_QUIET;
}

int logIsDefault(void) {
  return verbosity == MSG_DEFAULT;
}

int logIsVerbose(void) {
  return verbosity >= MSG_ERROR;
}

void logSetLogLevel(int level) {
  check(level >= MSG_QUIET && level <= MSG_DEBUG);
  verbosity = level;
}

char *vStringPrintf(char *buf, const char *fmt, va_list ap) {
  int offset    = buf ? strlen(buf) : 0;
  int len       = 80;
  check(buf     = realloc(buf, offset + len));
  va_list aq;
  va_copy(aq, ap);
  int p         = vsnprintf(buf + offset, len, fmt, aq);
  va_end(aq);
  if (p >= len) {
    check(buf   = realloc(buf, offset + p + 1));
    va_copy(aq, ap);
    check(vsnprintf(buf + offset, p + 1, fmt, aq) == p);
    va_end(aq);
  } else if (p < 0) {
    int inc     = 256;
    do {
      len      += inc;
      check(len < (1 << 20));
      if (inc < (32 << 10)) {
        inc   <<= 1;
      }
      check(buf = realloc(buf, offset + len));
      va_copy(aq, ap);
      p         = vsnprintf(buf + offset, len, fmt, ap);
      va_end(aq);
    } while (p < 0 || p >= len);
  }
  return buf;
}

char *stringPrintf(char *buf, const char *fmt, ...) {
  va_list ap;
  va_start(ap, fmt);
  char *s = vStringPrintf(buf, fmt, ap);
  va_end(ap);
  return s;
}

char *stringPrintfUnchecked(char *buf, const char *fmt, ...)
#ifdef HAVE_ATTRIBUTE_ALIAS
  __attribute__((alias("stringPrintf")));
#else
{
  va_list ap;
  va_start(ap, fmt);
  char *s = vStringPrintf(buf, fmt, ap);
  va_end(ap);
  return s;
}
#endif