1 /**
2  * @file
3  * Logging Dispatcher
4  *
5  * @authors
6  * Copyright (C) 2017 Richard Russon <rich@flatcap.org>
7  *
8  * @copyright
9  * This program is free software: you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License as published by the Free Software
11  * Foundation, either version 2 of the License, or (at your option) any later
12  * version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef MUTT_LIB_LOGGING_H
24 #define MUTT_LIB_LOGGING_H
25 
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <time.h>
29 #include "queue.h"
30 
31 /**
32  * enum LogLevel - Names for the Logging Levels
33  */
34 enum LogLevel
35 {
36   LL_PERROR  = -3, ///< Log perror (using errno)
37   LL_ERROR   = -2, ///< Log error
38   LL_WARNING = -1, ///< Log warning
39   LL_MESSAGE =  0, ///< Log informational message
40   LL_DEBUG1  =  1, ///< Log at debug level 1
41   LL_DEBUG2  =  2, ///< Log at debug level 2
42   LL_DEBUG3  =  3, ///< Log at debug level 3
43   LL_DEBUG4  =  4, ///< Log at debug level 4
44   LL_DEBUG5  =  5, ///< Log at debug level 5
45   LL_NOTIFY  =  6, ///< Log of notifications
46 
47   LL_MAX,
48 };
49 
50 /**
51  * @defgroup logging_api Logging API
52  *
53  * Prototype for a Logging Function
54  *
55  * @param stamp    Unix time (optional)
56  * @param file     Source file
57  * @param line     Source line
58  * @param function Source function
59  * @param level    Logging level, e.g. #LL_WARNING
60  * @param ...      Format string and parameters, like printf()
61  * @retval -1 Error
62  * @retval  0 Success, filtered
63  * @retval >0 Success, number of characters written
64  */
65 typedef int (*log_dispatcher_t)(time_t stamp, const char *file, int line, const char *function, enum LogLevel level, ...);
66 
67 extern log_dispatcher_t MuttLogger;
68 
69 /**
70  * struct LogLine - A Log line
71  */
72 struct LogLine
73 {
74   time_t time;                   ///< Timestamp of the message
75   const char *file;              ///< Source file
76   int line;                      ///< Line number in source file
77   const char *function;          ///< C function
78   enum LogLevel level;           ///< Log level, e.g. #LL_DEBUG1
79   char *message;                 ///< Message to be logged
80   STAILQ_ENTRY(LogLine) entries; ///< Linked list
81 };
82 STAILQ_HEAD(LogLineList, LogLine);
83 
84 #define mutt_debug(LEVEL, ...) MuttLogger(0, __FILE__, __LINE__, __func__, LEVEL,      __VA_ARGS__) ///< @ingroup logging_api
85 #define mutt_warning(...)      MuttLogger(0, __FILE__, __LINE__, __func__, LL_WARNING, __VA_ARGS__) ///< @ingroup logging_api
86 #define mutt_message(...)      MuttLogger(0, __FILE__, __LINE__, __func__, LL_MESSAGE, __VA_ARGS__) ///< @ingroup logging_api
87 #define mutt_error(...)        MuttLogger(0, __FILE__, __LINE__, __func__, LL_ERROR,   __VA_ARGS__) ///< @ingroup logging_api
88 #define mutt_perror(...)       MuttLogger(0, __FILE__, __LINE__, __func__, LL_PERROR,  __VA_ARGS__) ///< @ingroup logging_api
89 
90 int  log_disp_file    (time_t stamp, const char *file, int line, const char *function, enum LogLevel level, ...);
91 int  log_disp_null    (time_t stamp, const char *file, int line, const char *function, enum LogLevel level, ...);
92 int  log_disp_queue   (time_t stamp, const char *file, int line, const char *function, enum LogLevel level, ...);
93 int  log_disp_terminal(time_t stamp, const char *file, int line, const char *function, enum LogLevel level, ...);
94 
95 int  log_queue_add(struct LogLine *ll);
96 void log_queue_empty(void);
97 void log_queue_flush(log_dispatcher_t disp);
98 int  log_queue_save(FILE *fp);
99 void log_queue_set_max_size(int size);
100 
101 void log_file_close(bool verbose);
102 int  log_file_open(bool verbose);
103 bool log_file_running(void);
104 int  log_file_set_filename(const char *file, bool verbose);
105 int  log_file_set_level(enum LogLevel level, bool verbose);
106 void log_file_set_version(const char *version);
107 
108 #endif /* MUTT_LIB_LOGGING_H */
109