xref: /freebsd/usr.sbin/ppp/log.c (revision 46df5aa7bc98cd1d4769ce244d518f4e161f3e61)
1c39934eaSBrian Somers /*-
2c39934eaSBrian Somers  * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
3c39934eaSBrian Somers  * All rights reserved.
4c39934eaSBrian Somers  *
5c39934eaSBrian Somers  * Redistribution and use in source and binary forms, with or without
6c39934eaSBrian Somers  * modification, are permitted provided that the following conditions
7c39934eaSBrian Somers  * are met:
8c39934eaSBrian Somers  * 1. Redistributions of source code must retain the above copyright
9c39934eaSBrian Somers  *    notice, this list of conditions and the following disclaimer.
10c39934eaSBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
11c39934eaSBrian Somers  *    notice, this list of conditions and the following disclaimer in the
12c39934eaSBrian Somers  *    documentation and/or other materials provided with the distribution.
13c39934eaSBrian Somers  *
14c39934eaSBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15c39934eaSBrian Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16c39934eaSBrian Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17c39934eaSBrian Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18c39934eaSBrian Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19c39934eaSBrian Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20c39934eaSBrian Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21c39934eaSBrian Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22c39934eaSBrian Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23c39934eaSBrian Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24c39934eaSBrian Somers  * SUCH DAMAGE.
25c39934eaSBrian Somers  *
2697d92980SPeter Wemm  * $FreeBSD$
2775240ed1SBrian Somers  */
2875240ed1SBrian Somers 
292764b86aSBrian Somers #include <sys/types.h>
3075240ed1SBrian Somers 
31ba16c840SBrian Somers #include <ctype.h>
3253c9f6c0SAtsushi Murai #include <stdarg.h>
3353c9f6c0SAtsushi Murai #include <stdio.h>
3470a91e4cSBrian Somers #include <string.h>
3575240ed1SBrian Somers #include <syslog.h>
3685b542cfSBrian Somers #include <termios.h>
3775240ed1SBrian Somers 
38c9e11a11SBrian Somers #include "defs.h"
39b6e82f33SBrian Somers #include "command.h"
40927145beSBrian Somers #include "mbuf.h"
41927145beSBrian Somers #include "log.h"
4285b542cfSBrian Somers #include "descriptor.h"
4385b542cfSBrian Somers #include "prompt.h"
44af57ed9fSAtsushi Murai 
45182c898aSBrian Somers static const char *const LogNames[] = {
46927145beSBrian Somers   "Async",
4792b09558SBrian Somers   "CBCP",
48cb611434SBrian Somers   "CCP",
49927145beSBrian Somers   "Chat",
50927145beSBrian Somers   "Command",
51927145beSBrian Somers   "Connect",
52927145beSBrian Somers   "Debug",
5352c9ca19SBrian Somers   "DNS",
5406a43ce0SBrian Somers   "Filter",			/* Log discarded packets */
55927145beSBrian Somers   "HDLC",
565106c671SBrian Somers   "ID0",
57cb611434SBrian Somers   "IPCP",
5830949fd4SBrian Somers   "IPV6CP",
59927145beSBrian Somers   "LCP",
60927145beSBrian Somers   "LQM",
61927145beSBrian Somers   "Phase",
626815097bSBrian Somers   "Physical",
636815097bSBrian Somers   "Sync",
64927145beSBrian Somers   "TCP/IP",
65b1ac9332SBrian Somers   "Timer",
66927145beSBrian Somers   "Tun",
67927145beSBrian Somers   "Warning",
68927145beSBrian Somers   "Error",
69927145beSBrian Somers   "Alert"
70927145beSBrian Somers };
71af57ed9fSAtsushi Murai 
72927145beSBrian Somers #define MSK(n) (1<<((n)-1))
73af57ed9fSAtsushi Murai 
74d47dceb8SBrian Somers static u_long LogMask = MSK(LogPHASE);
75a1e8f937SBrian Somers static u_long LogMaskLocal = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
76927145beSBrian Somers static int LogTunno = -1;
770f2f3eb3SBrian Somers static struct prompt *promptlist;	/* Where to log local stuff */
78b4f63b0bSBrian Somers struct prompt *log_PromptContext;
790bdcbcbeSBrian Somers int log_PromptListChanged;
800f2f3eb3SBrian Somers 
810f2f3eb3SBrian Somers struct prompt *
820f2f3eb3SBrian Somers log_PromptList()
830f2f3eb3SBrian Somers {
840f2f3eb3SBrian Somers   return promptlist;
850f2f3eb3SBrian Somers }
86b6217683SBrian Somers 
87b6217683SBrian Somers void
88b6217683SBrian Somers log_RegisterPrompt(struct prompt *prompt)
89b6217683SBrian Somers {
900f2f3eb3SBrian Somers   prompt->next = promptlist;
910f2f3eb3SBrian Somers   promptlist = prompt;
920f2f3eb3SBrian Somers   prompt->active = 1;
930f2f3eb3SBrian Somers   log_DiscardAllLocal(&prompt->logmask);
94b6217683SBrian Somers }
950f2f3eb3SBrian Somers 
960f2f3eb3SBrian Somers void
970f2f3eb3SBrian Somers log_ActivatePrompt(struct prompt *prompt)
980f2f3eb3SBrian Somers {
990f2f3eb3SBrian Somers   prompt->active = 1;
1000f2f3eb3SBrian Somers   LogMaskLocal |= prompt->logmask;
101b6217683SBrian Somers }
102b6217683SBrian Somers 
10367568487SBrian Somers static void
10467568487SBrian Somers LogSetMaskLocal(void)
10567568487SBrian Somers {
10667568487SBrian Somers   struct prompt *p;
10767568487SBrian Somers 
10867568487SBrian Somers   LogMaskLocal = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
1090f2f3eb3SBrian Somers   for (p = promptlist; p; p = p->next)
11067568487SBrian Somers     LogMaskLocal |= p->logmask;
11167568487SBrian Somers }
11267568487SBrian Somers 
113b6217683SBrian Somers void
1140f2f3eb3SBrian Somers log_DeactivatePrompt(struct prompt *prompt)
1150f2f3eb3SBrian Somers {
1160f2f3eb3SBrian Somers   if (prompt->active) {
1170f2f3eb3SBrian Somers     prompt->active = 0;
1180f2f3eb3SBrian Somers     LogSetMaskLocal();
1190f2f3eb3SBrian Somers   }
1200f2f3eb3SBrian Somers }
1210f2f3eb3SBrian Somers 
1220f2f3eb3SBrian Somers void
123b6217683SBrian Somers log_UnRegisterPrompt(struct prompt *prompt)
124b6217683SBrian Somers {
125b6217683SBrian Somers   if (prompt) {
126b6217683SBrian Somers     struct prompt **p;
127b6217683SBrian Somers 
1280f2f3eb3SBrian Somers     for (p = &promptlist; *p; p = &(*p)->next)
129b6217683SBrian Somers       if (*p == prompt) {
1300f2f3eb3SBrian Somers         *p = prompt->next;
1310f2f3eb3SBrian Somers         prompt->next = NULL;
132b6217683SBrian Somers         break;
133b6217683SBrian Somers       }
13467568487SBrian Somers     LogSetMaskLocal();
1350bdcbcbeSBrian Somers     log_PromptListChanged++;
136b6217683SBrian Somers   }
137b6217683SBrian Somers }
138af57ed9fSAtsushi Murai 
1390f2f3eb3SBrian Somers void
1400f2f3eb3SBrian Somers log_DestroyPrompts(struct server *s)
1410f2f3eb3SBrian Somers {
142a39fd214SBrian Somers   struct prompt *p, *pn, *pl;
1430f2f3eb3SBrian Somers 
1440f2f3eb3SBrian Somers   p = promptlist;
145a39fd214SBrian Somers   pl = NULL;
1460f2f3eb3SBrian Somers   while (p) {
1470f2f3eb3SBrian Somers     pn = p->next;
148a39fd214SBrian Somers     if (s && p->owner == s) {
149a39fd214SBrian Somers       if (pl)
150a39fd214SBrian Somers         pl->next = p->next;
151a39fd214SBrian Somers       else
152a39fd214SBrian Somers         promptlist = p->next;
1530f2f3eb3SBrian Somers       p->next = NULL;
1540f2f3eb3SBrian Somers       prompt_Destroy(p, 1);
155a39fd214SBrian Somers     } else
156a39fd214SBrian Somers       pl = p;
1570f2f3eb3SBrian Somers     p = pn;
1580f2f3eb3SBrian Somers   }
1590f2f3eb3SBrian Somers }
1600f2f3eb3SBrian Somers 
1610f2f3eb3SBrian Somers void
1620f2f3eb3SBrian Somers log_DisplayPrompts()
1630f2f3eb3SBrian Somers {
1640f2f3eb3SBrian Somers   struct prompt *p;
1650f2f3eb3SBrian Somers 
1660f2f3eb3SBrian Somers   for (p = promptlist; p; p = p->next)
1670f2f3eb3SBrian Somers     prompt_Required(p);
1680f2f3eb3SBrian Somers }
1690f2f3eb3SBrian Somers 
1700f2f3eb3SBrian Somers void
171bf1d3ff6SBrian Somers log_WritePrompts(struct datalink *dl, const char *fmt,...)
1720f2f3eb3SBrian Somers {
173bf1d3ff6SBrian Somers   va_list ap;
1740f2f3eb3SBrian Somers   struct prompt *p;
1750f2f3eb3SBrian Somers 
176bf1d3ff6SBrian Somers   va_start(ap, fmt);
1770f2f3eb3SBrian Somers   for (p = promptlist; p; p = p->next)
1780f2f3eb3SBrian Somers     if (prompt_IsTermMode(p, dl))
179bf1d3ff6SBrian Somers       prompt_vPrintf(p, fmt, ap);
180bf1d3ff6SBrian Somers   va_end(ap);
1810f2f3eb3SBrian Somers }
1820f2f3eb3SBrian Somers 
1830f2f3eb3SBrian Somers void
1840f2f3eb3SBrian Somers log_SetTtyCommandMode(struct datalink *dl)
1850f2f3eb3SBrian Somers {
1860f2f3eb3SBrian Somers   struct prompt *p;
1870f2f3eb3SBrian Somers 
1880f2f3eb3SBrian Somers   for (p = promptlist; p; p = p->next)
1890f2f3eb3SBrian Somers     if (prompt_IsTermMode(p, dl))
1900f2f3eb3SBrian Somers       prompt_TtyCommandMode(p);
1910f2f3eb3SBrian Somers }
1920f2f3eb3SBrian Somers 
193927145beSBrian Somers static int
194927145beSBrian Somers syslogLevel(int lev)
195927145beSBrian Somers {
196927145beSBrian Somers   switch (lev) {
19746df5aa7SBrian Somers   case LogLOG:
19846df5aa7SBrian Somers     return LOG_INFO;
1996f384573SBrian Somers   case LogDEBUG:
2006f384573SBrian Somers   case LogTIMER:
2016f384573SBrian Somers     return LOG_DEBUG;
202944f7098SBrian Somers   case LogWARN:
203944f7098SBrian Somers     return LOG_WARNING;
204944f7098SBrian Somers   case LogERROR:
205944f7098SBrian Somers     return LOG_ERR;
206944f7098SBrian Somers   case LogALERT:
207944f7098SBrian Somers     return LOG_ALERT;
208927145beSBrian Somers   }
209927145beSBrian Somers   return lev >= LogMIN && lev <= LogMAX ? LOG_INFO : 0;
210927145beSBrian Somers }
211af57ed9fSAtsushi Murai 
212927145beSBrian Somers const char *
213dd7e2610SBrian Somers log_Name(int id)
214927145beSBrian Somers {
21546df5aa7SBrian Somers   if (id == LogLOG)
21646df5aa7SBrian Somers     return "LOG";
217927145beSBrian Somers   return id < LogMIN || id > LogMAX ? "Unknown" : LogNames[id - 1];
218927145beSBrian Somers }
219af57ed9fSAtsushi Murai 
220af57ed9fSAtsushi Murai void
221dd7e2610SBrian Somers log_Keep(int id)
222af57ed9fSAtsushi Murai {
223927145beSBrian Somers   if (id >= LogMIN && id <= LogMAXCONF)
224927145beSBrian Somers     LogMask |= MSK(id);
225af57ed9fSAtsushi Murai }
226927145beSBrian Somers 
227927145beSBrian Somers void
228dd7e2610SBrian Somers log_KeepLocal(int id, u_long *mask)
229a1e8f937SBrian Somers {
230b6217683SBrian Somers   if (id >= LogMIN && id <= LogMAXCONF) {
231a1e8f937SBrian Somers     LogMaskLocal |= MSK(id);
232b6217683SBrian Somers     *mask |= MSK(id);
233b6217683SBrian Somers   }
234a1e8f937SBrian Somers }
235a1e8f937SBrian Somers 
236a1e8f937SBrian Somers void
237dd7e2610SBrian Somers log_Discard(int id)
238927145beSBrian Somers {
239927145beSBrian Somers   if (id >= LogMIN && id <= LogMAXCONF)
240927145beSBrian Somers     LogMask &= ~MSK(id);
241927145beSBrian Somers }
242927145beSBrian Somers 
243927145beSBrian Somers void
244dd7e2610SBrian Somers log_DiscardLocal(int id, u_long *mask)
245a1e8f937SBrian Somers {
246b6217683SBrian Somers   if (id >= LogMIN && id <= LogMAXCONF) {
247b6217683SBrian Somers     *mask &= ~MSK(id);
24867568487SBrian Somers     LogSetMaskLocal();
249b6217683SBrian Somers   }
250a1e8f937SBrian Somers }
251a1e8f937SBrian Somers 
252a1e8f937SBrian Somers void
253dd7e2610SBrian Somers log_DiscardAll()
254927145beSBrian Somers {
255927145beSBrian Somers   LogMask = 0;
256af57ed9fSAtsushi Murai }
257af57ed9fSAtsushi Murai 
258a1e8f937SBrian Somers void
259dd7e2610SBrian Somers log_DiscardAllLocal(u_long *mask)
260a1e8f937SBrian Somers {
26167568487SBrian Somers   *mask = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
26267568487SBrian Somers   LogSetMaskLocal();
263a1e8f937SBrian Somers }
264a1e8f937SBrian Somers 
265af57ed9fSAtsushi Murai int
266dd7e2610SBrian Somers log_IsKept(int id)
267af57ed9fSAtsushi Murai {
26846df5aa7SBrian Somers   if (id == LogLOG)
26946df5aa7SBrian Somers     return LOG_KEPT_SYSLOG;
270a1e8f937SBrian Somers   if (id < LogMIN || id > LogMAX)
271927145beSBrian Somers     return 0;
272a1e8f937SBrian Somers   if (id > LogMAXCONF)
273a1e8f937SBrian Somers     return LOG_KEPT_LOCAL | LOG_KEPT_SYSLOG;
274a1e8f937SBrian Somers 
275a1e8f937SBrian Somers   return ((LogMaskLocal & MSK(id)) ? LOG_KEPT_LOCAL : 0) |
276a1e8f937SBrian Somers     ((LogMask & MSK(id)) ? LOG_KEPT_SYSLOG : 0);
277af57ed9fSAtsushi Murai }
278af57ed9fSAtsushi Murai 
279b6217683SBrian Somers int
280dd7e2610SBrian Somers log_IsKeptLocal(int id, u_long mask)
281b6217683SBrian Somers {
282b6217683SBrian Somers   if (id < LogMIN || id > LogMAX)
283b6217683SBrian Somers     return 0;
284b6217683SBrian Somers   if (id > LogMAXCONF)
285b6217683SBrian Somers     return LOG_KEPT_LOCAL | LOG_KEPT_SYSLOG;
286b6217683SBrian Somers 
287b6217683SBrian Somers   return ((mask & MSK(id)) ? LOG_KEPT_LOCAL : 0) |
288b6217683SBrian Somers     ((LogMask & MSK(id)) ? LOG_KEPT_SYSLOG : 0);
289b6217683SBrian Somers }
290b6217683SBrian Somers 
291af57ed9fSAtsushi Murai void
292dd7e2610SBrian Somers log_Open(const char *Name)
293af57ed9fSAtsushi Murai {
294927145beSBrian Somers   openlog(Name, LOG_PID, LOG_DAEMON);
295af57ed9fSAtsushi Murai }
296af57ed9fSAtsushi Murai 
297af57ed9fSAtsushi Murai void
298dd7e2610SBrian Somers log_SetTun(int tunno)
299af57ed9fSAtsushi Murai {
300927145beSBrian Somers   LogTunno = tunno;
301af57ed9fSAtsushi Murai }
302af57ed9fSAtsushi Murai 
303af57ed9fSAtsushi Murai void
304dd7e2610SBrian Somers log_Close()
305af57ed9fSAtsushi Murai {
306927145beSBrian Somers   closelog();
307927145beSBrian Somers   LogTunno = -1;
3086ed9fb2fSBrian Somers }
309af57ed9fSAtsushi Murai 
310af57ed9fSAtsushi Murai void
311dd7e2610SBrian Somers log_Printf(int lev, const char *fmt,...)
31253c9f6c0SAtsushi Murai {
31353c9f6c0SAtsushi Murai   va_list ap;
314b6217683SBrian Somers   struct prompt *prompt;
315944f7098SBrian Somers 
316dd7e2610SBrian Somers   if (log_IsKept(lev)) {
317d93d3a9cSBrian Somers     char nfmt[200];
31853c9f6c0SAtsushi Murai 
31990d25921SBrian Somers     va_start(ap, fmt);
320b4f63b0bSBrian Somers     if (promptlist && (log_IsKept(lev) & LOG_KEPT_LOCAL)) {
321dd7e2610SBrian Somers       if ((log_IsKept(LogTUN) & LOG_KEPT_LOCAL) && LogTunno != -1)
3221384bd27SBrian Somers         snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
323dd7e2610SBrian Somers 	         LogTunno, log_Name(lev), fmt);
324927145beSBrian Somers       else
325dd7e2610SBrian Somers         snprintf(nfmt, sizeof nfmt, "%s: %s", log_Name(lev), fmt);
326b6217683SBrian Somers 
327b4f63b0bSBrian Somers       if (log_PromptContext && lev == LogWARN)
328b4f63b0bSBrian Somers         /* Warnings just go to the current prompt */
329b4f63b0bSBrian Somers         prompt_vPrintf(log_PromptContext, nfmt, ap);
330b4f63b0bSBrian Somers       else for (prompt = promptlist; prompt; prompt = prompt->next)
331b6217683SBrian Somers         if (lev > LogMAXCONF || (prompt->logmask & MSK(lev)))
332b6217683SBrian Somers           prompt_vPrintf(prompt, nfmt, ap);
333a1e8f937SBrian Somers     }
33490d25921SBrian Somers     va_end(ap);
335a1e8f937SBrian Somers 
33690d25921SBrian Somers     va_start(ap, fmt);
3370f2f3eb3SBrian Somers     if ((log_IsKept(lev) & LOG_KEPT_SYSLOG) &&
338b4f63b0bSBrian Somers         (lev != LogWARN || !log_PromptContext)) {
339dd7e2610SBrian Somers       if ((log_IsKept(LogTUN) & LOG_KEPT_SYSLOG) && LogTunno != -1)
3401384bd27SBrian Somers         snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
341dd7e2610SBrian Somers 	         LogTunno, log_Name(lev), fmt);
342a1e8f937SBrian Somers       else
343dd7e2610SBrian Somers         snprintf(nfmt, sizeof nfmt, "%s: %s", log_Name(lev), fmt);
344927145beSBrian Somers       vsyslog(syslogLevel(lev), nfmt, ap);
345927145beSBrian Somers     }
34653c9f6c0SAtsushi Murai     va_end(ap);
34753c9f6c0SAtsushi Murai   }
34890d25921SBrian Somers }
34953c9f6c0SAtsushi Murai 
35053c9f6c0SAtsushi Murai void
351dd7e2610SBrian Somers log_DumpBp(int lev, const char *hdr, const struct mbuf *bp)
352af57ed9fSAtsushi Murai {
353dd7e2610SBrian Somers   if (log_IsKept(lev)) {
354ba16c840SBrian Somers     char buf[68];
355ba16c840SBrian Somers     char *b, *c;
3569773f8c0SBrian Somers     const u_char *ptr;
357a9e8f807SBrian Somers     int f;
358a9e8f807SBrian Somers 
359a9e8f807SBrian Somers     if (hdr && *hdr)
360dd7e2610SBrian Somers       log_Printf(lev, "%s\n", hdr);
361a9e8f807SBrian Somers 
362a9e8f807SBrian Somers     b = buf;
363ba16c840SBrian Somers     c = b + 50;
364a9e8f807SBrian Somers     do {
36526af0ae9SBrian Somers       f = bp->m_len;
3669773f8c0SBrian Somers       ptr = CONST_MBUF_CTOP(bp);
367a9e8f807SBrian Somers       while (f--) {
368ba16c840SBrian Somers 	sprintf(b, " %02x", (int) *ptr);
369ba16c840SBrian Somers         *c++ = isprint(*ptr) ? *ptr : '.';
370ba16c840SBrian Somers         ptr++;
371a9e8f807SBrian Somers         b += 3;
372ba16c840SBrian Somers         if (b == buf + 48) {
373ba16c840SBrian Somers           memset(b, ' ', 2);
3749cf01ccfSBrian Somers           *c = '\0';
3759cf01ccfSBrian Somers           log_Printf(lev, "%s\n", buf);
376a9e8f807SBrian Somers           b = buf;
377ba16c840SBrian Somers           c = b + 50;
378a9e8f807SBrian Somers         }
379a9e8f807SBrian Somers       }
38026af0ae9SBrian Somers     } while ((bp = bp->m_next) != NULL);
381a9e8f807SBrian Somers 
382a1e8f937SBrian Somers     if (b > buf) {
383ba16c840SBrian Somers       memset(b, ' ', 50 - (b - buf));
3849cf01ccfSBrian Somers       *c = '\0';
3859cf01ccfSBrian Somers       log_Printf(lev, "%s\n", buf);
386a9e8f807SBrian Somers     }
387af57ed9fSAtsushi Murai   }
388a1e8f937SBrian Somers }
389af57ed9fSAtsushi Murai 
390af57ed9fSAtsushi Murai void
391dd7e2610SBrian Somers log_DumpBuff(int lev, const char *hdr, const u_char *ptr, int n)
392af57ed9fSAtsushi Murai {
393dd7e2610SBrian Somers   if (log_IsKept(lev)) {
394ba16c840SBrian Somers     char buf[68];
395ba16c840SBrian Somers     char *b, *c;
396af57ed9fSAtsushi Murai 
397927145beSBrian Somers     if (hdr && *hdr)
398dd7e2610SBrian Somers       log_Printf(lev, "%s\n", hdr);
399927145beSBrian Somers     while (n > 0) {
400927145beSBrian Somers       b = buf;
401ba16c840SBrian Somers       c = b + 50;
402ba16c840SBrian Somers       for (b = buf; b != buf + 48 && n--; b += 3, ptr++) {
403ba16c840SBrian Somers 	sprintf(b, " %02x", (int) *ptr);
404ba16c840SBrian Somers         *c++ = isprint(*ptr) ? *ptr : '.';
405ba16c840SBrian Somers       }
406ba16c840SBrian Somers       memset(b, ' ', 50 - (b - buf));
4079cf01ccfSBrian Somers       *c = '\0';
4089cf01ccfSBrian Somers       log_Printf(lev, "%s\n", buf);
409af57ed9fSAtsushi Murai     }
410af57ed9fSAtsushi Murai   }
411c6c740beSBrian Somers }
412b6217683SBrian Somers 
413b6217683SBrian Somers int
414b6217683SBrian Somers log_ShowLevel(struct cmdargs const *arg)
415b6217683SBrian Somers {
416b6217683SBrian Somers   int i;
417b6217683SBrian Somers 
418b6217683SBrian Somers   prompt_Printf(arg->prompt, "Log:  ");
419b6217683SBrian Somers   for (i = LogMIN; i <= LogMAX; i++)
420dd7e2610SBrian Somers     if (log_IsKept(i) & LOG_KEPT_SYSLOG)
421dd7e2610SBrian Somers       prompt_Printf(arg->prompt, " %s", log_Name(i));
422b6217683SBrian Somers 
423b6217683SBrian Somers   prompt_Printf(arg->prompt, "\nLocal:");
424b6217683SBrian Somers   for (i = LogMIN; i <= LogMAX; i++)
425dd7e2610SBrian Somers     if (log_IsKeptLocal(i, arg->prompt->logmask) & LOG_KEPT_LOCAL)
426dd7e2610SBrian Somers       prompt_Printf(arg->prompt, " %s", log_Name(i));
427b6217683SBrian Somers 
428b6217683SBrian Somers   prompt_Printf(arg->prompt, "\n");
429b6217683SBrian Somers 
430b6217683SBrian Somers   return 0;
431b6217683SBrian Somers }
432b6217683SBrian Somers 
433b6217683SBrian Somers int
434b6217683SBrian Somers log_SetLevel(struct cmdargs const *arg)
435b6217683SBrian Somers {
436b6217683SBrian Somers   int i, res, argc, local;
437b6217683SBrian Somers   char const *const *argv, *argp;
438b6217683SBrian Somers 
43925092092SBrian Somers   argc = arg->argc - arg->argn;
44025092092SBrian Somers   argv = arg->argv + arg->argn;
441b6217683SBrian Somers   res = 0;
442b6217683SBrian Somers 
443b6217683SBrian Somers   if (argc == 0 || strcasecmp(argv[0], "local"))
444b6217683SBrian Somers     local = 0;
445b6217683SBrian Somers   else {
446b6217683SBrian Somers     if (arg->prompt == NULL) {
4479b996792SBrian Somers       log_Printf(LogWARN, "set log local: Only available on the"
4489b996792SBrian Somers                  " command line\n");
449b6217683SBrian Somers       return 1;
450b6217683SBrian Somers     }
451b6217683SBrian Somers     argc--;
452b6217683SBrian Somers     argv++;
453b6217683SBrian Somers     local = 1;
454b6217683SBrian Somers   }
455b6217683SBrian Somers 
456e43ebac1SBrian Somers   if (argc == 0 || (argv[0][0] != '+' && argv[0][0] != '-')) {
457b6217683SBrian Somers     if (local)
458dd7e2610SBrian Somers       log_DiscardAllLocal(&arg->prompt->logmask);
459b6217683SBrian Somers     else
460dd7e2610SBrian Somers       log_DiscardAll();
461e43ebac1SBrian Somers   }
462b6217683SBrian Somers 
463b6217683SBrian Somers   while (argc--) {
464b6217683SBrian Somers     argp = **argv == '+' || **argv == '-' ? *argv + 1 : *argv;
4657f03ca53SBrian Somers     /* Special case 'all' */
4667f03ca53SBrian Somers     if (strcasecmp(argp, "all") == 0) {
4677f03ca53SBrian Somers         if (**argv == '-') {
4687f03ca53SBrian Somers           if (local)
4697f03ca53SBrian Somers             for (i = LogMIN; i <= LogMAX; i++)
4707f03ca53SBrian Somers               log_DiscardLocal(i, &arg->prompt->logmask);
4717f03ca53SBrian Somers           else
4727f03ca53SBrian Somers             for (i = LogMIN; i <= LogMAX; i++)
4737f03ca53SBrian Somers               log_Discard(i);
4747f03ca53SBrian Somers         } else if (local)
4757f03ca53SBrian Somers           for (i = LogMIN; i <= LogMAX; i++)
4767f03ca53SBrian Somers             log_KeepLocal(i, &arg->prompt->logmask);
4777f03ca53SBrian Somers         else
4787f03ca53SBrian Somers           for (i = LogMIN; i <= LogMAX; i++)
4797f03ca53SBrian Somers             log_Keep(i);
4807f03ca53SBrian Somers         argv++;
4817f03ca53SBrian Somers         continue;
4827f03ca53SBrian Somers     }
483b6217683SBrian Somers     for (i = LogMIN; i <= LogMAX; i++)
484dd7e2610SBrian Somers       if (strcasecmp(argp, log_Name(i)) == 0) {
485e43ebac1SBrian Somers 	if (**argv == '-') {
486b6217683SBrian Somers           if (local)
487dd7e2610SBrian Somers             log_DiscardLocal(i, &arg->prompt->logmask);
488b6217683SBrian Somers           else
489dd7e2610SBrian Somers 	    log_Discard(i);
490e43ebac1SBrian Somers 	} else if (local)
491dd7e2610SBrian Somers           log_KeepLocal(i, &arg->prompt->logmask);
492b6217683SBrian Somers         else
493dd7e2610SBrian Somers           log_Keep(i);
494b6217683SBrian Somers 	break;
495b6217683SBrian Somers       }
496b6217683SBrian Somers     if (i > LogMAX) {
497dd7e2610SBrian Somers       log_Printf(LogWARN, "%s: Invalid log value\n", argp);
498b6217683SBrian Somers       res = -1;
499b6217683SBrian Somers     }
500b6217683SBrian Somers     argv++;
501b6217683SBrian Somers   }
502b6217683SBrian Somers   return res;
503b6217683SBrian Somers }
504b6217683SBrian Somers 
505b6217683SBrian Somers int
506b6217683SBrian Somers log_ShowWho(struct cmdargs const *arg)
507b6217683SBrian Somers {
508b6217683SBrian Somers   struct prompt *p;
509b6217683SBrian Somers 
5100f2f3eb3SBrian Somers   for (p = promptlist; p; p = p->next) {
511565e35e5SBrian Somers     prompt_Printf(arg->prompt, "%s (%s)", p->src.type, p->src.from);
512f91ad6b0SBrian Somers     if (p == arg->prompt)
513f91ad6b0SBrian Somers       prompt_Printf(arg->prompt, " *");
514f91ad6b0SBrian Somers     if (!p->active)
515f91ad6b0SBrian Somers       prompt_Printf(arg->prompt, " ^Z");
516f91ad6b0SBrian Somers     prompt_Printf(arg->prompt, "\n");
517f91ad6b0SBrian Somers   }
518b6217683SBrian Somers 
519b6217683SBrian Somers   return 0;
520b6217683SBrian Somers }
521