File: logging.c

package info (click to toggle)
neomutt 20250510%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 23,372 kB
  • sloc: ansic: 171,730; sh: 5,357; perl: 1,121; tcl: 1,065; python: 443; makefile: 48
file content (133 lines) | stat: -rw-r--r-- 3,365 bytes parent folder | download | duplicates (4)
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
/**
 * @file
 * Log everything to the terminal
 *
 * @authors
 * Copyright (C) 2023 Richard Russon <rich@flatcap.org>
 *
 * @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/>.
 */

/**
 * @page debug_logging Log everything to the terminal
 *
 * Log everything to the terminal
 */

#include "config.h"
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "mutt/lib.h"
#include "lib.h"

bool DebugLogColor = false;     ///< Output ANSI colours
bool DebugLogLevel = false;     ///< Prefix log level, e.g. [E]
bool DebugLogTimestamp = false; ///< Show the timestamp

extern const char *LevelAbbr;

/**
 * log_timestamp - Write a timestamp to a buffer
 * @param buf    Buffer for the result
 * @param buflen Length of the buffer
 * @param time   Timestamp
 * @retval num Bytes written to buffer
 */
static int log_timestamp(char *buf, size_t buflen, time_t time)
{
  return mutt_date_localtime_format(buf, buflen, "[%H:%M:%S]", time);
}

/**
 * log_level - Write a log level to a buffer
 * @param buf    Buffer for the result
 * @param buflen Length of the buffer
 * @param level  Log level
 * @retval num Bytes written to buffer
 *
 * Write the log level, e.g. [E]
 */
static int log_level(char *buf, size_t buflen, enum LogLevel level)
{
  return snprintf(buf, buflen, "<%c>", LevelAbbr[level + 3]);
}

/**
 * log_disp_debug - Display a log line on screen - Implements ::log_dispatcher_t - @ingroup logging_api
 */
int log_disp_debug(time_t stamp, const char *file, int line, const char *function,
                   enum LogLevel level, const char *format, ...)
{
  char buf[LOG_LINE_MAX_LEN] = { 0 };
  size_t buflen = sizeof(buf);
  int err = errno;
  int colour = 0;
  int bytes = 0;

  if (DebugLogColor)
  {
    switch (level)
    {
      case LL_PERROR:
      case LL_ERROR:
        colour = 31;
        break;
      case LL_WARNING:
        colour = 33;
        break;
      case LL_MESSAGE:
      default:
        break;
    }

    if (colour > 0)
    {
      bytes += snprintf(buf + bytes, buflen - bytes, "\033[1;%dm", colour); // Escape
    }
  }

  if (DebugLogTimestamp)
  {
    bytes += log_timestamp(buf + bytes, buflen - bytes, stamp);
  }

  if (DebugLogLevel)
  {
    bytes += log_level(buf + bytes, buflen - bytes, level);
  }

  va_list ap;
  va_start(ap, format);
  bytes += vsnprintf(buf + bytes, buflen - bytes, format, ap);
  va_end(ap);

  if (level == LL_PERROR)
    bytes += snprintf(buf + bytes, buflen - bytes, ": %s", strerror(err));

  if (colour > 0)
  {
    bytes += snprintf(buf + bytes, buflen - bytes, "\033[0m"); // Escape
  }

  if (level < LL_DEBUG1)
    bytes += snprintf(buf + bytes, buflen - bytes, "\n");

  fputs(buf, stdout);
  return bytes;
}