#include <stdbool.h>
#include <stdio.h>
#include <time.h>
#include "queue.h"
Go to the source code of this file.
|
#define | mutt_debug(LEVEL, ...) MuttLogger(0, __FILE__, __LINE__, __func__, LEVEL, __VA_ARGS__) |
|
#define | mutt_warning(...) MuttLogger(0, __FILE__, __LINE__, __func__, LL_WARNING, __VA_ARGS__) |
|
#define | mutt_message(...) MuttLogger(0, __FILE__, __LINE__, __func__, LL_MESSAGE, __VA_ARGS__) |
|
#define | mutt_error(...) MuttLogger(0, __FILE__, __LINE__, __func__, LL_ERROR, __VA_ARGS__) |
|
#define | mutt_perror(...) MuttLogger(0, __FILE__, __LINE__, __func__, LL_PERROR, __VA_ARGS__) |
|
|
typedef int(* | log_dispatcher_t) (time_t stamp, const char *file, int line, const char *function, enum LogLevel level,...) |
| Prototype for a logging function. More...
|
|
|
enum | LogLevel {
LL_PERROR = -3,
LL_ERROR = -2,
LL_WARNING = -1,
LL_MESSAGE = 0,
LL_DEBUG1 = 1,
LL_DEBUG2 = 2,
LL_DEBUG3 = 3,
LL_DEBUG4 = 4,
LL_DEBUG5 = 5,
LL_NOTIFY = 6,
LL_MAX
} |
| Names for the Logging Levels. More...
|
|
|
| STAILQ_HEAD (LogLineList, LogLine) |
|
int | log_disp_file (time_t stamp, const char *file, int line, const char *function, enum LogLevel level,...) |
| Save a log line to a file - Implements log_dispatcher_t. More...
|
|
int | log_disp_null (time_t stamp, const char *file, int line, const char *function, enum LogLevel level,...) |
| Discard log lines - Implements log_dispatcher_t. More...
|
|
int | log_disp_queue (time_t stamp, const char *file, int line, const char *function, enum LogLevel level,...) |
| Save a log line to an internal queue - Implements log_dispatcher_t. More...
|
|
int | log_disp_terminal (time_t stamp, const char *file, int line, const char *function, enum LogLevel level,...) |
| Save a log line to the terminal - Implements log_dispatcher_t. More...
|
|
int | log_queue_add (struct LogLine *ll) |
| Add a LogLine to the queue. More...
|
|
void | log_queue_empty (void) |
| Free the contents of the queue. More...
|
|
void | log_queue_flush (log_dispatcher_t disp) |
| Replay the log queue. More...
|
|
int | log_queue_save (FILE *fp) |
| Save the contents of the queue to a temporary file. More...
|
|
void | log_queue_set_max_size (int size) |
| Set a upper limit for the queue length. More...
|
|
void | log_file_close (bool verbose) |
| Close the log file. More...
|
|
int | log_file_open (bool verbose) |
| Start logging to a file. More...
|
|
bool | log_file_running (void) |
| Is the log file running? More...
|
|
int | log_file_set_filename (const char *file, bool verbose) |
| Set the filename for the log. More...
|
|
int | log_file_set_level (enum LogLevel level, bool verbose) |
| Set the logging level. More...
|
|
void | log_file_set_version (const char *version) |
| Set the program's version number. More...
|
|
Logging Dispatcher
- Authors
-
- Copyright
- This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
Definition in file logging.h.
◆ mutt_debug
#define mutt_debug |
( |
|
LEVEL, |
|
|
|
... |
|
) |
| MuttLogger(0, __FILE__, __LINE__, __func__, LEVEL, __VA_ARGS__) |
◆ mutt_warning
◆ mutt_message
◆ mutt_error
#define mutt_error |
( |
|
... | ) |
MuttLogger(0, __FILE__, __LINE__, __func__, LL_ERROR, __VA_ARGS__) |
◆ mutt_perror
◆ log_dispatcher_t
typedef int(* log_dispatcher_t) (time_t stamp, const char *file, int line, const char *function, enum LogLevel level,...) |
Prototype for a logging function.
- Parameters
-
stamp | Unix time (optional) |
file | Source file |
line | Source line |
function | Source function |
level | Logging level, e.g. LL_WARNING |
... | Format string and parameters, like printf() |
- Return values
-
-1 | Error |
0 | Success, filtered |
>0 | Success, number of characters written |
Definition at line 62 of file logging.h.
◆ LogLevel
Names for the Logging Levels.
Enumerator |
---|
LL_PERROR | Log perror (using errno)
|
LL_ERROR | Log error.
|
LL_WARNING | Log warning.
|
LL_MESSAGE | Log informational message.
|
LL_DEBUG1 | Log at debug level 1.
|
LL_DEBUG2 | Log at debug level 2.
|
LL_DEBUG3 | Log at debug level 3.
|
LL_DEBUG4 | Log at debug level 4.
|
LL_DEBUG5 | Log at debug level 5.
|
LL_NOTIFY | Log of notifications.
|
LL_MAX | |
Definition at line 34 of file logging.h.
◆ STAILQ_HEAD()
STAILQ_HEAD |
( |
LogLineList |
, |
|
|
LogLine |
|
|
) |
| |
◆ log_disp_file()
int log_disp_file |
( |
time_t |
stamp, |
|
|
const char * |
file, |
|
|
int |
line, |
|
|
const char * |
function, |
|
|
enum LogLevel |
level, |
|
|
|
... |
|
) |
| |
Save a log line to a file - Implements log_dispatcher_t.
This log dispatcher saves a line of text to a file. The format is:
[TIMESTAMP]<LEVEL> FUNCTION() FORMATTED-MESSAGE
The caller must first set LogFileName and LogFileLevel, then call log_file_open(). Any logging above LogFileLevel will be ignored.
If stamp is 0, then the current time will be used.
Definition at line 246 of file logging.c.
256 function =
"UNKNOWN";
263 const char *fmt = va_arg(ap,
const char *);
269 fprintf(
LogFileFP,
": %s\n", strerror(err));
◆ log_disp_null()
int log_disp_null |
( |
time_t |
stamp, |
|
|
const char * |
file, |
|
|
int |
line, |
|
|
const char * |
function, |
|
|
enum LogLevel |
level, |
|
|
|
... |
|
) |
| |
◆ log_disp_queue()
int log_disp_queue |
( |
time_t |
stamp, |
|
|
const char * |
file, |
|
|
int |
line, |
|
|
const char * |
function, |
|
|
enum LogLevel |
level, |
|
|
|
... |
|
) |
| |
Save a log line to an internal queue - Implements log_dispatcher_t.
This log dispatcher saves a line of text to a queue. The format string and parameters are expanded and the other parameters are stored as they are.
- See also
- log_queue_set_max_size(), log_queue_flush(), log_queue_empty()
- Warning
- Log lines are limited to 1024 bytes.
Definition at line 400 of file logging.c.
403 char buf[1024] = { 0 };
408 const char *fmt = va_arg(ap,
const char *);
409 int ret = vsnprintf(buf,
sizeof(buf), fmt, ap);
414 ret += snprintf(buf + ret,
sizeof(buf) - ret,
": %s", strerror(err));
◆ log_disp_terminal()
int log_disp_terminal |
( |
time_t |
stamp, |
|
|
const char * |
file, |
|
|
int |
line, |
|
|
const char * |
function, |
|
|
enum LogLevel |
level, |
|
|
|
... |
|
) |
| |
Save a log line to the terminal - Implements log_dispatcher_t.
This log dispatcher saves a line of text to the terminal. The format is:
[TIMESTAMP]<LEVEL> FUNCTION() FORMATTED-MESSAGE
- Note
- The output will be coloured using ANSI escape sequences, unless the output is redirected.
Definition at line 441 of file logging.c.
451 const char *fmt = va_arg(ap,
const char *);
452 int ret = vsnprintf(buf,
sizeof(buf), fmt, ap);
460 bool tty = (isatty(fileno(fp)) == 1);
488 ret += fprintf(fp,
"\033[1;%dm", colour);
493 ret += fprintf(fp,
": %s", strerror(err));
496 ret += fprintf(fp,
"\033[0m");
498 ret += fprintf(fp,
"\n");
◆ log_queue_add()
int log_queue_add |
( |
struct LogLine * |
ll | ) |
|
Add a LogLine to the queue.
- Parameters
-
- Return values
-
If LogQueueMax is non-zero, the queue will be limited to this many items.
Definition at line 287 of file logging.c.
◆ log_queue_empty()
void log_queue_empty |
( |
void |
| ) |
|
Free the contents of the queue.
Free any log lines in the queue.
Definition at line 326 of file logging.c.
◆ log_queue_flush()
Replay the log queue.
- Parameters
-
Pass all of the log entries in the queue to the log dispatcher provided. The queue will be emptied afterwards.
Definition at line 348 of file logging.c.
◆ log_queue_save()
int log_queue_save |
( |
FILE * |
fp | ) |
|
Save the contents of the queue to a temporary file.
- Parameters
-
- Return values
-
num | Lines written to the file |
The queue is written to a temporary file. The format is:
[HH:MM:SS]<LEVEL> FORMATTED-MESSAGE
- Note
- The caller should free the returned string and delete the file.
Definition at line 369 of file logging.c.
◆ log_queue_set_max_size()
void log_queue_set_max_size |
( |
int |
size | ) |
|
Set a upper limit for the queue length.
- Parameters
-
size | New maximum queue length |
- Note
- size of 0 means unlimited
Definition at line 314 of file logging.c.
◆ log_file_close()
void log_file_close |
( |
bool |
verbose | ) |
|
Close the log file.
- Parameters
-
verbose | If true, then log the event |
Definition at line 98 of file logging.c.
104 fprintf(
LogFileFP,
"# vim: syntax=neomuttlog\n");
◆ log_file_open()
int log_file_open |
( |
bool |
verbose | ) |
|
◆ log_file_running()
bool log_file_running |
( |
void |
| ) |
|
Is the log file running?
- Return values
-
true | If the log file is running |
Definition at line 230 of file logging.c.
◆ log_file_set_filename()
int log_file_set_filename |
( |
const char * |
file, |
|
|
bool |
verbose |
|
) |
| |
Set the filename for the log.
- Parameters
-
file | Name to use |
verbose | If true, then log the event |
- Return values
-
0 | Success, file opened |
-1 | Error, see errno |
Definition at line 149 of file logging.c.
◆ log_file_set_level()
int log_file_set_level |
( |
enum LogLevel |
level, |
|
|
bool |
verbose |
|
) |
| |
Set the logging level.
- Parameters
-
level | Logging level |
verbose | If true, then log the event |
- Return values
-
0 | Success |
-1 | Error, level is out of range |
The level should be: LL_MESSAGE <= level < LL_MAX.
Definition at line 175 of file logging.c.
206 " Logging at this level can reveal personal information.\n"
207 " Review the log carefully before posting in bug reports.\n"
◆ log_file_set_version()
void log_file_set_version |
( |
const char * |
version | ) |
|
Set the program's version number.
- Parameters
-
The string will be appended directly to 'NeoMutt', so it should begin with a hyphen.
Definition at line 221 of file logging.c.
◆ MuttLogger
The log dispatcher.
This function pointer controls where log messages are redirected.
Definition at line 52 of file logging.c.
char * LogFileVersion
Program version.
int log_queue_add(struct LogLine *ll)
Add a LogLine to the queue.
int line
Line number in source file.
int LogQueueCount
Number of entries currently in the log queue.
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
@ LL_DEBUG3
Log at debug level 3.
#define STAILQ_REMOVE(head, elm, type, field)
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
@ LL_PERROR
Log perror (using errno)
char * mutt_str_dup(const char *str)
Copy a string, safely.
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
@ LL_DEBUG1
Log at debug level 1.
#define STAILQ_FIRST(head)
void log_queue_empty(void)
Free the contents of the queue.
@ LL_DEBUG5
Log at debug level 5.
#define STAILQ_FOREACH(var, head, field)
const char * function
C function.
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
static const char * timestamp(time_t stamp)
Create a YYYY-MM-DD HH:MM:SS timestamp.
static struct LogLineList LogQueue
In-memory list of log lines.
time_t time
Timestamp of the message.
size_t mutt_date_localtime_format(char *buf, size_t buflen, const char *format, time_t t)
Format localtime.
char * message
Message to be logged.
@ LL_MESSAGE
Log informational message.
FILE * LogFileFP
Log file handle.
enum LogLevel level
Log level, e.g. LL_DEBUG1.
#define STAILQ_REMOVE_HEAD(head, field)
int LogFileLevel
Log file level.
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
time_t mutt_date_epoch(void)
Return the number of seconds since the Unix epoch.
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
#define STAILQ_INSERT_TAIL(head, elm, field)
@ LL_NOTIFY
Log of notifications.
void log_file_close(bool verbose)
Close the log file.
int log_file_open(bool verbose)
Start logging to a file.
char * LogFileName
Log file name.
#define mutt_message(...)
@ LL_DEBUG4
Log at debug level 4.
int LogQueueMax
Maximum number of entries in the log queue.
@ LL_DEBUG2
Log at debug level 2.
const char * file
Source file.
const char * LevelAbbr
Abbreviations of logging level names.
int log_disp_file(time_t stamp, const char *file, int line, const char *function, enum LogLevel level,...)
Save a log line to a file - Implements log_dispatcher_t.