summaryrefslogtreecommitdiffstats
path: root/src/libply/ply-logger.c
blob: 22f7233683739ca7c6a0ec2b46b78da1c77d1386 (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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/* ply-logger.c - logging and tracing facilities
 *
 * Copyright (C) 2007 Red Hat, Inc.
 *
 * 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, 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.  
 *
 * Written by: Ray Strode <rstrode@redhat.com>
 */
#include "config.h"
#include "ply-logger.h"

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/fcntl.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "ply-utils.h"

#ifndef PLY_LOGGER_OPEN_FLAGS 
#define PLY_LOGGER_OPEN_FLAGS (O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW)
#endif

#ifndef PLY_LOGGER_MAX_INJECTION_SIZE
#define PLY_LOGGER_MAX_INJECTION_SIZE 4096
#endif 

#ifndef PLY_LOGGER_MAX_BUFFER_CAPACITY
#define PLY_LOGGER_MAX_BUFFER_CAPACITY (8 * 4096)
#endif

struct _ply_logger 
{
  int output_fd;
  char *filename;

  char *buffer;
  size_t buffer_size;
  size_t buffer_capacity;

  ply_logger_flush_policy_t flush_policy;

  uint32_t is_enabled : 1;
  uint32_t tracing_is_enabled : 1;
};

static bool ply_text_is_loggable (const char *string,
                                  ssize_t     length);
static void ply_logger_write_exception (ply_logger_t   *logger,
                                        const char     *string);
static bool ply_logger_write (ply_logger_t   *logger,
                              const char     *string,
                              size_t          length,
                              bool            should_report_failures);

static bool ply_logger_buffer (ply_logger_t   *logger,
                               const char     *string,
                               size_t          length);
static bool ply_logger_flush_buffer (ply_logger_t *logger);

static bool
ply_text_is_loggable (const char *string,
                      ssize_t     length)
{
  /* I guess we should let everything through since there
   * isn't really any specified encoding
   */

  return true;
}

static void
ply_logger_write_exception (ply_logger_t   *logger,
                            const char     *string)
{
  char *message;
  int number_of_bytes;

  if (!ply_text_is_loggable (string, -1))
    return;

  message = NULL;
  asprintf (&message, 
            "[couldn't write a log entry: %s]\n%n",
            string, &number_of_bytes);

  assert (message != NULL);

  ply_logger_write (logger, message, number_of_bytes, false);
  free (message);
}

static bool
ply_logger_write (ply_logger_t *logger,
                  const char   *string,
                  size_t        length,
                  bool          should_report_failures)
{
  if (!ply_text_is_loggable (string, length))
    {
      if (should_report_failures)
        ply_logger_write_exception (logger,
                                    "log text contains unloggable bytes");
      /* we return true here, because returning false would mean
       * "you aren't allowed to write to the log file anymore"
       */
      return true;
    }

  if (!ply_write (logger->output_fd, string, length))
    {
      if (should_report_failures)
        ply_logger_write_exception (logger, strerror (errno));

      return false;
    }

  return true;
}

static bool
ply_logger_flush_buffer (ply_logger_t *logger)
{
  assert (logger != NULL);

  if (logger->buffer_size == 0)
    return true;

  if (!ply_logger_write (logger, logger->buffer, logger->buffer_size, true))
    return false;

  memset (logger->buffer, '\0', logger->buffer_size);
  logger->buffer_size = 0;

  return true;
}

static bool
ply_logger_increase_buffer_size (ply_logger_t *logger)
{
  assert (logger != NULL);

  if ((logger->buffer_capacity * 2) > PLY_LOGGER_MAX_BUFFER_CAPACITY)
    return false;

  logger->buffer_capacity *= 2;

  logger->buffer = realloc (logger->buffer, logger->buffer_capacity);
  return true;
}

static void
ply_logger_decapitate_buffer (ply_logger_t *logger,
                              size_t        bytes_in_head)
{
  assert (logger != NULL);

  bytes_in_head = MIN (logger->buffer_size, bytes_in_head);

  if (bytes_in_head == logger->buffer_size)
    logger->buffer_size = 0;
  else
    {
      memmove (logger->buffer, logger->buffer + bytes_in_head,
               logger->buffer_size - bytes_in_head);
      logger->buffer_size -= bytes_in_head;
    }
}

static bool 
ply_logger_buffer (ply_logger_t *logger,
                   const char   *string,
                   size_t        length)
{
  assert (logger != NULL);

  if ((logger->buffer_size + length) >= logger->buffer_capacity)
    {
      if (!ply_logger_increase_buffer_size (logger))
        {
          ply_logger_decapitate_buffer (logger, length);
          
          if ((logger->buffer_size + length) >= logger->buffer_capacity)
            if (!ply_logger_increase_buffer_size (logger))
              return false;
        }
    }

  assert (logger->buffer_size + length < logger->buffer_capacity);

  memcpy (logger->buffer + logger->buffer_size, 
          string, length);

  logger->buffer_size += length;

  return true;
}

ply_logger_t *
ply_logger_new (void)
{
  ply_logger_t *logger;

  logger = calloc (1, sizeof (ply_logger_t));

  logger->output_fd = -1;
  logger->filename = NULL;
  logger->is_enabled = true;
  logger->tracing_is_enabled = false;

  logger->buffer_capacity = 4096;
  logger->buffer = calloc (1, logger->buffer_capacity);
  logger->buffer_size = 0;

  return logger;
}

ply_logger_t *
ply_logger_get_default (void)
{
  static ply_logger_t *logger = NULL;

  if (logger == NULL)
    {
      logger = ply_logger_new ();
      ply_logger_set_output_fd (logger, STDOUT_FILENO);
    }

  return logger;
}

ply_logger_t *
ply_logger_get_error_default (void)
{
  static ply_logger_t *logger = NULL;

  if (logger == NULL)
    {
      logger = ply_logger_new ();
      ply_logger_set_output_fd (logger, STDERR_FILENO);
      ply_logger_set_flush_policy (logger, 
                                   PLY_LOGGER_FLUSH_POLICY_EVERY_TIME);
    }

  return logger;
}

void
ply_logger_free (ply_logger_t *logger)
{
  if (logger == NULL)
    return;

  if (logger->output_fd >= 0)
    {
      if (ply_logger_is_logging (logger))
        ply_logger_flush (logger);
      close (logger->output_fd);
    }

  free (logger->filename);
  free (logger);
}

bool 
ply_logger_open_file (ply_logger_t    *logger,
                      const char      *filename)
{
  int fd;

  assert (logger != NULL);
  assert (filename != NULL);

  fd = open (filename, PLY_LOGGER_OPEN_FLAGS, 0600);

  if (fd < 0)
    return false;

  ply_logger_set_output_fd (logger, fd);

  free (logger->filename);

  logger->filename = strdup (filename);

  return true;
}

void
ply_logger_close_file (ply_logger_t *logger)
{
  assert (logger != NULL);

  close (logger->output_fd);
  ply_logger_set_output_fd (logger, -1);
}

void 
ply_logger_set_output_fd (ply_logger_t *logger,
                          int           fd)
{
  assert (logger != NULL);

  logger->output_fd = fd;
}

int 
ply_logger_get_output_fd (ply_logger_t *logger)
{
  assert (logger != NULL);

  return logger->output_fd;
}

bool 
ply_logger_flush (ply_logger_t *logger)
{
  assert (logger != NULL);
  
  if (!ply_logger_is_logging (logger))
    return false;

  if (logger->output_fd < 0)
    return false;

  if (!ply_logger_flush_buffer (logger))
    return false;

  if ((fdatasync (logger->output_fd) < 0) &&
      ((errno != EROFS) && (errno != EINVAL)))
    return false;

  return true;
}

void 
ply_logger_set_flush_policy (ply_logger_t              *logger,
                             ply_logger_flush_policy_t  policy)
{
  assert (logger != NULL);

  logger->flush_policy = policy;
}

ply_logger_flush_policy_t
ply_logger_get_flush_policy (ply_logger_t *logger)
{
  assert (logger != NULL);

  return logger->flush_policy;
}

void 
ply_logger_toggle_logging (ply_logger_t *logger)
{
  assert (logger != NULL);

  logger->is_enabled = !logger->is_enabled;
}

bool 
ply_logger_is_logging (ply_logger_t *logger)
{
  assert (logger != NULL);

  return logger->is_enabled != false;
}

static bool
ply_logger_validate_format_string (ply_logger_t   *logger,
                                   const char     *format)
{
  char *n, *p;

  p = (char *) format;

  /* lame checks to limit the damage
   * of some potential exploits.
   */
  while ((n = strstr (p, "%n")) != NULL)
    {
      if (n == format)
          return false;

      if (n[-1] != '%')
          return false;

      p = n + 1;
    }

  return true;
}

void
ply_logger_inject_with_non_literal_format_string (ply_logger_t   *logger,
                                                  const char     *format,
                                                  ...)
{
  va_list args;
  size_t string_size;
  char write_buffer[PLY_LOGGER_MAX_INJECTION_SIZE] = "";

  assert (logger != NULL);

  if (!ply_logger_is_logging (logger))
    return;

  if (!ply_logger_validate_format_string (logger, format))
    {
      ply_logger_write_exception (logger, "log format string invalid");
      return;
    }

  va_start (args, format);
  string_size = vsnprintf (write_buffer, 0, format, args) + 1;
  va_end (args);

  if (string_size > PLY_LOGGER_MAX_INJECTION_SIZE)  
    {
      ply_logger_write_exception (logger, "log text too long");
      return;
    }

  va_start (args, format);
  vsnprintf (write_buffer, PLY_LOGGER_MAX_INJECTION_SIZE,
             format, args);
  va_end (args);

  ply_logger_inject_bytes (logger, write_buffer, string_size - 1);

}

void 
ply_logger_inject_bytes (ply_logger_t *logger, 
		                 const void   *bytes,
		                 size_t        number_of_bytes)
{
  assert (logger != NULL);
  assert (bytes != NULL);
  assert (number_of_bytes != 0);

  ply_logger_buffer (logger, bytes, number_of_bytes);

  assert ((logger->flush_policy == PLY_LOGGER_FLUSH_POLICY_WHEN_ASKED)
          || (logger->flush_policy == PLY_LOGGER_FLUSH_POLICY_EVERY_TIME));

  if (logger->flush_policy == PLY_LOGGER_FLUSH_POLICY_EVERY_TIME)
    ply_logger_flush (logger);
}

#ifdef PLY_ENABLE_TRACING
void
ply_logger_toggle_tracing (ply_logger_t *logger)
{
  assert (logger != NULL);

  logger->tracing_is_enabled = !logger->tracing_is_enabled;
}

bool
ply_logger_is_tracing_enabled (ply_logger_t *logger)
{
  assert (logger != NULL);

  return logger->tracing_is_enabled != false;
} 
#endif /* PLY_ENABLE_TRACING */ 

#ifdef PLY_LOGGER_ENABLE_TEST

int
main (int    argc, 
      char **argv)
{
  int exit_code;
  ply_logger_t *logger;
  
  exit_code = 0;
  logger = ply_logger_new ();

  ply_logger_inject (logger, "yo yo yo\n");
  ply_logger_set_output_fd (logger, 1);
  ply_logger_inject (logger, "yo yo yo yo\n");
  ply_logger_flush (logger);
  ply_logger_free (logger);

  return exit_code;
}

#endif /* PLY_LOGGER_ENABLE_TEST */
/* vim: set ts=4 sw=4 expandtab autoindent cindent cino={.5s,(0: */