aboutsummaryrefslogtreecommitdiff
path: root/src/log.c
blob: 775bd2cb9888737e03941456a1198948fb78b367 (plain)
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
#include "beaker_globals.h"
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>

enum {
  LOG_LEVEL_DEBUG,
  LOG_LEVEL_INFO,
  LOG_LEVEL_WARN,
  LOG_LEVEL_ERROR,
  LOG_LEVEL_NONE
};

static int configured_log_level(void) {
  const char *level = getenv("BEAKER_LOG_LEVEL");

  if (level == NULL || strcasecmp(level, "INFO") == 0)
    return LOG_LEVEL_INFO;
  if (strcasecmp(level, "DEBUG") == 0)
    return LOG_LEVEL_DEBUG;
  if (strcasecmp(level, "WARN") == 0 || strcasecmp(level, "WARNING") == 0)
    return LOG_LEVEL_WARN;
  if (strcasecmp(level, "ERROR") == 0)
    return LOG_LEVEL_ERROR;
  if (strcasecmp(level, "NONE") == 0 || strcasecmp(level, "OFF") == 0)
    return LOG_LEVEL_NONE;
  return LOG_LEVEL_INFO;
}

static int message_log_level(const char *level) {
  if (strcasecmp(level, "DEBUG") == 0)
    return LOG_LEVEL_DEBUG;
  if (strcasecmp(level, "INFO") == 0 || strcasecmp(level, "ACCESS") == 0)
    return LOG_LEVEL_INFO;
  if (strcasecmp(level, "WARN") == 0 || strcasecmp(level, "WARNING") == 0 ||
      strcasecmp(level, "SECURITY") == 0)
    return LOG_LEVEL_WARN;
  return LOG_LEVEL_ERROR;
}

static void log_timestamp(char *buffer, size_t size) {
  struct timespec now;
  struct tm local;

  clock_gettime(CLOCK_REALTIME, &now);
  localtime_r(&now.tv_sec, &local);
  strftime(buffer, size, "%Y-%m-%dT%H:%M:%S%z", &local);
}

static void log_safe_value(char *output, const char *input, size_t size) {
  size_t i;

  for (i = 0; i + 1 < size && input[i] != '\0'; i++) {
    unsigned char c = input[i];
    output[i] = c <= ' ' || c == 127 || c == '"' || c == '\\' ? '_' : c;
  }
  output[i] = '\0';
}

void beaker_log(const char *level, const char *format, ...) {
  char message[BUFFER_SIZE];
  char timestamp[32];
  va_list args;

  if (message_log_level(level) < configured_log_level())
    return;
  va_start(args, format);
  vsnprintf(message, sizeof(message), format, args);
  va_end(args);
  size_t length = strlen(message);
  while (length > 0 &&
         (message[length - 1] == '\n' || message[length - 1] == '\r'))
    message[--length] = '\0';
  log_timestamp(timestamp, sizeof(timestamp));
  flockfile(stderr);
  fprintf(stderr, "%s %-6s %s\n", timestamp, level, message);
  funlockfile(stderr);
}

void beaker_log_errno_format(const char *level, const char *format, ...) {
  char message[BUFFER_SIZE];
  int error = errno;
  va_list args;

  va_start(args, format);
  vsnprintf(message, sizeof(message), format, args);
  va_end(args);

  size_t length = strlen(message);
  while (length > 0 &&
         (message[length - 1] == '\n' || message[length - 1] == '\r'))
    message[--length] = '\0';
  if (length > 0 && message[length - 1] == '.')
    message[length - 1] = '\0';
  beaker_log(level, "%s: %s", message, strerror(error));
}

void beaker_log_errno(const char *message) {
  int error = errno;
  beaker_log("ERROR", "%s: %s", message, strerror(error));
}

void beaker_log_request(const char *remote_addr, const char *method,
                        const char *path, int status, size_t response_size,
                        double duration_ms) {
  char safe_method[16];
  char safe_path[MAX_PATH_LEN];

  log_safe_value(safe_method, method, sizeof(safe_method));
  log_safe_value(safe_path, path, sizeof(safe_path));
  beaker_log("ACCESS", "%s \"%s %s\" %03d %zu %.3fms", remote_addr, safe_method,
             safe_path, status, response_size, duration_ms);
}