summaryrefslogtreecommitdiff
path: root/log-client.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>1999-10-27 13:42:43 +1000
committerDamien Miller <djm@mindrot.org>1999-10-27 13:42:43 +1000
commitd4a8b7e34dd619a4debf9a206c81db26d1402ea6 (patch)
treea47d770a2f790f40d18b0982d4e55fa7cfb1fa3b /log-client.c
Initial revision
Diffstat (limited to 'log-client.c')
-rw-r--r--log-client.c138
1 files changed, 138 insertions, 0 deletions
diff --git a/log-client.c b/log-client.c
new file mode 100644
index 000000000..1792ba847
--- /dev/null
+++ b/log-client.c
@@ -0,0 +1,138 @@
1/*
2
3log-client.c
4
5Author: Tatu Ylonen <ylo@cs.hut.fi>
6
7Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 All rights reserved
9
10Created: Mon Mar 20 21:13:40 1995 ylo
11
12Client-side versions of debug(), log(), etc. These print to stderr.
13
14*/
15
16#include "includes.h"
17RCSID("$Id: log-client.c,v 1.1 1999/10/27 03:42:44 damien Exp $");
18
19#include "xmalloc.h"
20#include "ssh.h"
21
22static int log_debug = 0;
23static int log_quiet = 0;
24
25void log_init(char *av0, int on_stderr, int debug, int quiet,
26 SyslogFacility facility)
27{
28 log_debug = debug;
29 log_quiet = quiet;
30}
31
32void log(const char *fmt, ...)
33{
34 va_list args;
35
36 if (log_quiet)
37 return;
38 va_start(args, fmt);
39 vfprintf(stderr, fmt, args);
40 fprintf(stderr, "\r\n");
41 va_end(args);
42}
43
44void debug(const char *fmt, ...)
45{
46 va_list args;
47 if (log_quiet || !log_debug)
48 return;
49 va_start(args, fmt);
50 fprintf(stderr, "debug: ");
51 vfprintf(stderr, fmt, args);
52 fprintf(stderr, "\r\n");
53 va_end(args);
54}
55
56void error(const char *fmt, ...)
57{
58 va_list args;
59 if (log_quiet)
60 return;
61 va_start(args, fmt);
62 vfprintf(stderr, fmt, args);
63 fprintf(stderr, "\r\n");
64 va_end(args);
65}
66
67struct fatal_cleanup
68{
69 struct fatal_cleanup *next;
70 void (*proc)(void *);
71 void *context;
72};
73
74static struct fatal_cleanup *fatal_cleanups = NULL;
75
76/* Registers a cleanup function to be called by fatal() before exiting. */
77
78void fatal_add_cleanup(void (*proc)(void *), void *context)
79{
80 struct fatal_cleanup *cu;
81
82 cu = xmalloc(sizeof(*cu));
83 cu->proc = proc;
84 cu->context = context;
85 cu->next = fatal_cleanups;
86 fatal_cleanups = cu;
87}
88
89/* Removes a cleanup frunction to be called at fatal(). */
90
91void fatal_remove_cleanup(void (*proc)(void *context), void *context)
92{
93 struct fatal_cleanup **cup, *cu;
94
95 for (cup = &fatal_cleanups; *cup; cup = &cu->next)
96 {
97 cu = *cup;
98 if (cu->proc == proc && cu->context == context)
99 {
100 *cup = cu->next;
101 xfree(cu);
102 return;
103 }
104 }
105 fatal("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx\n",
106 (unsigned long)proc, (unsigned long)context);
107}
108
109/* Function to display an error message and exit. This is in this file because
110 this needs to restore terminal modes before exiting. See log-client.c
111 for other related functions. */
112
113void fatal(const char *fmt, ...)
114{
115 va_list args;
116 struct fatal_cleanup *cu, *next_cu;
117 static int fatal_called = 0;
118
119 if (!fatal_called)
120 {
121 fatal_called = 1;
122
123 /* Call cleanup functions. */
124 for (cu = fatal_cleanups; cu; cu = next_cu)
125 {
126 next_cu = cu->next;
127 (*cu->proc)(cu->context);
128 }
129 }
130
131 va_start(args, fmt);
132 vfprintf(stderr, fmt, args);
133 fprintf(stderr, "\r\n");
134 va_end(args);
135 exit(255);
136}
137
138/* fatal() is in ssh.c so that it can properly reset terminal modes. */