summaryrefslogtreecommitdiff
path: root/web/src/alpined.d/debug.c
blob: d3c4d2b723274f45105621af9bdb18eb01c3bc21 (plain)
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
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: wimap.c 73 2006-06-13 16:46:59Z hubert@u.washington.edu $";
#endif

/* ========================================================================
 * Copyright 2006-2007 University of Washington
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * ========================================================================
 */

/*======================================================================
    debug.c
    Provide debug support routines
 ====*/

#include <system.h>
#include <general.h>

#include "../../../c-client/c-client.h"

#include "../../../pith/state.h"
#include "../../../pith/debug.h"

#include "debug.h"


#define	MAX_DEBUG_FMT	1024


#ifndef	DEBUG
/*
 * Preserve debug for syslog trace
 */
int	debug;
#endif


void
debug_init(void)
{
#if	HAVE_SYSLOG
    openlog("alpined", LOG_PID, LOG_MAIL);
#endif
}


void
output_debug_msg(int dlevel, char *fmt, ...)
{
    /* always write SYSDBG */
    if((dlevel & SYSDBG) || dlevel <= debug){
#if	HAVE_SYSLOG
	va_list args;
	char    fmt2[MAX_DEBUG_FMT], *p, *q, *trailing = NULL;
	int	priority = LOG_DEBUG, leading = 1;

	/* whack nl's */
	for(p = fmt, q = fmt2; *p && p - fmt < MAX_DEBUG_FMT - 2; p++){
	    if(*p == '\n'){
		if(!leading && !trailing)
		  trailing = q;
	    }
	    else{
		leading = 0;
		if(trailing){
		    *q++ = '_';
		    trailing = NULL;
		}

		*q++ = *p;
	    }
	}

	*q = '\0';
	if(trailing)
	  *trailing = '\0';

	if(dlevel & SYSDBG)
	  switch(dlevel){
	    case SYSDBG_ALERT : priority = LOG_ALERT; break;
	    case SYSDBG_ERR   : priority = LOG_ERR;   break;
	    case SYSDBG_INFO  : priority = LOG_INFO;  break;
	    default	      : priority = LOG_DEBUG; break;
	  }


	va_start(args, fmt);
	vsyslog(priority, fmt2, args);
	va_end(args);
#else
# error Write something to record error/debugging output
#endif
    }
}

#ifdef DEBUG

void
dump_configuration(int brief)
{
    dprint((8, "asked to dump_configuration"));
}


void
dump_contexts(void)
{
    dprint((8, "asked to dump_contexts"));
}


void
setup_imap_debug(void)
{
    int olddebug;

    olddebug = debug;

    if(debug > 7)
      ps_global->debug_imap = 4;
    else if(debug > 6)
      ps_global->debug_imap = 3;
    else if(debug > 4)
      ps_global->debug_imap = 2;
    else if(debug > 2)
      ps_global->debug_imap = 1;
    else
      ps_global->debug_imap = 0;

    if(ps_global->mail_stream){
	if(ps_global->debug_imap > 0){
	    mail_debug(ps_global->mail_stream);
	}
	else{
	    mail_nodebug(ps_global->mail_stream);
	}
    }

    if(debug > 7 && olddebug <= 7)
      mail_parameters(NULL, SET_TCPDEBUG, (void *) TRUE);
    else if(debug <= 7 && olddebug > 7 && !ps_global->debugmem)
      mail_parameters(NULL, SET_TCPDEBUG, (void *) FALSE);

}
#endif	/* DEBUG */