-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.h
154 lines (121 loc) · 4.56 KB
/
log.h
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
152
153
154
#ifndef _LOG_H
#define _LOG_H
#include "common.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
/* ===[ Data types ]========================================================= */
/**
* Allows for up to 8 different log levels
*/
typedef unsigned char loglevel_t;
/**
* A log file. (!)
*/
struct log_file {
FILE *file;
loglevel_t maxlevel;
bool wrap, prompted, auto_prompt;
char prompt;
struct log_file *next;
};
/* ===[ Log levels ]========================================================= */
#define LOG_DEBUG 1
#define LOG_USERINPUT 2
#define LOG_ERROR 4
#define LOG_ERROR_VERBOSE 8
#define LOG_WARNING 16
#define LOG_INFO 32
#define LOG_INFO_VERBOSE 64
#define LOG_CONSOLE 128
#define LOG_ALL 255
/* ===[ Constants ] ========================================================= */
#define TIMESTAMP_FORMAT "%5d.%03hd "
/* ===[ Data ]=============================================================== */
/**
* The list of opened log_files.
*/
extern struct log_file *log_files;
/* ===[ Functions ]========================================================== */
/**
* Create a new log_file using a new or an existing file on the filesystem.
* The file is opened in APPEND mode.
* Log messages are written on the log_file if their (level & maxlevel) != 0.
* All log_files opened with this function are wrapped.
* @param const char *filename the filename of a new or existing file on the
* filesystem
* @param loglevel_t maxlevel a combination of log levels obtained by or-ing
* multiple log level constants
* @return the newly created log_file
*/
struct log_file *open_log(const char *filename, loglevel_t maxlevel);
/**
* Create a new log_file using an already open FILE descriptor (writable). This
* function can be used to log messages to special FILEs, e.g. stdout, stderr.
* Log messages are written on the log_file if their (level & maxlevel) != 0.
* Wrapped log_files will have log delimiters with opening and closing times and
* the log level associated to every single log message.
* @param FILE *file a writable FILE descriptor
* @param loglevel_t maxlevel a combination of log levels obtained by or-ing
* multiple log level constants
* @param bool wrap whether the log_file should be wrapped or unwrapped
* @return the newly created log_file
*/
struct log_file *new_log(FILE *file, loglevel_t maxlevel, bool wrap);
/**
* Close a log_file.
* Before closing, write a closing delimiter if the log_file is wrapped.
* @param struct log_file *logfile
* @return the ->next member of the now closed log_file
*/
struct log_file *close_log(struct log_file*);
/**
* Close all open log_files.
* @see close_log(struct log_file*)
*/
void close_logs(void);
/**
* Log a message to all open log_files with (level & maxlevel) != 0.
* @param loglevel_t level the log level of the message, normally one of the
* LOG_* constants, except LOG_ALL. OR-ing multiple LOG_* will cause the message
* to be printed on all log_files associated with at least one of the OR-ed
* levels and to show as marked with the *smallest* log_level among the matching
* ones.
* @param const char *message the string to be logged, preferably without any
* trailing space
* @return int the number of open log_files with matching level
*/
int log_message(loglevel_t level, const char *message);
/**
* Print a multiline message. Each line will have its own level mark.
* @param loglevel_t level @see log_message()
* @param const char *message the multiline message, lines are separated by \n
* @return int @see log_message()
*/
int log_multiline(loglevel_t level, const char *message);
/**
* Log a formatted message to all open log_files with (level & maxlevel) != 0.
* @see printf(const char *format, ...)
* @param loglevel_t level @see log_message()
* @param const char *format the format string containing some format specifiers
* @param ... additional arguments, used to replace specifiers in the format
* string
* @return int @see log_message()
*/
int flog_message(loglevel_t level, const char *format, ...);
/**
* Log a message of level LOG_ERROR followed by a description of the error
* specified in errno.
* @see strerror()
* @param const char *message
* @return int @see log_message()
*/
int log_error(const char *message);
/**
* Print a prompt on a log_file, if logfile->prompt is set.
* @param struct log_file *logfile
* @return int 1 if logfile->prompt is set, 0 otherwise
*/
int log_prompt(struct log_file *file);
/* ========================================================================== */
#endif