1 /*
2  * wzdftpd - a modular and cool ftp server
3  * Copyright (C) 2002-2004  Pierre Chifflier
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * As a special exemption, Pierre Chifflier
20  * and other respective copyright holders give permission to link this program
21  * with OpenSSL, and distribute the resulting executable, without including
22  * the source code for OpenSSL in the source distribution.
23  */
24 
25 #ifndef __WZD_LOG__
26 #define __WZD_LOG__
27 
28 /* colors */
29 
30 #ifndef _MSC_VER
31 #define CLR_BOLD	""
32 
33 #define	CLR_BLUE	""
34 #define	CLR_CYAN	""
35 #define	CLR_GREEN	""
36 #define	CLR_RED		""
37 
38 #define	CLR_NOCOLOR	""
39 #else
40 #define CLR_BOLD	""
41 
42 #define	CLR_BLUE	""
43 #define	CLR_CYAN	""
44 #define	CLR_GREEN	""
45 #define	CLR_RED		""
46 
47 #define	CLR_NOCOLOR	""
48 #endif
49 
50 
51 /* DEBUG & LOG */
52 #define LEVEL_LOWEST	0
53 #define	LEVEL_FLOOD	1
54 #define	LEVEL_INFO	3
55 #define	LEVEL_NORMAL	5
56 #define	LEVEL_HIGH	7
57 #define	LEVEL_CRITICAL	9
58 
59 /** \brief Initialize logging facilities
60  *
61  * Init structures used for logging
62  */
63 int log_init(void);
64 
65 /** \brief Open file for logging
66  */
67 int log_open(const char * filename, int filemode);
68 
69 /** \brief Close log file
70  */
71 void log_close(int fd);
72 
73 /** \brief Close all log descriptors
74  */
75 void log_fini(void);
76 
77 /** \brief Open log file descriptor, and set mainConfig->logfile
78  * to the corresponding FILE *
79  * \deprecated Use \ref log_open
80  */
81 int log_open_old(const char *filename, int filemode);
82 
83 /** \brief Close logfile opened using \ref log_open_old
84  * \deprecated Use \ref log_close
85  */
86 void log_close_old(void);
87 
88 /** \brief Get file descriptor asociated to log level
89  *
90  * Get file decriptor corresponding to all messages sent to the
91  * log level.
92  */
93 int log_get(unsigned int level);
94 
95 /** \brief Redirect log level to file descriptor
96  *
97  * Set file decriptor corresponding to all messages sent to the
98  * log level.
99  *
100  * \note fd must have been returned from log_open()
101  */
102 int log_set(unsigned int level, int fd);
103 
104 /** \brief Use syslog for specified level
105  *
106  * Set value to 0 to disable syslogging level, otherwise us syslog
107  */
108 int log_set_syslog(unsigned int level, int syslog_value);
109 
110 /** \brief Open file of type xferlog and returns file descriptor if ok
111  */
112 int xferlog_open(const char *filename, unsigned int filemode);
113 
114 /** \brief Close xferlog file opened using \ref xferlog_open
115  */
116 void xferlog_close(int fd);
117 
118 /** \brief Send message to the server logger
119  */
120 void out_log(int level,const char *fmt,...)
121 #ifdef __GNUC__
122   __attribute__((__format__(printf,2,3)))
123 #endif
124 ;
125 
126 /** \brief Send message to the server error stream
127  * \note This function does nothing in release mode
128  */
129 void out_err(int level, const char *fmt,...)
130 #ifdef __GNUC__
131   __attribute__((__format__(printf,2,3)))
132 #endif
133 ;
134 
135 /** \brief Automatically create and send the message in xferlog format
136  * after a file transfer has been completed or interrupted.
137  */
138 void out_xferlog(struct wzd_context_t * context, int is_complete);
139 
140 /** \brief Format in a standard way and send message to the server logger
141  * using LEVEL_NORMAL
142  *
143  * A newline is appended.
144  */
145 void log_message(const char *event, const char *fmt, ...)
146 #ifdef __GNUC__
147   __attribute__((__format__(printf,2,3)))
148 #endif
149 ;
150 
151 struct memory_log_t {
152   int size; /**< number of messages kept */
153   char ** data;
154 };
155 
156 /** \brief Return a pointer to the log buffer (last log messages, stored in memory)
157  *
158  * The structure must not be changed or freed
159  */
160 struct memory_log_t * get_log_buffer(void);
161 
162 /** \brief Convert a string containing the log level name ("lowest", "flood", etc.)
163  * into the corresponding constant (LEVEL_LOWEST)
164  * \return The constant, or -1 on error
165  */
166 int str2loglevel(const char *s);
167 
168 /** \brief Convert a standard log level (LEVEL_LOWEST) into the
169  * corresponding string ("lowest")
170  * \return The constant string, or an empty string ("") on error
171  */
172 const char * loglevel2str(int l);
173 
174 #endif /* __WZD_LOG__ */
175